Skip to content

Commit 84e117b

Browse files
committed
Utilize documentation above symbol for onCompletion items
1 parent 2e02f9b commit 84e117b

File tree

3 files changed

+67
-58
lines changed

3 files changed

+67
-58
lines changed

server/src/__tests__/server.test.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -367,18 +367,20 @@ describe('server', () => {
367367
)
368368

369369
expect(resultFunction).toMatchInlineSnapshot(`
370-
Array [
371-
Object {
372-
"data": Object {
373-
"name": "add_a_user",
374-
"type": 3,
375-
},
376-
"documentation": "Function defined in ../issue101.sh",
377-
"kind": 3,
378-
"label": "add_a_user",
379-
},
380-
]
381-
`)
370+
Array [
371+
Object {
372+
"data": Object {
373+
"name": "add_a_user",
374+
"type": 3,
375+
},
376+
"documentation": "Function defined in ../issue101.sh
377+
378+
Helper function to add a user",
379+
"kind": 3,
380+
"label": "add_a_user",
381+
},
382+
]
383+
`)
382384
})
383385

384386
it('responds to onCompletion with local symbol when word is found in multiple files', async () => {

server/src/server.ts

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,56 @@ export default class BashServer {
135135
)
136136
}
137137

138+
private getDocumentationForSymbol({
139+
currentUri,
140+
symbol,
141+
}: {
142+
symbol: LSP.SymbolInformation
143+
currentUri: string
144+
}): string {
145+
const getCommentsAbove = (uri: string, line: number): string => {
146+
const comment = this.analyzer.commentsAbove(uri, line)
147+
return comment ? `\n\n${comment}` : ''
148+
}
149+
150+
return symbol.location.uri !== currentUri
151+
? `${symbolKindToDescription(symbol.kind)} defined in ${path.relative(
152+
currentUri,
153+
symbol.location.uri,
154+
)}${getCommentsAbove(symbol.location.uri, symbol.location.range.start.line)}`
155+
: `${symbolKindToDescription(symbol.kind)} defined on line ${symbol.location.range
156+
.start.line + 1}${getCommentsAbove(
157+
currentUri,
158+
symbol.location.range.start.line,
159+
)}`
160+
}
161+
162+
private getCompletionItemsForSymbols({
163+
symbols,
164+
currentUri,
165+
}: {
166+
symbols: LSP.SymbolInformation[]
167+
currentUri: string
168+
}): BashCompletionItem[] {
169+
return deduplicateSymbols({ symbols, currentUri }).map(
170+
(symbol: LSP.SymbolInformation) => ({
171+
label: symbol.name,
172+
kind: symbolKindToCompletionKind(symbol.kind),
173+
data: {
174+
name: symbol.name,
175+
type: CompletionItemDataType.Symbol,
176+
},
177+
documentation:
178+
symbol.location.uri !== currentUri
179+
? this.getDocumentationForSymbol({
180+
currentUri,
181+
symbol,
182+
})
183+
: undefined,
184+
}),
185+
)
186+
}
187+
138188
private async onHover(
139189
params: LSP.TextDocumentPositionParams,
140190
): Promise<LSP.Hover | null> {
@@ -180,11 +230,6 @@ export default class BashServer {
180230
return { contents: getMarkdownContent(shellDocumentation) }
181231
}
182232
} else {
183-
const getCommentsAbove = (uri: string, line: number): string => {
184-
const comment = this.analyzer.commentsAbove(uri, line)
185-
return comment ? `\n\n${comment}` : ''
186-
}
187-
188233
const symbolDocumentation = deduplicateSymbols({
189234
symbols: this.analyzer.findSymbolsMatchingWord({
190235
exactMatch: true,
@@ -195,19 +240,7 @@ export default class BashServer {
195240
// do not return hover referencing for the current line
196241
.filter(symbol => symbol.location.range.start.line !== params.position.line)
197242
.map((symbol: LSP.SymbolInformation) =>
198-
symbol.location.uri !== currentUri
199-
? `${symbolKindToDescription(symbol.kind)} defined in ${path.relative(
200-
currentUri,
201-
symbol.location.uri,
202-
)}${getCommentsAbove(
203-
symbol.location.uri,
204-
symbol.location.range.start.line,
205-
)}`
206-
: `${symbolKindToDescription(symbol.kind)} defined on line ${symbol.location
207-
.range.start.line + 1}${getCommentsAbove(
208-
params.textDocument.uri,
209-
symbol.location.range.start.line,
210-
)}`,
243+
this.getDocumentationForSymbol({ currentUri, symbol }),
211244
)
212245

213246
if (symbolDocumentation.length === 1) {
@@ -268,7 +301,7 @@ export default class BashServer {
268301
const symbolCompletions =
269302
word === null
270303
? []
271-
: getCompletionItemsForSymbols({
304+
: this.getCompletionItemsForSymbols({
272305
symbols: this.analyzer.findSymbolsMatchingWord({
273306
exactMatch: false,
274307
word,
@@ -391,32 +424,6 @@ function deduplicateSymbols({
391424
return uniqueBasedOnHash([...symbolsCurrentFile, ...symbolsOtherFiles], getSymbolId)
392425
}
393426

394-
function getCompletionItemsForSymbols({
395-
symbols,
396-
currentUri,
397-
}: {
398-
symbols: LSP.SymbolInformation[]
399-
currentUri: string
400-
}): BashCompletionItem[] {
401-
return deduplicateSymbols({ symbols, currentUri }).map(
402-
(symbol: LSP.SymbolInformation) => ({
403-
label: symbol.name,
404-
kind: symbolKindToCompletionKind(symbol.kind),
405-
data: {
406-
name: symbol.name,
407-
type: CompletionItemDataType.Symbol,
408-
},
409-
documentation:
410-
symbol.location.uri !== currentUri
411-
? `${symbolKindToDescription(symbol.kind)} defined in ${path.relative(
412-
currentUri,
413-
symbol.location.uri,
414-
)}`
415-
: undefined,
416-
}),
417-
)
418-
}
419-
420427
function symbolKindToCompletionKind(s: LSP.SymbolKind): LSP.CompletionItemKind {
421428
switch (s) {
422429
case LSP.SymbolKind.File:

testing/fixtures/issue101.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/sh
2-
# A simple script with a function...
32

3+
# Helper function to add a user
44
add_a_user()
55
{
66
USER=$1

0 commit comments

Comments
 (0)