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;
}
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;
}
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("------------");
}
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);
}
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);
}
Aggregations