use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.
the class FoldLinesLikeThis method getSingleLineSelection.
@Nullable
private static String getSingleLineSelection(@NotNull Editor editor) {
final SelectionModel model = editor.getSelectionModel();
final Document document = editor.getDocument();
if (!model.hasSelection()) {
final int offset = editor.getCaretModel().getOffset();
if (offset <= document.getTextLength()) {
final int lineNumber = document.getLineNumber(offset);
final String line = document.getText().substring(document.getLineStartOffset(lineNumber), document.getLineEndOffset(lineNumber)).trim();
if (StringUtil.isNotEmpty(line)) {
return line;
}
}
return null;
}
final int start = model.getSelectionStart();
final int end = model.getSelectionEnd();
if (document.getLineNumber(start) == document.getLineNumber(end)) {
final String selection = document.getText().substring(start, end).trim();
if (StringUtil.isNotEmpty(selection)) {
return selection;
}
}
return null;
}
use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.
the class IterateOverIterableIntention method getIterableExpression.
@Nullable
private static PsiExpression getIterableExpression(Editor editor, PsiFile file) {
final SelectionModel selectionModel = editor.getSelectionModel();
if (selectionModel.hasSelection()) {
PsiElement elementAtStart = file.findElementAt(selectionModel.getSelectionStart());
PsiElement elementAtEnd = file.findElementAt(selectionModel.getSelectionEnd() - 1);
if (elementAtStart == null || elementAtStart instanceof PsiWhiteSpace || elementAtStart instanceof PsiComment) {
elementAtStart = PsiTreeUtil.skipSiblingsForward(elementAtStart, PsiWhiteSpace.class, PsiComment.class);
if (elementAtStart == null)
return null;
}
if (elementAtEnd == null || elementAtEnd instanceof PsiWhiteSpace || elementAtEnd instanceof PsiComment) {
elementAtEnd = PsiTreeUtil.skipSiblingsBackward(elementAtEnd, PsiWhiteSpace.class, PsiComment.class);
if (elementAtEnd == null)
return null;
}
PsiElement parent = PsiTreeUtil.findCommonParent(elementAtStart, elementAtEnd);
if (parent instanceof PsiExpression) {
final PsiType type = ((PsiExpression) parent).getType();
return type instanceof PsiArrayType || InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_LANG_ITERABLE) ? (PsiExpression) parent : null;
}
return null;
}
PsiElement element = file.findElementAt(editor.getCaretModel().getOffset());
while (element instanceof PsiWhiteSpace) {
element = element.getPrevSibling();
}
if (element instanceof PsiExpressionStatement) {
element = ((PsiExpressionStatement) element).getExpression().getLastChild();
}
while ((element = PsiTreeUtil.getParentOfType(element, PsiExpression.class, true)) != null) {
final PsiElement parent = element.getParent();
if (parent instanceof PsiMethodCallExpression)
continue;
if (!(parent instanceof PsiExpressionStatement))
return null;
final PsiType type = ((PsiExpression) element).getType();
if (type instanceof PsiArrayType || InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_LANG_ITERABLE))
return (PsiExpression) element;
}
return null;
}
use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.
the class SelectionQuotingTypedHandler method beforeSelectionRemoved.
@Override
public Result beforeSelectionRemoved(char c, Project project, Editor editor, PsiFile file) {
SelectionModel selectionModel = editor.getSelectionModel();
if (CodeInsightSettings.getInstance().SURROUND_SELECTION_ON_QUOTE_TYPED && selectionModel.hasSelection() && isDelimiter(c)) {
String selectedText = selectionModel.getSelectedText();
if (!StringUtil.isEmpty(selectedText)) {
final int selectionStart = selectionModel.getSelectionStart();
final int selectionEnd = selectionModel.getSelectionEnd();
if (selectedText.length() > 1) {
final char firstChar = selectedText.charAt(0);
final char lastChar = selectedText.charAt(selectedText.length() - 1);
if (isSimilarDelimiters(firstChar, c) && lastChar == getMatchingDelimiter(firstChar) && (isQuote(firstChar) || firstChar != c) && !shouldSkipReplacementOfQuotesOrBraces(file, editor, selectedText, c) && selectedText.indexOf(lastChar, 1) == selectedText.length() - 1) {
selectedText = selectedText.substring(1, selectedText.length() - 1);
}
}
final int caretOffset = selectionModel.getSelectionStart();
final char c2 = getMatchingDelimiter(c);
final String newText = String.valueOf(c) + selectedText + c2;
boolean ltrSelection = selectionModel.getLeadSelectionOffset() != selectionModel.getSelectionEnd();
boolean restoreStickySelection = editor instanceof EditorEx && ((EditorEx) editor).isStickySelection();
selectionModel.removeSelection();
editor.getDocument().replaceString(selectionStart, selectionEnd, newText);
TextRange replacedTextRange = Registry.is("editor.smarterSelectionQuoting") ? new TextRange(caretOffset + 1, caretOffset + newText.length() - 1) : new TextRange(caretOffset, caretOffset + newText.length());
// selection is removed here
if (replacedTextRange.getEndOffset() <= editor.getDocument().getTextLength()) {
if (restoreStickySelection) {
EditorEx editorEx = (EditorEx) editor;
CaretModel caretModel = editorEx.getCaretModel();
caretModel.moveToOffset(ltrSelection ? replacedTextRange.getStartOffset() : replacedTextRange.getEndOffset());
editorEx.setStickySelection(true);
caretModel.moveToOffset(ltrSelection ? replacedTextRange.getEndOffset() : replacedTextRange.getStartOffset());
} else {
if (ltrSelection || editor instanceof EditorWindow) {
editor.getSelectionModel().setSelection(replacedTextRange.getStartOffset(), replacedTextRange.getEndOffset());
} else {
editor.getSelectionModel().setSelection(replacedTextRange.getEndOffset(), replacedTextRange.getStartOffset());
}
if (Registry.is("editor.smarterSelectionQuoting")) {
editor.getCaretModel().moveToOffset(ltrSelection ? replacedTextRange.getEndOffset() : replacedTextRange.getStartOffset());
}
}
}
return Result.STOP;
}
}
return super.beforeSelectionRemoved(c, project, editor, file);
}
use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.
the class IntroduceFunctionalParameterTest method doTest.
private void doTest(String conflict) {
boolean enabled = true;
try {
configureByFile("/refactoring/introduceFunctionalParameter/before" + getTestName(false) + ".java");
enabled = myEditor.getSettings().isVariableInplaceRenameEnabled();
myEditor.getSettings().setVariableInplaceRenameEnabled(false);
final SelectionModel selectionModel = getEditor().getSelectionModel();
if (selectionModel.hasSelection()) {
PsiElement[] elements = ExtractMethodHandler.getElements(getProject(), getEditor(), getFile());
new IntroduceParameterHandler().introduceStrategy(getProject(), getEditor(), getFile(), elements);
}
checkResultByFile("/refactoring/introduceFunctionalParameter/after" + getTestName(false) + ".java");
if (conflict != null) {
fail("Conflict expected");
}
} catch (BaseRefactoringProcessor.ConflictsInTestsException e) {
if (conflict == null) {
throw e;
}
assertEquals(conflict, e.getMessage());
} finally {
myEditor.getSettings().setVariableInplaceRenameEnabled(enabled);
}
}
use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.
the class GroovyLiteralCopyPasteProcessor method preprocessOnPaste.
@NotNull
@Override
public String preprocessOnPaste(Project project, PsiFile file, Editor editor, String text, 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 (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 || "\n".equals(breaker) && text.endsWith("\n")) {
buffer.append(breaker);
}
}
text = buffer.toString();
}
return text;
}
Aggregations