diff --git a/server/src/analyser.ts b/server/src/analyser.ts index 39743cda3..6e2898a21 100644 --- a/server/src/analyser.ts +++ b/server/src/analyser.ts @@ -8,6 +8,7 @@ import * as bash from 'tree-sitter-bash' import * as LSP from 'vscode-languageserver' import { uniqueBasedOnHash } from './util/array' +import { flattenArray, flattenObjectValues } from './util/flatten' import * as TreeSitterUtil from './util/tree-sitter' type Kinds = { [type: string]: LSP.SymbolKind } @@ -87,11 +88,8 @@ export default class Analyzer { * Find all the locations where something named name has been defined. */ public findReferences(name: string): LSP.Location[] { - const locations = [] - Object.keys(this.uriToTreeSitterDocument).forEach(uri => { - this.findOccurrences(uri, name).forEach(l => locations.push(l)) - }) - return locations + const uris = Object.keys(this.uriToTreeSitterDocument) + return flattenArray(uris.map(uri => this.findOccurrences(uri, name))) } /** @@ -130,12 +128,8 @@ export default class Analyzer { * Find all symbol definitions in the given file. */ public findSymbols(uri: string): LSP.SymbolInformation[] { - const declarationsInFile = this.uriToDeclarations[uri] || [] - const ds = [] - Object.keys(declarationsInFile).forEach(n => { - declarationsInFile[n].forEach(d => ds.push(d)) - }) - return ds + const declarationsInFile = this.uriToDeclarations[uri] || {} + return flattenObjectValues(declarationsInFile) } /** diff --git a/server/src/util/__tests__/flatten.test.ts b/server/src/util/__tests__/flatten.test.ts new file mode 100644 index 000000000..f77577710 --- /dev/null +++ b/server/src/util/__tests__/flatten.test.ts @@ -0,0 +1,22 @@ +import { flattenArray, flattenObjectValues } from '../flatten' + +describe('flattenArray', () => { + it('works on array with one element', () => { + expect(flattenArray([[1, 2, 3]])).toEqual([1, 2, 3]) + }) + + it('works on array with multiple elements', () => { + expect(flattenArray([[1], [2, 3], [4]])).toEqual([1, 2, 3, 4]) + }) +}) + +describe('flattenObjectValues', () => { + it('flatten object values', () => { + expect( + flattenObjectValues({ + 'foo.sh': [1], + 'baz.sh': [2], + }), + ).toEqual([1, 2]) + }) +}) diff --git a/server/src/util/flatten.ts b/server/src/util/flatten.ts new file mode 100644 index 000000000..cd1a2ccaa --- /dev/null +++ b/server/src/util/flatten.ts @@ -0,0 +1,7 @@ +export function flattenArray(nestedArray: Array>): Array { + return nestedArray.reduce((acc, array) => [...acc, ...array], []) +} + +export function flattenObjectValues(object: { [key: string]: Array }): Array { + return flattenArray(Object.keys(object).map(objectKey => object[objectKey])) +}