Skip to content

Commit f4396f5

Browse files
authored
Fix comment blocks getting incorrectly completed on fn decls (#1713)
1 parent 5d64b4a commit f4396f5

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/editor/CommentCompletion.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,19 @@ class DocCommentCompletionProvider implements vscode.CompletionItemProvider {
6060
return;
6161
}
6262
// Fixes https://github.com/swiftlang/vscode-swift/issues/1648
63-
const match = /^(\s*)(\/\/)?\s?(.+)?/.exec(document.lineAt(position.line).text);
63+
const lineText = document.lineAt(position.line).text;
64+
// Continue the comment if its a white space only line, or if VS Code has already continued
65+
// the comment by adding a // on the new line.
66+
const match =
67+
lineText.trim().length === 0
68+
? [lineText, lineText, ""]
69+
: /^(\s*)\/\/\s(.+)/.exec(lineText);
6470
if (match) {
6571
await vscode.window.activeTextEditor.edit(
6672
edit => {
6773
edit.replace(
6874
new vscode.Range(position.line, 0, position.line, match[0].length),
69-
`${match[1]}/// ${match[3] ?? ""}`
75+
`${match[1]}/// ${match[2]}`
7076
);
7177
},
7278
{ undoStopBefore: false, undoStopAfter: true }

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,20 @@ suite("CommentCompletion Test Suite", () => {
290290

291291
assert.strictEqual(newLine, "/// bbb", "New line should continue the comment block");
292292
});
293+
294+
test("Should not continue a comment on a line that has content", async () => {
295+
const { document, positions } = await openDocument(`
296+
/// aaa
297+
public func foo(param: Int, a1️⃣) {}`);
298+
299+
const originalText = document.getText();
300+
const position = positions["1️⃣"];
301+
await provider.docCommentCompletion.provideCompletionItems(document, position);
302+
303+
const documentText = document.getText();
304+
305+
assert.deepEqual(documentText, originalText, "Document text should not change");
306+
});
293307
});
294308

295309
function expectedCompletionItem(snippet: string): vscode.CompletionItem {

0 commit comments

Comments
 (0)