Skip to content

Commit

Permalink
fix(position): Invalid end column (#2159)
Browse files Browse the repository at this point in the history
Hi! I've found out that `getEndColumn()` from `CompoundSourcePositionImpl` works incorrectly.
Here are 2 screenshots to illustrate the problem.    
Now:
![not_ok](https://user-images.githubusercontent.com/32983915/42217181-e2d869a0-7ecc-11e8-8914-7855230e52d8.png)
Expected:
![ok](https://user-images.githubusercontent.com/32983915/42217212-fae6828e-7ecc-11e8-8875-63645851cfa6.png)

The problem is that only `getEndLine()` has been overriden, but not `getEndColumn()`.
So this PR contains fix and additional test.
  • Loading branch information
Egor18 authored and surli committed Jul 3, 2018
1 parent 9a45459 commit 7aee315
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,16 @@ public int getNameEnd() {
return super.getSourceEnd();
}

@Override
public int getEndLine() {
return searchLineNumber(declarationSourceEnd);
}

@Override
public int getEndColumn() {
return searchColumnNumber(declarationSourceEnd);
}

@Override
public String getSourceDetails() {
return super.getSourceDetails()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected int searchLineNumber(int position) {
/**
* Search the column number
*/
private int searchColumnNumber(int position) {
protected int searchColumnNumber(int position) {
if (lineSeparatorPositions == null) {
return -1;
}
Expand Down
14 changes: 13 additions & 1 deletion src/test/java/spoon/test/position/PositionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1024,5 +1024,17 @@ public void testFooForEach() throws Exception {
assertEquals("items", contentAtPosition(classContent, forEach.getExpression().getPosition()));
}
}


@Test
public void testEndColumn() throws Exception {
//contract: check end column
final Factory build = build(FooStatement.class);
final CtType<FooStatement> foo = build.Type().get(FooStatement.class);
CtMethod<?> m = foo.getMethodsByName("m").get(0);
SourcePosition pos = m.getPosition();
assertEquals(7, pos.getLine());
assertEquals(14, pos.getColumn());
assertEquals(23, pos.getEndLine());
assertEquals(2, pos.getEndColumn());
}
}

0 comments on commit 7aee315

Please sign in to comment.