Search in sources :

Example 6 with PsiDocumentManagerBase

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

the class InjectedSelfElementInfo method getInjectedFileIn.

private PsiFile getInjectedFileIn(@NotNull final PsiElement hostContext, @NotNull final PsiFile hostFile, @NotNull final TextRange rangeInHostFile) {
    final PsiDocumentManagerBase docManager = (PsiDocumentManagerBase) PsiDocumentManager.getInstance(getProject());
    final PsiFile[] result = { null };
    final PsiLanguageInjectionHost.InjectedPsiVisitor visitor = new PsiLanguageInjectionHost.InjectedPsiVisitor() {

        @Override
        public void visit(@NotNull PsiFile injectedPsi, @NotNull List<PsiLanguageInjectionHost.Shred> places) {
            Document document = docManager.getDocument(injectedPsi);
            if (document instanceof DocumentWindow) {
                DocumentWindow window = (DocumentWindow) docManager.getLastCommittedDocument(document);
                TextRange hostRange = window.injectedToHost(new TextRange(0, injectedPsi.getTextLength()));
                if (hostRange.contains(rangeInHostFile)) {
                    result[0] = injectedPsi;
                }
            }
        }
    };
    PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
    Document document = documentManager.getDocument(hostFile);
    if (document != null && documentManager.isUncommited(document)) {
        for (DocumentWindow documentWindow : InjectedLanguageManager.getInstance(getProject()).getCachedInjectedDocuments(hostFile)) {
            PsiFile injected = documentManager.getPsiFile(documentWindow);
            if (injected != null) {
                visitor.visit(injected, Collections.<PsiLanguageInjectionHost.Shred>emptyList());
            }
        }
    } else {
        List<Pair<PsiElement, TextRange>> injected = InjectedLanguageManager.getInstance(getProject()).getInjectedPsiFiles(hostContext);
        if (injected != null) {
            for (Pair<PsiElement, TextRange> pair : injected) {
                PsiFile injectedFile = pair.first.getContainingFile();
                visitor.visit(injectedFile, ContainerUtil.<PsiLanguageInjectionHost.Shred>emptyList());
            }
        }
    }
    return result[0];
}
Also used : ProperTextRange(com.intellij.openapi.util.ProperTextRange) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) PsiDocumentManagerBase(com.intellij.psi.impl.PsiDocumentManagerBase) NotNull(org.jetbrains.annotations.NotNull) DocumentWindow(com.intellij.injected.editor.DocumentWindow) List(java.util.List) Pair(com.intellij.openapi.util.Pair)

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