From b207f7716d31776e222e7552b53c55d44966e5c6 Mon Sep 17 00:00:00 2001 From: peter Date: Sun, 14 Feb 2010 21:58:05 +0000 Subject: [PATCH] groovy select word now selects list/map literals and method arguments before qualifier (IDEA-52012) --- .../editor/selection/GroovyArgListSelectioner.java | 17 +++++++++++---- .../editor/selection/GroovyLiteralSelectioner.java | 20 ++++++++++-------- ...vyActionsTest.java => GroovyActionsTest.groovy} | 24 +++++++++++++++++++++- 3 files changed, 47 insertions(+), 14 deletions(-) rename plugins/groovy/test/org/jetbrains/plugins/groovy/{GroovyActionsTest.java => GroovyActionsTest.groovy} (80%) diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/editor/selection/GroovyArgListSelectioner.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/editor/selection/GroovyArgListSelectioner.java index cf2d621c3d..6339efa59b 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/editor/selection/GroovyArgListSelectioner.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/editor/selection/GroovyArgListSelectioner.java @@ -16,11 +16,12 @@ package org.jetbrains.plugins.groovy.editor.selection; -import com.intellij.psi.PsiElement; -import com.intellij.openapi.util.TextRange; import com.intellij.openapi.editor.Editor; -import org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocMethodParams; +import com.intellij.openapi.util.TextRange; +import com.intellij.psi.PsiElement; import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression; import java.util.List; @@ -29,7 +30,7 @@ import java.util.List; */ public class GroovyArgListSelectioner extends GroovyBasicSelectioner { public boolean canSelect(PsiElement e) { - return e instanceof GrArgumentList; + return e instanceof GrArgumentList || e.getParent() instanceof GrReferenceExpression && e.getParent().getParent() instanceof GrCall; } public List select(PsiElement element, CharSequence editorText, int cursorOffset, Editor editor) { @@ -57,6 +58,14 @@ public class GroovyArgListSelectioner extends GroovyBasicSelectioner { } } } + final PsiElement parent = element.getParent(); + if (parent instanceof GrReferenceExpression) { + final GrArgumentList argumentList = ((GrCall)parent.getParent()).getArgumentList(); + final PsiElement refName = ((GrReferenceExpression)parent).getReferenceNameElement(); + if (argumentList != null && refName == element) { + result.add(new TextRange(refName.getTextRange().getStartOffset(), argumentList.getTextRange().getEndOffset())); + } + } return result; } } \ No newline at end of file diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/editor/selection/GroovyLiteralSelectioner.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/editor/selection/GroovyLiteralSelectioner.java index bbeed25f29..e7a515419a 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/editor/selection/GroovyLiteralSelectioner.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/editor/selection/GroovyLiteralSelectioner.java @@ -21,22 +21,28 @@ import com.intellij.openapi.editor.Editor; import com.intellij.openapi.util.TextRange; import com.intellij.psi.PsiElement; import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes; -import static org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes.mGSTRING_CONTENT; +import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap; import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral; import org.jetbrains.plugins.groovy.lang.psi.util.GrStringUtil; import java.util.List; +import static org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes.mGSTRING_CONTENT; + /** * @author ilyas */ public class GroovyLiteralSelectioner extends GroovyBasicSelectioner { public boolean canSelect(PsiElement e) { PsiElement parent = e.getParent(); - return isStringLiteral(e) || isStringLiteral(parent); + return isLiteral(e) || isLiteral(parent); } - private static boolean isStringLiteral(PsiElement element) { + private static boolean isLiteral(PsiElement element) { + if (element instanceof GrListOrMap) { + return true; + } + if (!(element instanceof GrLiteral)) return false; ASTNode node = element.getNode(); if (node == null) return false; @@ -49,13 +55,9 @@ public class GroovyLiteralSelectioner extends GroovyBasicSelectioner { public List select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) { List result = super.select(e, editorText, cursorOffset, editor); -/* TextRange range = e.getTextRange(); - if (range.getLength() <= 2) { - result.add(range); + if (e instanceof GrListOrMap) { + return result; } - else { - result.add(new TextRange(range.getStartOffset() + 1, range.getEndOffset() - 1)); - }*/ int startOffset = -1; int endOffset = -1; diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/GroovyActionsTest.java b/plugins/groovy/test/org/jetbrains/plugins/groovy/GroovyActionsTest.groovy similarity index 80% rename from plugins/groovy/test/org/jetbrains/plugins/groovy/GroovyActionsTest.java rename to plugins/groovy/test/org/jetbrains/plugins/groovy/GroovyActionsTest.groovy index 24fb8d43f5..4cde1e0857 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/GroovyActionsTest.java +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/GroovyActionsTest.groovy @@ -43,13 +43,35 @@ public class GroovyActionsTest extends LightCodeInsightFixtureTestCase { public void testSWInGString5() throws Exception {doTestForSelectWord(5);} public void testSWInParameterList() throws Exception {doTestForSelectWord(3);} + public void testSWListLiteralArgument() throws Exception { + doTestForSelectWord 2, +"foo([a], b)", +"foo([a], b)" + } + + public void testSWMethodParametersBeforeQualifier() throws Exception { + doTestForSelectWord 2, +"a.foo(b)", +"a.foo(b)" + } + + private void doTestForSelectWord(int count, String input, String expected) throws Exception { + myFixture.configureByText("a.groovy", input); + selectWord(count) + myFixture.checkResult(expected); + } + private void doTestForSelectWord(int count) throws Exception { myFixture.configureByFile(getTestName(false) + ".groovy"); + selectWord(count) + myFixture.checkResultByFile(getTestName(false) + "_after.groovy"); + } + + private def selectWord(int count) { myFixture.getEditor().getSettings().setCamelWords(true); for (int i = 0; i < count; i++) { performEditorAction(IdeActions.ACTION_EDITOR_SELECT_WORD_AT_CARET); } - myFixture.checkResultByFile(getTestName(false) + "_after.groovy"); } private void performEditorAction(final String actionId) { -- 2.11.4.GIT