Skip to content

Commit

Permalink
Include samples in completions when the document is empty (#2009)
Browse files Browse the repository at this point in the history
Fixing the behavior that regressed in #1947 when I was attempting to
limit samples to empty documents only.
  • Loading branch information
minestarks authored Nov 15, 2024
1 parent d2c3d80 commit d845c5c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
14 changes: 12 additions & 2 deletions vscode/src/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
18 changes: 18 additions & 0 deletions vscode/test/suites/language-service/language-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit d845c5c

Please sign in to comment.