Search in sources :

Example 61 with SelectionModel

use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.

the class UnwrapDescriptorBase method findTargetElement.

@Nullable
protected PsiElement findTargetElement(Editor editor, PsiFile file) {
    int offset = editor.getCaretModel().getOffset();
    PsiElement endElement = file.findElementAt(offset);
    SelectionModel selectionModel = editor.getSelectionModel();
    if (selectionModel.hasSelection() && selectionModel.getSelectionStart() < offset) {
        PsiElement startElement = file.findElementAt(selectionModel.getSelectionStart());
        if (startElement != null && startElement != endElement && startElement.getTextRange().getEndOffset() == offset) {
            return startElement;
        }
    }
    return endElement;
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 62 with SelectionModel

use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.

the class StringLiteralCopyPasteProcessor method preprocessOnPaste.

@NotNull
@Override
public String preprocessOnPaste(final Project project, final PsiFile file, final Editor editor, String text, final RawText rawText) {
    final Document document = editor.getDocument();
    PsiDocumentManager.getInstance(project).commitDocument(document);
    final SelectionModel selectionModel = editor.getSelectionModel();
    // pastes in block selection mode (column mode) are not handled by a CopyPasteProcessor
    final int selectionStart = selectionModel.getSelectionStart();
    final int selectionEnd = selectionModel.getSelectionEnd();
    PsiElement token = findLiteralTokenType(file, selectionStart, selectionEnd);
    if (token == null) {
        return text;
    }
    if (rawText != null && wasUnescaped(text, rawText.rawText))
        return rawText.rawText;
    if (isStringLiteral(token)) {
        StringBuilder buffer = new StringBuilder(text.length());
        @NonNls String breaker = getLineBreaker(token);
        final String[] lines = LineTokenizer.tokenize(text.toCharArray(), false, true);
        for (int i = 0; i < lines.length; i++) {
            buffer.append(escapeCharCharacters(lines[i], token));
            if (i != lines.length - 1) {
                buffer.append(breaker);
            } else if (text.endsWith("\n")) {
                buffer.append("\\n");
            }
        }
        text = buffer.toString();
    } else if (isCharLiteral(token)) {
        return escapeCharCharacters(text, token);
    }
    return text;
}
Also used : NonNls(org.jetbrains.annotations.NonNls) SelectionModel(com.intellij.openapi.editor.SelectionModel) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 63 with SelectionModel

use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.

the class LivePreview method dumpEditorMarkupAndSelection.

private void dumpEditorMarkupAndSelection(PrintStream dumpStream) {
    dumpStream.println(mySearchResults.getFindModel());
    if (myReplacementPreviewText != null) {
        dumpStream.println("--");
        dumpStream.println("Replacement Preview: " + myReplacementPreviewText);
    }
    dumpStream.println("--");
    Editor editor = mySearchResults.getEditor();
    RangeHighlighter[] highlighters = editor.getMarkupModel().getAllHighlighters();
    List<Pair<Integer, Character>> ranges = new ArrayList<>();
    for (RangeHighlighter highlighter : highlighters) {
        ranges.add(new Pair<>(highlighter.getStartOffset(), '['));
        ranges.add(new Pair<>(highlighter.getEndOffset(), ']'));
    }
    SelectionModel selectionModel = editor.getSelectionModel();
    if (selectionModel.getSelectionStart() != selectionModel.getSelectionEnd()) {
        ranges.add(new Pair<>(selectionModel.getSelectionStart(), '<'));
        ranges.add(new Pair<>(selectionModel.getSelectionEnd(), '>'));
    }
    ranges.add(new Pair<>(-1, '\n'));
    ranges.add(new Pair<>(editor.getDocument().getTextLength() + 1, '\n'));
    ContainerUtil.sort(ranges, (pair, pair2) -> {
        int res = pair.first - pair2.first;
        if (res == 0) {
            Character c1 = pair.second;
            Character c2 = pair2.second;
            if (c1 == '<' && c2 == '[') {
                return 1;
            } else if (c1 == '[' && c2 == '<') {
                return -1;
            }
            return c1.compareTo(c2);
        }
        return res;
    });
    Document document = editor.getDocument();
    for (int i = 0; i < ranges.size() - 1; ++i) {
        Pair<Integer, Character> pair = ranges.get(i);
        Pair<Integer, Character> pair1 = ranges.get(i + 1);
        dumpStream.print(pair.second + document.getText(TextRange.create(Math.max(pair.first, 0), Math.min(pair1.first, document.getTextLength()))));
    }
    dumpStream.println("\n--");
    if (NotFound) {
        dumpStream.println("Not Found");
        dumpStream.println("--");
        NotFound = false;
    }
    for (RangeHighlighter highlighter : highlighters) {
        dumpStream.println(highlighter + " : " + highlighter.getTextAttributes());
    }
    dumpStream.println("------------");
}
Also used : ArrayList(java.util.ArrayList) Document(com.intellij.openapi.editor.Document) RelativePoint(com.intellij.ui.awt.RelativePoint) RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) SelectionModel(com.intellij.openapi.editor.SelectionModel) Editor(com.intellij.openapi.editor.Editor) Pair(com.intellij.openapi.util.Pair)

Example 64 with SelectionModel

use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.

the class LivePreview method updateInSelectionHighlighters.

private void updateInSelectionHighlighters() {
    if (mySearchResults.getEditor() == null)
        return;
    final SelectionModel selectionModel = mySearchResults.getEditor().getSelectionModel();
    int[] starts = selectionModel.getBlockSelectionStarts();
    int[] ends = selectionModel.getBlockSelectionEnds();
    final HashSet<RangeHighlighter> toRemove = new HashSet<>();
    Set<RangeHighlighter> toAdd = new HashSet<>();
    for (RangeHighlighter highlighter : myHighlighters) {
        if (!highlighter.isValid())
            continue;
        boolean intersectsWithSelection = false;
        for (int i = 0; i < starts.length; ++i) {
            TextRange selectionRange = new TextRange(starts[i], ends[i]);
            intersectsWithSelection = selectionRange.intersects(highlighter.getStartOffset(), highlighter.getEndOffset()) && selectionRange.getEndOffset() != highlighter.getStartOffset() && highlighter.getEndOffset() != selectionRange.getStartOffset();
            if (intersectsWithSelection)
                break;
        }
        final Object userData = highlighter.getUserData(IN_SELECTION_KEY);
        if (userData != null) {
            if (!intersectsWithSelection) {
                if (userData == IN_SELECTION2) {
                    HighlightManager.getInstance(mySearchResults.getProject()).removeSegmentHighlighter(mySearchResults.getEditor(), highlighter);
                    toRemove.add(highlighter);
                } else {
                    highlighter.putUserData(IN_SELECTION_KEY, null);
                }
            }
        } else if (intersectsWithSelection) {
            TextRange cursor = mySearchResults.getCursor();
            if (cursor != null && highlighter.getStartOffset() == cursor.getStartOffset() && highlighter.getEndOffset() == cursor.getEndOffset())
                continue;
            final RangeHighlighter toAnnotate = highlightRange(new TextRange(highlighter.getStartOffset(), highlighter.getEndOffset()), new TextAttributes(null, null, Color.WHITE, EffectType.ROUNDED_BOX, Font.PLAIN), toAdd);
            highlighter.putUserData(IN_SELECTION_KEY, IN_SELECTION1);
            toAnnotate.putUserData(IN_SELECTION_KEY, IN_SELECTION2);
        }
    }
    myHighlighters.removeAll(toRemove);
    myHighlighters.addAll(toAdd);
}
Also used : RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) SelectionModel(com.intellij.openapi.editor.SelectionModel) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) TextRange(com.intellij.openapi.util.TextRange) RelativePoint(com.intellij.ui.awt.RelativePoint) HashSet(java.util.HashSet)

Example 65 with SelectionModel

use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.

the class BraceHighlighter method runActivity.

@Override
public void runActivity(@NotNull final Project project) {
    // sorry, upsource
    if (ApplicationManager.getApplication().isHeadlessEnvironment())
        return;
    final EditorEventMulticaster eventMulticaster = EditorFactory.getInstance().getEventMulticaster();
    CaretListener myCaretListener = new CaretAdapter() {

        @Override
        public void caretPositionChanged(CaretEvent e) {
            myAlarm.cancelAllRequests();
            Editor editor = e.getEditor();
            final SelectionModel selectionModel = editor.getSelectionModel();
            // Don't update braces in case of the active selection.
            if (editor.getProject() != project || selectionModel.hasSelection()) {
                return;
            }
            final Document document = editor.getDocument();
            int line = e.getNewPosition().line;
            if (line < 0 || line >= document.getLineCount()) {
                return;
            }
            updateBraces(editor, myAlarm);
        }
    };
    eventMulticaster.addCaretListener(myCaretListener, project);
    final SelectionListener mySelectionListener = new SelectionListener() {

        @Override
        public void selectionChanged(SelectionEvent e) {
            myAlarm.cancelAllRequests();
            Editor editor = e.getEditor();
            if (editor.getProject() != project) {
                return;
            }
            final TextRange oldRange = e.getOldRange();
            final TextRange newRange = e.getNewRange();
            if (oldRange != null && newRange != null && !(oldRange.isEmpty() ^ newRange.isEmpty())) {
                // Don't perform braces update in case of active/absent selection.
                return;
            }
            updateBraces(editor, myAlarm);
        }
    };
    eventMulticaster.addSelectionListener(mySelectionListener, project);
    DocumentListener documentListener = new DocumentAdapter() {

        @Override
        public void documentChanged(DocumentEvent e) {
            myAlarm.cancelAllRequests();
            Editor[] editors = EditorFactory.getInstance().getEditors(e.getDocument(), project);
            for (Editor editor : editors) {
                updateBraces(editor, myAlarm);
            }
        }
    };
    eventMulticaster.addDocumentListener(documentListener, project);
    final FocusChangeListener myFocusChangeListener = new FocusChangeListener() {

        @Override
        public void focusLost(Editor editor) {
            clearBraces(editor);
        }

        @Override
        public void focusGained(Editor editor) {
            updateBraces(editor, myAlarm);
        }
    };
    ((EditorEventMulticasterEx) eventMulticaster).addFocusChangeListner(myFocusChangeListener, project);
    final FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
    fileEditorManager.addFileEditorManagerListener(new FileEditorManagerListener() {

        @Override
        public void selectionChanged(@NotNull FileEditorManagerEvent e) {
            myAlarm.cancelAllRequests();
        }
    }, project);
}
Also used : TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) EditorEventMulticasterEx(com.intellij.openapi.editor.ex.EditorEventMulticasterEx) SelectionModel(com.intellij.openapi.editor.SelectionModel) Editor(com.intellij.openapi.editor.Editor) FileEditorManagerEvent(com.intellij.openapi.fileEditor.FileEditorManagerEvent) FileEditorManagerListener(com.intellij.openapi.fileEditor.FileEditorManagerListener) FocusChangeListener(com.intellij.openapi.editor.ex.FocusChangeListener)

Aggregations

SelectionModel (com.intellij.openapi.editor.SelectionModel)76 TextRange (com.intellij.openapi.util.TextRange)21 Document (com.intellij.openapi.editor.Document)19 PsiElement (com.intellij.psi.PsiElement)19 NotNull (org.jetbrains.annotations.NotNull)16 Editor (com.intellij.openapi.editor.Editor)14 Nullable (org.jetbrains.annotations.Nullable)11 CaretModel (com.intellij.openapi.editor.CaretModel)10 PsiFile (com.intellij.psi.PsiFile)8 Project (com.intellij.openapi.project.Project)7 ArrayList (java.util.ArrayList)6 TemplateState (com.intellij.codeInsight.template.impl.TemplateState)3 SurroundDescriptor (com.intellij.lang.surroundWith.SurroundDescriptor)3 Pass (com.intellij.openapi.util.Pass)3 List (java.util.List)3 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)3 EditorWindow (com.intellij.injected.editor.EditorWindow)2 ApplicationManager (com.intellij.openapi.application.ApplicationManager)2 RangeHighlighter (com.intellij.openapi.editor.markup.RangeHighlighter)2 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)2