Skip to content

Commit

Permalink
Fix for #1247: revert GString offset when name begins with $
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Apr 16, 2021
1 parent 6d330a5 commit 0c731d7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2020 the original author or authors.
* Copyright 2009-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -164,7 +164,7 @@ final class CodeSelectPropertiesTests extends BrowsingTestSuite {
String contents = '''\
|class C {
| String string = ""
| def meth() {
| void test() {
| def str = string
| }
|}
Expand All @@ -173,12 +173,12 @@ final class CodeSelectPropertiesTests extends BrowsingTestSuite {
}

@Test
void testCodeSelectGetProperty1a() {
void testCodeSelectGetProperty2() {
String contents = '''\
|@groovy.transform.TypeChecked
|class C {
| String string = ""
| def meth() {
| void test() {
| def str = string
| }
|}
Expand All @@ -187,7 +187,7 @@ final class CodeSelectPropertiesTests extends BrowsingTestSuite {
}

@Test
void testCodeSelectGetProperty1b() {
void testCodeSelectGetProperty3() {
String contents = '''\
|@groovy.transform.CompileStatic
|class C {
Expand All @@ -200,12 +200,27 @@ final class CodeSelectPropertiesTests extends BrowsingTestSuite {
assertCodeSelect([contents], 'string')
}

@Test
void testCodeSelectGetProperty4() {
String contents = '''\
|class C {
| def getFoo() { }
|}
|class D extends C {
| void test() {
| foo
| }
|}
|'''.stripMargin()
assertCodeSelect([contents], 'foo', 'getFoo')
}

@Test
void testCodeSelectSetProperty1() {
String contents = '''\
|class C {
| String string
| def meth() {
| void test() {
| string = ""
| }
|}
Expand All @@ -214,12 +229,12 @@ final class CodeSelectPropertiesTests extends BrowsingTestSuite {
}

@Test
void testCodeSelectSetProperty1a() {
void testCodeSelectSetProperty2() {
String contents = '''\
|@groovy.transform.TypeChecked
|class C {
| String string
| def meth() {
| void test() {
| string = ""
| }
|}
Expand All @@ -228,19 +243,33 @@ final class CodeSelectPropertiesTests extends BrowsingTestSuite {
}

@Test
void testCodeSelectSetProperty1b() {
void testCodeSelectSetProperty3() {
String contents = '''\
|@groovy.transform.CompileStatic
|class C {
| String string
| def meth() {
| void test() {
| string = ""
| }
|}
|'''.stripMargin()
assertCodeSelect([contents], 'string')
}

@Test // https://github.com/groovy/groovy-eclipse/issues/1247
void testCodeSelectSetProperty4() {
String contents = '''\
|@groovy.transform.CompileStatic
|class C {
| void set$xyz(value) { }
| void test() {
| $xyz = ""
| }
|}
|'''.stripMargin()
assertCodeSelect([contents], '$xyz', 'set$xyz')
}

@Test
void testCodeSelectGettersAndField1() {
String contents = '''\
Expand Down Expand Up @@ -485,22 +514,6 @@ final class CodeSelectPropertiesTests extends BrowsingTestSuite {
assert elements[0].elementName == 'value'
}

@Test
void testCodeSelectNonStaticProperty1() {
String contents = '''\
|class Super {
| def getSql() { null }
|}
|
|class Foo extends Super {
| def foo() {
| sql
| }
|}
|'''.stripMargin()
assertCodeSelect([contents], 'sql', 'getSql')
}

@Test
void testCodeSelectStaticProperty1() {
String contents = '''\
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2020 the original author or authors.
* Copyright 2009-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -76,6 +76,11 @@ public IJavaElement[] select(final GroovyCompilationUnit unit, int start, int le
Object[] result = findNodeForRegion(module, select);
ASTNode node = (ASTNode) result[0];
Region region = (Region) result[1];
// check for name that begins with '$' and revert GRECLIPSE-1330
if (node != null && start > 0 && node.getStart() == start - 1 && contents[start - 1] == '$') {
start -= 1;
length += 1;
}
if (node instanceof AnnotatedNode && !isKeyword(node, contents, start, length) && !isStringLiteral(node, contents, start, length)) {
// shortcut: check to see if we are looking for this type itself
if (isTypeDeclaration(node, module)) {
Expand Down

0 comments on commit 0c731d7

Please sign in to comment.