Skip to content

Fix comment regex not handling params/types/fn names containing func #1697

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

Merged
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
2 changes: 1 addition & 1 deletion src/editor/CommentCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class FunctionDocumentationCompletionProvider implements vscode.CompletionItemPr
position: vscode.Position
): FunctionDetails | null {
const parser = new DocumentParser(document, position);
if (!parser.match(/^[^{]*\b(?:func|init)/)) {
if (!parser.match(/\b(?:func|init)\b(?=[^{]*\{)/)) {
return null;
}
const funcName = parser.match(/^([^(<]*)\s*(\(|<)/);
Expand Down
49 changes: 49 additions & 0 deletions test/integration-tests/editor/CommentCompletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,55 @@ suite("CommentCompletion Test Suite", () => {
); // ! ensures trailing white space is not trimmed when this file is formatted.
});

test("Comment completion on function with default parameter using #function", async () => {
const { document, positions } = await openDocument(`
/// 1️⃣
func foo(f: String = #function) {}`);
const position = positions["1️⃣"];

const items = await provider.functionCommentCompletion.provideCompletionItems(
document,
position
);
assert.deepEqual(items, [
expectedCompletionItem(` $1
/// - Parameter f: $2`),
]);
});

test("Comment completion on function with parameter named 'func'", async () => {
const { document, positions } = await openDocument(`
/// 1️⃣
func foo(func: String) {}`);
const position = positions["1️⃣"];

const items = await provider.functionCommentCompletion.provideCompletionItems(
document,
position
);
assert.deepEqual(items, [
expectedCompletionItem(` $1
/// - Parameter func: $2`),
]);
});

test("Comment completion on function with function and parameter named 'func' and #function default, returning function type", async () => {
const { document, positions } = await openDocument(`
/// 1️⃣
public func \`func\`(func: #function) -> function {}`);
const position = positions["1️⃣"];

const items = await provider.functionCommentCompletion.provideCompletionItems(
document,
position
);
assert.deepEqual(items, [
expectedCompletionItem(` $1
/// - Parameter func: $2
/// - Returns: $3`),
]);
});

function expectedCompletionItem(snippet: string): vscode.CompletionItem {
const expected = new vscode.CompletionItem(
"/// - parameters:",
Expand Down