Search in sources :

Example 41 with EditorWindow

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

the class HighlightManagerImpl method addOccurrenceHighlights.

@Override
public void addOccurrenceHighlights(@NotNull Editor editor, @NotNull PsiElement[] elements, @NotNull TextAttributes attributes, boolean hideByTextChange, Collection<RangeHighlighter> outHighlighters) {
    if (elements.length == 0)
        return;
    int flags = HIDE_BY_ESCAPE;
    if (hideByTextChange) {
        flags |= HIDE_BY_TEXT_CHANGE;
    }
    Color scrollmarkColor = getScrollMarkColor(attributes);
    if (editor instanceof EditorWindow) {
        editor = ((EditorWindow) editor).getDelegate();
    }
    for (PsiElement element : elements) {
        TextRange range = element.getTextRange();
        range = InjectedLanguageManager.getInstance(myProject).injectedToHost(element, range);
        addOccurrenceHighlight(editor, trimOffsetToDocumentSize(editor, range.getStartOffset()), trimOffsetToDocumentSize(editor, range.getEndOffset()), attributes, flags, outHighlighters, scrollmarkColor);
    }
}
Also used : TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) EditorWindow(com.intellij.injected.editor.EditorWindow)

Example 42 with EditorWindow

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

the class HighlightUsagesHandler method clearHighlights.

private static void clearHighlights(Editor editor, HighlightManager highlightManager, List<TextRange> rangesToHighlight, TextAttributes attributes) {
    if (editor instanceof EditorWindow)
        editor = ((EditorWindow) editor).getDelegate();
    RangeHighlighter[] highlighters = ((HighlightManagerImpl) highlightManager).getHighlighters(editor);
    Arrays.sort(highlighters, (o1, o2) -> o1.getStartOffset() - o2.getStartOffset());
    Collections.sort(rangesToHighlight, (o1, o2) -> o1.getStartOffset() - o2.getStartOffset());
    int i = 0;
    int j = 0;
    while (i < highlighters.length && j < rangesToHighlight.size()) {
        RangeHighlighter highlighter = highlighters[i];
        TextRange highlighterRange = TextRange.create(highlighter);
        TextRange refRange = rangesToHighlight.get(j);
        if (refRange.equals(highlighterRange) && attributes.equals(highlighter.getTextAttributes()) && highlighter.getLayer() == HighlighterLayer.SELECTION - 1) {
            highlightManager.removeSegmentHighlighter(editor, highlighter);
            i++;
        } else if (refRange.getStartOffset() > highlighterRange.getEndOffset()) {
            i++;
        } else if (refRange.getEndOffset() < highlighterRange.getStartOffset()) {
            j++;
        } else {
            i++;
            j++;
        }
    }
}
Also used : RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) TextRange(com.intellij.openapi.util.TextRange) EditorWindow(com.intellij.injected.editor.EditorWindow)

Example 43 with EditorWindow

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

the class RenameTo method applyFix.

@SuppressWarnings({ "SSBasedInspection" })
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
    DictionarySuggestionProvider provider = findProvider();
    if (provider != null) {
        provider.setActive(true);
    }
    Editor editor = getEditorFromFocus();
    HashMap<String, Object> map = new HashMap<>();
    PsiElement psiElement = descriptor.getPsiElement();
    if (psiElement == null)
        return;
    PsiFile containingFile = psiElement.getContainingFile();
    if (editor == null) {
        editor = InjectedLanguageUtil.openEditorFor(containingFile, project);
    }
    if (editor == null)
        return;
    if (editor instanceof EditorWindow) {
        map.put(CommonDataKeys.EDITOR.getName(), editor);
        map.put(CommonDataKeys.PSI_ELEMENT.getName(), psiElement);
    } else if (ApplicationManager.getApplication().isUnitTestMode()) {
        // TextEditorComponent / FiledEditorManagerImpl give away the data in real life
        map.put(CommonDataKeys.PSI_ELEMENT.getName(), new TextEditorPsiDataProvider().getData(CommonDataKeys.PSI_ELEMENT.getName(), editor, editor.getCaretModel().getCurrentCaret()));
    }
    final Boolean selectAll = editor.getUserData(RenameHandlerRegistry.SELECT_ALL);
    try {
        editor.putUserData(RenameHandlerRegistry.SELECT_ALL, true);
        DataContext dataContext = SimpleDataContext.getSimpleContext(map, DataManager.getInstance().getDataContext(editor.getComponent()));
        AnAction action = new RenameElementAction();
        AnActionEvent event = AnActionEvent.createFromAnAction(action, null, "", dataContext);
        action.actionPerformed(event);
        if (provider != null) {
            provider.setActive(false);
        }
    } finally {
        editor.putUserData(RenameHandlerRegistry.SELECT_ALL, selectAll);
    }
}
Also used : HashMap(com.intellij.util.containers.HashMap) EditorWindow(com.intellij.injected.editor.EditorWindow) RenameElementAction(com.intellij.refactoring.actions.RenameElementAction) TextEditorPsiDataProvider(com.intellij.openapi.fileEditor.impl.text.TextEditorPsiDataProvider) SimpleDataContext(com.intellij.openapi.actionSystem.impl.SimpleDataContext) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement)

Example 44 with EditorWindow

use of com.intellij.injected.editor.EditorWindow in project intellij by bazelbuild.

the class ProjectViewEnterHandler method preprocessEnter.

@Override
public Result preprocessEnter(PsiFile file, Editor editor, Ref<Integer> caretOffset, Ref<Integer> caretAdvance, DataContext dataContext, EditorActionHandler originalHandler) {
    int offset = caretOffset.get();
    if (editor instanceof EditorWindow) {
        file = InjectedLanguageManager.getInstance(file.getProject()).getTopLevelFile(file);
        editor = InjectedLanguageUtil.getTopLevelEditor(editor);
        offset = editor.getCaretModel().getOffset();
    }
    if (!isApplicable(file, dataContext) || !insertIndent(file, offset)) {
        return Result.Continue;
    }
    int indent = SectionParser.INDENT;
    editor.getCaretModel().moveToOffset(offset);
    Document doc = editor.getDocument();
    PsiDocumentManager.getInstance(file.getProject()).commitDocument(doc);
    originalHandler.execute(editor, editor.getCaretModel().getCurrentCaret(), dataContext);
    LogicalPosition position = editor.getCaretModel().getLogicalPosition();
    if (position.column < indent) {
        String spaces = StringUtil.repeatSymbol(' ', indent - position.column);
        doc.insertString(editor.getCaretModel().getOffset(), spaces);
    }
    editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(position.line, indent));
    return Result.Stop;
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) Document(com.intellij.openapi.editor.Document) EditorWindow(com.intellij.injected.editor.EditorWindow)

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