Skip to content
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

Include samples in completions when the document is empty #2009

Merged
merged 5 commits into from
Nov 15, 2024
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
14 changes: 12 additions & 2 deletions vscode/src/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
billti marked this conversation as resolved.
Show resolved Hide resolved
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
Loading