Separate GUI logic from smartSelectExpression

Previously smartSelectExpression did both the determination of suitable
expressions and displaying the GUI. Separate the actual logic into a new
method, getSmartSelectSuggestions so that smart selection can be tested
in the future.
This commit is contained in:
Tuomas Tynkkynen
2014-02-07 13:26:12 -08:00
committed by Nikolay Krasko
parent 7429cc8819
commit 815ca2fe57
@@ -383,16 +383,22 @@ public class JetRefactoringUtil {
} }
} }
private static void smartSelectExpression(@NotNull Editor editor, @NotNull PsiFile file, int offset, public static List<JetExpression> getSmartSelectSuggestions(
@NotNull final SelectExpressionCallback callback) @NotNull PsiFile file,
throws IntroduceRefactoringException { int offset
if (offset < 0) throw new IntroduceRefactoringException(JetRefactoringBundle.message("cannot.refactor.not.expression")); ) throws IntroduceRefactoringException {
PsiElement element = file.findElementAt(offset); if (offset < 0) {
if (element == null) throw new IntroduceRefactoringException(JetRefactoringBundle.message("cannot.refactor.not.expression")); return new ArrayList<JetExpression>();
if (element instanceof PsiWhiteSpace) {
smartSelectExpression(editor, file, offset - 1, callback);
return;
} }
PsiElement element = file.findElementAt(offset);
if (element == null) {
return new ArrayList<JetExpression>();
}
if (element instanceof PsiWhiteSpace) {
return getSmartSelectSuggestions(file, offset - 1);
}
ArrayList<JetExpression> expressions = new ArrayList<JetExpression>(); ArrayList<JetExpression> expressions = new ArrayList<JetExpression>();
while (element != null && !(element instanceof JetBlockExpression && !(element.getParent() instanceof JetFunctionLiteral)) && while (element != null && !(element instanceof JetBlockExpression && !(element.getParent() instanceof JetFunctionLiteral)) &&
!(element instanceof JetNamedFunction) !(element instanceof JetNamedFunction)
@@ -427,6 +433,13 @@ public class JetRefactoringUtil {
} }
element = element.getParent(); element = element.getParent();
} }
return expressions;
}
private static void smartSelectExpression(
@NotNull Editor editor, @NotNull PsiFile file, int offset,
@NotNull final SelectExpressionCallback callback) throws IntroduceRefactoringException {
List<JetExpression> expressions = getSmartSelectSuggestions(file, offset);
if (expressions.size() == 0) throw new IntroduceRefactoringException(JetRefactoringBundle.message("cannot.refactor.not.expression")); if (expressions.size() == 0) throw new IntroduceRefactoringException(JetRefactoringBundle.message("cannot.refactor.not.expression"));
if (expressions.size() == 1) { if (expressions.size() == 1) {