use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.
the class PyClassRefactoringHandler method invoke.
public void invoke(@NotNull Project project, Editor editor, PsiFile file, DataContext dataContext) {
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);
} 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));
element2 = file.findElementAt(document.getLineEndOffset(lineNumber) - 1);
}
}
if (element1 == null || element2 == null) {
CommonRefactoringUtil.showErrorHint(project, editor, PyBundle.message("refactoring.introduce.selection.error"), getTitle(), "members.pull.up");
return;
}
doRefactor(project, element1, element2, editor, file, dataContext);
}
use of com.intellij.openapi.editor.SelectionModel in project kotlin by JetBrains.
the class AbstractSurroundWithTest method getElementsToSurround.
@Nullable
private PsiElement[] getElementsToSurround(@NotNull Surrounder surrounder) {
List<SurroundDescriptor> surroundDescriptors = LanguageSurrounders.INSTANCE.allForLanguage(getFile().getViewProvider().getBaseLanguage());
String surrounderDescription = surrounder.getTemplateDescription();
for (SurroundDescriptor descriptor : surroundDescriptors) {
Surrounder[] surrounders = descriptor.getSurrounders();
for (Surrounder surrounderInDescriptor : surrounders) {
if (surrounderInDescriptor.getTemplateDescription().equals(surrounderDescription)) {
SelectionModel selection = getEditor().getSelectionModel();
PsiElement[] elements = descriptor.getElementsToSurround(getFile(), selection.getSelectionStart(), selection.getSelectionEnd());
return elements;
}
}
}
return null;
}
use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.
the class CCAddAnswerPlaceholder method addPlaceholder.
private void addPlaceholder(@NotNull CCState state) {
Editor editor = state.getEditor();
Project project = state.getProject();
Document document = editor.getDocument();
FileDocumentManager.getInstance().saveDocument(document);
final SelectionModel model = editor.getSelectionModel();
final int offset = model.hasSelection() ? model.getSelectionStart() : editor.getCaretModel().getOffset();
TaskFile taskFile = state.getTaskFile();
int subtaskIndex = state.getTaskFile().getTask().getActiveSubtaskIndex();
final AnswerPlaceholder answerPlaceholder = new AnswerPlaceholder();
AnswerPlaceholderSubtaskInfo info = new AnswerPlaceholderSubtaskInfo();
answerPlaceholder.getSubtaskInfos().put(subtaskIndex, info);
int index = taskFile.getAnswerPlaceholders().size();
answerPlaceholder.setIndex(index);
answerPlaceholder.setTaskFile(taskFile);
taskFile.sortAnswerPlaceholders();
answerPlaceholder.setOffset(offset);
answerPlaceholder.setUseLength(false);
String defaultPlaceholderText = "type here";
CCCreateAnswerPlaceholderDialog dlg = createDialog(project, answerPlaceholder);
if (!dlg.showAndGet()) {
return;
}
String answerPlaceholderText = dlg.getTaskText();
answerPlaceholder.setPossibleAnswer(model.hasSelection() ? model.getSelectedText() : defaultPlaceholderText);
answerPlaceholder.setTaskText(StringUtil.notNullize(answerPlaceholderText));
answerPlaceholder.setLength(StringUtil.notNullize(answerPlaceholderText).length());
answerPlaceholder.setHints(dlg.getHints());
if (!model.hasSelection()) {
DocumentUtil.writeInRunUndoTransparentAction(() -> document.insertString(offset, defaultPlaceholderText));
}
answerPlaceholder.setPossibleAnswer(model.hasSelection() ? model.getSelectedText() : defaultPlaceholderText);
AddAction action = new AddAction(answerPlaceholder, taskFile, editor);
EduUtils.runUndoableAction(project, "Add Answer Placeholder", action);
}
use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.
the class PyEmacsHandler method changeIndent.
/**
* Tries to make active line(s) belong to another code block by changing their indentation.
*
* @param project current project
* @param editor current editor
* @param file current file
* @return {@link Result#STOP} if indentation level is changed and further processing should be stopped;
* {@link Result#CONTINUE} otherwise
*/
@NotNull
@Override
public Result changeIndent(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
// The algorithm is as follows:
// 1. Check if the editor has selection. Do nothing then as Emacs behaves so;
// 2. Indent current line one level right if possible;
// 3. Indent current line to the left-most position if possible;
SelectionModel selectionModel = editor.getSelectionModel();
// Emacs Tab doesn't adjust indent in case of active selection. So do we.
if (selectionModel.hasSelection()) {
return Result.CONTINUE;
}
// Check if current line is empty. Return eagerly then.
Document document = editor.getDocument();
int caretOffset = editor.getCaretModel().getOffset();
int caretLine = document.getLineNumber(caretOffset);
if (isLineContainsWhiteSpacesOnlyEmpty(document, caretLine)) {
return Result.CONTINUE;
}
ChangeIndentContext context = new ChangeIndentContext(project, file, editor, document, caretLine);
int targetLineIndent = getLineIndent(context, context.targetLine);
int soleLineIndent = getSoleIndent(context);
int lineStart = context.document.getLineStartOffset(context.targetLine);
if (caretOffset - lineStart < targetLineIndent) {
changeIndent(context, soleLineIndent);
return Result.STOP;
}
switch(tryToIndentToRight(context)) {
case STOP_SUCCESSFUL:
return Result.STOP;
case STOP_UNSUCCESSFUL:
return Result.CONTINUE;
case CONTINUE:
break;
}
if (tryToIndentToLeft(context)) {
return Result.STOP;
}
return Result.CONTINUE;
}
use of com.intellij.openapi.editor.SelectionModel in project ECTranslation by Skykai521.
the class ECTranslation method getTranslation.
private void getTranslation(AnActionEvent event) {
Editor mEditor = event.getData(PlatformDataKeys.EDITOR);
if (null == mEditor) {
return;
}
SelectionModel model = mEditor.getSelectionModel();
String selectedText = model.getSelectedText();
if (TextUtils.isEmpty(selectedText)) {
selectedText = getCurrentWords(mEditor);
if (TextUtils.isEmpty(selectedText)) {
return;
}
}
String queryText = strip(addBlanks(selectedText));
new Thread(new RequestRunnable(mEditor, queryText)).start();
}
Aggregations