Search in sources :

Example 71 with SelectionModel

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);
}
Also used : CaretModel(com.intellij.openapi.editor.CaretModel) SelectionModel(com.intellij.openapi.editor.SelectionModel) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement)

Example 72 with SelectionModel

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;
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel) SurroundDescriptor(com.intellij.lang.surroundWith.SurroundDescriptor) Surrounder(com.intellij.lang.surroundWith.Surrounder) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 73 with SelectionModel

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);
}
Also used : TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) Project(com.intellij.openapi.project.Project) AnswerPlaceholder(com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder) AnswerPlaceholderSubtaskInfo(com.jetbrains.edu.learning.courseFormat.AnswerPlaceholderSubtaskInfo) SelectionModel(com.intellij.openapi.editor.SelectionModel) Editor(com.intellij.openapi.editor.Editor) Document(com.intellij.openapi.editor.Document)

Example 74 with SelectionModel

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;
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel) Document(com.intellij.openapi.editor.Document) NotNull(org.jetbrains.annotations.NotNull)

Example 75 with SelectionModel

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();
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel) Editor(com.intellij.openapi.editor.Editor)

Aggregations

SelectionModel (com.intellij.openapi.editor.SelectionModel)76 TextRange (com.intellij.openapi.util.TextRange)21 Document (com.intellij.openapi.editor.Document)19 PsiElement (com.intellij.psi.PsiElement)19 NotNull (org.jetbrains.annotations.NotNull)16 Editor (com.intellij.openapi.editor.Editor)14 Nullable (org.jetbrains.annotations.Nullable)11 CaretModel (com.intellij.openapi.editor.CaretModel)10 PsiFile (com.intellij.psi.PsiFile)8 Project (com.intellij.openapi.project.Project)7 ArrayList (java.util.ArrayList)6 TemplateState (com.intellij.codeInsight.template.impl.TemplateState)3 SurroundDescriptor (com.intellij.lang.surroundWith.SurroundDescriptor)3 Pass (com.intellij.openapi.util.Pass)3 List (java.util.List)3 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)3 EditorWindow (com.intellij.injected.editor.EditorWindow)2 ApplicationManager (com.intellij.openapi.application.ApplicationManager)2 RangeHighlighter (com.intellij.openapi.editor.markup.RangeHighlighter)2 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)2