Skip to content

Commit

Permalink
Fix source offsets for multi-variable declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Apr 20, 2018
1 parent 3d3d060 commit 05efc57
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2259,11 +2259,16 @@ public List<Expression> visitTypeNamePairs(TypeNamePairsContext ctx) {

@Override
public VariableExpression visitTypeNamePair(TypeNamePairContext ctx) {
/* GRECLIPSE edit
return configureAST(
new VariableExpression(
this.visitVariableDeclaratorId(ctx.variableDeclaratorId()).getName(),
this.visitType(ctx.type())),
ctx);
*/
VariableExpression var = new VariableExpression(visitIdentifier(ctx.variableDeclaratorId().identifier()), visitType(ctx.type()));
return configureAST(var, ctx.variableDeclaratorId());
// GRECLIPSE end
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
package org.codehaus.groovy.eclipse.test.ui

import static org.codehaus.groovy.eclipse.editor.highlighting.HighlightedTypedPosition.HighlightKind.*
import static org.eclipse.jdt.groovy.core.tests.GroovyBundle.isParrotParser
import static org.junit.Assert.assertEquals
import static org.junit.Assume.assumeTrue

import org.codehaus.groovy.eclipse.GroovyPlugin
import org.codehaus.groovy.eclipse.core.preferences.PreferenceConstants
Expand Down Expand Up @@ -760,6 +762,92 @@ final class SemanticHighlightingTests extends GroovyEclipseTestSuite {
new HighlightedTypedPosition(contents.lastIndexOf('ex'), 2, VARIABLE))
}

@Test
void testForParam() {
String contents = '''\
for (int i = 0; i < n; i++) {
i
}
'''.stripIndent()

assertHighlighting(contents,
new HighlightedTypedPosition(contents.indexOf('i ='), 1, VARIABLE),
new HighlightedTypedPosition(contents.indexOf('0' ), 1, NUMBER ),
new HighlightedTypedPosition(contents.indexOf('i <'), 1, VARIABLE),
new HighlightedTypedPosition(contents.indexOf('n; '), 1, UNKNOWN ),
new HighlightedTypedPosition(contents.indexOf('i++'), 1, VARIABLE),
new HighlightedTypedPosition(contents.lastIndexOf('i'), 1, VARIABLE))
}

@Test
void testForParams() {
assumeTrue(isParrotParser())

String contents = '''\
for (int i = 0, n = 999; i < n; i++) {
i
n
}
'''.stripIndent()

assertHighlighting(contents,
new HighlightedTypedPosition(contents.indexOf('i ='), 1, VARIABLE),
new HighlightedTypedPosition(contents.indexOf('0' ), 1, NUMBER ),
new HighlightedTypedPosition(contents.indexOf('n ='), 1, VARIABLE),
new HighlightedTypedPosition(contents.indexOf('999'), 3, NUMBER ),
new HighlightedTypedPosition(contents.indexOf('i <'), 1, VARIABLE),
new HighlightedTypedPosition(contents.indexOf('n; '), 1, VARIABLE),
new HighlightedTypedPosition(contents.indexOf('i++'), 1, VARIABLE),
new HighlightedTypedPosition(contents.lastIndexOf('i'), 1, VARIABLE),
new HighlightedTypedPosition(contents.lastIndexOf('n'), 1, VARIABLE))
}

@Test
void testForParams2() {
assumeTrue(isParrotParser())

String contents = '''\
for (def (i, n) = [0, 999]; i < n; i++) {
i
n
}
'''.stripIndent()

assertHighlighting(contents,
new HighlightedTypedPosition(contents.indexOf('i'), 1, VARIABLE),
new HighlightedTypedPosition(contents.indexOf('n'), 1, VARIABLE),
new HighlightedTypedPosition(contents.indexOf('0' ), 1, NUMBER ),
new HighlightedTypedPosition(contents.indexOf('999'), 3, NUMBER ),
new HighlightedTypedPosition(contents.indexOf('i <'), 1, VARIABLE),
new HighlightedTypedPosition(contents.indexOf('n; '), 1, VARIABLE),
new HighlightedTypedPosition(contents.indexOf('i++'), 1, VARIABLE),
new HighlightedTypedPosition(contents.lastIndexOf('i'), 1, VARIABLE),
new HighlightedTypedPosition(contents.lastIndexOf('n'), 1, VARIABLE))
}

@Test
void testForParams3() {
assumeTrue(isParrotParser())

String contents = '''\
for (def (int i, int n) = [0, 999]; i < n; i++) {
i
n
}
'''.stripIndent()

assertHighlighting(contents,
new HighlightedTypedPosition(contents.indexOf('i,'), 1, VARIABLE),
new HighlightedTypedPosition(contents.indexOf('n)'), 1, VARIABLE),
new HighlightedTypedPosition(contents.indexOf('0' ), 1, NUMBER ),
new HighlightedTypedPosition(contents.indexOf('999'), 3, NUMBER ),
new HighlightedTypedPosition(contents.indexOf('i <'), 1, VARIABLE),
new HighlightedTypedPosition(contents.indexOf('n; '), 1, VARIABLE),
new HighlightedTypedPosition(contents.indexOf('i++'), 1, VARIABLE),
new HighlightedTypedPosition(contents.lastIndexOf('i'), 1, VARIABLE),
new HighlightedTypedPosition(contents.lastIndexOf('n'), 1, VARIABLE))
}

@Test
void testForEachParam() {
// don't want PARAMETER
Expand Down

0 comments on commit 05efc57

Please sign in to comment.