Search in sources :

Example 1 with PsiDocumentManagerBase

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

the class UsageViewImpl method updateLater.

private void updateLater() {
    myUpdateAlarm.cancelAllRequests();
    myUpdateAlarm.addRequest(() -> {
        if (myProject.isDisposed())
            return;
        PsiDocumentManagerBase documentManager = (PsiDocumentManagerBase) PsiDocumentManager.getInstance(myProject);
        documentManager.cancelAndRunWhenAllCommitted("UpdateUsageView", this::updateImmediately);
    }, 300);
}
Also used : PsiDocumentManagerBase(com.intellij.psi.impl.PsiDocumentManagerBase)

Example 2 with PsiDocumentManagerBase

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

the class SelfElementInfo method getRange.

@Override
@Nullable
public Segment getRange() {
    if (hasRange()) {
        Document document = getDocumentToSynchronize();
        if (document != null) {
            PsiDocumentManagerBase documentManager = myManager.getPsiDocumentManager();
            List<DocumentEvent> events = documentManager.getEventsSinceCommit(document);
            if (!events.isEmpty()) {
                SmartPointerTracker tracker = myManager.getTracker(getVirtualFile());
                if (tracker != null) {
                    return tracker.getUpdatedRange(this, (FrozenDocument) documentManager.getLastCommittedDocument(document), events);
                }
            }
        }
    }
    return calcPsiRange();
}
Also used : Document(com.intellij.openapi.editor.Document) FrozenDocument(com.intellij.openapi.editor.impl.FrozenDocument) DocumentEvent(com.intellij.openapi.editor.event.DocumentEvent) PsiDocumentManagerBase(com.intellij.psi.impl.PsiDocumentManagerBase) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with PsiDocumentManagerBase

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

the class InjectedSelfElementInfo method hostToInjected.

@Nullable
private static ProperTextRange hostToInjected(boolean psi, Segment hostRange, @Nullable PsiFile injectedFile, @Nullable AffixOffsets affixOffsets) {
    VirtualFile virtualFile = injectedFile == null ? null : injectedFile.getVirtualFile();
    if (virtualFile instanceof VirtualFileWindow) {
        Project project = injectedFile.getProject();
        DocumentWindow documentWindow = ((VirtualFileWindow) virtualFile).getDocumentWindow();
        if (psi) {
            documentWindow = (DocumentWindow) ((PsiDocumentManagerBase) PsiDocumentManager.getInstance(project)).getLastCommittedDocument(documentWindow);
        }
        int start = documentWindow.hostToInjected(hostRange.getStartOffset());
        int end = documentWindow.hostToInjected(hostRange.getEndOffset());
        if (affixOffsets != null) {
            return affixOffsets.expandRangeToAffixes(start, end, InjectedLanguageManager.getInstance(project).getNonEditableFragments(documentWindow));
        }
        return ProperTextRange.create(start, end);
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DocumentWindow(com.intellij.injected.editor.DocumentWindow) Project(com.intellij.openapi.project.Project) VirtualFileWindow(com.intellij.injected.editor.VirtualFileWindow) PsiDocumentManagerBase(com.intellij.psi.impl.PsiDocumentManagerBase) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with PsiDocumentManagerBase

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

the class MultiHostRegistrarImpl method freezeWindow.

@NotNull
public static DocumentWindow freezeWindow(@NotNull DocumentWindowImpl window) {
    Place shreds = window.getShreds();
    Project project = shreds.getHostPointer().getProject();
    DocumentEx delegate = ((PsiDocumentManagerBase) PsiDocumentManager.getInstance(project)).getLastCommittedDocument(window.getDelegate());
    Place place = new Place(ContainerUtil.map(shreds, shred -> ((ShredImpl) shred).withPsiRange()));
    return new DocumentWindowImpl(delegate, window.isOneLine(), place);
}
Also used : DocumentEx(com.intellij.openapi.editor.ex.DocumentEx) Language(com.intellij.lang.Language) com.intellij.openapi.util(com.intellij.openapi.util) VirtualFileWindow(com.intellij.injected.editor.VirtualFileWindow) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) TreeElement(com.intellij.psi.impl.source.tree.TreeElement) SmartPointerManagerImpl(com.intellij.psi.impl.smartPointers.SmartPointerManagerImpl) IElementType(com.intellij.psi.tree.IElementType) VirtualFile(com.intellij.openapi.vfs.VirtualFile) NonNls(org.jetbrains.annotations.NonNls) ContainerUtil(com.intellij.util.containers.ContainerUtil) BlockSupportImpl(com.intellij.psi.impl.source.text.BlockSupportImpl) ArrayList(java.util.ArrayList) FileContextUtil(com.intellij.psi.impl.source.resolve.FileContextUtil) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) SyntaxHighlighter(com.intellij.openapi.fileTypes.SyntaxHighlighter) SmartList(com.intellij.util.SmartList) Map(java.util.Map) Project(com.intellij.openapi.project.Project) DocumentEx(com.intellij.openapi.editor.ex.DocumentEx) SyntaxHighlighterFactory(com.intellij.openapi.fileTypes.SyntaxHighlighterFactory) DocumentImpl(com.intellij.openapi.editor.impl.DocumentImpl) MultiHostRegistrar(com.intellij.lang.injection.MultiHostRegistrar) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) Lexer(com.intellij.lexer.Lexer) DebugUtil(com.intellij.psi.impl.DebugUtil) DiffLog(com.intellij.psi.impl.source.text.DiffLog) DocumentWindow(com.intellij.injected.editor.DocumentWindow) StringUtil(com.intellij.openapi.util.text.StringUtil) DocumentWindowImpl(com.intellij.injected.editor.DocumentWindowImpl) TreeUtil(com.intellij.psi.impl.source.tree.TreeUtil) FileElement(com.intellij.psi.impl.source.tree.FileElement) ReferenceInjector(com.intellij.psi.injection.ReferenceInjector) FileDocumentManagerImpl(com.intellij.openapi.fileEditor.impl.FileDocumentManagerImpl) ASTNode(com.intellij.lang.ASTNode) ParserDefinition(com.intellij.lang.ParserDefinition) DocumentCommitThread(com.intellij.psi.impl.DocumentCommitThread) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) PsiUtilCore(com.intellij.psi.util.PsiUtilCore) PsiDocumentManagerBase(com.intellij.psi.impl.PsiDocumentManagerBase) LeafElement(com.intellij.psi.impl.source.tree.LeafElement) DaemonProgressIndicator(com.intellij.codeInsight.daemon.impl.DaemonProgressIndicator) LanguageParserDefinitions(com.intellij.lang.LanguageParserDefinitions) com.intellij.psi(com.intellij.psi) PathUtil(com.intellij.util.PathUtil) NotNull(org.jetbrains.annotations.NotNull) VirtualFileWindowImpl(com.intellij.injected.editor.VirtualFileWindowImpl) Project(com.intellij.openapi.project.Project) DocumentWindowImpl(com.intellij.injected.editor.DocumentWindowImpl) PsiDocumentManagerBase(com.intellij.psi.impl.PsiDocumentManagerBase) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with PsiDocumentManagerBase

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

the class MultiHostRegistrarImpl method doneInjecting.

@Override
public void doneInjecting() {
    try {
        if (shreds.isEmpty()) {
            throw new IllegalStateException("Seems you haven't called addPlace()");
        }
        if (myReferenceInjector != null) {
            addToResults(new Place(shreds), null);
            return;
        }
        PsiDocumentManagerBase documentManager = (PsiDocumentManagerBase) PsiDocumentManager.getInstance(myProject);
        Place place = new Place(shreds);
        DocumentWindowImpl documentWindow = new DocumentWindowImpl(myHostDocument, isOneLineEditor, place);
        String fileName = PathUtil.makeFileName(myHostVirtualFile.getName(), fileExtension);
        VirtualFileWindowImpl virtualFile = new VirtualFileWindowImpl(fileName, myHostVirtualFile, documentWindow, myLanguage, outChars);
        Language forcedLanguage = myContextElement.getUserData(InjectedFileViewProvider.LANGUAGE_FOR_INJECTED_COPY_KEY);
        myLanguage = forcedLanguage == null ? LanguageSubstitutors.INSTANCE.substituteLanguage(myLanguage, virtualFile, myProject) : forcedLanguage;
        createDocument(virtualFile);
        InjectedFileViewProvider viewProvider = new InjectedFileViewProvider(myPsiManager, virtualFile, documentWindow, myLanguage);
        ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(myLanguage);
        assert parserDefinition != null : "Parser definition for language " + myLanguage + " is null";
        PsiFile psiFile = parserDefinition.createFile(viewProvider);
        SmartPsiElementPointer<PsiLanguageInjectionHost> pointer = ((ShredImpl) shreds.get(0)).getSmartPointer();
        synchronized (InjectedLanguageManagerImpl.ourInjectionPsiLock) {
            final ASTNode parsedNode = keepTreeFromChameleoningBack(psiFile);
            assert parsedNode instanceof FileElement : "Parsed to " + parsedNode + " instead of FileElement";
            String documentText = documentManager.getLastCommittedDocument(documentWindow).getText();
            assert ((FileElement) parsedNode).textMatches(outChars) : exceptionContext("Before patch: doc:\n'" + documentText + "'\n---PSI:\n'" + parsedNode.getText() + "'\n---chars:\n'" + outChars + "'");
            viewProvider.setPatchingLeaves(true);
            try {
                patchLeaves(parsedNode, escapers, place);
            } catch (ProcessCanceledException e) {
                throw e;
            } catch (RuntimeException e) {
                throw new RuntimeException(exceptionContext("Patch error"), e);
            } finally {
                viewProvider.setPatchingLeaves(false);
            }
            if (!((FileElement) parsedNode).textMatches(documentText)) {
                throw new AssertionError(exceptionContext("After patch: doc:\n'" + documentText + "'\n---PSI:\n'" + parsedNode.getText() + "'\n---chars:\n'" + outChars + "'"));
            }
            virtualFile.setContent(null, documentWindow.getText(), false);
            virtualFile.setWritable(virtualFile.getDelegate().isWritable());
            cacheEverything(place, documentWindow, viewProvider, psiFile, pointer);
            PsiFile cachedPsiFile = documentManager.getCachedPsiFile(documentWindow);
            assert cachedPsiFile == psiFile : "Cached psi :" + cachedPsiFile + " instead of " + psiFile;
            assert place.isValid();
            assert viewProvider.isValid();
            PsiFile newFile = registerDocument(documentWindow, psiFile, place, myHostPsiFile, documentManager);
            boolean mergeHappened = newFile != psiFile;
            if (mergeHappened) {
                InjectedLanguageUtil.clearCaches(psiFile, documentWindow);
                psiFile = newFile;
                viewProvider = (InjectedFileViewProvider) psiFile.getViewProvider();
                documentWindow = (DocumentWindowImpl) viewProvider.getDocument();
                virtualFile = (VirtualFileWindowImpl) viewProvider.getVirtualFile();
                boolean shredsRewritten = cacheEverything(place, documentWindow, viewProvider, psiFile, pointer);
                if (!shredsRewritten) {
                    place.dispose();
                    place = documentWindow.getShreds();
                }
            }
            assert psiFile.isValid();
            assert place.isValid();
            assert viewProvider.isValid();
            try {
                List<Trinity<IElementType, SmartPsiElementPointer<PsiLanguageInjectionHost>, TextRange>> tokens = obtainHighlightTokensFromLexer(myLanguage, outChars, escapers, place, virtualFile, myProject);
                psiFile.putUserData(InjectedLanguageUtil.HIGHLIGHT_TOKENS, tokens);
            } catch (ProcessCanceledException e) {
                throw e;
            } catch (RuntimeException e) {
                throw new RuntimeException(exceptionContext("Obtaining tokens error"), e);
            }
            addToResults(place, psiFile);
            assertEverythingIsAllright(documentManager, documentWindow, psiFile);
        }
    } finally {
        clear();
    }
}
Also used : VirtualFileWindowImpl(com.intellij.injected.editor.VirtualFileWindowImpl) PsiDocumentManagerBase(com.intellij.psi.impl.PsiDocumentManagerBase) ParserDefinition(com.intellij.lang.ParserDefinition) DocumentWindowImpl(com.intellij.injected.editor.DocumentWindowImpl) Language(com.intellij.lang.Language) ASTNode(com.intellij.lang.ASTNode) FileElement(com.intellij.psi.impl.source.tree.FileElement) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Aggregations

PsiDocumentManagerBase (com.intellij.psi.impl.PsiDocumentManagerBase)6 DocumentWindow (com.intellij.injected.editor.DocumentWindow)3 Nullable (org.jetbrains.annotations.Nullable)3 DocumentWindowImpl (com.intellij.injected.editor.DocumentWindowImpl)2 VirtualFileWindow (com.intellij.injected.editor.VirtualFileWindow)2 VirtualFileWindowImpl (com.intellij.injected.editor.VirtualFileWindowImpl)2 ASTNode (com.intellij.lang.ASTNode)2 Language (com.intellij.lang.Language)2 ParserDefinition (com.intellij.lang.ParserDefinition)2 Document (com.intellij.openapi.editor.Document)2 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)2 Project (com.intellij.openapi.project.Project)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 FileElement (com.intellij.psi.impl.source.tree.FileElement)2 DaemonProgressIndicator (com.intellij.codeInsight.daemon.impl.DaemonProgressIndicator)1 LanguageParserDefinitions (com.intellij.lang.LanguageParserDefinitions)1 MultiHostRegistrar (com.intellij.lang.injection.MultiHostRegistrar)1 Lexer (com.intellij.lexer.Lexer)1 DocumentEvent (com.intellij.openapi.editor.event.DocumentEvent)1 DocumentEx (com.intellij.openapi.editor.ex.DocumentEx)1