use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.
the class PyStatementMover method beforeMove.
@Override
public void beforeMove(@NotNull final Editor editor, @NotNull final MoveInfo info, final boolean down) {
final LineRange toMove = info.toMove;
final LineRange toMove2 = info.toMove2;
if (toMove instanceof MyLineRange && toMove2 instanceof ScopeRange) {
PostprocessReformattingAspect.getInstance(editor.getProject()).disablePostprocessFormattingInside(() -> {
final PsiElement startToMove = ((MyLineRange) toMove).myStartElement;
final PsiElement endToMove = ((MyLineRange) toMove).myEndElement;
final PsiFile file = startToMove.getContainingFile();
final SelectionModel selectionModel = editor.getSelectionModel();
final CaretModel caretModel = editor.getCaretModel();
final int selectionStart = selectionModel.getSelectionStart();
boolean isSelectionStartAtCaret = caretModel.getOffset() == selectionStart;
final SelectionContainer selectionLen = getSelectionLenContainer(editor, ((MyLineRange) toMove));
int shift = getCaretShift(startToMove, endToMove, caretModel, isSelectionStartAtCaret);
final boolean hasSelection = selectionModel.hasSelection();
int offset;
if (((ScopeRange) toMove2).isTheSameLevel()) {
offset = moveTheSameLevel((ScopeRange) toMove2, (MyLineRange) toMove);
} else {
offset = moveInOut(((MyLineRange) toMove), editor, info);
}
restoreCaretAndSelection(file, editor, isSelectionStartAtCaret, hasSelection, selectionLen, shift, offset, (MyLineRange) toMove);
//do not move further
info.toMove2 = info.toMove;
});
}
}
use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.
the class IntroduceHandler method performAction.
public void performAction(IntroduceOperation 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();
boolean singleElementSelection = false;
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);
}
if (element1 == element2) {
singleElementSelection = true;
}
} 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 = PyRefactoringUtil.getSelectedExpression(project, file, element1, element2);
final PyComprehensionElement comprehension = PsiTreeUtil.getParentOfType(element1, PyComprehensionElement.class, true);
if (element1 == null || comprehension != null) {
showCannotPerformError(project, editor);
return;
}
if (singleElementSelection && element1 instanceof PyStringLiteralExpression) {
final PyStringLiteralExpression literal = (PyStringLiteralExpression) element1;
// Currently introduce for substrings of a multi-part string literals is not supported
if (literal.getStringNodes().size() > 1) {
showCannotPerformError(project, editor);
return;
}
final int offset = element1.getTextOffset();
final TextRange selectionRange = TextRange.create(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd());
final TextRange elementRange = element1.getTextRange();
if (!elementRange.equals(selectionRange) && elementRange.contains(selectionRange)) {
final TextRange innerRange = literal.getStringValueTextRange();
final TextRange intersection = selectionRange.shiftRight(-offset).intersection(innerRange);
final TextRange finalRange = intersection != null ? intersection : selectionRange;
final String text = literal.getText();
if (getFormatValueExpression(literal) != null && breaksStringFormatting(text, finalRange) || getNewStyleFormatValueExpression(literal) != null && breaksNewStyleStringFormatting(text, finalRange) || breaksStringEscaping(text, finalRange)) {
showCannotPerformError(project, editor);
return;
}
element1.putUserData(PyReplaceExpressionUtil.SELECTION_BREAKS_AST_NODE, Pair.create(element1, finalRange));
}
}
if (!checkIntroduceContext(file, editor, element1)) {
return;
}
operation.setElement(element1);
performActionOnElement(operation);
}
use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.
the class PyIntroduceFieldHandler method isTestClass.
private static boolean isTestClass(PsiFile file, Editor editor) {
PsiElement element1 = null;
final SelectionModel selectionModel = editor.getSelectionModel();
if (selectionModel.hasSelection()) {
element1 = file.findElementAt(selectionModel.getSelectionStart());
} else {
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));
}
}
if (element1 != null) {
final PyClass clazz = PyUtil.getContainingClassOrSelf(element1);
if (clazz != null && PythonUnitTestUtil.isTestCaseClass(clazz, null))
return true;
}
return false;
}
use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.
the class XmlDequotingFilter method skipReplacementQuotesOrBraces.
@Override
public boolean skipReplacementQuotesOrBraces(@NotNull PsiFile file, @NotNull Editor editor, @NotNull String selectedText, char c) {
if (selectedText.startsWith("<") && selectedText.endsWith(">")) {
SelectionModel model = editor.getSelectionModel();
int startIndex = model.getSelectionStart();
PsiElement start = file.findElementAt(startIndex);
int endIndex = model.getSelectionEnd();
PsiElement end = file.findElementAt(endIndex > 0 ? endIndex - 1 : endIndex);
if (start != null && (start.getNode().getElementType() == XmlTokenType.XML_START_TAG_START || start.getNode().getElementType() == XmlTokenType.XML_END_TAG_START) && end != null && (end.getNode().getElementType() == XmlTokenType.XML_TAG_END || end.getNode().getElementType() == XmlTokenType.XML_EMPTY_ELEMENT_END)) {
return true;
}
}
return false;
}
use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.
the class GroovyExtractMethodHandler method invoke.
@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file, @Nullable DataContext dataContext) {
editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
final SelectionModel model = editor.getSelectionModel();
if (model.hasSelection()) {
invokeImpl(project, editor, file, model.getSelectionStart(), model.getSelectionEnd());
} else {
final List<GrExpression> expressions = GrIntroduceHandlerBase.collectExpressions(file, editor, editor.getCaretModel().getOffset(), true);
final Pass<GrExpression> callback = new Callback(project, editor, file);
if (expressions.size() == 1) {
callback.pass(expressions.get(0));
} else if (expressions.isEmpty()) {
model.selectLineAtCaret();
invokeImpl(project, editor, file, model.getSelectionStart(), model.getSelectionEnd());
} else {
IntroduceTargetChooser.showChooser(editor, expressions, callback, GrIntroduceHandlerBase.GR_EXPRESSION_RENDERER);
}
}
}
Aggregations