Skip to content

Commit d845c5c

Browse files
authored
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.
1 parent d2c3d80 commit d845c5c

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

vscode/src/completion.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,17 @@ class QSharpCompletionItemProvider implements vscode.CompletionItemProvider {
8989
return item;
9090
});
9191

92-
// Include the samples list for empty documents
93-
return document.lineCount > 0 ? results : results.concat(this.samples);
92+
// Include the samples in contexts that are syntactically appropriate.
93+
// The presence of the "operation" keyword in the completion list is a
94+
// hint that the cursor is at a point we can insert the sample code.
95+
96+
const shouldIncludeSamples =
97+
results.findIndex(
98+
(i) =>
99+
i.kind === vscode.CompletionItemKind.Keyword &&
100+
i.label === "operation",
101+
) !== -1;
102+
103+
return !shouldIncludeSamples ? results : results.concat(this.samples);
94104
}
95105
}

vscode/test/suites/language-service/language-service.test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,24 @@ suite("Q# Language Service Tests", function suite() {
7979
actualCompletionList.items.map((i) => i.label),
8080
"operation",
8181
);
82+
83+
assert.include(
84+
actualCompletionList.items.map((i) => i.label),
85+
"Shor sample",
86+
);
87+
});
88+
89+
test("Completions - don't include samples when syntactically inappropriate", async () => {
90+
const actualCompletionList = (await vscode.commands.executeCommand(
91+
"vscode.executeCompletionItemProvider",
92+
testQs,
93+
new vscode.Position(12, 0), // put the cursor after the namespace declaration
94+
)) as vscode.CompletionList;
95+
96+
assert.notInclude(
97+
actualCompletionList.items.map((i) => i.label),
98+
"Shor sample",
99+
);
82100
});
83101

84102
test("Definition", async () => {

0 commit comments

Comments
 (0)