Skip to content

Commit ae9843a

Browse files
authoredMay 15, 2020
Merge pull request #235 from bash-lsp/comments-as-documentation
Use comments as symbol documentation
2 parents 2e02f9b + 5df765f commit ae9843a

File tree

5 files changed

+71
-59
lines changed

5 files changed

+71
-59
lines changed
 

‎server/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Bash Language Server
22

3+
## 1.15.0
4+
5+
* Use comments above symbols for documentation (https://github.com/bash-lsp/bash-language-server/pull/234, https://github.com/bash-lsp/bash-language-server/pull/235)
6+
7+
38
## 1.14.0
49

510
* onHover and onCompletion documentation improvements (https://github.com/bash-lsp/bash-language-server/pull/230)

‎server/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "A language server for Bash",
44
"author": "Mads Hartmann",
55
"license": "MIT",
6-
"version": "1.14.0",
6+
"version": "1.15.0",
77
"publisher": "mads-hartmann",
88
"main": "./out/server.js",
99
"typings": "./out/server.d.ts",

‎server/src/__tests__/server.test.ts

+14-12
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

+50-45
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,54 @@ 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 symbolUri = symbol.location.uri
146+
const symbolStarLine = symbol.location.range.start.line
147+
148+
const commentAboveSymbol = this.analyzer.commentsAbove(symbolUri, symbolStarLine)
149+
const symbolDocumentation = commentAboveSymbol ? `\n\n${commentAboveSymbol}` : ''
150+
151+
return symbolUri !== currentUri
152+
? `${symbolKindToDescription(symbol.kind)} defined in ${path.relative(
153+
currentUri,
154+
symbolUri,
155+
)}${symbolDocumentation}`
156+
: `${symbolKindToDescription(symbol.kind)} defined on line ${symbolStarLine +
157+
1}${symbolDocumentation}`
158+
}
159+
160+
private getCompletionItemsForSymbols({
161+
symbols,
162+
currentUri,
163+
}: {
164+
symbols: LSP.SymbolInformation[]
165+
currentUri: string
166+
}): BashCompletionItem[] {
167+
return deduplicateSymbols({ symbols, currentUri }).map(
168+
(symbol: LSP.SymbolInformation) => ({
169+
label: symbol.name,
170+
kind: symbolKindToCompletionKind(symbol.kind),
171+
data: {
172+
name: symbol.name,
173+
type: CompletionItemDataType.Symbol,
174+
},
175+
documentation:
176+
symbol.location.uri !== currentUri
177+
? this.getDocumentationForSymbol({
178+
currentUri,
179+
symbol,
180+
})
181+
: undefined,
182+
}),
183+
)
184+
}
185+
138186
private async onHover(
139187
params: LSP.TextDocumentPositionParams,
140188
): Promise<LSP.Hover | null> {
@@ -180,11 +228,6 @@ export default class BashServer {
180228
return { contents: getMarkdownContent(shellDocumentation) }
181229
}
182230
} 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-
188231
const symbolDocumentation = deduplicateSymbols({
189232
symbols: this.analyzer.findSymbolsMatchingWord({
190233
exactMatch: true,
@@ -195,19 +238,7 @@ export default class BashServer {
195238
// do not return hover referencing for the current line
196239
.filter(symbol => symbol.location.range.start.line !== params.position.line)
197240
.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-
)}`,
241+
this.getDocumentationForSymbol({ currentUri, symbol }),
211242
)
212243

213244
if (symbolDocumentation.length === 1) {
@@ -268,7 +299,7 @@ export default class BashServer {
268299
const symbolCompletions =
269300
word === null
270301
? []
271-
: getCompletionItemsForSymbols({
302+
: this.getCompletionItemsForSymbols({
272303
symbols: this.analyzer.findSymbolsMatchingWord({
273304
exactMatch: false,
274305
word,
@@ -391,32 +422,6 @@ function deduplicateSymbols({
391422
return uniqueBasedOnHash([...symbolsCurrentFile, ...symbolsOtherFiles], getSymbolId)
392423
}
393424

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-
420425
function symbolKindToCompletionKind(s: LSP.SymbolKind): LSP.CompletionItemKind {
421426
switch (s) {
422427
case LSP.SymbolKind.File:

‎testing/fixtures/issue101.sh

+1-1
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)
Please sign in to comment.