Skip to content

Commit 6532769

Browse files
committed
Refactor ScipOccurrences to a stateful accumulator; drop ScipRole
1 parent dca274c commit 6532769

4 files changed

Lines changed: 44 additions & 72 deletions

File tree

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,22 @@
22

33
import org.scip_code.scip.Occurrence;
44

5-
import java.util.ArrayList;
5+
import java.util.Collection;
66
import java.util.LinkedHashMap;
77
import java.util.List;
88
import java.util.Objects;
99

1010
/**
11-
* Helpers for deduplicating SCIP {@link Occurrence} entries by their {@code (symbol, range, roles)}
12-
* key. Variants that differ only in whether {@code enclosing_range} is set are collapsed,
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,
1313
* preferring the one that carries the enclosing range.
1414
*/
1515
final class ScipOccurrences {
1616

17-
private ScipOccurrences() {}
17+
private final LinkedHashMap<Key, Occurrence> out = new LinkedHashMap<>();
1818

19-
/** Returns a new list with duplicate occurrences collapsed in insertion order. */
20-
static List<Occurrence> deduplicate(List<Occurrence> occurrences) {
21-
LinkedHashMap<Key, Occurrence> out = new LinkedHashMap<>();
22-
for (Occurrence occ : occurrences) put(out, occ);
23-
return new ArrayList<>(out.values());
24-
}
25-
26-
/** Inserts {@code occ} into {@code out}, collapsing duplicates by {@link Key}. */
27-
static void put(LinkedHashMap<Key, Occurrence> out, Occurrence occ) {
19+
/** Adds {@code occ}, collapsing it into any existing entry with the same {@link Key}. */
20+
void add(Occurrence occ) {
2821
Key key = Key.of(occ);
2922
Occurrence existing = out.get(key);
3023
if (existing == null) {
@@ -36,7 +29,17 @@ static void put(LinkedHashMap<Key, Occurrence> out, Occurrence occ) {
3629
}
3730
}
3831

39-
static final class Key {
32+
/** Adds every occurrence in {@code occs}. */
33+
void addAll(Iterable<Occurrence> occs) {
34+
for (Occurrence occ : occs) add(occ);
35+
}
36+
37+
/** Returns the deduplicated occurrences in insertion order. */
38+
Collection<Occurrence> values() {
39+
return out.values();
40+
}
41+
42+
private static final class Key {
4043
final String symbol;
4144
final List<Integer> range;
4245
final int roles;

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

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

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ private static Document mergeDocuments(Document a, Document b) {
8585

8686
// Deduplicate occurrences by (range, symbol, roles). Variants that differ only in
8787
// enclosing_range get collapsed, preferring the one that carries the enclosing range.
88-
LinkedHashMap<ScipOccurrences.Key, Occurrence> occurrences = new LinkedHashMap<>();
89-
for (Occurrence occ : a.getOccurrencesList()) ScipOccurrences.put(occurrences, occ);
90-
for (Occurrence occ : b.getOccurrencesList()) ScipOccurrences.put(occurrences, occ);
88+
ScipOccurrences occurrences = new ScipOccurrences();
89+
occurrences.addAll(a.getOccurrencesList());
90+
occurrences.addAll(b.getOccurrencesList());
9191
builder.addAllOccurrences(occurrences.values());
9292

9393
// Deduplicate symbols by symbol string; merge relationships and documentation.

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

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public final class ScipVisitor extends TreePathScanner<Void, Void> {
6868
private final CompilationUnitTree compUnitTree;
6969
private final Elements elements;
7070
private final SemanticdbJavacOptions options;
71-
private final ArrayList<Occurrence> occurrences;
71+
private final ScipOccurrences occurrences;
7272
private final LinkedHashMap<String, SymbolInformation> symbols;
7373
private final String source;
7474
private final String relativePath;
@@ -88,7 +88,7 @@ public ScipVisitor(
8888
this.elements = elements;
8989
this.trees = trees;
9090
this.compUnitTree = compUnitTree;
91-
this.occurrences = new ArrayList<>();
91+
this.occurrences = new ScipOccurrences();
9292
this.symbols = new LinkedHashMap<>();
9393
this.source = sourceText(compUnitTree);
9494
this.relativePath = sourceRelativePath(compUnitTree, options);
@@ -105,7 +105,7 @@ public Index buildShard(CompilationUnitTree tree) {
105105
if (options.includeText) {
106106
document.setText(source);
107107
}
108-
document.addAllOccurrences(ScipOccurrences.deduplicate(occurrences));
108+
document.addAllOccurrences(occurrences.values());
109109
document.addAllSymbols(symbols.values());
110110

111111
return Index.newBuilder().addDocuments(document).build();
@@ -119,20 +119,20 @@ public Index buildShard(CompilationUnitTree tree) {
119119
// ==========================
120120

121121
private Optional<ScipRange> emitSymbolOccurrence(
122-
Element sym, Tree tree, Name name, ScipRole role, CompilerRange kind) {
122+
Element sym, Tree tree, Name name, int roles, CompilerRange kind) {
123123
if (sym == null || name == null) return Optional.empty();
124124
Optional<ScipRange> range = scipRangeOf(tree, kind, sym, name.toString());
125-
if (role == ScipRole.DEFINITION) {
126-
emitOccurrence(sym, range, role, computeEnclosingRange(tree));
125+
if (roles == SymbolRole.Definition_VALUE) {
126+
emitOccurrence(sym, range, roles, computeEnclosingRange(tree));
127127
emitSymbolInformation(sym, tree);
128128
return range;
129129
}
130-
emitOccurrence(sym, range, role, Optional.empty());
130+
emitOccurrence(sym, range, roles, Optional.empty());
131131
return range;
132132
}
133133

134134
private void emitOccurrence(
135-
Element sym, Optional<ScipRange> range, ScipRole role, Optional<ScipRange> enclosingRange) {
135+
Element sym, Optional<ScipRange> range, int roles, Optional<ScipRange> enclosingRange) {
136136
if (sym == null || !range.isPresent()) return;
137137
String semanticdbSymbol = semanticdbSymbol(sym);
138138
if (semanticdbSymbol.equals(SemanticdbSymbols.NONE)) return;
@@ -141,7 +141,7 @@ private void emitOccurrence(
141141
Occurrence.newBuilder()
142142
.addAllRange(range.get().asScipRange())
143143
.setSymbol(ScipSymbols.fromSemanticdbSymbol(semanticdbSymbol))
144-
.setSymbolRoles(scipRole(role));
144+
.setSymbolRoles(roles);
145145
enclosingRange.ifPresent(r -> occ.addAllEnclosingRange(r.asScipRange()));
146146
occurrences.add(occ.build());
147147
}
@@ -244,16 +244,9 @@ private static boolean supportsReferenceRelationship(Element sym) {
244244
}
245245

246246
// =================================================
247-
// Role / kind translation for SCIP emission.
247+
// Kind translation for SCIP emission.
248248
// =================================================
249249

250-
private static int scipRole(ScipRole role) {
251-
if (role == ScipRole.DEFINITION || role == ScipRole.SYNTHETIC_DEFINITION) {
252-
return SymbolRole.Definition_VALUE;
253-
}
254-
return 0;
255-
}
256-
257250
private static SymbolInformation.Kind scipKind(Element sym) {
258251
Set<Modifier> modifiers = sym.getModifiers();
259252
boolean isStatic = modifiers.contains(Modifier.STATIC);
@@ -369,7 +362,7 @@ private void resolveClassTree(ClassTree node, TreePath treePath) {
369362
sym,
370363
node,
371364
sym.getSimpleName(),
372-
ScipRole.DEFINITION,
365+
SymbolRole.Definition_VALUE,
373366
CompilerRange.FROM_POINT_WITH_TEXT_SEARCH);
374367
}
375368
}
@@ -381,7 +374,7 @@ private void resolveTypeParameterTree(TypeParameterTree node, TreePath treePath)
381374
sym,
382375
node,
383376
sym.getSimpleName(),
384-
ScipRole.DEFINITION,
377+
SymbolRole.Definition_VALUE,
385378
CompilerRange.FROM_POINT_TO_SYMBOL_NAME);
386379
}
387380
}
@@ -396,7 +389,11 @@ private void resolveMethodTree(MethodTree node, TreePath treePath) {
396389
else name = sym.getSimpleName();
397390

398391
emitSymbolOccurrence(
399-
sym, node, name, ScipRole.DEFINITION, CompilerRange.FROM_POINT_WITH_TEXT_SEARCH);
392+
sym,
393+
node,
394+
name,
395+
SymbolRole.Definition_VALUE,
396+
CompilerRange.FROM_POINT_WITH_TEXT_SEARCH);
400397
}
401398
}
402399
}
@@ -409,12 +406,12 @@ private void resolveVariableTree(VariableTree node, TreePath treePath) {
409406
sym,
410407
node,
411408
sym.getSimpleName(),
412-
ScipRole.DEFINITION,
409+
SymbolRole.Definition_VALUE,
413410
CompilerRange.FROM_POINT_WITH_TEXT_SEARCH);
414411
if (sym.getKind() == ElementKind.ENUM_CONSTANT) {
415412
TreePath typeTreePath = nodes.get(node.getInitializer());
416413
Element typeSym = trees.getElement(typeTreePath);
417-
if (typeSym != null) emitOccurrence(typeSym, range, ScipRole.REFERENCE, Optional.empty());
414+
if (typeSym != null) emitOccurrence(typeSym, range, 0, Optional.empty());
418415
}
419416
}
420417
}
@@ -431,11 +428,7 @@ private void resolveIdentifierTree(IdentifierTree node, TreePath treePath) {
431428
Element parentSym = trees.getElement(parentPath);
432429
if (parentSym == null || parentSym.getKind() != null) {
433430
emitSymbolOccurrence(
434-
sym,
435-
node,
436-
sym.getSimpleName(),
437-
ScipRole.REFERENCE,
438-
CompilerRange.FROM_START_TO_END);
431+
sym, node, sym.getSimpleName(), 0, CompilerRange.FROM_START_TO_END);
439432
}
440433
}
441434
}
@@ -446,23 +439,15 @@ private void resolveMemberReferenceTree(MemberReferenceTree node, TreePath treeP
446439
Element sym = trees.getElement(treePath);
447440
if (sym != null) {
448441
emitSymbolOccurrence(
449-
sym,
450-
node,
451-
sym.getSimpleName(),
452-
ScipRole.REFERENCE,
453-
CompilerRange.FROM_END_TO_SYMBOL_NAME);
442+
sym, node, sym.getSimpleName(), 0, CompilerRange.FROM_END_TO_SYMBOL_NAME);
454443
}
455444
}
456445

457446
private void resolveMemberSelectTree(MemberSelectTree node, TreePath treePath) {
458447
Element sym = trees.getElement(treePath);
459448
if (sym != null) {
460449
emitSymbolOccurrence(
461-
sym,
462-
node,
463-
sym.getSimpleName(),
464-
ScipRole.REFERENCE,
465-
CompilerRange.FROM_END_TO_SYMBOL_NAME);
450+
sym, node, sym.getSimpleName(), 0, CompilerRange.FROM_END_TO_SYMBOL_NAME);
466451
}
467452
}
468453

@@ -477,18 +462,13 @@ private void resolveNewClassTree(NewClassTree node, TreePath treePath) {
477462
Element identifierSym = trees.getElement(identifierTreePath);
478463
if (identifierSym != null) {
479464
emitSymbolOccurrence(
480-
sym,
481-
node,
482-
identifierSym.getSimpleName(),
483-
ScipRole.REFERENCE,
484-
CompilerRange.FROM_TEXT_SEARCH);
465+
sym, node, identifierSym.getSimpleName(), 0, CompilerRange.FROM_TEXT_SEARCH);
485466
} else if (node.getIdentifier().getKind() == Tree.Kind.ANNOTATED_TYPE) {
486467
AnnotatedTypeTree annotatedTypeTree = (AnnotatedTypeTree) node.getIdentifier();
487468
if (annotatedTypeTree.getUnderlyingType() != null
488469
&& annotatedTypeTree.getUnderlyingType().getKind() == Tree.Kind.IDENTIFIER) {
489470
IdentifierTree ident = (IdentifierTree) annotatedTypeTree.getUnderlyingType();
490-
emitSymbolOccurrence(
491-
sym, ident, ident.getName(), ScipRole.REFERENCE, CompilerRange.FROM_TEXT_SEARCH);
471+
emitSymbolOccurrence(sym, ident, ident.getName(), 0, CompilerRange.FROM_TEXT_SEARCH);
492472
}
493473
}
494474
}

0 commit comments

Comments
 (0)