Skip to content

Commit 7e07b96

Browse files
authored
Merge pull request #684 from bash-lsp/avoid-completion-in-the-middle
Skip completions in the middle of a non word
2 parents eeaaada + d061818 commit 7e07b96

File tree

4 files changed

+52
-8
lines changed

4 files changed

+52
-8
lines changed

server/src/__tests__/server.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,29 @@ describe('server', () => {
563563
expect(result && 'length' in result && result.length).toBeGreaterThanOrEqual(50)
564564
})
565565

566+
it('responds to onCompletion with empty list when the following characters is not an empty string or whitespace', async () => {
567+
const { connection } = await initializeServer()
568+
569+
const onCompletion = connection.onCompletion.mock.calls[0][0]
570+
571+
const result = await onCompletion(
572+
{
573+
textDocument: {
574+
uri: FIXTURE_URI.INSTALL,
575+
},
576+
position: {
577+
// {
578+
line: 271,
579+
character: 21,
580+
},
581+
},
582+
{} as any,
583+
{} as any,
584+
)
585+
586+
expect(result).toEqual([])
587+
})
588+
566589
it('responds to onCompletion with empty list when word is a comment', async () => {
567590
const { connection } = await initializeServer()
568591

server/src/analyser.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,13 @@ export default class Analyzer {
359359
return getAllDeclarationsInTree({ uri, tree })
360360
}
361361

362+
/**
363+
* Get the document for the given URI.
364+
*/
365+
public getDocument(uri: string): TextDocument | undefined {
366+
return this.uriToAnalyzedDocument[uri]?.document
367+
}
368+
362369
// TODO: move somewhere else than the analyzer...
363370
public async getExplainshellDocumentation({
364371
params,
@@ -389,9 +396,8 @@ export default class Analyzer {
389396
return {}
390397
}
391398

392-
const cmd = analyzedDocument.document
393-
.getText()
394-
.slice(interestingNode.startIndex, interestingNode.endIndex)
399+
const cmd = interestingNode.text
400+
395401
type ExplainshellResponse = {
396402
matches?: Array<{ helpHTML: string; start: number; end: number }>
397403
}

server/src/server.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -437,21 +437,34 @@ export default class BashServer {
437437
character: Math.max(params.position.character - 1, 0),
438438
},
439439
})
440+
440441
this.logRequest({ request: 'onCompletion', params, word })
441442

442-
if (word && word.startsWith('#')) {
443+
if (word?.startsWith('#')) {
443444
// Inside a comment block
444445
return []
445446
}
446-
if (word && word === '{') {
447+
448+
if (word === '{') {
447449
// We should not complete when it is not prefixed by a $.
448-
// This case needs to be here
449-
// because { is a completionProvider triggerCharacter.
450+
// This case needs to be here as "{" is a completionProvider triggerCharacter.
450451
return []
451452
}
452453

454+
if (!word) {
455+
const nextCharacter = this.analyzer.getDocument(params.textDocument.uri)?.getText({
456+
start: params.position,
457+
end: { ...params.position, character: params.position.character + 1 },
458+
})
459+
const isNextCharacterSpaceOrEmpty = nextCharacter === '' || nextCharacter === ' '
460+
if (!isNextCharacterSpaceOrEmpty) {
461+
// We are in the middle of something, so don't complete
462+
return []
463+
}
464+
}
465+
453466
let options: string[] = []
454-
if (word && word.startsWith('-')) {
467+
if (word?.startsWith('-')) {
455468
const commandName = this.analyzer.commandNameAtPoint(
456469
params.textDocument.uri,
457470
params.position.line,

testing/fixtures/install.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,5 @@ if [ $ret -ne 0 ]; then
268268
echo "It failed" >&2
269269
fi
270270
exit $ret
271+
272+
iverilog -i wave.vpp *{}.v

0 commit comments

Comments
 (0)