Search in sources :

Example 1 with SmartPointerManagerImpl

use of com.intellij.psi.impl.smartPointers.SmartPointerManagerImpl in project intellij-community by JetBrains.

the class PomModelImpl method startTransaction.

private void startTransaction(@NotNull PomTransaction transaction) {
    final ProgressIndicator progressIndicator = ProgressIndicatorProvider.getGlobalProgressIndicator();
    if (progressIndicator != null)
        progressIndicator.startNonCancelableSection();
    final PsiDocumentManagerBase manager = (PsiDocumentManagerBase) PsiDocumentManager.getInstance(myProject);
    final PsiToDocumentSynchronizer synchronizer = manager.getSynchronizer();
    final PsiElement changeScope = transaction.getChangeScope();
    final PsiFile containingFileByTree = getContainingFileByTree(changeScope);
    if (containingFileByTree != null && !(containingFileByTree instanceof DummyHolder) && !manager.isCommitInProgress()) {
        PsiUtilCore.ensureValid(containingFileByTree);
    }
    boolean physical = changeScope.isPhysical();
    if (physical && synchronizer.toProcessPsiEvent()) {
        // so it's important to throw something outside event processing
        if (isDocumentUncommitted(containingFileByTree)) {
            throw new IllegalStateException("Attempt to modify PSI for non-committed Document!");
        }
        CommandProcessor commandProcessor = CommandProcessor.getInstance();
        if (!commandProcessor.isUndoTransparentActionInProgress() && commandProcessor.getCurrentCommand() == null) {
            throw new IncorrectOperationException("Must not change PSI outside command or undo-transparent action. See com.intellij.openapi.command.WriteCommandAction or com.intellij.openapi.command.CommandProcessor");
        }
    }
    if (containingFileByTree != null) {
        ((SmartPointerManagerImpl) SmartPointerManager.getInstance(myProject)).fastenBelts(containingFileByTree.getViewProvider().getVirtualFile());
        if (containingFileByTree instanceof PsiFileImpl) {
            ((PsiFileImpl) containingFileByTree).beforeAstChange();
        }
    }
    BlockSupportImpl.sendBeforeChildrenChangeEvent((PsiManagerImpl) PsiManager.getInstance(myProject), changeScope, true);
    Document document = containingFileByTree == null ? null : physical ? manager.getDocument(containingFileByTree) : manager.getCachedDocument(containingFileByTree);
    if (document != null) {
        synchronizer.startTransaction(myProject, document, changeScope);
    }
}
Also used : DummyHolder(com.intellij.psi.impl.source.DummyHolder) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) Document(com.intellij.openapi.editor.Document) EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) CommandProcessor(com.intellij.openapi.command.CommandProcessor) IncorrectOperationException(com.intellij.util.IncorrectOperationException) SmartPointerManagerImpl(com.intellij.psi.impl.smartPointers.SmartPointerManagerImpl)

Example 2 with SmartPointerManagerImpl

use of com.intellij.psi.impl.smartPointers.SmartPointerManagerImpl in project intellij-community by JetBrains.

the class MultiHostRegistrarImpl method addPlace.

@Override
@NotNull
public MultiHostRegistrar addPlace(@NonNls @Nullable String prefix, @NonNls @Nullable String suffix, @NotNull PsiLanguageInjectionHost host, @NotNull TextRange rangeInsideHost) {
    PsiFile containingFile = PsiUtilCore.getTemplateLanguageFile(host);
    assert containingFile == myHostPsiFile : exceptionContext("Trying to inject into foreign file: " + containingFile);
    TextRange hostTextRange = host.getTextRange();
    if (!hostTextRange.contains(rangeInsideHost.shiftRight(hostTextRange.getStartOffset()))) {
        clear();
        throw new IllegalArgumentException("rangeInsideHost must lie within host text range. rangeInsideHost:" + rangeInsideHost + "; host textRange:" + hostTextRange);
    }
    if (myLanguage == null && myReferenceInjector == null) {
        clear();
        throw new IllegalStateException("Seems you haven't called startInjecting()");
    }
    if (prefix == null)
        prefix = "";
    if (suffix == null)
        suffix = "";
    cleared = false;
    int startOffset = outChars.length();
    outChars.append(prefix);
    LiteralTextEscaper<? extends PsiLanguageInjectionHost> textEscaper = host.createLiteralTextEscaper();
    escapers.add(textEscaper);
    isOneLineEditor |= textEscaper.isOneLine();
    TextRange relevantRange = textEscaper.getRelevantTextRange().intersection(rangeInsideHost);
    if (relevantRange == null) {
        relevantRange = TextRange.from(textEscaper.getRelevantTextRange().getStartOffset(), 0);
    } else {
        int before = outChars.length();
        boolean result = textEscaper.decode(relevantRange, outChars);
        int after = outChars.length();
        assert after >= before : "Escaper " + textEscaper + "(" + textEscaper.getClass() + ") must not mangle char buffer";
        if (!result) {
            // if there are invalid chars, adjust the range
            int offsetInHost = textEscaper.getOffsetInHost(outChars.length() - before, rangeInsideHost);
            relevantRange = relevantRange.intersection(new ProperTextRange(0, offsetInHost));
        }
    }
    outChars.append(suffix);
    int endOffset = outChars.length();
    TextRange relevantRangeInHost = relevantRange.shiftRight(hostTextRange.getStartOffset());
    SmartPointerManagerImpl manager = (SmartPointerManagerImpl) SmartPointerManager.getInstance(myProject);
    shreds.add(new ShredImpl(manager.createSmartPsiFileRangePointer(myHostPsiFile, relevantRangeInHost, true), manager.createSmartPsiElementPointer(host, myHostPsiFile, true), prefix, suffix, new ProperTextRange(startOffset, endOffset), false));
    return this;
}
Also used : SmartPointerManagerImpl(com.intellij.psi.impl.smartPointers.SmartPointerManagerImpl) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

SmartPointerManagerImpl (com.intellij.psi.impl.smartPointers.SmartPointerManagerImpl)2 CommandProcessor (com.intellij.openapi.command.CommandProcessor)1 Document (com.intellij.openapi.editor.Document)1 EmptyProgressIndicator (com.intellij.openapi.progress.EmptyProgressIndicator)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 DummyHolder (com.intellij.psi.impl.source.DummyHolder)1 PsiFileImpl (com.intellij.psi.impl.source.PsiFileImpl)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 NotNull (org.jetbrains.annotations.NotNull)1