Skip to content

Commit a73732c

Browse files
committed
Drop dead helpers, inline one-call wrappers, prefer LinkedHashSet
1 parent f140db0 commit a73732c

10 files changed

Lines changed: 57 additions & 79 deletions

File tree

scip-semanticdb/src/main/java/com/sourcegraph/scip_semanticdb/ScipShardAggregator.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Collections;
2222
import java.util.Enumeration;
2323
import java.util.LinkedHashMap;
24+
import java.util.LinkedHashSet;
2425
import java.util.List;
2526
import java.util.Map;
2627
import java.util.jar.JarEntry;
@@ -219,10 +220,9 @@ public int hashCode() {
219220

220221
private static SymbolInformation mergeSymbol(SymbolInformation a, SymbolInformation b) {
221222
SymbolInformation.Builder builder = b.toBuilder();
222-
LinkedHashMap<Relationship, Relationship> rels = new LinkedHashMap<>();
223-
for (Relationship r : a.getRelationshipsList()) rels.put(r, r);
224-
for (Relationship r : b.getRelationshipsList()) rels.put(r, r);
225-
builder.clearRelationships().addAllRelationships(rels.values());
223+
LinkedHashSet<Relationship> rels = new LinkedHashSet<>(a.getRelationshipsList());
224+
rels.addAll(b.getRelationshipsList());
225+
builder.clearRelationships().addAllRelationships(rels);
226226
return builder.build();
227227
}
228228

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
import java.nio.file.Path;
1313
import java.util.ArrayList;
1414
import java.util.LinkedHashMap;
15+
import java.util.LinkedHashSet;
1516
import java.util.List;
17+
import java.util.Map;
1618

1719
/**
1820
* Writes and merges per-source SCIP shards produced by the compiler plugin.
@@ -90,7 +92,7 @@ private static Document mergeDocuments(Document a, Document b) {
9092
builder.addAllOccurrences(occurrences.values());
9193

9294
// Deduplicate symbols by symbol string; merge relationships and documentation.
93-
LinkedHashMap<String, SymbolInformation> bySymbol = new LinkedHashMap<>();
95+
Map<String, SymbolInformation> bySymbol = new LinkedHashMap<>();
9496
for (SymbolInformation info : a.getSymbolsList()) bySymbol.put(info.getSymbol(), info);
9597
for (SymbolInformation info : b.getSymbolsList()) {
9698
SymbolInformation existing = bySymbol.get(info.getSymbol());
@@ -104,10 +106,9 @@ private static Document mergeDocuments(Document a, Document b) {
104106
private static SymbolInformation mergeSymbol(SymbolInformation a, SymbolInformation b) {
105107
SymbolInformation.Builder builder = b.toBuilder();
106108
// Merge relationships, deduplicating by structural equality with deterministic ordering.
107-
LinkedHashMap<Relationship, Relationship> rels = new LinkedHashMap<>();
108-
for (Relationship r : a.getRelationshipsList()) rels.put(r, r);
109-
for (Relationship r : b.getRelationshipsList()) rels.put(r, r);
110-
builder.clearRelationships().addAllRelationships(rels.values());
109+
LinkedHashSet<Relationship> rels = new LinkedHashSet<>(a.getRelationshipsList());
110+
rels.addAll(b.getRelationshipsList());
111+
builder.clearRelationships().addAllRelationships(rels);
111112

112113
// Merge documentation, preserving order and avoiding duplicates.
113114
List<String> docs = new ArrayList<>(a.getDocumentationList());

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ public ScipVisitor(
9393
this.nodes = new LinkedHashMap<>();
9494
}
9595

96-
/** Builds a single-document {@link Index} shard for the given compilation unit. */
97-
public Index buildShard(CompilationUnitTree tree) {
98-
this.scan(tree, null);
96+
/** Builds a single-document {@link Index} shard for this compilation unit. */
97+
public Index buildShard() {
98+
this.scan(compUnitTree, null);
9999
resolveNodes();
100100

101101
Document.Builder document =

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ private void onFinishedAnalyze(TaskEvent e) {
113113
try {
114114
Index shard =
115115
new ScipVisitor(globals, e.getCompilationUnit(), options, types, trees, elements)
116-
.buildShard(e.getCompilationUnit());
116+
.buildShard();
117117
ScipShardWriter.writeOrMerge(shardPath, shard);
118118
} catch (IOException ex) {
119119
this.reportException(ex, e);

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ class PostAnalysisExtension(
2929
try {
3030
for ((ktSourceFile, visitor) in AnalyzerCheckers.visitors) {
3131
try {
32-
scipShardOutPathForFile(ktSourceFile)?.apply {
33-
ScipShardWriter.write(this, visitor.buildScipIndex())
32+
scipShardOutPathForFile(ktSourceFile)?.let { outPath ->
33+
Files.createDirectories(outPath.parent)
34+
Files.write(outPath, visitor.buildScipIndex().toByteArray())
3435
}
3536
} catch (e: Exception) {
3637
handleException(e)
@@ -46,15 +47,11 @@ class PostAnalysisExtension(
4647
if (normalizedPath.startsWith(sourceRoot)) {
4748
val relative = sourceRoot.relativize(normalizedPath)
4849
val filename = relative.fileName.toString() + ".scip"
49-
val outPath =
50-
targetRoot
51-
.resolve("META-INF")
52-
.resolve("scip")
53-
.resolve(relative)
54-
.resolveSibling(filename)
55-
56-
Files.createDirectories(outPath.parent)
57-
return outPath
50+
return targetRoot
51+
.resolve("META-INF")
52+
.resolve("scip")
53+
.resolve(relative)
54+
.resolveSibling(filename)
5855
}
5956
System.err.println(
6057
"given file is not under the sourceroot.\n\tSourceroot: $sourceRoot\n\tFile path: ${file.path}\n\tNormalized file path: $normalizedPath")

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ internal class ScipOccurrences {
2525
}
2626
}
2727

28-
/** Adds every occurrence in [occs]. */
29-
fun addAll(occs: Iterable<Occurrence>) {
30-
for (occ in occs) add(occ)
31-
}
32-
3328
/** Returns the deduplicated occurrences in insertion order. */
3429
fun values(): Collection<Occurrence> = out.values
3530

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

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,40 @@
11
package com.sourcegraph.semanticdb_kotlinc
22

33
/**
4-
* Converts SemanticDB-style symbol strings into the placeholder SCIP form expected by the
5-
* aggregator: globals are prefixed with [PLACEHOLDER_PREFIX] (rewritten to
6-
* `scip-java maven <g> <a> <v> <descriptor>` once coordinates are known), locals use the canonical
7-
* `local N` form and pass through unchanged. Mirrors the Java `ScipSymbols` helper.
4+
* Helpers for emitting SCIP symbol strings from the Kotlin compiler plug-in.
5+
*
6+
* Mirrors the Java `ScipSymbols` helper. Because the compiler plug-in does not know the final
7+
* Maven coordinates (`groupId:artifactId:version`) at compile time, it emits global symbols using
8+
* a sentinel placeholder scheme that is rewritten by the aggregator to its final form:
9+
*
10+
* `. . . . ` + <semanticdb-style descriptor path>
11+
* -> "scip-java maven <groupId> <artifactId> <version> <descriptor path>"
12+
*
13+
* Local symbols are emitted using the canonical SCIP `local N` form (with the space) and are NOT
14+
* rewritten by the aggregator.
815
*/
916
object ScipSymbols {
1017

18+
/**
19+
* Prefix marking a global symbol whose package coordinates must be filled in by the aggregator.
20+
* The trailing space matches the SCIP grammar requirement of separating the scheme from the
21+
* package fields.
22+
*/
1123
const val PLACEHOLDER_PREFIX: String = ". . . . "
1224

13-
fun fromSemanticdbSymbol(symbol: String?): String {
14-
if (symbol.isNullOrEmpty()) return ""
15-
if (symbol.startsWith("local")) {
16-
return "local " + symbol.substring("local".length)
25+
/**
26+
* Converts a SemanticDB-style [Symbol] into the SCIP symbol form expected by the aggregator.
27+
*
28+
* - [Symbol.NONE] becomes the empty string.
29+
* - Local symbols of the form `local42` become `local 42`.
30+
* - Everything else is prefixed with [PLACEHOLDER_PREFIX].
31+
*/
32+
fun fromSemanticdbSymbol(symbol: Symbol): String {
33+
if (symbol == Symbol.NONE) return ""
34+
val raw = symbol.toString()
35+
if (symbol.isLocal()) {
36+
return "local " + raw.substring("local".length)
1737
}
18-
return PLACEHOLDER_PREFIX + symbol
38+
return PLACEHOLDER_PREFIX + raw
1939
}
20-
21-
fun fromSemanticdbSymbol(symbol: Symbol): String = fromSemanticdbSymbol(symbol.toString())
2240
}

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import org.scip_code.scip.SymbolInformation
99
import org.scip_code.scip.SymbolRole
1010
import kotlin.contracts.ExperimentalContracts
1111
import org.jetbrains.kotlin.KtSourceElement
12-
import org.jetbrains.kotlin.KtSourceFile
1312
import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext
1413
import org.jetbrains.kotlin.fir.analysis.checkers.directOverriddenSymbolsSafe
1514
import org.jetbrains.kotlin.fir.analysis.checkers.toClassLikeSymbol
@@ -35,7 +34,6 @@ import org.jetbrains.kotlin.text
3534
*/
3635
@ExperimentalContracts
3736
class ScipTextDocumentBuilder(
38-
private val file: KtSourceFile,
3937
private val lineMap: LineMap,
4038
private val cache: SymbolsCache,
4139
private val relativePath: String,
@@ -105,12 +103,6 @@ class ScipTextDocumentBuilder(
105103
.setDisplayName(displayName(firBasedSymbol, element))
106104
.setKind(scipKind(firBasedSymbol))
107105

108-
if (symbol.isLocal()) {
109-
// Locals are convenient to navigate from when the enclosing symbol is known.
110-
// We don't have direct access to the enclosing FirBasedSymbol here, but the
111-
// information is non-essential — leaving enclosing_symbol unset is fine.
112-
}
113-
114106
val signature = signatureText(firBasedSymbol)
115107
if (signature.isNotEmpty()) {
116108
builder.setSignatureDocumentation(

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class SemanticdbVisitor(
3030
) {
3131
private val cache = SymbolsCache(globals, locals)
3232
private val relativePath: String = computeRelativePath(sourceroot, file)
33-
private val scipBuilder = ScipTextDocumentBuilder(file, lineMap, cache, relativePath)
33+
private val scipBuilder = ScipTextDocumentBuilder(lineMap, cache, relativePath)
3434

3535
private data class SymbolDescriptorPair(
3636
val firBasedSymbol: FirBasedSymbol<*>?,
@@ -39,19 +39,16 @@ class SemanticdbVisitor(
3939

4040
fun buildScipIndex(): Index = scipBuilder.buildIndex()
4141

42-
fun scipRelativePath(): String = relativePath
43-
4442
private fun Sequence<SymbolDescriptorPair>?.emitAll(
4543
element: KtSourceElement,
4644
roles: Int,
4745
context: CheckerContext,
4846
enclosingSource: KtSourceElement? = null,
49-
): List<Symbol>? =
50-
this?.onEach { (firBasedSymbol, symbol) ->
51-
scipBuilder.emitScipData(firBasedSymbol, symbol, element, roles, context, enclosingSource)
52-
}
53-
?.map { it.symbol }
54-
?.toList()
47+
) {
48+
this?.forEach { (firBasedSymbol, symbol) ->
49+
scipBuilder.emitScipData(firBasedSymbol, symbol, element, roles, context, enclosingSource)
50+
}
51+
}
5552

5653
private fun Sequence<Symbol>.with(firBasedSymbol: FirBasedSymbol<*>?) =
5754
this.map { SymbolDescriptorPair(firBasedSymbol, it) }

0 commit comments

Comments
 (0)