Skip to content

Commit

Permalink
Fix for issue #560: fix breakpoint activation for method with annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Apr 13, 2018
1 parent d7029fc commit b9cd2e7
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ public ModuleNode buildAST(SourceUnit sourceUnit, ClassLoader classLoader, Reduc

// GRECLIPSE add
fixModuleNodeLocations();
output.putNodeMetaData(LocationSupport.class, locations);
// GRECLIPSE end
}
catch (ASTRuntimeException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ public ModuleNode buildAST(SourceUnit sourceUnit, ClassLoader classLoader, Reduc

// GRECLIPSE add
fixModuleNodeLocations();
output.putNodeMetaData(LocationSupport.class, locations);
// GRECLIPSE end
}
catch (ASTRuntimeException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,7 @@ public ModuleNode visitCompilationUnit(CompilationUnitContext ctx) {
runMethod.setLastColumnNumber(omega.getLastColumnNumber());
}
}
moduleNode.putNodeMetaData(LocationSupport.class, locationSupport);
sourceUnit.setComments(lexer.getComments());
// GRECLIPSE end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ public ModuleNode buildAST(SourceUnit sourceUnit, ClassLoader classLoader, Reduc
runMethod.setLastColumnNumber(last.getLastColumnNumber());
}
}
output.putNodeMetaData(LocationSupport.class, locations);
// GRECLIPSE end
}
catch (ASTRuntimeException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,18 @@ final class BreakpointLocationTests extends GroovyEclipseTestSuite {
assert node instanceof MethodNode
assert node.lineNumber == 5
}

@Test
void testBreakpointInClass13() {
def node = findBreakpointLocation 'def m', '''\
class Class {
@Deprecated
def m() {
here()
}
}
'''.stripIndent()

assert node instanceof MethodNode
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2018 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 All @@ -23,7 +23,6 @@

import org.codehaus.groovy.antlr.AntlrParserPlugin;
import org.codehaus.groovy.antlr.GroovySourceAST;
import org.codehaus.groovy.antlr.LocationSupport;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.MethodNode;
import org.codehaus.groovy.ast.ModuleNode;
Expand All @@ -47,12 +46,6 @@
*/
public class GroovySnippetParser {

private LocationSupport locations;

public LocationSupport getLocations() {
return locations;
}

private CategorizedProblem[] problems;

public CategorizedProblem[] getProblems() {
Expand Down Expand Up @@ -108,8 +101,6 @@ private GroovyCompilationUnitDeclaration dietParse(CharSequence source) {
CompilationResult compilationResult = new CompilationResult(unit, 0, 0, compilerOptions.maxProblemsPerUnit);

GroovyCompilationUnitDeclaration gcud = (GroovyCompilationUnitDeclaration) parser.dietParse(unit, compilationResult);
locations = (LocationSupport) ReflectionUtils.getPrivateField(AntlrParserPlugin.class, "locations",
ReflectionUtils.getPrivateField(SourceUnit.class, "parserPlugin", gcud.getSourceUnit()));
problems = compilationResult.getProblems();
return gcud;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2018 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 All @@ -20,6 +20,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.codehaus.groovy.antlr.LocationSupport;
import org.codehaus.groovy.ast.Comment;
import org.codehaus.groovy.ast.ModuleNode;
import org.codehaus.groovy.eclipse.core.ISourceBuffer;
Expand Down Expand Up @@ -341,14 +342,16 @@ private boolean isComment(int index) {
GroovySnippetParser parser = new GroovySnippetParser();
ModuleNode module = parser.parse(source);

LocationSupport locator = module.getNodeMetaData(LocationSupport.class);

// extract the comment ranges from the parse results
List<Comment> list = module.getContext().getComments();
int i = 0, n = (list == null ? 0 : list.size());
comments = new int[n * 2];
if (n > 0) {
for (Comment comment : list) {
comments[i++] = parser.getLocations().findOffset(comment.sline, comment.scol);
comments[i++] = parser.getLocations().findOffset(comment.eline, comment.ecol);
comments[i++] = locator.findOffset(comment.sline, comment.scol);
comments[i++] = locator.findOffset(comment.eline, comment.ecol);
}
Arrays.sort(comments); // should be sorted already, but let's be sure
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Comparator;
import java.util.TreeSet;

import org.codehaus.groovy.antlr.LocationSupport;
import org.codehaus.groovy.ast.ASTNode;
import org.codehaus.groovy.ast.AnnotationNode;
import org.codehaus.groovy.ast.MethodNode;
Expand All @@ -31,6 +32,7 @@

public class BreakpointLocationFinder {

protected final LocationSupport locator;
protected final Iterable<ASTNode> nodes;

public BreakpointLocationFinder(ModuleNode module) {
Expand Down Expand Up @@ -62,6 +64,7 @@ public void visitMethod(MethodNode method) {
}.visitModule(module);

this.nodes = Collections.unmodifiableSet(nodes);
this.locator = module.getNodeMetaData(LocationSupport.class);
}

public ASTNode findBreakpointLocation(int lineNumber) {
Expand All @@ -75,12 +78,23 @@ public ASTNode findBreakpointLocation(int lineNumber) {
// variable expression in a declaration expression with no initializer
skipNext = true;
}
} else if (node.getLineNumber() >= lineNumber) {
} else if (lineNumber(node) >= lineNumber) {
bestMatch = node;
break;
}
}

return bestMatch;
}

protected int lineNumber(ASTNode node) {
if (locator != null && node instanceof MethodNode) {
// annotations, modifiers and generics may be on separate line(s)
int[] row_col = locator.getRowCol(((MethodNode) node).getNameStart());
if (row_col != null && row_col.length > 0) {
return row_col[0];
}
}
return node.getLineNumber();
}
}

0 comments on commit b9cd2e7

Please sign in to comment.