use of com.intellij.openapi.editor.SelectionModel in project ideavim by JetBrains.
the class SortHandler method getLineRange.
@NotNull
private LineRange getLineRange(@NotNull Editor editor, @NotNull DataContext context, @NotNull ExCommand cmd) {
final LineRange range = cmd.getLineRange(editor, context);
final LineRange normalizedRange;
// Something like "30,20sort" gets converted to "20,30sort"
if (range.getEndLine() < range.getStartLine()) {
normalizedRange = new LineRange(range.getEndLine(), range.getStartLine());
} else {
normalizedRange = range;
}
// If we don't have a range, we either have "sort", a selection, or a block
if (normalizedRange.getEndLine() - normalizedRange.getStartLine() == 0) {
// If we have a selection.
final SelectionModel selectionModel = editor.getSelectionModel();
if (selectionModel.hasSelection()) {
final int start = selectionModel.getSelectionStart();
final int end = selectionModel.getSelectionEnd();
final int startLine = editor.offsetToLogicalPosition(start).line;
final int endLine = editor.offsetToLogicalPosition(end).line;
return new LineRange(startLine, endLine);
} else // If we have a generic selection, i.e. "sort" entire document
{
return new LineRange(0, editor.getDocument().getLineCount() - 1);
}
}
return normalizedRange;
}
use of com.intellij.openapi.editor.SelectionModel in project android by JetBrains.
the class PermissionUsageQuickFixRunnable method run.
@Override
public void run() {
final Document document = editor.getDocument();
PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document);
assert psiFile != null;
SelectionModel selectionModel = editor.getSelectionModel();
setLineSelection(document, selectionModel);
selectionModel = editor.getSelectionModel();
document.insertString(selectionModel.getSelectionEnd(), TEXT_AFTER);
document.insertString(selectionModel.getSelectionStart(), TEXT_BEFORE);
PsiDocumentManager.getInstance(project).commitDocument(document);
CodeStyleManager.getInstance(project).reformat(psiFile.getNavigationElement());
JavaCodeStyleManager.getInstance(project).shortenClassReferences(psiFile.getNavigationElement());
selectionModel.removeSelection(true);
}
use of com.intellij.openapi.editor.SelectionModel in project android by JetBrains.
the class AndroidBaseXmlRefactoringAction method getNonEmptySelectionRange.
@Nullable
private static TextRange getNonEmptySelectionRange(Editor editor) {
if (editor != null) {
final SelectionModel model = editor.getSelectionModel();
if (model.hasSelection()) {
final int start = model.getSelectionStart();
final int end = model.getSelectionEnd();
if (start < end) {
return TextRange.create(start, end);
}
}
}
return null;
}
use of com.intellij.openapi.editor.SelectionModel in project intellij-plugins by JetBrains.
the class DartServerExtractLocalVariableDialog method perform.
public void perform() {
final SelectionModel selectionModel = editor.getSelectionModel();
final int offset = selectionModel.getSelectionStart();
final int length = selectionModel.getSelectionEnd() - offset;
// create refactoring
createRefactoring(offset, length);
if (refactoring == null) {
return;
}
// prepare expressions
final java.util.List<DartExpression> expressions;
{
final int[] offsets = refactoring.getCoveringExpressionOffsets();
final int[] lengths = refactoring.getCoveringExpressionLengths();
expressions = getDartExpressions(offsets, lengths);
if (expressions == null) {
return;
}
}
// select the expression to extract
if (expressions.size() == 1 || ApplicationManager.getApplication().isUnitTestMode()) {
performOnExpression(expressions.get(0));
} else if (expressions.size() > 1) {
IntroduceTargetChooser.showChooser(editor, expressions, new Pass<DartExpression>() {
@Override
public void pass(DartExpression expression) {
performOnExpression(expression);
}
}, PsiElement::getText);
}
}
use of com.intellij.openapi.editor.SelectionModel in project intellij-plugins by JetBrains.
the class DartIntroduceHandler method performAction.
public void performAction(DartIntroduceOperation operation) {
final PsiFile file = operation.getFile();
if (!CommonRefactoringUtil.checkReadOnlyStatus(file)) {
return;
}
final Editor editor = operation.getEditor();
if (editor.getSettings().isVariableInplaceRenameEnabled()) {
final TemplateState templateState = TemplateManagerImpl.getTemplateState(operation.getEditor());
if (templateState != null && !templateState.isFinished()) {
return;
}
}
PsiElement element1 = null;
PsiElement element2 = null;
final SelectionModel selectionModel = editor.getSelectionModel();
if (selectionModel.hasSelection()) {
element1 = file.findElementAt(selectionModel.getSelectionStart());
element2 = file.findElementAt(selectionModel.getSelectionEnd() - 1);
if (element1 instanceof PsiWhiteSpace) {
int startOffset = element1.getTextRange().getEndOffset();
element1 = file.findElementAt(startOffset);
}
if (element2 instanceof PsiWhiteSpace) {
int endOffset = element2.getTextRange().getStartOffset();
element2 = file.findElementAt(endOffset - 1);
}
} else {
if (smartIntroduce(operation)) {
return;
}
final CaretModel caretModel = editor.getCaretModel();
final Document document = editor.getDocument();
int lineNumber = document.getLineNumber(caretModel.getOffset());
if ((lineNumber >= 0) && (lineNumber < document.getLineCount())) {
element1 = file.findElementAt(document.getLineStartOffset(lineNumber));
element2 = file.findElementAt(document.getLineEndOffset(lineNumber) - 1);
}
}
final Project project = operation.getProject();
if (element1 == null || element2 == null) {
showCannotPerformError(project, editor);
return;
}
element1 = DartRefactoringUtil.getSelectedExpression(project, file, element1, element2);
if (element1 == null) {
showCannotPerformError(project, editor);
return;
}
if (!checkIntroduceContext(file, editor, element1)) {
return;
}
operation.setElement(element1);
performActionOnElement(operation);
}
Aggregations