Skip to content

Commit 8496b0d

Browse files
authoredOct 27, 2022
Don't emit duplicate occurrences (sourcegraph#194)
Fixes sourcegraph#152
1 parent 6d69827 commit 8496b0d

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed
 

‎snapshots/output/pure-js/src/main.js

-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
// documentation ```ts\nvar a: number\n```
6868
print_fib(a)
6969
//^^^^^^^^^ reference pure-js 1.0.0 src/`main.js`/print_fib().
70-
// ^ reference pure-js 1.0.0 src/`main.js`/a.
7170
// ^ reference pure-js 1.0.0 src/`main.js`/a.
7271

7372
function forever() {

‎snapshots/output/syntax/src/local.ts

-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@
3737
// ^ reference local 8
3838
// ^^ reference local 9
3939
// ^^^^^^ reference typescript 4.8.4 lib/`lib.es5.d.ts`/Array#reduce().
40-
// ^^^^^^ reference typescript 4.8.4 lib/`lib.es5.d.ts`/Array#reduce().
41-
// ^^^^^^ reference typescript 4.8.4 lib/`lib.es5.d.ts`/Array#reduce().
4240
// ^^^^^^^^^^^^^ definition local 16
4341
// documentation ```ts\n(parameter) previousValue: string\n```
4442
// ^^^^^^^^^^^^ definition local 17

‎snapshots/output/syntax/src/structural-type.ts

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.symbol.wellknown.d.ts`/Promise#
1818
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2018.promise.d.ts`/Promise#
1919
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.promise.d.ts`/PromiseConstructor#resolve().
20-
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.promise.d.ts`/PromiseConstructor#resolve().
2120
// ^^^^^^ definition syntax 1.0.0 src/`structural-type.ts`/member0:
2221
// documentation ```ts\n(property) member: number\n```
2322
}

‎src/FileIndexer.ts

+38-4
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class FileIndexer {
4545
if (symbol.isEmpty()) {
4646
return
4747
}
48-
this.document.occurrences.push(
48+
this.pushOccurrence(
4949
new scip.scip.Occurrence({
5050
range: [0, 0, 0],
5151
symbol: symbol.value,
@@ -111,7 +111,7 @@ export class FileIndexer {
111111
// Skip empty symbols
112112
continue
113113
}
114-
this.document.occurrences.push(
114+
this.pushOccurrence(
115115
new scip.scip.Occurrence({
116116
range,
117117
symbol: scipSymbol.value,
@@ -153,7 +153,7 @@ export class FileIndexer {
153153
if (scipSymbol.isEmpty()) {
154154
continue
155155
}
156-
this.document.occurrences.push(
156+
this.pushOccurrence(
157157
new scip.scip.Occurrence({
158158
range,
159159
symbol: scipSymbol.value,
@@ -190,7 +190,7 @@ export class FileIndexer {
190190
if (scipSymbol.isEmpty()) {
191191
continue
192192
}
193-
this.document.occurrences.push(
193+
this.pushOccurrence(
194194
new scip.scip.Occurrence({
195195
range,
196196
symbol: scipSymbol.value,
@@ -227,6 +227,17 @@ export class FileIndexer {
227227
)
228228
}
229229

230+
private pushOccurrence(occurrence: scip.scip.Occurrence): void {
231+
if (this.document.occurrences.length > 0) {
232+
const lastOccurrence =
233+
this.document.occurrences[this.document.occurrences.length - 1]
234+
if (isEqualOccurrence(lastOccurrence, occurrence)) {
235+
return
236+
}
237+
}
238+
this.document.occurrences.push(occurrence)
239+
}
240+
230241
private relationships(
231242
declaration: ts.Node,
232243
declarationSymbol: ScipSymbol
@@ -673,3 +684,26 @@ function scriptElementKind(
673684
}
674685
return ts.ScriptElementKind.unknown
675686
}
687+
688+
function isEqualOccurrence(
689+
a: scip.scip.Occurrence,
690+
b: scip.scip.Occurrence
691+
): boolean {
692+
return (
693+
a.symbol_roles === b.symbol_roles &&
694+
a.symbol === b.symbol &&
695+
isEqualArray(a.range, b.range)
696+
)
697+
}
698+
699+
function isEqualArray<T>(a: T[], b: T[]): boolean {
700+
if (a.length !== b.length) {
701+
return false
702+
}
703+
for (let index = 0; index < a.length; index++) {
704+
if (a[index] !== b[index]) {
705+
return false
706+
}
707+
}
708+
return true
709+
}

0 commit comments

Comments
 (0)
Please sign in to comment.