Skip to content

Commit 5fb810e

Browse files
committed
Drop dead helpers, inline one-call wrappers, prefer LinkedHashSet
1 parent 990ecab commit 5fb810e

10 files changed

Lines changed: 34 additions & 83 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;
@@ -235,10 +236,9 @@ public int hashCode() {
235236

236237
private static SymbolInformation mergeSymbol(SymbolInformation a, SymbolInformation b) {
237238
SymbolInformation.Builder builder = b.toBuilder();
238-
LinkedHashMap<Relationship, Relationship> rels = new LinkedHashMap<>();
239-
for (Relationship r : a.getRelationshipsList()) rels.put(r, r);
240-
for (Relationship r : b.getRelationshipsList()) rels.put(r, r);
241-
builder.clearRelationships().addAllRelationships(rels.values());
239+
LinkedHashSet<Relationship> rels = new LinkedHashSet<>(a.getRelationshipsList());
240+
rels.addAll(b.getRelationshipsList());
241+
builder.clearRelationships().addAllRelationships(rels);
242242
return builder.build();
243243
}
244244

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
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;
1617
import java.util.Map;
1718

@@ -105,10 +106,9 @@ private static Document mergeDocuments(Document a, Document b) {
105106
private static SymbolInformation mergeSymbol(SymbolInformation a, SymbolInformation b) {
106107
SymbolInformation.Builder builder = b.toBuilder();
107108
// Merge relationships, deduplicating by structural equality with deterministic ordering.
108-
Map<Relationship, Relationship> rels = new LinkedHashMap<>();
109-
for (Relationship r : a.getRelationshipsList()) rels.put(r, r);
110-
for (Relationship r : b.getRelationshipsList()) rels.put(r, r);
111-
builder.clearRelationships().addAllRelationships(rels.values());
109+
LinkedHashSet<Relationship> rels = new LinkedHashSet<>(a.getRelationshipsList());
110+
rels.addAll(b.getRelationshipsList());
111+
builder.clearRelationships().addAllRelationships(rels);
112112

113113
// Merge documentation, preserving order and avoiding duplicates.
114114
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
@@ -95,9 +95,9 @@ public ScipVisitor(
9595
this.nodes = new LinkedHashMap<>();
9696
}
9797

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

103103
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 & 28 deletions
This file was deleted.

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,18 @@ object ScipSymbols {
2323
const val PLACEHOLDER_PREFIX: String = ". . . . "
2424

2525
/**
26-
* Converts a SemanticDB-style symbol string into the SCIP symbol form expected by the
27-
* aggregator.
26+
* Converts a SemanticDB-style [Symbol] into the SCIP symbol form expected by the aggregator.
2827
*
29-
* - Empty strings stay empty.
28+
* - [Symbol.NONE] becomes the empty string.
3029
* - Local symbols of the form `local42` become `local 42`.
3130
* - Everything else is prefixed with [PLACEHOLDER_PREFIX].
3231
*/
33-
fun fromSemanticdbSymbol(symbol: String?): String {
34-
if (symbol.isNullOrEmpty()) return ""
35-
if (symbol.startsWith("local")) {
36-
return "local " + symbol.substring("local".length)
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)
3737
}
38-
return PLACEHOLDER_PREFIX + symbol
38+
return PLACEHOLDER_PREFIX + raw
3939
}
40-
41-
fun fromSemanticdbSymbol(symbol: Symbol): String = fromSemanticdbSymbol(symbol.toString())
4240
}

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)