Skip to content

Don't emit duplicate subsequent occurrences #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion snapshots/output/pure-js/src/main.js
Original file line number Diff line number Diff line change
@@ -67,7 +67,6 @@
// documentation ```ts\nvar a: number\n```
print_fib(a)
//^^^^^^^^^ reference pure-js 1.0.0 src/`main.js`/print_fib().
// ^ reference pure-js 1.0.0 src/`main.js`/a.
// ^ reference pure-js 1.0.0 src/`main.js`/a.

function forever() {
2 changes: 0 additions & 2 deletions snapshots/output/syntax/src/local.ts
Original file line number Diff line number Diff line change
@@ -37,8 +37,6 @@
// ^ reference local 8
// ^^ reference local 9
// ^^^^^^ reference typescript 4.8.4 lib/`lib.es5.d.ts`/Array#reduce().
// ^^^^^^ reference typescript 4.8.4 lib/`lib.es5.d.ts`/Array#reduce().
// ^^^^^^ reference typescript 4.8.4 lib/`lib.es5.d.ts`/Array#reduce().
// ^^^^^^^^^^^^^ definition local 16
// documentation ```ts\n(parameter) previousValue: string\n```
// ^^^^^^^^^^^^ definition local 17
1 change: 0 additions & 1 deletion snapshots/output/syntax/src/structural-type.ts
Original file line number Diff line number Diff line change
@@ -17,7 +17,6 @@
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.symbol.wellknown.d.ts`/Promise#
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2018.promise.d.ts`/Promise#
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.promise.d.ts`/PromiseConstructor#resolve().
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.promise.d.ts`/PromiseConstructor#resolve().
// ^^^^^^ definition syntax 1.0.0 src/`structural-type.ts`/member0:
// documentation ```ts\n(property) member: number\n```
}
42 changes: 38 additions & 4 deletions src/FileIndexer.ts
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ export class FileIndexer {
if (symbol.isEmpty()) {
return
}
this.document.occurrences.push(
this.pushOccurrence(
new scip.scip.Occurrence({
range: [0, 0, 0],
symbol: symbol.value,
@@ -111,7 +111,7 @@ export class FileIndexer {
// Skip empty symbols
continue
}
this.document.occurrences.push(
this.pushOccurrence(
new scip.scip.Occurrence({
range,
symbol: scipSymbol.value,
@@ -153,7 +153,7 @@ export class FileIndexer {
if (scipSymbol.isEmpty()) {
continue
}
this.document.occurrences.push(
this.pushOccurrence(
new scip.scip.Occurrence({
range,
symbol: scipSymbol.value,
@@ -190,7 +190,7 @@ export class FileIndexer {
if (scipSymbol.isEmpty()) {
continue
}
this.document.occurrences.push(
this.pushOccurrence(
new scip.scip.Occurrence({
range,
symbol: scipSymbol.value,
@@ -227,6 +227,17 @@ export class FileIndexer {
)
}

private pushOccurrence(occurrence: scip.scip.Occurrence): void {
if (this.document.occurrences.length > 0) {
const lastOccurrence =
this.document.occurrences[this.document.occurrences.length - 1]
if (isEqualOccurrence(lastOccurrence, occurrence)) {
return
}
}
this.document.occurrences.push(occurrence)
}

private relationships(
declaration: ts.Node,
declarationSymbol: ScipSymbol
@@ -673,3 +684,26 @@ function scriptElementKind(
}
return ts.ScriptElementKind.unknown
}

function isEqualOccurrence(
a: scip.scip.Occurrence,
b: scip.scip.Occurrence
): boolean {
return (
a.symbol_roles === b.symbol_roles &&
a.symbol === b.symbol &&
isEqualArray(a.range, b.range)
)
}

function isEqualArray<T>(a: T[], b: T[]): boolean {
if (a.length !== b.length) {
return false
}
for (let index = 0; index < a.length; index++) {
if (a[index] !== b[index]) {
return false
}
}
return true
}