Skip to content

Commit c2402be

Browse files
committed
Tighten new comments in Kotlin SCIP code
1 parent f93f143 commit c2402be

8 files changed

Lines changed: 29 additions & 89 deletions

File tree

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@
88
import java.util.Objects;
99

1010
/**
11-
* Accumulator that deduplicates SCIP {@link Occurrence} entries by their {@code (symbol, range,
12-
* roles)} key. Variants that differ only in whether {@code enclosing_range} is set are collapsed,
13-
* preferring the one that carries the enclosing range.
11+
* Accumulator that deduplicates SCIP {@link Occurrence} entries by {@code (symbol, range, roles)}.
12+
* Variants differing only in whether {@code enclosing_range} is set are collapsed, preferring the
13+
* one that carries it.
1414
*/
1515
final class ScipOccurrences {
1616

1717
private final LinkedHashMap<Key, Occurrence> out = new LinkedHashMap<>();
1818

19-
/** Adds {@code occ}, collapsing it into any existing entry with the same {@link Key}. */
2019
void add(Occurrence occ) {
2120
Key key = Key.of(occ);
2221
Occurrence existing = out.get(key);
@@ -29,12 +28,10 @@ void add(Occurrence occ) {
2928
}
3029
}
3130

32-
/** Adds every occurrence in {@code occs}. */
3331
void addAll(Iterable<Occurrence> occs) {
3432
for (Occurrence occ : occs) add(occ);
3533
}
3634

37-
/** Returns the deduplicated occurrences in insertion order. */
3835
Collection<Occurrence> values() {
3936
return out.values();
4037
}

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

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,13 @@
1616
import java.util.Map;
1717

1818
/**
19-
* Writes and merges per-source SCIP shards produced by the compiler plugin.
20-
*
21-
* <p>Each source file produces a self-contained {@link Index} shard containing a single {@link
22-
* Document}. When a shard already exists on disk (e.g. during annotation processing rounds), the
23-
* new document is merged into the existing one, deduplicating occurrences, symbols and
24-
* relationships.
19+
* Writes a per-source SCIP shard, merging it with any pre-existing shard at the same path (e.g. one
20+
* written by a prior annotation-processing round).
2521
*/
2622
public final class ScipShardWriter {
2723

2824
private ScipShardWriter() {}
2925

30-
/**
31-
* Writes the given {@code shard} to {@code output}, creating parent directories as needed. If
32-
* {@code output} already exists, the existing shard is parsed and merged with the new one.
33-
*/
3426
public static void writeOrMerge(Path output, Index shard) throws IOException {
3527
Files.createDirectories(output.getParent());
3628
if (Files.exists(output)) {
@@ -43,10 +35,7 @@ public static void writeOrMerge(Path output, Index shard) throws IOException {
4335
Files.write(output, shard.toByteArray());
4436
}
4537

46-
/**
47-
* Merges two SCIP shards by combining their document lists. Documents that share a {@code
48-
* relative_path} have their occurrences, symbols and external symbols deduplicated.
49-
*/
38+
/** Merges two shards: documents sharing a {@code relative_path} are dedup-merged. */
5039
static Index merge(Index a, Index b) {
5140
Index.Builder builder = Index.newBuilder();
5241
if (b.hasMetadata()) {
@@ -69,7 +58,7 @@ static Index merge(Index a, Index b) {
6958
}
7059
builder.addAllDocuments(byPath.values());
7160

72-
// External symbols: deduplicate by symbol string. Last writer wins to keep latest data.
61+
// External symbols: last writer wins to keep the most recent data.
7362
LinkedHashMap<String, SymbolInformation> externals = new LinkedHashMap<>();
7463
for (SymbolInformation s : a.getExternalSymbolsList()) externals.put(s.getSymbol(), s);
7564
for (SymbolInformation s : b.getExternalSymbolsList()) externals.put(s.getSymbol(), s);
@@ -79,18 +68,15 @@ static Index merge(Index a, Index b) {
7968
}
8069

8170
private static Document mergeDocuments(Document a, Document b) {
71+
// b.toBuilder() carries forward the most recent language/relative_path/text/encoding.
8272
Document.Builder builder = b.toBuilder().clearOccurrences().clearSymbols();
83-
// Use the most recent metadata for language/relative_path/text/encoding which already
84-
// come from b via toBuilder().
8573

86-
// Deduplicate occurrences by (range, symbol, roles). Variants that differ only in
87-
// enclosing_range get collapsed, preferring the one that carries the enclosing range.
8874
ScipOccurrences occurrences = new ScipOccurrences();
8975
occurrences.addAll(a.getOccurrencesList());
9076
occurrences.addAll(b.getOccurrencesList());
9177
builder.addAllOccurrences(occurrences.values());
9278

93-
// Deduplicate symbols by symbol string; merge relationships and documentation.
79+
// Dedup symbols by symbol string; merge relationships and documentation.
9480
Map<String, SymbolInformation> bySymbol = new LinkedHashMap<>();
9581
for (SymbolInformation info : a.getSymbolsList()) bySymbol.put(info.getSymbol(), info);
9682
for (SymbolInformation info : b.getSymbolsList()) {
@@ -104,12 +90,12 @@ private static Document mergeDocuments(Document a, Document b) {
10490

10591
private static SymbolInformation mergeSymbol(SymbolInformation a, SymbolInformation b) {
10692
SymbolInformation.Builder builder = b.toBuilder();
107-
// Merge relationships, deduplicating by structural equality with deterministic ordering.
93+
// Dedup relationships by structural equality; preserve insertion order.
10894
LinkedHashSet<Relationship> rels = new LinkedHashSet<>(a.getRelationshipsList());
10995
rels.addAll(b.getRelationshipsList());
11096
builder.clearRelationships().addAllRelationships(rels);
11197

112-
// Merge documentation, preserving order and avoiding duplicates.
98+
// Dedup documentation; preserve insertion order.
11399
List<String> docs = new ArrayList<>(a.getDocumentationList());
114100
for (String d : b.getDocumentationList()) {
115101
if (!docs.contains(d)) docs.add(d);

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,6 @@ private static boolean supportsReferenceRelationship(Element sym) {
237237
}
238238
}
239239

240-
// =================================================
241-
// Kind translation for SCIP emission.
242-
// =================================================
243-
244240
private static SymbolInformation.Kind scipKind(Element sym) {
245241
Set<Modifier> modifiers = sym.getModifiers();
246242
boolean isStatic = modifiers.contains(Modifier.STATIC);

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ import org.jetbrains.kotlin.config.CommonConfigurationKeys
1616
import org.jetbrains.kotlin.config.CompilerConfiguration
1717
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
1818

19-
/**
20-
* IR generation extension that runs after FIR analysis to write the per-source SCIP shards
21-
* collected by [SemanticdbVisitor]. The legacy SemanticDB protobuf output has been removed.
22-
*/
19+
/** Writes the per-source SCIP shards collected by [SemanticdbVisitor] after FIR analysis. */
2320
class PostAnalysisExtension(
2421
private val sourceRoot: Path,
2522
private val targetRoot: Path,

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@ package com.sourcegraph.semanticdb_kotlinc
33
import org.scip_code.scip.Occurrence
44

55
/**
6-
* Accumulator that deduplicates SCIP [Occurrence] entries by their `(symbol, range, roles)` key.
7-
* Variants that differ only in whether `enclosing_range` is set are collapsed, preferring the one
8-
* that carries the enclosing range. Mirrors the Java `ScipOccurrences` accumulator used by the
9-
* javac plug-in.
6+
* Accumulator that deduplicates SCIP [Occurrence] entries by `(symbol, range, roles)`. Variants
7+
* differing only in whether `enclosing_range` is set are collapsed, preferring the one that
8+
* carries it. Mirrors the Java `ScipOccurrences` accumulator.
109
*/
1110
internal class ScipOccurrences {
1211

1312
private val out = LinkedHashMap<Key, Occurrence>()
1413

15-
/** Adds [occ], collapsing it into any existing entry with the same [Key]. */
1614
fun add(occ: Occurrence) {
1715
val key = Key.of(occ)
1816
val existing = out[key]
@@ -25,7 +23,6 @@ internal class ScipOccurrences {
2523
}
2624
}
2725

28-
/** Returns the deduplicated occurrences in insertion order. */
2926
fun values(): Collection<Occurrence> = out.values
3027

3128
private data class Key(val symbol: String, val range: List<Int>, val roles: Int) {

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

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,15 @@
11
package com.sourcegraph.semanticdb_kotlinc
22

33
/**
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.
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.
158
*/
169
object ScipSymbols {
1710

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-
*/
2311
const val PLACEHOLDER_PREFIX: String = ". . . . "
2412

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-
*/
3213
fun fromSemanticdbSymbol(symbol: Symbol): String {
3314
if (symbol == Symbol.NONE) return ""
3415
val raw = symbol.toString()

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

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,9 @@ import org.jetbrains.kotlin.lexer.KtTokens
2222
import org.jetbrains.kotlin.text
2323

2424
/**
25-
* Builds a single-document [Index] shard for one Kotlin source file from per-element callbacks
26-
* fired by [SemanticdbVisitor].
27-
*
28-
* Symbols are emitted using the placeholder scheme [ScipSymbols.PLACEHOLDER_PREFIX]; the
29-
* `scip-aggregator` rewrites them into final `scip-java maven g a v ...` form once Maven
30-
* coordinates are resolved.
31-
*
32-
* Signature documentation is rendered directly via [FirRenderer] (Kotlin's built-in declaration
33-
* renderer) and assigned to `SymbolInformation.signature_documentation.text` as raw Kotlin source.
25+
* Builds a single-document [Index] shard for one Kotlin source from callbacks fired by
26+
* [SemanticdbVisitor]. Symbols use the [ScipSymbols.PLACEHOLDER_PREFIX] scheme; signature
27+
* documentation is rendered via [FirRenderer] as raw Kotlin source.
3428
*/
3529
@ExperimentalContracts
3630
class ScipTextDocumentBuilder(
@@ -39,8 +33,7 @@ class ScipTextDocumentBuilder(
3933
private val relativePath: String,
4034
) {
4135
private val occurrences = ScipOccurrences()
42-
// Keyed by symbol string so re-encounters of the same definition (multi-round compilation,
43-
// synthetic accessors) do not produce duplicate entries.
36+
// Keyed by symbol so re-encounters (multi-round compilation, synthetic accessors) don't dup.
4437
private val symbols = LinkedHashMap<String, SymbolInformation>()
4538

4639
fun buildIndex(): Index =
@@ -125,7 +118,7 @@ class ScipTextDocumentBuilder(
125118
.setIsReference(supportsRefRel))
126119
}
127120

128-
// Last write wins so newly discovered metadata takes precedence.
121+
// Last write wins: newly discovered metadata takes precedence.
129122
symbols[scipSymbolStr] = builder.build()
130123
}
131124

@@ -161,8 +154,7 @@ class ScipTextDocumentBuilder(
161154
val startLine = lineMap.lineNumber(element) - 1
162155
val startCharacter = lineMap.startCharacter(element)
163156
val endCharacter = lineMap.endCharacter(element)
164-
// SemanticDB visitor only emits single-line ranges, so the SCIP compact 3-int range
165-
// form is sufficient here.
157+
// SemanticdbVisitor only emits single-line ranges, so 3 ints suffice.
166158
return listOf(startLine, startCharacter, endCharacter)
167159
}
168160

@@ -195,11 +187,7 @@ class ScipTextDocumentBuilder(
195187
return stripKDocAsterisks(kdoc)
196188
}
197189

198-
/**
199-
* Returns the kdoc string with all leading and trailing slash/asterisk tokens removed. Naive
200-
* implementation that can be replaced with a utility method from the compiler in the future,
201-
* if one exists.
202-
*/
190+
/** Strips leading/trailing slash and asterisk tokens from each line of a kdoc string. */
203191
private fun stripKDocAsterisks(kdoc: String): String {
204192
if (kdoc.isEmpty()) return kdoc
205193
val out = StringBuilder()
@@ -284,8 +272,7 @@ class ScipTextDocumentBuilder(
284272
else -> SymbolInformation.Kind.UnspecifiedKind
285273
}
286274

287-
// Renders declarations as Kotlin source text — no markdown fence — and puts them into
288-
// SymbolInformation.signature_documentation.text.
275+
// Renders declarations as raw Kotlin source for SymbolInformation.signature_documentation.
289276
private val renderer: FirRenderer
290277
get() =
291278
FirRenderer(

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ import org.jetbrains.kotlin.fir.symbols.impl.FirClassLikeSymbol
1616
import org.jetbrains.kotlin.name.FqName
1717

1818
/**
19-
* Walks the FIR analysis tree for a single Kotlin source file and builds a self-contained
20-
* `Index` shard via [ScipTextDocumentBuilder]. The legacy SemanticDB protobuf path has been
21-
* removed; the visitor only emits SCIP.
19+
* Walks the FIR analysis tree of a Kotlin source and builds a self-contained `Index` shard via
20+
* [ScipTextDocumentBuilder].
2221
*/
2322
@ExperimentalContracts
2423
class SemanticdbVisitor(

0 commit comments

Comments
 (0)