Skip to content

Skip completions in the middle of a non word #684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions server/src/__tests__/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,29 @@ describe('server', () => {
expect(result && 'length' in result && result.length).toBeGreaterThanOrEqual(50)
})

it('responds to onCompletion with empty list when the following characters is not an empty string or whitespace', async () => {
const { connection } = await initializeServer()

const onCompletion = connection.onCompletion.mock.calls[0][0]

const result = await onCompletion(
{
textDocument: {
uri: FIXTURE_URI.INSTALL,
},
position: {
// {
line: 271,
character: 21,
},
},
{} as any,
{} as any,
)

expect(result).toEqual([])
})

it('responds to onCompletion with empty list when word is a comment', async () => {
const { connection } = await initializeServer()

Expand Down
12 changes: 9 additions & 3 deletions server/src/analyser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,13 @@ export default class Analyzer {
return getAllDeclarationsInTree({ uri, tree })
}

/**
* Get the document for the given URI.
*/
public getDocument(uri: string): TextDocument | undefined {
return this.uriToAnalyzedDocument[uri]?.document
}

// TODO: move somewhere else than the analyzer...
public async getExplainshellDocumentation({
params,
Expand Down Expand Up @@ -389,9 +396,8 @@ export default class Analyzer {
return {}
}

const cmd = analyzedDocument.document
.getText()
.slice(interestingNode.startIndex, interestingNode.endIndex)
const cmd = interestingNode.text

type ExplainshellResponse = {
matches?: Array<{ helpHTML: string; start: number; end: number }>
}
Expand Down
23 changes: 18 additions & 5 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,21 +437,34 @@ export default class BashServer {
character: Math.max(params.position.character - 1, 0),
},
})

this.logRequest({ request: 'onCompletion', params, word })

if (word && word.startsWith('#')) {
if (word?.startsWith('#')) {
// Inside a comment block
return []
}
if (word && word === '{') {

if (word === '{') {
// We should not complete when it is not prefixed by a $.
// This case needs to be here
// because { is a completionProvider triggerCharacter.
// This case needs to be here as "{" is a completionProvider triggerCharacter.
return []
}

if (!word) {
const nextCharacter = this.analyzer.getDocument(params.textDocument.uri)?.getText({
start: params.position,
end: { ...params.position, character: params.position.character + 1 },
})
const isNextCharacterSpaceOrEmpty = nextCharacter === '' || nextCharacter === ' '
if (!isNextCharacterSpaceOrEmpty) {
// We are in the middle of something, so don't complete
return []
}
}

let options: string[] = []
if (word && word.startsWith('-')) {
if (word?.startsWith('-')) {
const commandName = this.analyzer.commandNameAtPoint(
params.textDocument.uri,
params.position.line,
Expand Down
2 changes: 2 additions & 0 deletions testing/fixtures/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,5 @@ if [ $ret -ne 0 ]; then
echo "It failed" >&2
fi
exit $ret

iverilog -i wave.vpp *{}.v