Skip to content
Merged
Show file tree
Hide file tree
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
16 changes: 5 additions & 11 deletions server/src/analyser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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)))
}

/**
Expand Down Expand Up @@ -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)
}

/**
Expand Down
22 changes: 22 additions & 0 deletions server/src/util/__tests__/flatten.test.ts
Original file line number Diff line number Diff line change
@@ -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])
})
})
7 changes: 7 additions & 0 deletions server/src/util/flatten.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function flattenArray<T>(nestedArray: Array<Array<T>>): Array<T> {
return nestedArray.reduce((acc, array) => [...acc, ...array], [])
}

export function flattenObjectValues<T>(object: { [key: string]: Array<T> }): Array<T> {
return flattenArray(Object.keys(object).map(objectKey => object[objectKey]))
}