use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.
the class ReplaceOctalEscapeWithUnicodeEscapeIntention method processIntention.
@Override
protected void processIntention(Editor editor, @NotNull PsiElement element) {
final SelectionModel selectionModel = editor.getSelectionModel();
if (selectionModel.hasSelection()) {
// does not check if octal escape is inside char or string literal (garbage in, garbage out)
final Document document = editor.getDocument();
final int start = selectionModel.getSelectionStart();
final int end = selectionModel.getSelectionEnd();
final String text = document.getText(new TextRange(start, end));
final int textLength = end - start;
final StringBuilder replacement = new StringBuilder(textLength);
int anchor = 0;
while (true) {
final int index = indexOfOctalEscape(text, anchor + 1);
if (index < 0) {
break;
}
replacement.append(text.substring(anchor, index));
anchor = appendUnicodeEscape(text, index, replacement);
}
replacement.append(text.substring(anchor, textLength));
document.replaceString(start, end, replacement);
} else if (element instanceof PsiLiteralExpression) {
final PsiLiteralExpression literalExpression = (PsiLiteralExpression) element;
final String text = literalExpression.getText();
final CaretModel model = editor.getCaretModel();
final int offset = model.getOffset() - literalExpression.getTextOffset();
final StringBuilder newLiteralText = new StringBuilder();
final int index1 = indexOfOctalEscape(text, offset);
final int index2 = indexOfOctalEscape(text, offset + 1);
final int escapeStart = index2 == offset ? index2 : index1;
newLiteralText.append(text.substring(0, escapeStart));
final int escapeEnd = appendUnicodeEscape(text, escapeStart, newLiteralText);
newLiteralText.append(text.substring(escapeEnd, text.length()));
PsiReplacementUtil.replaceExpression(literalExpression, newLiteralText.toString());
}
}
use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.
the class IterateOverIterableIntention method invoke.
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
final TemplateImpl template = getTemplate();
SelectionModel selectionModel = editor.getSelectionModel();
if (!selectionModel.hasSelection()) {
final PsiExpression iterableExpression = getIterableExpression(editor, file);
LOG.assertTrue(iterableExpression != null);
TextRange textRange = iterableExpression.getTextRange();
selectionModel.setSelection(textRange.getStartOffset(), textRange.getEndOffset());
}
new InvokeTemplateAction(template, editor, project, new HashSet<>()).perform();
}
use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.
the class AbstractRegionToKillRingTest method parse.
/**
* Checks current editor and returns tuple of <code>(selected text; text over than selected)</code>.
*
* @return tuple of <code>(selected text; text over than selected)</code>.
*/
@NotNull
protected static Pair<String, String> parse() {
SelectionModel selectionModel = myEditor.getSelectionModel();
if (!selectionModel.hasSelection()) {
return new Pair<>(null, myEditor.getDocument().getText());
}
CharSequence text = myEditor.getDocument().getCharsSequence();
String selectedText = text.subSequence(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd()).toString();
StringBuilder nonSelectedText = new StringBuilder();
nonSelectedText.append(text.subSequence(0, selectionModel.getSelectionStart())).append(text.subSequence(selectionModel.getSelectionEnd(), text.length()));
return new Pair<>(selectedText, nonSelectedText.toString());
}
use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.
the class ToggleColumnModeAction method setSelected.
@Override
public void setSelected(AnActionEvent e, boolean state) {
final EditorEx editor = getEditor(e);
final SelectionModel selectionModel = editor.getSelectionModel();
final CaretModel caretModel = editor.getCaretModel();
if (state) {
caretModel.removeSecondaryCarets();
boolean hasSelection = selectionModel.hasSelection();
int selStart = selectionModel.getSelectionStart();
int selEnd = selectionModel.getSelectionEnd();
LogicalPosition blockStart, blockEnd;
if (caretModel.supportsMultipleCarets()) {
LogicalPosition logicalSelStart = editor.offsetToLogicalPosition(selStart);
LogicalPosition logicalSelEnd = editor.offsetToLogicalPosition(selEnd);
int caretOffset = caretModel.getOffset();
blockStart = selStart == caretOffset ? logicalSelEnd : logicalSelStart;
blockEnd = selStart == caretOffset ? logicalSelStart : logicalSelEnd;
} else {
blockStart = selStart == caretModel.getOffset() ? caretModel.getLogicalPosition() : editor.offsetToLogicalPosition(selStart);
blockEnd = selEnd == caretModel.getOffset() ? caretModel.getLogicalPosition() : editor.offsetToLogicalPosition(selEnd);
}
editor.setColumnMode(true);
if (hasSelection) {
selectionModel.setBlockSelection(blockStart, blockEnd);
} else {
selectionModel.removeSelection();
}
} else {
boolean hasSelection = false;
int selStart = 0;
int selEnd = 0;
if (caretModel.supportsMultipleCarets()) {
hasSelection = true;
List<Caret> allCarets = caretModel.getAllCarets();
Caret fromCaret = allCarets.get(0);
Caret toCaret = allCarets.get(allCarets.size() - 1);
if (fromCaret == caretModel.getPrimaryCaret()) {
Caret tmp = fromCaret;
fromCaret = toCaret;
toCaret = tmp;
}
selStart = fromCaret.getLeadSelectionOffset();
selEnd = toCaret.getSelectionStart() == toCaret.getLeadSelectionOffset() ? toCaret.getSelectionEnd() : toCaret.getSelectionStart();
}
editor.setColumnMode(false);
caretModel.removeSecondaryCarets();
if (hasSelection) {
selectionModel.setSelection(selStart, selEnd);
} else {
selectionModel.removeSelection();
}
}
}
use of com.intellij.openapi.editor.SelectionModel in project intellij-plugins by JetBrains.
the class DartServerExtractMethodDialog method invoke.
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file, DataContext dataContext) {
final SelectionModel selectionModel = editor.getSelectionModel();
if (!selectionModel.hasSelection())
selectionModel.selectLineAtCaret();
final int offset = selectionModel.getSelectionStart();
final int length = selectionModel.getSelectionEnd() - offset;
final ServerExtractMethodRefactoring refactoring = new ServerExtractMethodRefactoring(project, file.getVirtualFile(), offset, length);
// Validate initial status.
{
final RefactoringStatus initialStatus = refactoring.checkInitialConditions();
if (initialStatus == null) {
return;
}
if (initialStatus.hasError()) {
final String title = DartBundle.message("dart.refactoring.extract.method.error");
CommonRefactoringUtil.showErrorHint(project, editor, initialStatus.getMessage(), title, null);
return;
}
}
new DartServerExtractMethodDialog(project, editor, refactoring).show();
}
Aggregations