Skip to content

Commit 04287b3

Browse files
authored
Merge pull request #171 from sourcegraph/nsc/tabs
2 parents f521043 + 02a2243 commit 04287b3

File tree

4 files changed

+88
-5
lines changed

4 files changed

+88
-5
lines changed

build.sbt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,16 +230,19 @@ lazy val minimized = project
230230
.in(file("tests/minimized/.j11"))
231231
.settings(minimizedSettings)
232232
.dependsOn(agent, plugin)
233+
.disablePlugins(JavaFormatterPlugin)
233234

234235
lazy val minimized8 = project
235236
.in(file("tests/minimized/.j8"))
236237
.settings(minimizedSettings, javaToolchainVersion := "8")
237238
.dependsOn(agent, plugin)
239+
.disablePlugins(JavaFormatterPlugin)
238240

239241
lazy val minimized15 = project
240242
.in(file("tests/minimized/.j15"))
241243
.settings(minimizedSettings, javaToolchainVersion := "15")
242244
.dependsOn(agent, plugin)
245+
.disablePlugins(JavaFormatterPlugin)
243246

244247
lazy val minimizedScala = project
245248
.in(file("tests/minimized-scala"))

semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbVisitor.java

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ private String semanticdbSymbol(Symbol sym) {
255255

256256
private Optional<Semanticdb.Range> semanticdbRange(
257257
JCDiagnostic.DiagnosticPosition pos, CompilerRange kind, Symbol sym) {
258+
LineMap lineMap = event.getCompilationUnit().getLineMap();
258259
if (sym == null) return Optional.empty();
259260
int start, end;
260261
if (kind.isFromPoint() && sym.name != null) {
@@ -269,21 +270,53 @@ private Optional<Semanticdb.Range> semanticdbRange(
269270
}
270271

271272
if (kind.isFromTextSearch() && sym.name.length() > 0) {
272-
return RangeFinder.findRange(
273-
getCurrentPath(), trees, getCurrentPath().getCompilationUnit(), sym, start, this.source);
273+
Optional<Semanticdb.Range> range =
274+
RangeFinder.findRange(
275+
getCurrentPath(),
276+
trees,
277+
getCurrentPath().getCompilationUnit(),
278+
sym,
279+
start,
280+
this.source);
281+
if (range.isPresent()) return Optional.of(correctForTabs(range.get(), lineMap, start));
282+
else return range;
274283
} else if (start != Position.NOPOS && end != Position.NOPOS && end > start) {
275-
LineMap lineMap = event.getCompilationUnit().getLineMap();
276284
Semanticdb.Range range =
277285
Semanticdb.Range.newBuilder()
278286
.setStartLine((int) lineMap.getLineNumber(start) - 1)
279287
.setStartCharacter((int) lineMap.getColumnNumber(start) - 1)
280288
.setEndLine((int) lineMap.getLineNumber(end) - 1)
281289
.setEndCharacter((int) lineMap.getColumnNumber(end) - 1)
282290
.build();
291+
292+
range = correctForTabs(range, lineMap, start);
293+
283294
return Optional.of(range);
284-
} else {
285-
return Optional.empty();
286295
}
296+
297+
return Optional.empty();
298+
}
299+
300+
private Semanticdb.Range correctForTabs(Semanticdb.Range range, LineMap lineMap, int start) {
301+
int startLinePos = (int) lineMap.getPosition(lineMap.getLineNumber(start), 0);
302+
303+
// javac replaces every tab with 8 spaces in the linemap. As this is potentially inconsistent
304+
// with the source file itself, we adjust for that here if the line is actually indented with
305+
// tabs.
306+
// As for every tab there are 8 spaces, we remove 7 spaces for every tab to get the correct
307+
// char offset (note: different to _column_ offset your editor shows)
308+
if (this.source.charAt(startLinePos) == '\t') {
309+
int count = 1;
310+
while (this.source.charAt(++startLinePos) == '\t') count++;
311+
range =
312+
range
313+
.toBuilder()
314+
.setStartCharacter(range.getStartCharacter() - (count * 7))
315+
.setEndCharacter(range.getEndCharacter() - (count * 7))
316+
.build();
317+
}
318+
319+
return range;
287320
}
288321

289322
private Optional<Semanticdb.SymbolOccurrence> semanticdbOccurrence(
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package minimized;
2+
3+
public class TabIndented {
4+
public void app() {
5+
Object o = new Object() {
6+
@Override
7+
public boolean equals(Object other) {
8+
return false;
9+
}
10+
11+
@Override
12+
public String toString() {
13+
return "";
14+
}
15+
};
16+
}
17+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package minimized;
2+
3+
public class TabIndented {
4+
// ^^^^^^^^^^^ definition minimized/TabIndented# public class TabIndented
5+
// ^^^^^^^^^^^ definition minimized/TabIndented#`<init>`(). public TabIndented()
6+
public void app() {
7+
// ^^^ definition minimized/TabIndented#app(). public void app()
8+
→→Object o = new Object() {
9+
//^^^^^^ reference java/lang/Object#
10+
// ^ definition local0 Object o
11+
// ^^^^^^ reference java/lang/Object#
12+
→→→@Override
13+
// ^^^^^^^^ reference java/lang/Override#
14+
→→→public boolean equals(Object other) {
15+
// ^^^^^^ definition local3 @Override public boolean equals(Object other)
16+
// ^^^^^^ reference java/lang/Object#
17+
// ^^^^^ definition local5 Object other
18+
→→→→return false;
19+
→→→}
20+
21+
→→→@Override
22+
// ^^^^^^^^ reference java/lang/Override#
23+
→→→public String toString() {
24+
// ^^^^^^ reference java/lang/String#
25+
// ^^^^^^^^ definition local4 @Override public String toString()
26+
→→→→return "";
27+
→→→}
28+
→→};
29+
→}
30+
}

0 commit comments

Comments
 (0)