Search in sources :

Example 36 with EditorWindow

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

the class CreateVariableFix method invoke.

public void invoke(@NotNull final Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    editor = editor instanceof EditorWindow ? ((EditorWindow) editor).getDelegate() : editor;
    XmlTag tag = PsiTreeUtil.getContextOfType(myReference, XmlTag.class, true);
    if (tag == null)
        return;
    XmlTag xmlTag = tag.createChildTag("variable", XsltSupport.XSLT_NS, null, false);
    xmlTag.setAttribute("name", myReference.getReferencedName());
    xmlTag.setAttribute("select", "dummy");
    final XmlAttribute select = xmlTag.getAttribute("select", null);
    assert select != null;
    final PsiElement dummy = XsltSupport.getAttValueToken(select);
    assert dummy != null;
    final TemplateBuilderImpl builder = createTemplateBuilder(xmlTag);
    builder.replaceElement(dummy, new MacroCallNode(new CompleteMacro()));
    builder.setEndVariableAfter(select);
    final Template template = builder.buildTemplate();
    template.addTextSegment("\n");
    template.setToIndent(true);
    final XmlTag insertionPoint = findVariableInsertionPoint(tag);
    moveTo(editor, insertionPoint);
    TemplateManager.getInstance(project).startTemplate(editor, template);
}
Also used : TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) XmlAttribute(com.intellij.psi.xml.XmlAttribute) MacroCallNode(com.intellij.codeInsight.template.impl.MacroCallNode) CompleteMacro(com.intellij.codeInsight.template.macro.CompleteMacro) PsiElement(com.intellij.psi.PsiElement) EditorWindow(com.intellij.injected.editor.EditorWindow) XmlTag(com.intellij.psi.xml.XmlTag) Template(com.intellij.codeInsight.template.Template)

Example 37 with EditorWindow

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

the class LightDaemonAnalyzerTestCase method doHighlighting.

@NotNull
protected List<HighlightInfo> doHighlighting() {
    PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
    TIntArrayList toIgnoreList = new TIntArrayList();
    if (!doFolding()) {
        toIgnoreList.add(Pass.UPDATE_FOLDING);
    }
    if (!doInspections()) {
        toIgnoreList.add(Pass.LOCAL_INSPECTIONS);
        toIgnoreList.add(Pass.WHOLE_FILE_LOCAL_INSPECTIONS);
    }
    int[] toIgnore = toIgnoreList.isEmpty() ? ArrayUtil.EMPTY_INT_ARRAY : toIgnoreList.toNativeArray();
    Editor editor = getEditor();
    PsiFile file = getFile();
    if (editor instanceof EditorWindow) {
        editor = ((EditorWindow) editor).getDelegate();
        file = InjectedLanguageManager.getInstance(file.getProject()).getTopLevelFile(file);
    }
    return CodeInsightTestFixtureImpl.instantiateAndRun(file, editor, toIgnore, false);
}
Also used : PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor) TIntArrayList(gnu.trove.TIntArrayList) EditorWindow(com.intellij.injected.editor.EditorWindow) NotNull(org.jetbrains.annotations.NotNull)

Example 38 with EditorWindow

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

the class PassExecutorService method submitPasses.

void submitPasses(@NotNull Map<FileEditor, HighlightingPass[]> passesMap, @NotNull DaemonProgressIndicator updateProgress) {
    if (isDisposed())
        return;
    // null keys are ok
    MultiMap<Document, FileEditor> documentToEditors = MultiMap.createSet();
    MultiMap<FileEditor, TextEditorHighlightingPass> documentBoundPasses = MultiMap.createSmart();
    MultiMap<FileEditor, EditorBoundHighlightingPass> editorBoundPasses = MultiMap.createSmart();
    List<Pair<FileEditor, TextEditorHighlightingPass>> passesWithNoDocuments = new ArrayList<>();
    Set<VirtualFile> vFiles = new HashSet<>();
    for (Map.Entry<FileEditor, HighlightingPass[]> entry : passesMap.entrySet()) {
        FileEditor fileEditor = entry.getKey();
        HighlightingPass[] passes = entry.getValue();
        Document document;
        if (fileEditor instanceof TextEditor) {
            Editor editor = ((TextEditor) fileEditor).getEditor();
            LOG.assertTrue(!(editor instanceof EditorWindow));
            document = editor.getDocument();
        } else {
            VirtualFile virtualFile = ((FileEditorManagerEx) FileEditorManager.getInstance(myProject)).getFile(fileEditor);
            document = virtualFile == null ? null : FileDocumentManager.getInstance().getDocument(virtualFile);
        }
        if (document != null) {
            vFiles.add(FileDocumentManager.getInstance().getFile(document));
        }
        int prevId = 0;
        for (final HighlightingPass pass : passes) {
            if (pass instanceof EditorBoundHighlightingPass) {
                EditorBoundHighlightingPass editorPass = (EditorBoundHighlightingPass) pass;
                // have to make ids unique for this document
                editorPass.setId(nextPassId.incrementAndGet());
                editorBoundPasses.putValue(fileEditor, editorPass);
            } else {
                TextEditorHighlightingPass textEditorHighlightingPass = convertToTextHighlightingPass(pass, document, nextPassId, prevId);
                document = textEditorHighlightingPass.getDocument();
                documentBoundPasses.putValue(fileEditor, textEditorHighlightingPass);
                if (document == null) {
                    passesWithNoDocuments.add(Pair.create(fileEditor, textEditorHighlightingPass));
                } else {
                    documentToEditors.putValue(document, fileEditor);
                }
                prevId = textEditorHighlightingPass.getId();
            }
        }
    }
    List<ScheduledPass> freePasses = new ArrayList<>(documentToEditors.size() * 5);
    List<ScheduledPass> dependentPasses = new ArrayList<>(documentToEditors.size() * 10);
    // (fileEditor, passId) -> created pass
    Map<Pair<FileEditor, Integer>, ScheduledPass> toBeSubmitted = new THashMap<>(passesMap.size());
    final AtomicInteger threadsToStartCountdown = new AtomicInteger(0);
    for (Map.Entry<Document, Collection<FileEditor>> entry : documentToEditors.entrySet()) {
        Collection<FileEditor> fileEditors = entry.getValue();
        Document document = entry.getKey();
        FileEditor preferredFileEditor = getPreferredFileEditor(document, fileEditors);
        List<TextEditorHighlightingPass> passes = (List<TextEditorHighlightingPass>) documentBoundPasses.get(preferredFileEditor);
        if (passes.isEmpty()) {
            continue;
        }
        sortById(passes);
        for (TextEditorHighlightingPass currentPass : passes) {
            createScheduledPass(preferredFileEditor, currentPass, toBeSubmitted, passes, freePasses, dependentPasses, updateProgress, threadsToStartCountdown);
        }
    }
    for (Map.Entry<FileEditor, Collection<EditorBoundHighlightingPass>> entry : editorBoundPasses.entrySet()) {
        FileEditor fileEditor = entry.getKey();
        Collection<EditorBoundHighlightingPass> createdEditorBoundPasses = entry.getValue();
        List<TextEditorHighlightingPass> createdDocumentBoundPasses = (List<TextEditorHighlightingPass>) documentBoundPasses.get(fileEditor);
        List<TextEditorHighlightingPass> allCreatedPasses = new ArrayList<>(createdDocumentBoundPasses);
        allCreatedPasses.addAll(createdEditorBoundPasses);
        for (EditorBoundHighlightingPass pass : createdEditorBoundPasses) {
            createScheduledPass(fileEditor, pass, toBeSubmitted, allCreatedPasses, freePasses, dependentPasses, updateProgress, threadsToStartCountdown);
        }
    }
    for (Pair<FileEditor, TextEditorHighlightingPass> pair : passesWithNoDocuments) {
        FileEditor fileEditor = pair.first;
        TextEditorHighlightingPass pass = pair.second;
        createScheduledPass(fileEditor, pass, toBeSubmitted, ContainerUtil.emptyList(), freePasses, dependentPasses, updateProgress, threadsToStartCountdown);
    }
    if (CHECK_CONSISTENCY && !ApplicationInfoImpl.isInStressTest()) {
        assertConsistency(freePasses, toBeSubmitted, threadsToStartCountdown);
    }
    log(updateProgress, null, vFiles + " ----- starting " + threadsToStartCountdown.get(), freePasses);
    for (ScheduledPass dependentPass : dependentPasses) {
        mySubmittedPasses.put(dependentPass, Job.NULL_JOB);
    }
    for (ScheduledPass freePass : freePasses) {
        submit(freePass);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileEditor(com.intellij.openapi.fileEditor.FileEditor) Document(com.intellij.openapi.editor.Document) THashMap(gnu.trove.THashMap) EditorBoundHighlightingPass(com.intellij.codeHighlighting.EditorBoundHighlightingPass) Pair(com.intellij.openapi.util.Pair) HighlightingPass(com.intellij.codeHighlighting.HighlightingPass) EditorBoundHighlightingPass(com.intellij.codeHighlighting.EditorBoundHighlightingPass) TextEditorHighlightingPass(com.intellij.codeHighlighting.TextEditorHighlightingPass) FileEditorManagerEx(com.intellij.openapi.fileEditor.ex.FileEditorManagerEx) EditorWindow(com.intellij.injected.editor.EditorWindow) TextEditorHighlightingPass(com.intellij.codeHighlighting.TextEditorHighlightingPass) TextEditor(com.intellij.openapi.fileEditor.TextEditor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TextEditor(com.intellij.openapi.fileEditor.TextEditor) Editor(com.intellij.openapi.editor.Editor) FileEditor(com.intellij.openapi.fileEditor.FileEditor) THashMap(gnu.trove.THashMap) TIntObjectHashMap(gnu.trove.TIntObjectHashMap) MultiMap(com.intellij.util.containers.MultiMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 39 with EditorWindow

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

the class FoldingUpdate method updateInjectedFoldRegions.

@Nullable
public static Runnable updateInjectedFoldRegions(@NotNull final Editor editor, @NotNull final PsiFile file, final boolean applyDefaultState) {
    if (file instanceof PsiCompiledElement)
        return null;
    ApplicationManager.getApplication().assertReadAccessAllowed();
    final Project project = file.getProject();
    Document document = editor.getDocument();
    LOG.assertTrue(!PsiDocumentManager.getInstance(project).isUncommited(document));
    final FoldingModel foldingModel = editor.getFoldingModel();
    final long timeStamp = document.getModificationStamp();
    Object lastTimeStamp = editor.getUserData(LAST_UPDATE_INJECTED_STAMP_KEY);
    if (lastTimeStamp instanceof Long && ((Long) lastTimeStamp).longValue() == timeStamp)
        return null;
    List<DocumentWindow> injectedDocuments = InjectedLanguageUtil.getCachedInjectedDocuments(file);
    if (injectedDocuments.isEmpty())
        return null;
    final List<EditorWindow> injectedEditors = new ArrayList<>();
    final List<PsiFile> injectedFiles = new ArrayList<>();
    final List<FoldingMap> maps = new ArrayList<>();
    for (final DocumentWindow injectedDocument : injectedDocuments) {
        if (!injectedDocument.isValid()) {
            continue;
        }
        InjectedLanguageUtil.enumerate(injectedDocument, file, (injectedFile, places) -> {
            if (!injectedFile.isValid())
                return;
            Editor injectedEditor = InjectedLanguageUtil.getInjectedEditorForInjectedFile(editor, injectedFile);
            if (!(injectedEditor instanceof EditorWindow))
                return;
            injectedEditors.add((EditorWindow) injectedEditor);
            injectedFiles.add(injectedFile);
            final FoldingMap map = new FoldingMap();
            maps.add(map);
            getFoldingsFor(injectedFile, injectedEditor.getDocument(), map, false);
        });
    }
    return () -> {
        final ArrayList<Runnable> updateOperations = new ArrayList<>(injectedEditors.size());
        for (int i = 0; i < injectedEditors.size(); i++) {
            EditorWindow injectedEditor = injectedEditors.get(i);
            PsiFile injectedFile = injectedFiles.get(i);
            if (!injectedEditor.getDocument().isValid())
                continue;
            FoldingMap map = maps.get(i);
            updateOperations.add(new UpdateFoldRegionsOperation(project, injectedEditor, injectedFile, map, applyDefaultState ? EXCEPT_CARET_REGION : NO, !applyDefaultState, true));
        }
        foldingModel.runBatchFoldingOperation(() -> {
            for (Runnable operation : updateOperations) {
                operation.run();
            }
        });
        editor.putUserData(LAST_UPDATE_INJECTED_STAMP_KEY, timeStamp);
    };
}
Also used : Document(com.intellij.openapi.editor.Document) EditorWindow(com.intellij.injected.editor.EditorWindow) DocumentWindow(com.intellij.injected.editor.DocumentWindow) Project(com.intellij.openapi.project.Project) FoldingModel(com.intellij.openapi.editor.FoldingModel) Editor(com.intellij.openapi.editor.Editor) Nullable(org.jetbrains.annotations.Nullable)

Example 40 with EditorWindow

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

the class BraceHighlightingHandler method getHighlightersList.

@NotNull
private List<RangeHighlighter> getHighlightersList() {
    // braces are highlighted across the whole editor, not in each injected editor separately
    Editor editor = myEditor instanceof EditorWindow ? ((EditorWindow) myEditor).getDelegate() : myEditor;
    List<RangeHighlighter> highlighters = editor.getUserData(BRACE_HIGHLIGHTERS_IN_EDITOR_VIEW_KEY);
    if (highlighters == null) {
        highlighters = new ArrayList<>();
        editor.putUserData(BRACE_HIGHLIGHTERS_IN_EDITOR_VIEW_KEY, highlighters);
    }
    return highlighters;
}
Also used : Editor(com.intellij.openapi.editor.Editor) EditorWindow(com.intellij.injected.editor.EditorWindow) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

EditorWindow (com.intellij.injected.editor.EditorWindow)44 Editor (com.intellij.openapi.editor.Editor)15 TextRange (com.intellij.openapi.util.TextRange)10 Project (com.intellij.openapi.project.Project)9 Document (com.intellij.openapi.editor.Document)8 PsiFile (com.intellij.psi.PsiFile)8 NotNull (org.jetbrains.annotations.NotNull)8 PsiElement (com.intellij.psi.PsiElement)7 com.intellij.openapi.fileEditor (com.intellij.openapi.fileEditor)5 RangeHighlighter (com.intellij.openapi.editor.markup.RangeHighlighter)4 TemplateState (com.intellij.codeInsight.template.impl.TemplateState)3 EditorEx (com.intellij.openapi.editor.ex.EditorEx)3 LexerEditorHighlighter (com.intellij.openapi.editor.ex.util.LexerEditorHighlighter)3 EditorHighlighter (com.intellij.openapi.editor.highlighter.EditorHighlighter)3 ArrayList (java.util.ArrayList)3 HighlightManager (com.intellij.codeInsight.highlighting.HighlightManager)2 DocumentWindow (com.intellij.injected.editor.DocumentWindow)2 InjectedCaret (com.intellij.injected.editor.InjectedCaret)2 SimpleDataContext (com.intellij.openapi.actionSystem.impl.SimpleDataContext)2 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)2