Search in sources :

Example 36 with DocumentWindow

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

the class TrailingSpacesStripper method clearLineModificationFlags.

// clears line modification flags except lines which was not stripped because the caret was in the way
public void clearLineModificationFlags(@NotNull Document document) {
    if (document instanceof DocumentWindow) {
        document = ((DocumentWindow) document).getDelegate();
    }
    if (!(document instanceof DocumentImpl)) {
        return;
    }
    Editor activeEditor = getActiveEditor(document);
    // when virtual space enabled, we can strip whitespace anywhere
    boolean isVirtualSpaceEnabled = activeEditor == null || activeEditor.getSettings().isVirtualSpace();
    final EditorSettingsExternalizable settings = EditorSettingsExternalizable.getInstance();
    if (settings == null)
        return;
    boolean enabled = !Boolean.TRUE.equals(DISABLE_FOR_FILE_KEY.get(FileDocumentManager.getInstance().getFile(document)));
    if (!enabled)
        return;
    String stripTrailingSpaces = settings.getStripTrailingSpaces();
    final boolean doStrip = !stripTrailingSpaces.equals(EditorSettingsExternalizable.STRIP_TRAILING_SPACES_NONE);
    final boolean inChangedLinesOnly = !stripTrailingSpaces.equals(EditorSettingsExternalizable.STRIP_TRAILING_SPACES_WHOLE);
    int[] caretLines;
    if (activeEditor != null && inChangedLinesOnly && doStrip && !isVirtualSpaceEnabled) {
        List<Caret> carets = activeEditor.getCaretModel().getAllCarets();
        caretLines = new int[carets.size()];
        for (int i = 0; i < carets.size(); i++) {
            Caret caret = carets.get(i);
            caretLines[i] = caret.getLogicalPosition().line;
        }
    } else {
        caretLines = ArrayUtil.EMPTY_INT_ARRAY;
    }
    ((DocumentImpl) document).clearLineModificationFlagsExcept(caretLines);
}
Also used : DocumentWindow(com.intellij.injected.editor.DocumentWindow) EditorSettingsExternalizable(com.intellij.openapi.editor.ex.EditorSettingsExternalizable)

Example 37 with DocumentWindow

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

the class InjectedGeneralHighlightingPass method getInjectedPsiFiles.

@NotNull
private Set<PsiFile> getInjectedPsiFiles(@NotNull final List<PsiElement> elements1, @NotNull final List<PsiElement> elements2, @NotNull final ProgressIndicator progress) {
    ApplicationManager.getApplication().assertReadAccessAllowed();
    List<DocumentWindow> injected = InjectedLanguageUtil.getCachedInjectedDocuments(myFile);
    final Collection<PsiElement> hosts = new THashSet<>(elements1.size() + elements2.size() + injected.size());
    //since change in one place can lead to invalidation of injected PSI in (completely) other place.
    for (DocumentWindow documentRange : injected) {
        progress.checkCanceled();
        if (!documentRange.isValid())
            continue;
        PsiFile file = PsiDocumentManager.getInstance(myProject).getPsiFile(documentRange);
        if (file == null)
            continue;
        PsiElement context = InjectedLanguageManager.getInstance(file.getProject()).getInjectionHost(file);
        if (context != null && context.isValid() && !file.getProject().isDisposed() && (myUpdateAll || myRestrictRange.intersects(context.getTextRange()))) {
            hosts.add(context);
        }
    }
    InjectedLanguageManagerImpl injectedLanguageManager = InjectedLanguageManagerImpl.getInstanceImpl(myProject);
    Processor<PsiElement> collectInjectableProcessor = Processors.cancelableCollectProcessor(hosts);
    injectedLanguageManager.processInjectableElements(elements1, collectInjectableProcessor);
    injectedLanguageManager.processInjectableElements(elements2, collectInjectableProcessor);
    final Set<PsiFile> outInjected = new THashSet<>();
    final PsiLanguageInjectionHost.InjectedPsiVisitor visitor = new PsiLanguageInjectionHost.InjectedPsiVisitor() {

        @Override
        public void visit(@NotNull PsiFile injectedPsi, @NotNull List<PsiLanguageInjectionHost.Shred> places) {
            synchronized (outInjected) {
                outInjected.add(injectedPsi);
            }
        }
    };
    if (!JobLauncher.getInstance().invokeConcurrentlyUnderProgress(new ArrayList<>(hosts), progress, true, element -> {
        ApplicationManager.getApplication().assertReadAccessAllowed();
        progress.checkCanceled();
        InjectedLanguageUtil.enumerate(element, myFile, false, visitor);
        return true;
    })) {
        throw new ProcessCanceledException();
    }
    synchronized (outInjected) {
        return outInjected;
    }
}
Also used : Language(com.intellij.lang.Language) InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) com.intellij.openapi.util(com.intellij.openapi.util) java.util(java.util) IElementType(com.intellij.psi.tree.IElementType) JobLauncher(com.intellij.concurrency.JobLauncher) InjectedLanguageManagerImpl(com.intellij.psi.impl.source.tree.injected.InjectedLanguageManagerImpl) Document(com.intellij.openapi.editor.Document) THashSet(gnu.trove.THashSet) ContainerUtil(com.intellij.util.containers.ContainerUtil) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) SyntaxHighlighter(com.intellij.openapi.fileTypes.SyntaxHighlighter) Place(com.intellij.psi.impl.source.tree.injected.Place) Project(com.intellij.openapi.project.Project) HighlightInfoHolder(com.intellij.codeInsight.daemon.impl.analysis.HighlightInfoHolder) DumbAware(com.intellij.openapi.project.DumbAware) SyntaxHighlighterFactory(com.intellij.openapi.fileTypes.SyntaxHighlighterFactory) InjectedLanguageUtil(com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil) CommonProcessors(com.intellij.util.CommonProcessors) ProgressManager(com.intellij.openapi.progress.ProgressManager) DocumentWindow(com.intellij.injected.editor.DocumentWindow) Processors(com.intellij.util.Processors) HighlighterColors(com.intellij.openapi.editor.HighlighterColors) Editor(com.intellij.openapi.editor.Editor) Nullable(org.jetbrains.annotations.Nullable) TextAttributesKey(com.intellij.openapi.editor.colors.TextAttributesKey) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) EditorColors(com.intellij.openapi.editor.colors.EditorColors) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) Processor(com.intellij.util.Processor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) Registry(com.intellij.openapi.util.registry.Registry) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) NotNull(org.jetbrains.annotations.NotNull) THashSet(gnu.trove.THashSet) DocumentWindow(com.intellij.injected.editor.DocumentWindow) InjectedLanguageManagerImpl(com.intellij.psi.impl.source.tree.injected.InjectedLanguageManagerImpl) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) NotNull(org.jetbrains.annotations.NotNull)

Example 38 with DocumentWindow

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

the class LocalInspectionsPass method addDescriptorsFromInjectedResults.

private void addDescriptorsFromInjectedResults(@NotNull InspectionManager iManager, @NotNull GlobalInspectionContextImpl context) {
    InjectedLanguageManager ilManager = InjectedLanguageManager.getInstance(myProject);
    PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myProject);
    for (Map.Entry<PsiFile, List<InspectionResult>> entry : result.entrySet()) {
        PsiFile file = entry.getKey();
        // not injected
        if (file == getFile())
            continue;
        DocumentWindow documentRange = (DocumentWindow) documentManager.getDocument(file);
        List<InspectionResult> resultList = entry.getValue();
        for (InspectionResult inspectionResult : resultList) {
            LocalInspectionToolWrapper toolWrapper = inspectionResult.tool;
            for (ProblemDescriptor descriptor : inspectionResult.foundProblems) {
                PsiElement psiElement = descriptor.getPsiElement();
                if (psiElement == null)
                    continue;
                if (SuppressionUtil.inspectionResultSuppressed(psiElement, toolWrapper.getTool()))
                    continue;
                List<TextRange> editables = ilManager.intersectWithAllEditableFragments(file, ((ProblemDescriptorBase) descriptor).getTextRange());
                for (TextRange editable : editables) {
                    TextRange hostRange = documentRange.injectedToHost(editable);
                    QuickFix[] fixes = descriptor.getFixes();
                    LocalQuickFix[] localFixes = null;
                    if (fixes != null) {
                        localFixes = new LocalQuickFix[fixes.length];
                        for (int k = 0; k < fixes.length; k++) {
                            QuickFix fix = fixes[k];
                            localFixes[k] = (LocalQuickFix) fix;
                        }
                    }
                    ProblemDescriptor patchedDescriptor = iManager.createProblemDescriptor(getFile(), hostRange, descriptor.getDescriptionTemplate(), descriptor.getHighlightType(), true, localFixes);
                    addDescriptors(toolWrapper, patchedDescriptor, context);
                }
            }
        }
    }
}
Also used : TextRange(com.intellij.openapi.util.TextRange) InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) DocumentWindow(com.intellij.injected.editor.DocumentWindow) THashMap(gnu.trove.THashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 39 with DocumentWindow

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

the class LocalInspectionsPass method createHighlightsForDescriptor.

private void createHighlightsForDescriptor(@NotNull List<HighlightInfo> outInfos, @NotNull Set<Pair<TextRange, String>> emptyActionRegistered, @NotNull InjectedLanguageManager ilManager, @NotNull PsiFile file, @NotNull Document documentRange, @NotNull LocalInspectionToolWrapper toolWrapper, @NotNull HighlightSeverity severity, @NotNull ProblemDescriptor descriptor, @NotNull PsiElement element) {
    LocalInspectionTool tool = toolWrapper.getTool();
    if (myIgnoreSuppressed && SuppressionUtil.inspectionResultSuppressed(element, tool))
        return;
    HighlightInfoType level = ProblemDescriptorUtil.highlightTypeFromDescriptor(descriptor, severity, mySeverityRegistrar);
    HighlightInfo info = createHighlightInfo(descriptor, toolWrapper, level, emptyActionRegistered, element);
    if (info == null)
        return;
    PsiFile context = getTopLevelFileInBaseLanguage(element);
    PsiFile myContext = getTopLevelFileInBaseLanguage(getFile());
    if (context != getFile()) {
        LOG.error("Reported element " + element + " is not from the file '" + file + "' the inspection '" + toolWrapper + "' (" + tool.getClass() + ") " + "was invoked for. Message: '" + descriptor + "'.\n" + "Element' containing file: " + context + "\n" + "Inspection invoked for file: " + myContext + "\n");
    }
    boolean isInjected = file != getFile();
    if (!isInjected) {
        outInfos.add(info);
        return;
    }
    // todo we got to separate our "internal" prefixes/suffixes from user-defined ones
    // todo in the latter case the errors should be highlighted, otherwise not
    List<TextRange> editables = ilManager.intersectWithAllEditableFragments(file, new TextRange(info.startOffset, info.endOffset));
    for (TextRange editable : editables) {
        TextRange hostRange = ((DocumentWindow) documentRange).injectedToHost(editable);
        int start = hostRange.getStartOffset();
        int end = hostRange.getEndOffset();
        HighlightInfo.Builder builder = HighlightInfo.newHighlightInfo(info.type).range(element, start, end);
        String description = info.getDescription();
        if (description != null) {
            builder.description(description);
        }
        String toolTip = info.getToolTip();
        if (toolTip != null) {
            builder.escapedToolTip(toolTip);
        }
        HighlightInfo patched = builder.createUnconditionally();
        if (patched.startOffset != patched.endOffset || info.startOffset == info.endOffset) {
            patched.setFromInjection(true);
            registerQuickFixes(toolWrapper, descriptor, patched, emptyActionRegistered);
            outInfos.add(patched);
        }
    }
}
Also used : DocumentWindow(com.intellij.injected.editor.DocumentWindow) TextRange(com.intellij.openapi.util.TextRange)

Example 40 with DocumentWindow

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

the class InjectedGeneralHighlightingPass method addInjectedPsiHighlights.

private boolean addInjectedPsiHighlights(@NotNull PsiFile injectedPsi, TextAttributes injectedAttributes, @NotNull Collection<HighlightInfo> outInfos, @NotNull ProgressIndicator progress, @NotNull InjectedLanguageManager injectedLanguageManager) {
    DocumentWindow documentWindow = (DocumentWindow) PsiDocumentManager.getInstance(myProject).getCachedDocument(injectedPsi);
    if (documentWindow == null)
        return true;
    Place places = InjectedLanguageUtil.getShreds(injectedPsi);
    boolean addTooltips = places.size() < 100;
    for (PsiLanguageInjectionHost.Shred place : places) {
        PsiLanguageInjectionHost host = place.getHost();
        if (host == null)
            continue;
        TextRange textRange = place.getRangeInsideHost().shiftRight(host.getTextRange().getStartOffset());
        if (textRange.isEmpty())
            continue;
        HighlightInfo.Builder builder = HighlightInfo.newHighlightInfo(HighlightInfoType.INJECTED_LANGUAGE_BACKGROUND).range(textRange);
        if (injectedAttributes != null && InjectedLanguageUtil.isHighlightInjectionBackground(host)) {
            builder.textAttributes(injectedAttributes);
        }
        if (addTooltips) {
            String desc = injectedPsi.getLanguage().getDisplayName() + ": " + injectedPsi.getText();
            builder.unescapedToolTip(desc);
        }
        HighlightInfo info = builder.createUnconditionally();
        info.setFromInjection(true);
        outInfos.add(info);
    }
    HighlightInfoHolder holder = createInfoHolder(injectedPsi);
    runHighlightVisitorsForInjected(injectedPsi, holder, progress);
    for (int i = 0; i < holder.size(); i++) {
        HighlightInfo info = holder.get(i);
        final int startOffset = documentWindow.injectedToHost(info.startOffset);
        final TextRange fixedTextRange = getFixedTextRange(documentWindow, startOffset);
        addPatchedInfos(info, injectedPsi, documentWindow, injectedLanguageManager, fixedTextRange, outInfos);
    }
    int injectedStart = holder.size();
    highlightInjectedSyntax(injectedPsi, holder);
    for (int i = injectedStart; i < holder.size(); i++) {
        HighlightInfo info = holder.get(i);
        final int startOffset = info.startOffset;
        final TextRange fixedTextRange = getFixedTextRange(documentWindow, startOffset);
        if (fixedTextRange == null) {
            info.setFromInjection(true);
            outInfos.add(info);
        } else {
            HighlightInfo patched = new HighlightInfo(info.forcedTextAttributes, info.forcedTextAttributesKey, info.type, fixedTextRange.getStartOffset(), fixedTextRange.getEndOffset(), info.getDescription(), info.getToolTip(), info.type.getSeverity(null), info.isAfterEndOfLine(), null, false, 0, info.getProblemGroup(), info.getGutterIconRenderer());
            patched.setFromInjection(true);
            outInfos.add(patched);
        }
    }
    if (!isDumbMode()) {
        List<HighlightInfo> todos = new ArrayList<>();
        highlightTodos(injectedPsi, injectedPsi.getText(), 0, injectedPsi.getTextLength(), progress, myPriorityRange, todos, todos);
        for (HighlightInfo info : todos) {
            addPatchedInfos(info, injectedPsi, documentWindow, injectedLanguageManager, null, outInfos);
        }
    }
    advanceProgress(1);
    return true;
}
Also used : DocumentWindow(com.intellij.injected.editor.DocumentWindow) HighlightInfoHolder(com.intellij.codeInsight.daemon.impl.analysis.HighlightInfoHolder) Place(com.intellij.psi.impl.source.tree.injected.Place)

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