Skip to content

Commit 95582d7

Browse files
authored
Fix comment regex not handling params/types/fn names containing func (#1697)
The regex was matching on the last encountered `func`. Update it to match the first encountered `func` instead. Issue: #1694
1 parent 52b4f93 commit 95582d7

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/editor/CommentCompletion.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class FunctionDocumentationCompletionProvider implements vscode.CompletionItemPr
137137
position: vscode.Position
138138
): FunctionDetails | null {
139139
const parser = new DocumentParser(document, position);
140-
if (!parser.match(/^[^{]*\b(?:func|init)/)) {
140+
if (!parser.match(/\b(?:func|init)\b(?=[^{]*\{)/)) {
141141
return null;
142142
}
143143
const funcName = parser.match(/^([^(<]*)\s*(\(|<)/);

test/integration-tests/editor/CommentCompletion.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,55 @@ suite("CommentCompletion Test Suite", () => {
207207
); // ! ensures trailing white space is not trimmed when this file is formatted.
208208
});
209209

210+
test("Comment completion on function with default parameter using #function", async () => {
211+
const { document, positions } = await openDocument(`
212+
/// 1️⃣
213+
func foo(f: String = #function) {}`);
214+
const position = positions["1️⃣"];
215+
216+
const items = await provider.functionCommentCompletion.provideCompletionItems(
217+
document,
218+
position
219+
);
220+
assert.deepEqual(items, [
221+
expectedCompletionItem(` $1
222+
/// - Parameter f: $2`),
223+
]);
224+
});
225+
226+
test("Comment completion on function with parameter named 'func'", async () => {
227+
const { document, positions } = await openDocument(`
228+
/// 1️⃣
229+
func foo(func: String) {}`);
230+
const position = positions["1️⃣"];
231+
232+
const items = await provider.functionCommentCompletion.provideCompletionItems(
233+
document,
234+
position
235+
);
236+
assert.deepEqual(items, [
237+
expectedCompletionItem(` $1
238+
/// - Parameter func: $2`),
239+
]);
240+
});
241+
242+
test("Comment completion on function with function and parameter named 'func' and #function default, returning function type", async () => {
243+
const { document, positions } = await openDocument(`
244+
/// 1️⃣
245+
public func \`func\`(func: #function) -> function {}`);
246+
const position = positions["1️⃣"];
247+
248+
const items = await provider.functionCommentCompletion.provideCompletionItems(
249+
document,
250+
position
251+
);
252+
assert.deepEqual(items, [
253+
expectedCompletionItem(` $1
254+
/// - Parameter func: $2
255+
/// - Returns: $3`),
256+
]);
257+
});
258+
210259
function expectedCompletionItem(snippet: string): vscode.CompletionItem {
211260
const expected = new vscode.CompletionItem(
212261
"/// - parameters:",

0 commit comments

Comments
 (0)