Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a3d8ce7

Browse files
authored
Merge pull request #27 from mads-hartmann/util-refactoring
Extract flatten functions to utilities
2 parents b8d9f07 + d8035cc commit a3d8ce7

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

‎server/src/analyser.ts‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as bash from 'tree-sitter-bash'
88
import * as LSP from 'vscode-languageserver'
99

1010
import { uniqueBasedOnHash } from './util/array'
11+
import { flattenArray, flattenObjectValues } from './util/flatten'
1112
import * as TreeSitterUtil from './util/tree-sitter'
1213

1314
type Kinds = { [type: string]: LSP.SymbolKind }
@@ -87,11 +88,8 @@ export default class Analyzer {
8788
* Find all the locations where something named name has been defined.
8889
*/
8990
public findReferences(name: string): LSP.Location[] {
90-
const locations = []
91-
Object.keys(this.uriToTreeSitterDocument).forEach(uri => {
92-
this.findOccurrences(uri, name).forEach(l => locations.push(l))
93-
})
94-
return locations
91+
const uris = Object.keys(this.uriToTreeSitterDocument)
92+
return flattenArray(uris.map(uri => this.findOccurrences(uri, name)))
9593
}
9694

9795
/**
@@ -130,12 +128,8 @@ export default class Analyzer {
130128
* Find all symbol definitions in the given file.
131129
*/
132130
public findSymbols(uri: string): LSP.SymbolInformation[] {
133-
const declarationsInFile = this.uriToDeclarations[uri] || []
134-
const ds = []
135-
Object.keys(declarationsInFile).forEach(n => {
136-
declarationsInFile[n].forEach(d => ds.push(d))
137-
})
138-
return ds
131+
const declarationsInFile = this.uriToDeclarations[uri] || {}
132+
return flattenObjectValues(declarationsInFile)
139133
}
140134

141135
/**
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { flattenArray, flattenObjectValues } from '../flatten'
2+
3+
describe('flattenArray', () => {
4+
it('works on array with one element', () => {
5+
expect(flattenArray([[1, 2, 3]])).toEqual([1, 2, 3])
6+
})
7+
8+
it('works on array with multiple elements', () => {
9+
expect(flattenArray([[1], [2, 3], [4]])).toEqual([1, 2, 3, 4])
10+
})
11+
})
12+
13+
describe('flattenObjectValues', () => {
14+
it('flatten object values', () => {
15+
expect(
16+
flattenObjectValues({
17+
'foo.sh': [1],
18+
'baz.sh': [2],
19+
}),
20+
).toEqual([1, 2])
21+
})
22+
})

‎server/src/util/flatten.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function flattenArray<T>(nestedArray: Array<Array<T>>): Array<T> {
2+
return nestedArray.reduce((acc, array) => [...acc, ...array], [])
3+
}
4+
5+
export function flattenObjectValues<T>(object: { [key: string]: Array<T> }): Array<T> {
6+
return flattenArray(Object.keys(object).map(objectKey => object[objectKey]))
7+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /