Search in sources :

Example 1 with Place

use of com.intellij.psi.impl.source.tree.injected.Place in project intellij-community by JetBrains.

the class QuickEditAction method getExistingHandler.

public static QuickEditHandler getExistingHandler(@NotNull PsiFile injectedFile) {
    Place shreds = InjectedLanguageUtil.getShreds(injectedFile);
    DocumentWindow documentWindow = InjectedLanguageUtil.getDocumentWindow(injectedFile);
    if (shreds == null || documentWindow == null)
        return null;
    TextRange hostRange = TextRange.create(shreds.get(0).getHostRangeMarker().getStartOffset(), shreds.get(shreds.size() - 1).getHostRangeMarker().getEndOffset());
    for (Editor editor : EditorFactory.getInstance().getAllEditors()) {
        if (editor.getDocument() != documentWindow.getDelegate())
            continue;
        QuickEditHandler handler = editor.getUserData(QUICK_EDIT_HANDLER);
        if (handler != null && handler.changesRange(hostRange))
            return handler;
    }
    return null;
}
Also used : DocumentWindow(com.intellij.injected.editor.DocumentWindow) TextRange(com.intellij.openapi.util.TextRange) Editor(com.intellij.openapi.editor.Editor) Place(com.intellij.psi.impl.source.tree.injected.Place)

Example 2 with Place

use of com.intellij.psi.impl.source.tree.injected.Place 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 3 with Place

use of com.intellij.psi.impl.source.tree.injected.Place 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)

Example 4 with Place

use of com.intellij.psi.impl.source.tree.injected.Place in project intellij-community by JetBrains.

the class DocumentWindowImpl method areRangesEqual.

@Override
public boolean areRangesEqual(@NotNull DocumentWindow otherd) {
    DocumentWindowImpl window = (DocumentWindowImpl) otherd;
    Place shreds = getShreds();
    Place otherShreds = window.getShreds();
    if (shreds.size() != otherShreds.size())
        return false;
    for (int i = 0; i < shreds.size(); i++) {
        PsiLanguageInjectionHost.Shred shred = shreds.get(i);
        PsiLanguageInjectionHost.Shred otherShred = otherShreds.get(i);
        if (!shred.getPrefix().equals(otherShred.getPrefix()))
            return false;
        if (!shred.getSuffix().equals(otherShred.getSuffix()))
            return false;
        Segment hostRange = shred.getHostRangeMarker();
        Segment other = otherShred.getHostRangeMarker();
        if (hostRange == null || other == null || hostRange.getStartOffset() != other.getStartOffset())
            return false;
        if (hostRange.getEndOffset() != other.getEndOffset())
            return false;
    }
    return true;
}
Also used : PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) Place(com.intellij.psi.impl.source.tree.injected.Place)

Aggregations

Place (com.intellij.psi.impl.source.tree.injected.Place)4 DocumentWindow (com.intellij.injected.editor.DocumentWindow)3 HighlightInfoHolder (com.intellij.codeInsight.daemon.impl.analysis.HighlightInfoHolder)2 Editor (com.intellij.openapi.editor.Editor)2 JobLauncher (com.intellij.concurrency.JobLauncher)1 Language (com.intellij.lang.Language)1 InjectedLanguageManager (com.intellij.lang.injection.InjectedLanguageManager)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 Document (com.intellij.openapi.editor.Document)1 HighlighterColors (com.intellij.openapi.editor.HighlighterColors)1 EditorColors (com.intellij.openapi.editor.colors.EditorColors)1 TextAttributesKey (com.intellij.openapi.editor.colors.TextAttributesKey)1 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)1 SyntaxHighlighter (com.intellij.openapi.fileTypes.SyntaxHighlighter)1 SyntaxHighlighterFactory (com.intellij.openapi.fileTypes.SyntaxHighlighterFactory)1 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 ProgressManager (com.intellij.openapi.progress.ProgressManager)1 DumbAware (com.intellij.openapi.project.DumbAware)1 Project (com.intellij.openapi.project.Project)1