From d845c5c33ac580e2ef6f94c9352526cd38dedaca Mon Sep 17 00:00:00 2001 From: Mine Starks <16928427+minestarks@users.noreply.github.com> Date: Fri, 15 Nov 2024 09:09:21 -0800 Subject: [PATCH] Include samples in completions when the document is empty (#2009) Fixing the behavior that regressed in #1947 when I was attempting to limit samples to empty documents only. --- vscode/src/completion.ts | 14 ++++++++++++-- .../language-service/language-service.test.ts | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/vscode/src/completion.ts b/vscode/src/completion.ts index dd6600ae54..6526962c0b 100644 --- a/vscode/src/completion.ts +++ b/vscode/src/completion.ts @@ -89,7 +89,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 () => {