groovy string select word
authorMaxim Medvedev <maxim.medvedev@jetbrains.com>
Mon, 19 Oct 2009 11:05:48 +0000 (15:05 +0400)
committerMaxim Medvedev <maxim.medvedev@jetbrains.com>
Mon, 19 Oct 2009 11:11:46 +0000 (15:11 +0400)
17 files changed:
plugins/groovy/src/org/jetbrains/plugins/groovy/editor/selection/GroovyGStringSelectioner.java [new file with mode: 0644]
plugins/groovy/src/org/jetbrains/plugins/groovy/editor/selection/GroovyParameterListSelectioner.java [new file with mode: 0644]
plugins/groovy/src/org/jetbrains/plugins/groovy/editor/selection/GroovyWordSelectionFilter.java [new file with mode: 0644]
plugins/groovy/testdata/groovy/actions/SWInGString1.groovy [new file with mode: 0644]
plugins/groovy/testdata/groovy/actions/SWInGString1_after.groovy [new file with mode: 0644]
plugins/groovy/testdata/groovy/actions/SWInGString2.groovy [new file with mode: 0644]
plugins/groovy/testdata/groovy/actions/SWInGString2_after.groovy [new file with mode: 0644]
plugins/groovy/testdata/groovy/actions/SWInGString3.groovy [new file with mode: 0644]
plugins/groovy/testdata/groovy/actions/SWInGString3_after.groovy [new file with mode: 0644]
plugins/groovy/testdata/groovy/actions/SWInGString4.groovy [new file with mode: 0644]
plugins/groovy/testdata/groovy/actions/SWInGString4_after.groovy [new file with mode: 0644]
plugins/groovy/testdata/groovy/actions/SWInGString5.groovy [new file with mode: 0644]
plugins/groovy/testdata/groovy/actions/SWInGString5_after.groovy [new file with mode: 0644]
plugins/groovy/testdata/groovy/actions/SWInParameterList.groovy [new file with mode: 0644]
plugins/groovy/testdata/groovy/actions/SWInParameterList_after.groovy [new file with mode: 0644]
plugins/groovy/testdata/highlighting/FQNJavaClassesUsages.groovy [new file with mode: 0644]
plugins/groovy/testdata/parsing/groovy/statements/declaration/dollar.test [new file with mode: 0644]

diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/editor/selection/GroovyGStringSelectioner.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/editor/selection/GroovyGStringSelectioner.java
new file mode 100644 (file)
index 0000000..982fc13
--- /dev/null
@@ -0,0 +1,175 @@
+/*
+ * Copyright 2000-2009 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jetbrains.plugins.groovy.editor.selection;
+
+import com.intellij.codeInsight.editorActions.SelectWordUtil;
+import com.intellij.lang.ASTNode;
+import com.intellij.openapi.editor.Editor;
+import com.intellij.openapi.util.TextRange;
+import com.intellij.psi.PsiElement;
+import com.intellij.psi.tree.IElementType;
+import static org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes.*;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author Maxim.Medvedev
+ */
+public class GroovyGStringSelectioner extends GroovyBasicSelectioner {
+  public boolean canSelect(PsiElement e) {
+    PsiElement parent = e.getParent();
+    if (parent instanceof GrClosableBlock || parent instanceof GrReferenceExpression) {
+      parent = parent.getParent();
+    }
+    return parent instanceof GrStringInjection || parent instanceof GrString;
+  }
+
+  @Override
+  public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
+    List<TextRange> ranges = new ArrayList<TextRange>();
+    PsiElement parent = e.getParent();
+    if (parent instanceof GrReferenceExpression) {
+      e = parent;
+      parent = e.getParent();
+    }
+    if (parent instanceof GrClosableBlock) {
+      parent = parent.getParent();
+    }
+
+    if (parent instanceof GrString) {
+      final TextRange selection =
+        new TextRange(editor.getSelectionModel().getSelectionStart(), editor.getSelectionModel().getSelectionEnd());
+
+      TextRange range = getLineTextRange(e, cursorOffset);
+      ranges.add(range);
+      if (selection.contains(range)) {
+
+        PsiElement firstChild = parent.getFirstChild();
+        PsiElement lastChild = parent.getLastChild();
+        if (firstChild.getNode().getElementType() == mGSTRING_BEGIN) {
+          firstChild = firstChild.getNextSibling();
+        }
+        if (lastChild.getNode().getElementType() == mGSTRING_END) {
+          lastChild = lastChild.getPrevSibling();
+        }
+
+        if (firstChild != null && lastChild != null) {
+          range = new TextRange(firstChild.getTextOffset(), lastChild.getTextOffset() + lastChild.getTextLength());
+          ranges.add(range);
+        }
+
+        if (selection.contains(range) || firstChild == null || lastChild == null) {
+          ranges.add(parent.getTextRange());
+        }
+      }
+    }
+    else if (parent instanceof GrStringInjection) {
+      if (e instanceof GrReferenceExpression) {
+        List<TextRange> r = new ArrayList<TextRange>(2);
+        SelectWordUtil.addWordSelection(editor.getSettings().isCamelWords(), editorText, cursorOffset, r);
+        for (TextRange textRange : r) {
+          if (editorText.charAt(textRange.getStartOffset()) == '$') {
+            textRange = new TextRange(textRange.getStartOffset() + 1, textRange.getEndOffset());
+          }
+          ranges.add(textRange);
+        }
+
+      }
+      ranges.add(parent.getTextRange());
+      ranges.add(e.getTextRange());
+    }
+
+    return ranges;
+  }
+
+  private static TextRange getLineTextRange(PsiElement e, int cursorOffset) {
+    assert e.getParent() instanceof GrString;
+
+    PsiElement next = e;
+    int startOffset = cursorOffset;
+    int endOffset = cursorOffset;
+    if (e.getNode().getElementType() == mGSTRING_CONTENT) {
+      final String text = e.getText();
+      int cur;
+      int index = -1;
+      while (true) {
+        cur = text.indexOf('\n', index + 1);
+        if (cur < 0 || cur + e.getTextOffset() > cursorOffset) break;
+        index = cur;
+      }
+      if (index >= 0) {
+        startOffset = e.getTextOffset() + index + 1;
+      }
+
+      index = text.indexOf('\n', cursorOffset - e.getTextOffset());
+      if (index >= 0) {
+        endOffset = e.getTextOffset() + index + 1;
+      }
+    }
+
+    if (startOffset == cursorOffset) {
+      do {
+        if (next == null) break;
+        final ASTNode node = next.getNode();
+        if (node == null) break;
+        final IElementType type = node.getElementType();
+        if (type == mGSTRING_BEGIN) break;
+        if (type == mGSTRING_CONTENT) {
+          final int i = next.getText().lastIndexOf('\n');
+          if (i >= 0) {
+            startOffset = next.getTextOffset() + i + 1;
+            break;
+          }
+        }
+        startOffset = next.getTextOffset();
+        next = next.getPrevSibling();
+      }
+      while (true);
+    }
+
+    if (endOffset == cursorOffset) {
+      next = e;
+      do {
+        if (next == null) break;
+        final ASTNode node = next.getNode();
+        if (node == null) break;
+
+        final IElementType type = node.getElementType();
+        if (type == mGSTRING_END) {
+          endOffset = next.getTextOffset();
+          break;
+        }
+        if (type == mGSTRING_CONTENT) {
+          final int i = next.getText().indexOf('\n');
+          if (i >= 0) {
+            endOffset = next.getTextOffset() + i + 1;
+            break;
+          }
+        }
+        endOffset = next.getTextOffset() + next.getTextLength();
+        next = next.getNextSibling();
+      }
+      while (true);
+    }
+
+    return new TextRange(startOffset, endOffset);
+  }
+}
diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/editor/selection/GroovyParameterListSelectioner.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/editor/selection/GroovyParameterListSelectioner.java
new file mode 100644 (file)
index 0000000..a684a71
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2000-2009 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jetbrains.plugins.groovy.editor.selection;
+
+import com.intellij.openapi.editor.Editor;
+import com.intellij.openapi.util.TextRange;
+import com.intellij.psi.PsiElement;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList;
+
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * @author Maxim.Medvedev
+ */
+public class GroovyParameterListSelectioner extends GroovyBasicSelectioner {
+  @Override
+  public boolean canSelect(PsiElement e) {
+    return e instanceof GrParameterList || e.getParent() instanceof GrParameterList;
+  }
+
+  @Override
+  public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
+    if (e.getParent() instanceof GrParameterList) e = e.getParent();
+    if (!(e instanceof GrParameterList)) return Collections.emptyList();
+    return Collections.singletonList(e.getTextRange());
+  }
+}
diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/editor/selection/GroovyWordSelectionFilter.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/editor/selection/GroovyWordSelectionFilter.java
new file mode 100644 (file)
index 0000000..2d9ed0a
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2000-2009 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jetbrains.plugins.groovy.editor.selection;
+
+import com.intellij.lang.ASTNode;
+import com.intellij.openapi.util.Condition;
+import com.intellij.psi.PsiElement;
+import com.intellij.psi.tree.IElementType;
+import static org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes.*;
+import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList;
+
+/**
+ * @author Maxim.Medvedev
+ */
+public class GroovyWordSelectionFilter implements Condition<PsiElement> {
+  public boolean value(PsiElement element) {
+
+    final ASTNode node = element.getNode();
+    if (node == null) return false;
+
+    final IElementType type = node.getElementType();
+    if (type == mIDENT ||
+        type == mSTRING_LITERAL ||
+        type == mGSTRING_LITERAL ||
+        type == mGSTRING_CONTENT ||
+        type == mGDOC_COMMENT_DATA ||
+        type == mGDOC_TAG_NAME ||
+        type == mGDOC_TAG_PLAIN_VALUE_TOKEN ||
+        type == mGDOC_TAG_VALUE_TOKEN ||
+        type == mREGEX_BEGIN ||
+        type == mREGEX_CONTENT ||
+        type == mREGEX_END ||
+        type == mWRONG_REGEX_LITERAL) {
+      return true;
+    }
+
+    return !(element instanceof GrCodeBlock) &&
+           !(element instanceof GrListOrMap) &&
+           !(element instanceof GrParameterList) &&
+           !(element instanceof GrArgumentList) &&
+           !(type == mDOLLAR)
+
+      ;
+  }
+}
diff --git a/plugins/groovy/testdata/groovy/actions/SWInGString1.groovy b/plugins/groovy/testdata/groovy/actions/SWInGString1.groovy
new file mode 100644 (file)
index 0000000..1a81ad5
--- /dev/null
@@ -0,0 +1,4 @@
+print """My fir<caret>stName is $firstName.
+My last name is $lastName.
+I am ${2009 - yearOfBirth} now.
+"""
\ No newline at end of file
diff --git a/plugins/groovy/testdata/groovy/actions/SWInGString1_after.groovy b/plugins/groovy/testdata/groovy/actions/SWInGString1_after.groovy
new file mode 100644 (file)
index 0000000..70af9f5
--- /dev/null
@@ -0,0 +1,4 @@
+print """My <selection>fir<caret>st</selection>Name is $firstName.
+My last name is $lastName.
+I am ${2009 - yearOfBirth} now.
+"""
\ No newline at end of file
diff --git a/plugins/groovy/testdata/groovy/actions/SWInGString2.groovy b/plugins/groovy/testdata/groovy/actions/SWInGString2.groovy
new file mode 100644 (file)
index 0000000..1a81ad5
--- /dev/null
@@ -0,0 +1,4 @@
+print """My fir<caret>stName is $firstName.
+My last name is $lastName.
+I am ${2009 - yearOfBirth} now.
+"""
\ No newline at end of file
diff --git a/plugins/groovy/testdata/groovy/actions/SWInGString2_after.groovy b/plugins/groovy/testdata/groovy/actions/SWInGString2_after.groovy
new file mode 100644 (file)
index 0000000..9fe89d6
--- /dev/null
@@ -0,0 +1,4 @@
+print """My <selection>fir<caret>stName</selection> is $firstName.
+My last name is $lastName.
+I am ${2009 - yearOfBirth} now.
+"""
\ No newline at end of file
diff --git a/plugins/groovy/testdata/groovy/actions/SWInGString3.groovy b/plugins/groovy/testdata/groovy/actions/SWInGString3.groovy
new file mode 100644 (file)
index 0000000..1a81ad5
--- /dev/null
@@ -0,0 +1,4 @@
+print """My fir<caret>stName is $firstName.
+My last name is $lastName.
+I am ${2009 - yearOfBirth} now.
+"""
\ No newline at end of file
diff --git a/plugins/groovy/testdata/groovy/actions/SWInGString3_after.groovy b/plugins/groovy/testdata/groovy/actions/SWInGString3_after.groovy
new file mode 100644 (file)
index 0000000..3d14cc4
--- /dev/null
@@ -0,0 +1,4 @@
+print """<selection>My fir<caret>stName is </selection>$firstName.
+My last name is $lastName.
+I am ${2009 - yearOfBirth} now.
+"""
\ No newline at end of file
diff --git a/plugins/groovy/testdata/groovy/actions/SWInGString4.groovy b/plugins/groovy/testdata/groovy/actions/SWInGString4.groovy
new file mode 100644 (file)
index 0000000..1a81ad5
--- /dev/null
@@ -0,0 +1,4 @@
+print """My fir<caret>stName is $firstName.
+My last name is $lastName.
+I am ${2009 - yearOfBirth} now.
+"""
\ No newline at end of file
diff --git a/plugins/groovy/testdata/groovy/actions/SWInGString4_after.groovy b/plugins/groovy/testdata/groovy/actions/SWInGString4_after.groovy
new file mode 100644 (file)
index 0000000..b516742
--- /dev/null
@@ -0,0 +1,4 @@
+print """<selection>My fir<caret>stName is $firstName.
+</selection>My last name is $lastName.
+I am ${2009 - yearOfBirth} now.
+"""
\ No newline at end of file
diff --git a/plugins/groovy/testdata/groovy/actions/SWInGString5.groovy b/plugins/groovy/testdata/groovy/actions/SWInGString5.groovy
new file mode 100644 (file)
index 0000000..1a81ad5
--- /dev/null
@@ -0,0 +1,4 @@
+print """My fir<caret>stName is $firstName.
+My last name is $lastName.
+I am ${2009 - yearOfBirth} now.
+"""
\ No newline at end of file
diff --git a/plugins/groovy/testdata/groovy/actions/SWInGString5_after.groovy b/plugins/groovy/testdata/groovy/actions/SWInGString5_after.groovy
new file mode 100644 (file)
index 0000000..3608e54
--- /dev/null
@@ -0,0 +1,4 @@
+print """<selection>My fir<caret>stName is $firstName.
+My last name is $lastName.
+I am ${2009 - yearOfBirth} now.
+</selection>"""
\ No newline at end of file
diff --git a/plugins/groovy/testdata/groovy/actions/SWInParameterList.groovy b/plugins/groovy/testdata/groovy/actions/SWInParameterList.groovy
new file mode 100644 (file)
index 0000000..0a9cfc7
--- /dev/null
@@ -0,0 +1,3 @@
+def toString(int a, i<caret>nt b) {
+
+}
\ No newline at end of file
diff --git a/plugins/groovy/testdata/groovy/actions/SWInParameterList_after.groovy b/plugins/groovy/testdata/groovy/actions/SWInParameterList_after.groovy
new file mode 100644 (file)
index 0000000..7d693b1
--- /dev/null
@@ -0,0 +1,3 @@
+def toString(<selection>int a, i<caret>nt b</selection>) {
+
+}
\ No newline at end of file
diff --git a/plugins/groovy/testdata/highlighting/FQNJavaClassesUsages.groovy b/plugins/groovy/testdata/highlighting/FQNJavaClassesUsages.groovy
new file mode 100644 (file)
index 0000000..e63da66
--- /dev/null
@@ -0,0 +1 @@
+print java.util.Arrays.asList(1, 2, 3)
\ No newline at end of file
diff --git a/plugins/groovy/testdata/parsing/groovy/statements/declaration/dollar.test b/plugins/groovy/testdata/parsing/groovy/statements/declaration/dollar.test
new file mode 100644 (file)
index 0000000..4e416a7
--- /dev/null
@@ -0,0 +1,12 @@
+Map $map
+-----
+Groovy script
+  Variable definitions
+    Modifiers
+      <empty list>
+    Type element
+      Reference element
+        PsiElement(identifier)('Map')
+    PsiWhiteSpace(' ')
+    Variable
+      PsiElement(identifier)('$map')
\ No newline at end of file