Search in sources :

Example 76 with SelectionModel

use of com.intellij.openapi.editor.SelectionModel in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoIntroduceVariableBase method performAction.

protected static void performAction(GoIntroduceOperation operation) {
    SelectionModel selectionModel = operation.getEditor().getSelectionModel();
    boolean hasSelection = selectionModel.hasSelection();
    GoExpression expression = hasSelection ? findExpressionInSelection(operation.getFile(), selectionModel.getSelectionStart(), selectionModel.getSelectionEnd()) : findExpressionAtOffset(operation);
    if (expression instanceof GoParenthesesExpr)
        expression = ((GoParenthesesExpr) expression).getExpression();
    if (expression == null) {
        String message = RefactoringBundle.message(hasSelection ? "selected.block.should.represent.an.expression" : "refactoring.introduce.selection.error");
        showCannotPerform(operation, message);
        return;
    }
    List<GoExpression> extractableExpressions = collectExtractableExpressions(expression);
    if (extractableExpressions.isEmpty()) {
        showCannotPerform(operation, RefactoringBundle.message("refactoring.introduce.context.error"));
        return;
    }
    List<GoExpression> expressions = ContainerUtil.filter(extractableExpressions, expression12 -> GoInspectionUtil.getExpressionResultCount(expression12) == 1);
    if (expressions.isEmpty()) {
        GoExpression closestExpression = ContainerUtil.getFirstItem(extractableExpressions);
        int resultCount = closestExpression != null ? GoInspectionUtil.getExpressionResultCount(closestExpression) : 0;
        showCannotPerform(operation, "Expression " + (closestExpression != null ? closestExpression.getText() + " " : "") + (resultCount == 0 ? "doesn't return a value." : "returns multiple values."));
        return;
    }
    if (expressions.size() == 1 || hasSelection || ApplicationManager.getApplication().isUnitTestMode()) {
        // noinspection ConstantConditions
        operation.setExpression(ContainerUtil.getFirstItem(expressions));
        performOnElement(operation);
    } else {
        IntroduceTargetChooser.showChooser(operation.getEditor(), expressions, new Pass<GoExpression>() {

            @Override
            public void pass(GoExpression expression) {
                if (expression.isValid()) {
                    operation.setExpression(expression);
                    performOnElement(operation);
                }
            }
        }, expression1 -> expression1.isValid() ? expression1.getText() : "<invalid expression>");
    }
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel)

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