Skip to content

Commit 075f1d8

Browse files
authored
Make sure newline starts with /// when splitting doc comment (#1651)
* Make sure newline starts with /// when splitting doc comment The existing behaviour where splitting a /// line results in the next line starting with // is a vscode issue. Other examples follow a similar approach as this PR to accomplish the desired behaviour. Issue: #1648 * Fix review comment * Add changelog entry
1 parent 43c2cca commit 075f1d8

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Fixed
66

77
- Cleanup Swift diagnostics when the source file is moved or deleted ([#1653](https://github.com/swiftlang/vscode-swift/pull/1653))
8+
- Make sure newline starts with /// when splitting doc comment ([#1651](https://github.com/swiftlang/vscode-swift/pull/1651))
89

910
## 2.6.0 - 2025-06-26
1011

src/editor/CommentCompletion.ts

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
import * as vscode from "vscode";
1616
import { DocumentParser } from "./DocumentParser";
1717

18+
function isLineComment(document: vscode.TextDocument, line: number): boolean {
19+
// test if line consists of just '///'
20+
return /^\s*\/\/\//.test(document.lineAt(line).text);
21+
}
22+
1823
/** CompletionItem for Swift Comments */
1924
class CommentCompletion extends vscode.CompletionItem {
2025
constructor(
@@ -40,20 +45,26 @@ class CommentCompletionProvider implements vscode.CompletionItemProvider {
4045
position: vscode.Position
4146
): Promise<vscode.CompletionItem[] | undefined> {
4247
// Is line a '///' comment
43-
if (position.line === 0 || this.isLineComment(document, position.line - 1) === false) {
48+
if (position.line === 0 || isLineComment(document, position.line - 1) === false) {
49+
return undefined;
50+
}
51+
// Fixes https://github.com/swiftlang/vscode-swift/issues/1648
52+
const match = /^(\s*)\/\/\s(.+)/.exec(document.lineAt(position.line).text);
53+
if (match) {
54+
void vscode.window.activeTextEditor?.edit(
55+
edit => {
56+
void edit.replace(
57+
new vscode.Range(position.line, 0, position.line, match[0].length),
58+
`${match[1]}///${match[2]}`
59+
);
60+
},
61+
{ undoStopBefore: false, undoStopAfter: true }
62+
);
4463
return undefined;
4564
}
4665
const completion = new CommentCompletion("/// ", "///", "Documentation comment");
4766
return [completion];
4867
}
49-
50-
private isLineComment(document: vscode.TextDocument, line: number): boolean {
51-
// test if line starts with '///'
52-
if (/^\s*\/\/\//.test(document.lineAt(line).text)) {
53-
return true;
54-
}
55-
return false;
56-
}
5768
}
5869

5970
interface FunctionDetails {
@@ -73,7 +84,7 @@ class FunctionDocumentationCompletionProvider implements vscode.CompletionItemPr
7384
position: vscode.Position
7485
): Promise<vscode.CompletionItem[] | undefined> {
7586
// Is line a '///' comment
76-
const isComment = this.isLineComment(document, position.line);
87+
const isComment = isLineComment(document, position.line);
7788
if (isComment === false) {
7889
return undefined;
7990
}
@@ -117,14 +128,6 @@ class FunctionDocumentationCompletionProvider implements vscode.CompletionItemPr
117128
}
118129
}
119130

120-
private isLineComment(document: vscode.TextDocument, line: number): boolean {
121-
// test if line consists of just '///'
122-
if (/^\s*\/\/\/\s*$/.test(document.lineAt(line).text)) {
123-
return true;
124-
}
125-
return false;
126-
}
127-
128131
/**
129132
* Extract function details from line below. Inspiration for this code can be found
130133
* here https://github.com/fappelman/swift-add-documentation

0 commit comments

Comments
 (0)