Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ protected int searchColumnNumber(int position) {
if (getCompilationUnit() != null) {
tabSize = getCompilationUnit().getFactory().getEnvironment().getTabulationSize();
String source = getCompilationUnit().getOriginalSourceCode();
for (int j = lineSeparatorPositions[i]; j < position; j++) {
if (source.charAt(j) == '\t') {
tabCount++;
if (source != null) {
for (int j = lineSeparatorPositions[i]; j < position; j++) {
if (source.charAt(j) == '\t') {
tabCount++;
}
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/spoon/test/position/PositionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1435,6 +1435,20 @@ public void testLinePositionOkWithOneLineClassCode() {
assertTrue(listOfBadPositionElements.stream().allMatch(elt -> elt.getPosition().getLine() == 1), "Some Spoon elements have an invalid line position");
}

@Test
public void testGetEndColumnWithVirtualFileContainingNewlines() {
// contract: getEndColumn() should not throw NPE when using VirtualFile with newlines
final Launcher launcher = new Launcher();
launcher.addInputResource(new VirtualFile("class Source {\n// comment\n}", "x/y/z/Source.java"));
launcher.getEnvironment().setNoClasspath(true);
launcher.buildModel();

CtCompilationUnit cu = launcher.getFactory().CompilationUnit().getMap().values().iterator().next();
assertTrue(cu.getPosition().isValidPosition());
int endColumn = assertDoesNotThrow(() -> cu.getPosition().getEndColumn());
assertTrue(endColumn > 0, "getEndColumn should return a positive column");
}

@Test
public void testLambdaParameterPosition() {
// contract: position of lambda parameter is correct
Expand Down
Loading