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>");
}
}
Aggregations