Search in sources :

Example 26 with SelectionModel

use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.

the class SearchResults method getSelection.

private static void getSelection(final Editor editor, final FutureResult<int[]> starts, final FutureResult<int[]> ends) {
    if (ApplicationManager.getApplication().isDispatchThread()) {
        SelectionModel selection = editor.getSelectionModel();
        starts.set(selection.getBlockSelectionStarts());
        ends.set(selection.getBlockSelectionEnds());
    } else {
        try {
            SwingUtilities.invokeAndWait(() -> {
                SelectionModel selection = editor.getSelectionModel();
                starts.set(selection.getBlockSelectionStarts());
                ends.set(selection.getBlockSelectionEnds());
            });
        } catch (InterruptedException | InvocationTargetException ignore) {
        }
    }
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 27 with SelectionModel

use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.

the class ConfigureCodeStyleOnSelectedFragment method calculateAffectingSettings.

private static CodeStyleSettingsToShow calculateAffectingSettings(@NotNull Editor editor, @NotNull PsiFile file) {
    SelectionModel model = editor.getSelectionModel();
    int start = model.getSelectionStart();
    int end = model.getSelectionEnd();
    CodeStyleSettingsCodeFragmentFilter settingsProvider = new CodeStyleSettingsCodeFragmentFilter(file, new TextRange(start, end));
    return CodeFragmentCodeStyleSettingsPanel.calcSettingNamesToShow(settingsProvider);
}
Also used : CodeStyleSettingsCodeFragmentFilter(com.intellij.psi.codeStyle.CodeStyleSettingsCodeFragmentFilter) SelectionModel(com.intellij.openapi.editor.SelectionModel) TextRange(com.intellij.openapi.util.TextRange)

Example 28 with SelectionModel

use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.

the class SelectedTextFormatter method reformatSelectedText.

void reformatSelectedText(@NotNull CodeStyleSettings reformatSettings) {
    final SelectionModel model = myEditor.getSelectionModel();
    if (model.hasSelection()) {
        try {
            CodeStyleSettingsManager.getInstance(myProject).setTemporarySettings(reformatSettings);
            reformatRange(myFile, getSelectedRange());
        } finally {
            CodeStyleSettingsManager.getInstance(myProject).dropTemporarySettings();
        }
    }
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel)

Example 29 with SelectionModel

use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.

the class IntroduceHandler method invoke.

@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file, DataContext dataContext) {
    if (editor == null || file == null) {
        return;
    }
    if (!CommonRefactoringUtil.checkReadOnlyStatus(project, file)) {
        return;
    }
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    int caretCount = editor.getCaretModel().getCaretCount();
    if (caretCount != 1) {
        cannotPerformRefactoring(project, editor);
        return;
    }
    SelectionModel selectionModel = editor.getSelectionModel();
    if (selectionModel.hasSelection()) {
        invokeOnSelection(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd(), project, editor, file);
    } else {
        int offset = editor.getCaretModel().getOffset();
        Pair<List<Target>, Integer> targetInfo = collectTargets(offset, file, editor, project);
        List<Target> list = targetInfo.getFirst();
        if (list.isEmpty()) {
            cannotPerformRefactoring(project, editor);
        } else if (list.size() == 1) {
            invokeOnTarget(list.get(0), file, editor, project);
        } else {
            IntroduceTargetChooser.showIntroduceTargetChooser(editor, list, new Pass<Target>() {

                @Override
                public void pass(final Target target) {
                    invokeOnTarget(target, file, editor, project);
                }
            }, "Expressions", targetInfo.getSecond());
        }
    }
}
Also used : Pass(com.intellij.openapi.util.Pass) SelectionModel(com.intellij.openapi.editor.SelectionModel) List(java.util.List)

Example 30 with SelectionModel

use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.

the class RearrangeCodeAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    final Project project = e.getProject();
    if (project == null) {
        return;
    }
    final Editor editor = e.getData(CommonDataKeys.EDITOR);
    if (editor == null) {
        return;
    }
    PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
    Document document = editor.getDocument();
    documentManager.commitDocument(document);
    final PsiFile file = documentManager.getPsiFile(document);
    if (file == null) {
        return;
    }
    SelectionModel model = editor.getSelectionModel();
    if (model.hasSelection()) {
        new RearrangeCodeProcessor(file, model).run();
    } else {
        new RearrangeCodeProcessor(file).run();
    }
}
Also used : RearrangeCodeProcessor(com.intellij.codeInsight.actions.RearrangeCodeProcessor) Project(com.intellij.openapi.project.Project) SelectionModel(com.intellij.openapi.editor.SelectionModel) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor) Document(com.intellij.openapi.editor.Document) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

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