diff --git a/vscode/src/completion.ts b/vscode/src/completion.ts index 8753ee685b..cc0472a49b 100644 --- a/vscode/src/completion.ts +++ b/vscode/src/completion.ts @@ -86,7 +86,17 @@ class QSharpCompletionItemProvider implements vscode.CompletionItemProvider { return item; }); - // Include the samples list for empty documents - return document.lineCount > 0 ? results : results.concat(this.samples); + // Include the samples in contexts that are syntactically appropriate. + // The presence of the "operation" keyword in the completion list is a + // hint that the cursor is at a point we can insert the sample code. + + const shouldIncludeSamples = + results.findIndex( + (i) => + i.kind === vscode.CompletionItemKind.Keyword && + i.label === "operation", + ) !== -1; + + return !shouldIncludeSamples ? results : results.concat(this.samples); } } diff --git a/vscode/test/suites/language-service/language-service.test.ts b/vscode/test/suites/language-service/language-service.test.ts index 8ad2580349..cffadfe84c 100644 --- a/vscode/test/suites/language-service/language-service.test.ts +++ b/vscode/test/suites/language-service/language-service.test.ts @@ -79,6 +79,24 @@ suite("Q# Language Service Tests", function suite() { actualCompletionList.items.map((i) => i.label), "operation", ); + + assert.include( + actualCompletionList.items.map((i) => i.label), + "Shor sample", + ); + }); + + test("Completions - don't include samples when syntactically inappropriate", async () => { + const actualCompletionList = (await vscode.commands.executeCommand( + "vscode.executeCompletionItemProvider", + testQs, + new vscode.Position(12, 0), // put the cursor after the namespace declaration + )) as vscode.CompletionList; + + assert.notInclude( + actualCompletionList.items.map((i) => i.label), + "Shor sample", + ); }); test("Definition", async () => {