Search in sources :

Example 26 with DocumentWindow

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

the class LightPlatformCodeInsightTestCase method bringRealEditorBack.

protected static void bringRealEditorBack() {
    PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
    if (myEditor instanceof EditorWindow) {
        Document document = ((DocumentWindow) myEditor.getDocument()).getDelegate();
        myFile = PsiDocumentManager.getInstance(getProject()).getPsiFile(document);
        myEditor = ((EditorWindow) myEditor).getDelegate();
        myVFile = myFile.getVirtualFile();
    }
}
Also used : DocumentWindow(com.intellij.injected.editor.DocumentWindow) Document(com.intellij.openapi.editor.Document) EditorWindow(com.intellij.injected.editor.EditorWindow)

Example 27 with DocumentWindow

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

the class CssWriter method write.

@Nullable
private byte[] write(@NotNull StylesheetFile stylesheetFile, @NotNull Document document, @NotNull Module module) {
    CssStylesheet stylesheet = stylesheetFile.getStylesheet();
    if (stylesheet == null) {
        LOG.warn("Stylesheet is null for " + stylesheetFile.getName());
        return null;
    }
    rulesetVectorWriter.prepareIteration();
    DocumentWindow documentWindow = document instanceof DocumentWindow ? (DocumentWindow) document : null;
    for (CssRuleset ruleset : stylesheet.getRulesets()) {
        CssBlock block = ruleset.getBlock();
        if (block == null) {
            continue;
        }
        PrimitiveAmfOutputStream rulesetOut = rulesetVectorWriter.getOutputForIteration();
        int textOffset = ruleset.getTextOffset();
        if (documentWindow == null) {
            rulesetOut.writeUInt29(document.getLineNumber(textOffset) + 1);
        } else {
            rulesetOut.writeUInt29(documentWindow.injectedToHostLine(document.getLineNumber(textOffset)) + 1);
            textOffset = documentWindow.injectedToHost(textOffset);
        }
        rulesetOut.writeUInt29(textOffset);
        writeSelectors(ruleset, rulesetOut, module);
        declarationVectorWriter.prepareIteration();
        for (CssDeclaration declaration : block.getDeclarations()) {
            CssTermList value = declaration.getValue();
            if (value == null || PsiTreeUtil.getChildOfType(value, PsiErrorElement.class) != null) {
                continue;
            }
            propertyOut = declarationVectorWriter.getOutputForIteration();
            try {
                stringWriter.write(declaration.getPropertyName(), propertyOut);
                textOffset = declaration.getTextOffset();
                propertyOut.writeUInt29(documentWindow == null ? textOffset : documentWindow.injectedToHost(textOffset));
                CssPropertyDescriptor propertyDescriptor = ContainerUtil.getFirstItem(declaration.getDescriptors());
                writePropertyValue(value, propertyDescriptor != null && propertyDescriptor instanceof FlexCssPropertyDescriptor ? ((FlexCssPropertyDescriptor) propertyDescriptor).getStyleInfo() : null);
                continue;
            } catch (RuntimeException e) {
                problemsHolder.add(declaration, e, declaration.getPropertyName());
            } catch (Throwable e) {
                problemsHolder.add(e);
            }
            declarationVectorWriter.rollbackLastIteration();
        }
        // must be written in any case, IDEA-86219, ruleset without rules
        declarationVectorWriter.writeTo(rulesetOut);
    }
    PrimitiveAmfOutputStream outputForCustomData = rulesetVectorWriter.getOutputForCustomData();
    CssNamespace[] namespaces = stylesheet.getNamespaces();
    outputForCustomData.write(namespaces.length);
    if (namespaces.length > 0) {
        for (CssNamespace cssNamespace : namespaces) {
            stringWriter.writeNullable(cssNamespace.getPrefix(), outputForCustomData);
            stringWriter.writeNullable(cssNamespace.getUri(), outputForCustomData);
        }
    }
    return IOUtil.getBytes(rulesetVectorWriter);
}
Also used : FlexCssPropertyDescriptor(com.intellij.javascript.flex.css.FlexCssPropertyDescriptor) FlexCssPropertyDescriptor(com.intellij.javascript.flex.css.FlexCssPropertyDescriptor) DocumentWindow(com.intellij.injected.editor.DocumentWindow) Nullable(org.jetbrains.annotations.Nullable)

Example 28 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 29 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 30 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)

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