From 580a9caa017277c1836c44e81229bdf29240d57b Mon Sep 17 00:00:00 2001 From: Paul LeMarquand Date: Thu, 10 Jul 2025 17:12:18 -0400 Subject: [PATCH] Fix comment blocks getting incorrectly completed on fn decls A comment block may be continued when editing a function declaration. Fix and add a test case. --- src/editor/CommentCompletion.ts | 10 ++++++++-- .../editor/CommentCompletion.test.ts | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/editor/CommentCompletion.ts b/src/editor/CommentCompletion.ts index 71b9be50a..0c8b1e73b 100644 --- a/src/editor/CommentCompletion.ts +++ b/src/editor/CommentCompletion.ts @@ -60,13 +60,19 @@ class DocCommentCompletionProvider implements vscode.CompletionItemProvider { return; } // Fixes https://github.com/swiftlang/vscode-swift/issues/1648 - const match = /^(\s*)(\/\/)?\s?(.+)?/.exec(document.lineAt(position.line).text); + const lineText = document.lineAt(position.line).text; + // Continue the comment if its a white space only line, or if VS Code has already continued + // the comment by adding a // on the new line. + const match = + lineText.trim().length === 0 + ? [lineText, lineText, ""] + : /^(\s*)\/\/\s(.+)/.exec(lineText); if (match) { await vscode.window.activeTextEditor.edit( edit => { edit.replace( new vscode.Range(position.line, 0, position.line, match[0].length), - `${match[1]}/// ${match[3] ?? ""}` + `${match[1]}/// ${match[2]}` ); }, { undoStopBefore: false, undoStopAfter: true } diff --git a/test/integration-tests/editor/CommentCompletion.test.ts b/test/integration-tests/editor/CommentCompletion.test.ts index 8cd22ce31..140646a85 100644 --- a/test/integration-tests/editor/CommentCompletion.test.ts +++ b/test/integration-tests/editor/CommentCompletion.test.ts @@ -290,6 +290,20 @@ suite("CommentCompletion Test Suite", () => { assert.strictEqual(newLine, "/// bbb", "New line should continue the comment block"); }); + + test("Should not continue a comment on a line that has content", async () => { + const { document, positions } = await openDocument(` + /// aaa + public func foo(param: Int, a1️⃣) {}`); + + const originalText = document.getText(); + const position = positions["1️⃣"]; + await provider.docCommentCompletion.provideCompletionItems(document, position); + + const documentText = document.getText(); + + assert.deepEqual(documentText, originalText, "Document text should not change"); + }); }); function expectedCompletionItem(snippet: string): vscode.CompletionItem {