Skip to content

Commit 64a356f

Browse files
authored
Merge pull request #209 from bash-lsp/fix-invalid-document-highlights
Fix invalid document highlights
2 parents b5a23a2 + 7b75dee commit 64a356f

File tree

9 files changed

+617
-857
lines changed

9 files changed

+617
-857
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
"eslint-plugin-jest": "^22.17.0",
2727
"eslint-plugin-prettier": "^3.1.2",
2828
"eslint-plugin-simple-import-sort": "^4.0.0",
29-
"jest": "25.0.0",
29+
"jest": "^25.3.0",
3030
"prettier": "^1.19.1",
31-
"ts-jest": "^25.2.1",
31+
"ts-jest": "^25.3.1",
3232
"typescript": "^3.8.3",
3333
"vscode-languageserver": "^5.2.1"
3434
},

server/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Bash Language Server
22

3+
## 1.11.2
4+
5+
* Fix invalid documentHighlight response when word cannot be found (https://github.com/bash-lsp/bash-language-server/pull/209)
6+
37
## 1.11.1
48

59
* Workspace symbols are resolved using fuzzy search (not just starting with it)

server/package.json

Lines changed: 1 addition & 1 deletion
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.11.1",
6+
"version": "1.11.2",
77
"publisher": "mads-hartmann",
88
"main": "./out/server.js",
99
"typings": "./out/server.d.ts",

server/src/__tests__/analyzer.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ describe('fromRoot', () => {
127127

128128
expect(newAnalyzer).toBeDefined()
129129

130-
const FIXTURE_FILES_MATCHING_GLOB = 8
130+
const FIXTURE_FILES_MATCHING_GLOB = 9
131131

132132
// Intro, stats on glob, one file skipped due to shebang, and outro
133133
const LOG_LINES = FIXTURE_FILES_MATCHING_GLOB + 4

server/src/__tests__/server.test.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,85 @@ describe('server', () => {
132132
})
133133
})
134134

135+
it('responds to onDocumentHighlight', async () => {
136+
const { connection, server } = await initializeServer()
137+
server.register(connection)
138+
139+
const onDocumentHighlight = connection.onDocumentHighlight.mock.calls[0][0]
140+
141+
const result1 = await onDocumentHighlight(
142+
{
143+
textDocument: {
144+
uri: FIXTURE_URI.ISSUE206,
145+
},
146+
position: {
147+
// FOO
148+
line: 0,
149+
character: 10,
150+
},
151+
},
152+
{} as any,
153+
)
154+
155+
// TODO: there is a superfluous range here on line 0:
156+
expect(result1).toMatchInlineSnapshot(`
157+
Array [
158+
Object {
159+
"range": Object {
160+
"end": Object {
161+
"character": 12,
162+
"line": 0,
163+
},
164+
"start": Object {
165+
"character": 9,
166+
"line": 0,
167+
},
168+
},
169+
},
170+
Object {
171+
"range": Object {
172+
"end": Object {
173+
"character": 12,
174+
"line": 0,
175+
},
176+
"start": Object {
177+
"character": 9,
178+
"line": 0,
179+
},
180+
},
181+
},
182+
Object {
183+
"range": Object {
184+
"end": Object {
185+
"character": 28,
186+
"line": 1,
187+
},
188+
"start": Object {
189+
"character": 25,
190+
"line": 1,
191+
},
192+
},
193+
},
194+
]
195+
`)
196+
197+
const result2 = await onDocumentHighlight(
198+
{
199+
textDocument: {
200+
uri: FIXTURE_URI.ISSUE206,
201+
},
202+
position: {
203+
// readonly cannot be parsed as a word
204+
line: 0,
205+
character: 0,
206+
},
207+
},
208+
{} as any,
209+
)
210+
211+
expect(result2).toMatchInlineSnapshot(`Array []`)
212+
})
213+
135214
it('responds to onWorkspaceSymbol', async () => {
136215
const { connection, server } = await initializeServer()
137216
server.register(connection)

server/src/server.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ export default class BashServer {
119119
params: LSP.ReferenceParams | LSP.TextDocumentPositionParams
120120
word?: string | null
121121
}) {
122-
const wordLog = word ? `"${word}"` : ''
122+
const wordLog = word ? `"${word}"` : 'null'
123123
this.connection.console.log(
124-
`${request} ${params.position.line}:${params.position.character} ${wordLog}`,
124+
`${request} ${params.position.line}:${params.position.character} word=${wordLog}`,
125125
)
126126
}
127127

@@ -201,6 +201,11 @@ export default class BashServer {
201201
): LSP.DocumentHighlight[] {
202202
const word = this.getWordAtPoint(params)
203203
this.logRequest({ request: 'onDocumentHighlight', params, word })
204+
205+
if (!word) {
206+
return []
207+
}
208+
204209
return this.analyzer
205210
.findOccurrences(params.textDocument.uri, word)
206211
.map(n => ({ range: n.range }))

testing/fixtures.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ function getDocument(uri: string) {
1616
export const FIXTURE_URI = {
1717
MISSING_NODE: `file://${path.join(FIXTURE_FOLDER, 'missing-node.sh')}`,
1818
ISSUE101: `file://${path.join(FIXTURE_FOLDER, 'issue101.sh')}`,
19+
ISSUE206: `file://${path.join(FIXTURE_FOLDER, 'issue206.sh')}`,
1920
INSTALL: `file://${path.join(FIXTURE_FOLDER, 'install.sh')}`,
2021
PARSE_PROBLEMS: `file://${path.join(FIXTURE_FOLDER, 'parse-problems.sh')}`,
2122
}

testing/fixtures/issue206.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
readonly FOO=$1
2+
readonly BAR=$UNDEFINED/$FOO

0 commit comments

Comments
 (0)