From 84df42b02160b00f73a3387fe4920412d4d5ddd1 Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Wed, 22 Jul 2015 19:23:45 +0200 Subject: [PATCH] switch on 'introduce' refactorings for quick list if there is no selection but there are expressions in context --- .../extractMethod/ExtractMethodHandler.java | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodHandler.java b/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodHandler.java index 3e42023d392a..9e146ce1d3b8 100644 --- a/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodHandler.java +++ b/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodHandler.java @@ -21,13 +21,12 @@ import com.intellij.codeInsight.highlighting.HighlightManager; import com.intellij.lang.ContextAwareActionHandler; import com.intellij.openapi.actionSystem.CommonDataKeys; import com.intellij.openapi.actionSystem.DataContext; -import com.intellij.openapi.actionSystem.LangDataKeys; -import com.intellij.openapi.actionSystem.PlatformDataKeys; import com.intellij.openapi.command.CommandProcessor; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.LogicalPosition; import com.intellij.openapi.editor.ScrollType; +import com.intellij.openapi.editor.SelectionModel; import com.intellij.openapi.editor.colors.EditorColors; import com.intellij.openapi.editor.colors.EditorColorsManager; import com.intellij.openapi.editor.markup.TextAttributes; @@ -116,28 +115,34 @@ public class ExtractMethodHandler implements RefactoringActionHandler, ContextAw } public static PsiElement[] getElements(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) { - int startOffset = editor.getSelectionModel().getSelectionStart(); - int endOffset = editor.getSelectionModel().getSelectionEnd(); + final SelectionModel selectionModel = editor.getSelectionModel(); + if (selectionModel.hasSelection()) { + int startOffset = selectionModel.getSelectionStart(); + int endOffset = selectionModel.getSelectionEnd(); - PsiElement[] elements; - PsiExpression expr = CodeInsightUtil.findExpressionInRange(file, startOffset, endOffset); - if (expr != null) { - elements = new PsiElement[]{expr}; - } - else { - elements = CodeInsightUtil.findStatementsInRange(file, startOffset, endOffset); - if (elements.length == 0) { - final PsiExpression expression = IntroduceVariableBase.getSelectedExpression(project, file, startOffset, endOffset); - if (expression != null && IntroduceVariableBase.getErrorMessage(expression) == null) { - final PsiType originalType = RefactoringUtil.getTypeByExpressionWithExpectedType(expression); - if (originalType != null) { - elements = new PsiElement[]{expression}; + PsiElement[] elements; + PsiExpression expr = CodeInsightUtil.findExpressionInRange(file, startOffset, endOffset); + if (expr != null) { + elements = new PsiElement[]{expr}; + } + else { + elements = CodeInsightUtil.findStatementsInRange(file, startOffset, endOffset); + if (elements.length == 0) { + final PsiExpression expression = IntroduceVariableBase.getSelectedExpression(project, file, startOffset, endOffset); + if (expression != null && IntroduceVariableBase.getErrorMessage(expression) == null) { + final PsiType originalType = RefactoringUtil.getTypeByExpressionWithExpectedType(expression); + if (originalType != null) { + elements = new PsiElement[]{expression}; + } } } } + return elements; } - return elements; + + final List expressions = IntroduceVariableBase.collectExpressions(file, editor, editor.getCaretModel().getOffset()); + return expressions.toArray(new PsiElement[expressions.size()]); } private static void invokeOnElements(final Project project, final Editor editor, PsiFile file, PsiElement[] elements) {