Skip to content

Commit f93f143

Browse files
committed
Prune dead indexer code; drop ZINC/SBT URI scheme
1 parent a73732c commit f93f143

11 files changed

Lines changed: 39 additions & 198 deletions

File tree

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

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@ public enum CompilerRange {
3030
*/
3131
FROM_POINT_TO_SYMBOL_NAME,
3232

33-
/**
34-
* Map the compiler (point + 1) position to SemanticDB start and use (point + symbol name length +
35-
* 1) for the SemanticDB end position.
36-
*/
37-
FROM_POINT_TO_SYMBOL_NAME_PLUS_ONE,
38-
3933
/**
4034
* Use text search to find the start of the symbol name and use (found start + symbol name length)
4135
* for the SemanticDB end position.
@@ -46,18 +40,11 @@ public enum CompilerRange {
4640
* Use text search to find the start of the symbol name, using the point position as the starting
4741
* search offset and using (found start + symbol name length) for the SemanticDB end position.
4842
*/
49-
FROM_POINT_WITH_TEXT_SEARCH,
50-
51-
/**
52-
* Use text search to find the start of the symbol name, searching from the end instead of the
53-
* start.
54-
*/
55-
FROM_END_WITH_TEXT_SEARCH;
43+
FROM_POINT_WITH_TEXT_SEARCH;
5644

5745
public boolean isFromPoint() {
5846
switch (this) {
5947
case FROM_POINT_TO_SYMBOL_NAME:
60-
case FROM_POINT_TO_SYMBOL_NAME_PLUS_ONE:
6148
case FROM_POINT_WITH_TEXT_SEARCH:
6249
return true;
6350
default:
@@ -66,36 +53,16 @@ public boolean isFromPoint() {
6653
}
6754

6855
public boolean isFromEndPoint() {
69-
switch (this) {
70-
case FROM_END_TO_SYMBOL_NAME:
71-
case FROM_END_WITH_TEXT_SEARCH:
72-
return true;
73-
default:
74-
return false;
75-
}
56+
return this == FROM_END_TO_SYMBOL_NAME;
7657
}
7758

7859
public boolean isFromTextSearch() {
7960
switch (this) {
8061
case FROM_TEXT_SEARCH:
81-
case FROM_END_WITH_TEXT_SEARCH:
8262
case FROM_POINT_WITH_TEXT_SEARCH:
8363
return true;
8464
default:
8565
return false;
8666
}
8767
}
88-
89-
public boolean isPlusOne() {
90-
switch (this) {
91-
case FROM_POINT_TO_SYMBOL_NAME_PLUS_ONE:
92-
return true;
93-
default:
94-
return false;
95-
}
96-
}
97-
98-
public boolean isFromEnd() {
99-
return this == CompilerRange.FROM_END_WITH_TEXT_SEARCH;
100-
}
10168
}

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

Lines changed: 0 additions & 32 deletions
This file was deleted.

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

Lines changed: 15 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import javax.tools.Diagnostic;
44

5-
import javax.lang.model.element.Element;
65
import java.util.Optional;
76

87
public class RangeFinder {
@@ -16,81 +15,30 @@ public static class StartEndRange {
1615
}
1716
}
1817

18+
/**
19+
* Searches {@code source} for {@code name} starting at {@code start}. If the name cannot be found
20+
* and {@code fallback} is not {@link Diagnostic#NOPOS}, returns a range anchored at {@code
21+
* fallback}; otherwise returns {@link Optional#empty()}.
22+
*/
1923
public static Optional<StartEndRange> findRange(
20-
Element element,
21-
String name,
22-
int originalStartPos,
23-
int originalEndPos,
24-
String source,
25-
boolean fromEnd) {
26-
int startPos = findNameIn(name, originalStartPos, originalEndPos, element, source, fromEnd);
27-
int endPos = startPos + name.length();
28-
29-
if (endPos == -1 || startPos == -1) {
30-
return Optional.empty();
31-
}
32-
33-
return Optional.of(new StartEndRange(startPos, endPos));
34-
}
35-
36-
private static int findNameFromEnd(
37-
String name,
38-
int originalStartPos,
39-
int originalEndPos,
40-
int end,
41-
Element element,
42-
String source) {
43-
if (end < 0) return -1;
44-
int offset = source.lastIndexOf(name, end);
45-
if (offset == -1 && originalStartPos != Diagnostic.NOPOS && originalEndPos != Diagnostic.NOPOS)
46-
return originalStartPos;
47-
if (offset == -1) {
48-
return -1;
49-
}
50-
int endOfWord = offset + name.length();
51-
// found name in wrong word? e.g. finding `"A"` in `A("A")`
52-
if (offset > 0 && Character.isJavaIdentifierPart(source.charAt(offset - 1)))
53-
return findNameFromEnd(name, originalStartPos, originalEndPos, offset - 1, element, source);
54-
if (endOfWord < source.length() && Character.isJavaIdentifierPart(source.charAt(endOfWord)))
55-
return findNameFromEnd(name, originalStartPos, originalEndPos, offset - 1, element, source);
56-
57-
return offset;
24+
String name, int start, int fallback, String source) {
25+
if (source.length() == 0) return Optional.empty();
26+
int startPos = findNameFromStart(name, start, fallback, source);
27+
if (startPos == -1) return Optional.empty();
28+
return Optional.of(new StartEndRange(startPos, startPos + name.length()));
5829
}
5930

60-
private static int findNameFromStart(
61-
String name,
62-
int start,
63-
int originalStartPos,
64-
int originalEndPos,
65-
Element element,
66-
String source) {
31+
private static int findNameFromStart(String name, int start, int fallback, String source) {
6732
if (start >= source.length()) return -1;
6833
int offset = source.indexOf(name, start);
69-
if (offset == -1 && originalStartPos != Diagnostic.NOPOS && originalEndPos != Diagnostic.NOPOS)
70-
return originalStartPos;
71-
if (offset == -1) {
72-
return -1;
73-
}
34+
if (offset == -1 && fallback != Diagnostic.NOPOS) return fallback;
35+
if (offset == -1) return -1;
7436
int end = offset + name.length();
7537
// found name in wrong word? e.g. finding `"A"` in `A("A")`
7638
if (offset > 0 && Character.isJavaIdentifierPart(source.charAt(offset - 1)))
77-
return findNameFromStart(name, end + 1, originalStartPos, originalEndPos, element, source);
39+
return findNameFromStart(name, end + 1, fallback, source);
7840
if (end < source.length() && Character.isJavaIdentifierPart(source.charAt(end)))
79-
return findNameFromStart(name, end + 1, originalStartPos, originalEndPos, element, source);
80-
41+
return findNameFromStart(name, end + 1, fallback, source);
8142
return offset;
8243
}
83-
84-
private static int findNameIn(
85-
String name, int start, int end, Element element, String source, boolean fromEnd) {
86-
if (source.length() == 0) return -1;
87-
88-
int offset;
89-
if (fromEnd) offset = findNameFromEnd(name, start, start, end, element, source);
90-
else offset = findNameFromStart(name, start, end, end, element, source);
91-
if (offset > -1) {
92-
return offset;
93-
}
94-
return -1;
95-
}
9644
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import org.scip_code.scip.Document;
44
import org.scip_code.scip.Index;
5-
import org.scip_code.scip.Occurrence;
65
import org.scip_code.scip.Relationship;
76
import org.scip_code.scip.SymbolInformation;
87

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,12 +476,11 @@ private Optional<ScipRange> scipRangeOf(Tree tree, CompilerRange kind, Element s
476476
SourcePositions sourcePositions = trees.getSourcePositions();
477477
int start = (int) sourcePositions.getStartPosition(compUnitTree, tree);
478478
int end = (int) sourcePositions.getEndPosition(compUnitTree, tree);
479-
if (kind.isPlusOne()) start++;
480479

481480
if (name != null) {
482481
if (kind.isFromTextSearch() && name.length() > 0) {
483482
Optional<RangeFinder.StartEndRange> startEndRange =
484-
RangeFinder.findRange(sym, name, start, end, this.source, kind.isFromEnd());
483+
RangeFinder.findRange(name, start, end, this.source);
485484
if (startEndRange.isPresent()) {
486485
start = startEndRange.get().start;
487486
end = startEndRange.get().end;

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

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import com.sun.source.util.JavacTask;
1414

1515
import static javax.tools.StandardLocation.CLASS_OUTPUT;
16-
import static javax.tools.StandardLocation.SOURCE_OUTPUT;
1716

1817
/** Settings that can be configured alongside the -Xplugin compiler option. */
1918
public class SemanticdbJavacOptions {
@@ -29,7 +28,6 @@ public class SemanticdbJavacOptions {
2928
public boolean alreadyReportedErrors = false;
3029
public UriScheme uriScheme = UriScheme.DEFAULT;
3130
public NoRelativePathMode noRelativePath = NoRelativePathMode.INDEX_ANYWAY;
32-
public Path generatedTargetRoot;
3331

3432
public SemanticdbJavacOptions() {
3533
errors = new ArrayList<>();
@@ -53,7 +51,7 @@ public static SemanticdbJavacOptions parse(String[] args, JavacTask task) {
5351
String argValue = arg.substring("-targetroot:".length());
5452
if (argValue.equals(JAVAC_CLASSES_DIR_ARG)) {
5553
useJavacClassesDir = true;
56-
result.targetroot = getJavacClassesDir(result, task).classes;
54+
result.targetroot = getJavacClassesDir(result, task);
5755
} else {
5856
result.targetroot = Paths.get(argValue);
5957
}
@@ -83,9 +81,7 @@ public static SemanticdbJavacOptions parse(String[] args, JavacTask task) {
8381
} else if (arg.equals("-build-tool:bazel")) {
8482
result.uriScheme = UriScheme.BAZEL;
8583
useJavacClassesDir = true;
86-
TargetPaths paths = getJavacClassesDir(result, task);
87-
result.targetroot = paths.classes;
88-
result.generatedTargetRoot = paths.sources;
84+
result.targetroot = getJavacClassesDir(result, task);
8985
} else if (arg.equals("-text:on")) {
9086
result.includeText = true;
9187
} else if (arg.equals("-text:off")) {
@@ -128,28 +124,22 @@ private static boolean isSourcerootDefined(SemanticdbJavacOptions options) {
128124

129125
// warning - use of internal API
130126
// requires --add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
131-
private static TargetPaths getJavacClassesDir(SemanticdbJavacOptions result, JavacTask task) {
127+
private static Path getJavacClassesDir(SemanticdbJavacOptions result, JavacTask task) {
132128
// both Context and BasicJavacTask are internal JDK classes so not exported
133129
// under >= JDK 17
134130
// com.sun.tools.javac.util.Context ctx =
135131
// ((com.sun.tools.javac.api.BasicJavacTask)
136132
// task).getContext();
137133
// I'm not aware of a better way to get the class output directory from javac
138-
Path classOutputDir = null;
139-
Path sourceOutputDir = null;
140134
try {
141135
Method getContext = task.getClass().getMethod("getContext");
142136
Object context = getContext.invoke(task);
143137
Method get = context.getClass().getMethod("get", Class.class);
144138
JavaFileManager fm = (JavaFileManager) get.invoke(context, JavaFileManager.class);
145-
FileObject sourceOutputDirStub =
146-
fm.getJavaFileForOutput(
147-
SOURCE_OUTPUT, SemanticdbPlugin.stubClassName, JavaFileObject.Kind.SOURCE, null);
148-
FileObject clasSOutputDirStub =
139+
FileObject classOutputDirStub =
149140
fm.getJavaFileForOutput(
150141
CLASS_OUTPUT, SemanticdbPlugin.stubClassName, JavaFileObject.Kind.CLASS, null);
151-
classOutputDir = Paths.get(clasSOutputDirStub.toUri()).toAbsolutePath().getParent();
152-
sourceOutputDir = Paths.get(sourceOutputDirStub.toUri()).toAbsolutePath().getParent();
142+
return Paths.get(classOutputDirStub.toUri()).toAbsolutePath().getParent();
153143
} catch (Exception e) {
154144
ByteArrayOutputStream out = new ByteArrayOutputStream();
155145
e.printStackTrace(new PrintStream(out));
@@ -158,7 +148,7 @@ private static TargetPaths getJavacClassesDir(SemanticdbJavacOptions result, Jav
158148
"exception while processing SemanticDB option '-targetroot:%s'\n%s",
159149
JAVAC_CLASSES_DIR_ARG, out.toString());
160150
result.errors.add(errorMsg);
151+
return null;
161152
}
162-
return new TargetPaths(classOutputDir, sourceOutputDir);
163153
}
164154
}

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

Lines changed: 0 additions & 13 deletions
This file was deleted.

semanticdb-kotlinc/src/main/kotlin/com/sourcegraph/semanticdb_kotlinc/ScipTextDocumentBuilder.kt

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ class ScipTextDocumentBuilder(
4343
// synthetic accessors) do not produce duplicate entries.
4444
private val symbols = LinkedHashMap<String, SymbolInformation>()
4545

46-
fun build(): Document =
47-
Document
48-
.newBuilder()
49-
.setRelativePath(relativePath)
50-
.setLanguage(LANGUAGE_KOTLIN)
51-
.addAllOccurrences(occurrences.values())
52-
.addAllSymbols(symbols.values)
46+
fun buildIndex(): Index =
47+
Index.newBuilder()
48+
.addDocuments(
49+
Document.newBuilder()
50+
.setRelativePath(relativePath)
51+
.setLanguage(LANGUAGE_KOTLIN)
52+
.addAllOccurrences(occurrences.values())
53+
.addAllSymbols(symbols.values)
54+
.build())
5355
.build()
5456

55-
fun buildIndex(): Index = Index.newBuilder().addDocuments(build()).build()
56-
5757
fun emitScipData(
5858
firBasedSymbol: FirBasedSymbol<*>?,
5959
symbol: Symbol,
@@ -284,8 +284,7 @@ class ScipTextDocumentBuilder(
284284
else -> SymbolInformation.Kind.UnspecifiedKind
285285
}
286286

287-
// Mirrors the renderer used by SemanticdbTextDocumentBuilder for parity. We render the
288-
// declaration as Kotlin source text — no markdown fence — and put it into
287+
// Renders declarations as Kotlin source text — no markdown fence — and puts them into
289288
// SymbolInformation.signature_documentation.text.
290289
private val renderer: FirRenderer
291290
get() =

semanticdb-kotlinc/src/main/kotlin/com/sourcegraph/semanticdb_kotlinc/SemanticdbSymbols.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ value class Symbol(private val symbol: String) {
2323
override fun toString(): String = symbol
2424
}
2525

26-
fun String.symbol(): Symbol = Symbol(this)
27-
2826
data class SemanticdbSymbolDescriptor(
2927
val kind: Kind,
3028
val name: String,

semanticdb-kotlinc/src/main/kotlin/com/sourcegraph/semanticdb_kotlinc/SemanticdbVisitor.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ class SemanticdbVisitor(
2626
file: KtSourceFile,
2727
lineMap: LineMap,
2828
globals: GlobalSymbolsCache,
29-
locals: LocalSymbolsCache = LocalSymbolsCache()
3029
) {
31-
private val cache = SymbolsCache(globals, locals)
30+
private val cache = SymbolsCache(globals, LocalSymbolsCache())
3231
private val relativePath: String = computeRelativePath(sourceroot, file)
3332
private val scipBuilder = ScipTextDocumentBuilder(lineMap, cache, relativePath)
3433

0 commit comments

Comments
 (0)