use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.
the class SelectedTextFormatter method getSelectedRange.
@NotNull
TextRange getSelectedRange() {
SelectionModel model = myEditor.getSelectionModel();
int start = model.getSelectionStart();
int end = model.getSelectionEnd();
return TextRange.create(start, end);
}
use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.
the class PyStatementMover method restoreCaretAndSelection.
private static void restoreCaretAndSelection(@NotNull final PsiFile file, @NotNull final Editor editor, boolean selectionStartAtCaret, boolean hasSelection, @NotNull final SelectionContainer selectionContainer, int shift, int offset, @NotNull final MyLineRange toMove) {
final Document document = editor.getDocument();
final SelectionModel selectionModel = editor.getSelectionModel();
final CaretModel caretModel = editor.getCaretModel();
Integer selectionLen = selectionContainer.myLen;
final PsiElement at = file.findElementAt(offset);
if (at != null) {
final PsiElement added = getCommentOrStatement(document, at);
int size = toMove.size;
if (size > 1) {
PsiElement tmp = added.getNextSibling();
while (size > 1 && tmp != null) {
if (tmp instanceof PsiWhiteSpace) {
if (!selectionStartAtCaret)
shift += tmp.getTextLength();
selectionLen += tmp.getTextLength();
}
tmp = tmp.getNextSibling();
size -= 1;
}
}
if (shift < 0)
shift = 0;
final int column = editor.offsetToLogicalPosition(added.getTextRange().getStartOffset()).column;
if (selectionContainer.myAtTheBeginning || column < selectionContainer.myAdditional) {
selectionLen += column;
} else {
selectionLen += selectionContainer.myAdditional;
}
if (selectionContainer.myAtTheBeginning && selectionStartAtCaret)
shift = -column;
}
final int documentLength = document.getTextLength();
int newCaretOffset = offset + shift;
if (newCaretOffset >= documentLength)
newCaretOffset = documentLength;
caretModel.moveToOffset(newCaretOffset);
if (hasSelection) {
if (selectionStartAtCaret) {
int newSelectionEnd = newCaretOffset + selectionLen;
selectionModel.setSelection(newCaretOffset, newSelectionEnd);
} else {
int newSelectionStart = newCaretOffset - selectionLen;
selectionModel.setSelection(newSelectionStart, newCaretOffset);
}
}
}
use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.
the class PyMoveSymbolDelegate method collectAllMovableElementsInSelection.
@NotNull
private static List<PyElement> collectAllMovableElementsInSelection(@NotNull Editor editor, @NotNull PyFile pyFile) {
final SelectionModel selectionModel = editor.getSelectionModel();
final TextRange selectionRange = new TextRange(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd());
final List<PyElement> members = PyMoveModuleMembersHelper.getTopLevelModuleMembers(pyFile);
return ContainerUtil.filter(members, member -> {
final PsiElement body = PyMoveModuleMembersHelper.expandNamedElementBody((PsiNamedElement) member);
return body != null && selectionRange.contains(body.getTextRange());
});
}
use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.
the class PyMoveSymbolDelegate method selectionSpansMultipleLines.
private static boolean selectionSpansMultipleLines(@NotNull Editor editor) {
final SelectionModel selectionModel = editor.getSelectionModel();
final Document document = editor.getDocument();
return document.getLineNumber(selectionModel.getSelectionStart()) != document.getLineNumber(selectionModel.getSelectionEnd());
}
use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.
the class PyUnwrapDescriptor method findTargetElement.
@Override
@Nullable
protected PsiElement findTargetElement(Editor editor, PsiFile file) {
int offset = editor.getCaretModel().getOffset();
PsiElement endElement = PyUtil.findElementAtOffset(file, offset);
SelectionModel selectionModel = editor.getSelectionModel();
if (selectionModel.hasSelection() && selectionModel.getSelectionStart() < offset) {
PsiElement startElement = PyUtil.findElementAtOffset(file, selectionModel.getSelectionStart());
if (startElement != null && startElement != endElement && startElement.getTextRange().getEndOffset() == offset) {
return startElement;
}
}
return endElement;
}
Aggregations