Skip to content

Fix comment blocks getting incorrectly completed on fn decls #1713

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 1 commit into from
Jul 10, 2025
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
10 changes: 8 additions & 2 deletions src/editor/CommentCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
14 changes: 14 additions & 0 deletions test/integration-tests/editor/CommentCompletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down