Search in sources :

Example 31 with DocumentWindow

use of com.intellij.injected.editor.DocumentWindow in project intellij-community by JetBrains.

the class PsiDocumentManagerBase method getPsiFile.

@Override
@Nullable
public PsiFile getPsiFile(@NotNull Document document) {
    if (document instanceof DocumentWindow && !((DocumentWindow) document).isValid()) {
        return null;
    }
    final PsiFile userData = document.getUserData(HARD_REF_TO_PSI);
    if (userData != null) {
        return ensureValidFile(userData, "From hard ref");
    }
    PsiFile psiFile = getCachedPsiFile(document);
    if (psiFile != null) {
        return ensureValidFile(psiFile, "Cached PSI");
    }
    final VirtualFile virtualFile = FileDocumentManager.getInstance().getFile(document);
    if (virtualFile == null || !virtualFile.isValid())
        return null;
    psiFile = getPsiFile(virtualFile);
    if (psiFile == null)
        return null;
    fireFileCreated(document, psiFile);
    return psiFile;
}
Also used : DocumentWindow(com.intellij.injected.editor.DocumentWindow) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Nullable(org.jetbrains.annotations.Nullable)

Example 32 with DocumentWindow

use of com.intellij.injected.editor.DocumentWindow 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)

Example 33 with DocumentWindow

use of com.intellij.injected.editor.DocumentWindow in project intellij-community by JetBrains.

the class CodeStyleManagerRunnable method perform.

public T perform(PsiFile file, int offset, @Nullable TextRange range, T defaultValue) {
    if (file instanceof PsiCompiledFile) {
        file = ((PsiCompiledFile) file).getDecompiledPsiFile();
    }
    PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myCodeStyleManager.getProject());
    Document document = documentManager.getDocument(file);
    if (document instanceof DocumentWindow) {
        final DocumentWindow documentWindow = (DocumentWindow) document;
        final PsiFile topLevelFile = InjectedLanguageManager.getInstance(file.getProject()).getTopLevelFile(file);
        if (!file.equals(topLevelFile)) {
            if (range != null) {
                range = documentWindow.injectedToHost(range);
            }
            if (offset != -1) {
                offset = documentWindow.injectedToHost(offset);
            }
            return adjustResultForInjected(perform(topLevelFile, offset, range, defaultValue), documentWindow);
        }
    }
    final PsiFile templateFile = PsiUtilCore.getTemplateLanguageFile(file);
    if (templateFile != null) {
        file = templateFile;
        document = documentManager.getDocument(templateFile);
    }
    PsiElement element = null;
    if (offset != -1) {
        element = CodeStyleManagerImpl.findElementInTreeWithFormatterEnabled(file, offset);
        if (element == null && offset != file.getTextLength()) {
            return defaultValue;
        }
        if (isInsidePlainComment(offset, element)) {
            return computeValueInsidePlainComment(file, offset, defaultValue);
        }
    }
    final FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forContext(file);
    FormattingModelBuilder elementBuilder = element != null ? LanguageFormatting.INSTANCE.forContext(element) : builder;
    if (builder != null && elementBuilder != null) {
        mySettings = CodeStyleSettingsManager.getSettings(myCodeStyleManager.getProject());
        mySignificantRange = offset != -1 ? getSignificantRange(file, offset) : null;
        myIndentOptions = mySettings.getIndentOptionsByFile(file, mySignificantRange);
        FormattingMode currentMode = myCodeStyleManager.getCurrentFormattingMode();
        myCodeStyleManager.setCurrentFormattingMode(myMode);
        try {
            myModel = buildModel(builder, file, document);
            T result = doPerform(offset, range);
            if (result != null) {
                return result;
            }
        } finally {
            myCodeStyleManager.setCurrentFormattingMode(currentMode);
        }
    }
    return defaultValue;
}
Also used : DocumentWindow(com.intellij.injected.editor.DocumentWindow) FormattingModelBuilder(com.intellij.formatting.FormattingModelBuilder) FormattingMode(com.intellij.formatting.FormattingMode) Document(com.intellij.openapi.editor.Document)

Example 34 with DocumentWindow

use of com.intellij.injected.editor.DocumentWindow in project intellij-community by JetBrains.

the class AbstractBlock method buildInjectedBlocks.

@NotNull
private List<Block> buildInjectedBlocks() {
    if (myBuildIndentsOnly) {
        return EMPTY;
    }
    if (!(this instanceof SettingsAwareBlock)) {
        return EMPTY;
    }
    PsiElement psi = myNode.getPsi();
    if (psi == null) {
        return EMPTY;
    }
    PsiFile file = psi.getContainingFile();
    if (file == null) {
        return EMPTY;
    }
    if (InjectedLanguageUtil.getCachedInjectedDocuments(file).isEmpty()) {
        return EMPTY;
    }
    TextRange blockRange = myNode.getTextRange();
    List<DocumentWindow> documentWindows = InjectedLanguageUtil.getCachedInjectedDocuments(file);
    for (DocumentWindow documentWindow : documentWindows) {
        int startOffset = documentWindow.injectedToHost(0);
        int endOffset = startOffset + documentWindow.getTextLength();
        if (blockRange.containsRange(startOffset, endOffset)) {
            PsiFile injected = PsiDocumentManager.getInstance(psi.getProject()).getCachedPsiFile(documentWindow);
            if (injected != null) {
                List<Block> result = ContainerUtilRt.newArrayList();
                DefaultInjectedLanguageBlockBuilder builder = new DefaultInjectedLanguageBlockBuilder(((SettingsAwareBlock) this).getSettings());
                builder.addInjectedBlocks(result, myNode, getWrap(), getAlignment(), getIndent());
                return result;
            }
        }
    }
    return EMPTY;
}
Also used : DocumentWindow(com.intellij.injected.editor.DocumentWindow) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 35 with DocumentWindow

use of com.intellij.injected.editor.DocumentWindow in project intellij-community by JetBrains.

the class ChunkExtractor method extractChunks.

@NotNull
private TextChunk[] extractChunks(@NotNull UsageInfo2UsageAdapter usageInfo2UsageAdapter, @NotNull PsiFile file) {
    int absoluteStartOffset = usageInfo2UsageAdapter.getNavigationOffset();
    if (absoluteStartOffset == -1)
        return TextChunk.EMPTY_ARRAY;
    Document visibleDocument = myDocument instanceof DocumentWindow ? ((DocumentWindow) myDocument).getDelegate() : myDocument;
    int visibleStartOffset = myDocument instanceof DocumentWindow ? ((DocumentWindow) myDocument).injectedToHost(absoluteStartOffset) : absoluteStartOffset;
    int lineNumber = myDocument.getLineNumber(absoluteStartOffset);
    int visibleLineNumber = visibleDocument.getLineNumber(visibleStartOffset);
    List<TextChunk> result = new ArrayList<>();
    appendPrefix(result, visibleLineNumber);
    int fragmentToShowStart = myDocument.getLineStartOffset(lineNumber);
    int fragmentToShowEnd = fragmentToShowStart < myDocument.getTextLength() ? myDocument.getLineEndOffset(lineNumber) : 0;
    if (fragmentToShowStart > fragmentToShowEnd)
        return TextChunk.EMPTY_ARRAY;
    final CharSequence chars = myDocument.getCharsSequence();
    if (fragmentToShowEnd - fragmentToShowStart > MAX_LINE_LENGTH_TO_SHOW) {
        final int lineStartOffset = fragmentToShowStart;
        fragmentToShowStart = Math.max(lineStartOffset, absoluteStartOffset - OFFSET_BEFORE_TO_SHOW_WHEN_LONG_LINE);
        final int lineEndOffset = fragmentToShowEnd;
        Segment segment = usageInfo2UsageAdapter.getUsageInfo().getSegment();
        int usage_length = segment != null ? segment.getEndOffset() - segment.getStartOffset() : 0;
        fragmentToShowEnd = Math.min(lineEndOffset, absoluteStartOffset + usage_length + OFFSET_AFTER_TO_SHOW_WHEN_LONG_LINE);
        // this should not cause restarts of the lexer as the tokens are usually words
        if (usage_length > 0 && StringUtil.isJavaIdentifierStart(chars.charAt(absoluteStartOffset)) && StringUtil.isJavaIdentifierStart(chars.charAt(absoluteStartOffset + usage_length - 1))) {
            while (fragmentToShowEnd < lineEndOffset && StringUtil.isJavaIdentifierStart(chars.charAt(fragmentToShowEnd - 1))) ++fragmentToShowEnd;
            while (fragmentToShowStart > lineStartOffset && StringUtil.isJavaIdentifierStart(chars.charAt(fragmentToShowStart))) --fragmentToShowStart;
            if (fragmentToShowStart != lineStartOffset)
                ++fragmentToShowStart;
            if (fragmentToShowEnd != lineEndOffset)
                --fragmentToShowEnd;
        }
    }
    if (myDocument instanceof DocumentWindow) {
        List<TextRange> editable = InjectedLanguageManager.getInstance(file.getProject()).intersectWithAllEditableFragments(file, new TextRange(fragmentToShowStart, fragmentToShowEnd));
        for (TextRange range : editable) {
            createTextChunks(usageInfo2UsageAdapter, chars, range.getStartOffset(), range.getEndOffset(), true, result);
        }
        return result.toArray(new TextChunk[result.size()]);
    }
    return createTextChunks(usageInfo2UsageAdapter, chars, fragmentToShowStart, fragmentToShowEnd, true, result);
}
Also used : DocumentWindow(com.intellij.injected.editor.DocumentWindow) ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) Segment(com.intellij.openapi.util.Segment) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

DocumentWindow (com.intellij.injected.editor.DocumentWindow)42 TextRange (com.intellij.openapi.util.TextRange)18 Document (com.intellij.openapi.editor.Document)17 NotNull (org.jetbrains.annotations.NotNull)11 Nullable (org.jetbrains.annotations.Nullable)9 Project (com.intellij.openapi.project.Project)8 VirtualFileWindow (com.intellij.injected.editor.VirtualFileWindow)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)6 PsiFile (com.intellij.psi.PsiFile)6 InjectedLanguageManager (com.intellij.lang.injection.InjectedLanguageManager)5 DocumentWindowImpl (com.intellij.injected.editor.DocumentWindowImpl)4 Language (com.intellij.lang.Language)4 Editor (com.intellij.openapi.editor.Editor)4 Segment (com.intellij.openapi.util.Segment)4 PsiFileImpl (com.intellij.psi.impl.source.PsiFileImpl)4 DaemonProgressIndicator (com.intellij.codeInsight.daemon.impl.DaemonProgressIndicator)3 ASTNode (com.intellij.lang.ASTNode)3 com.intellij.psi (com.intellij.psi)3 PsiElement (com.intellij.psi.PsiElement)3 PsiDocumentManagerBase (com.intellij.psi.impl.PsiDocumentManagerBase)3