Skip to content

Commit 9153c00

Browse files
authored
Add space after /// when continuing doc comment (#1703)
* Add space after `///` when continuing doc comment When continuing a document comment the next line wouldn't insert the `/// ` automatically. When splitting an existing doc comment line the new line would be `///` instead of `/// ` (note the space). Issue: #1702
1 parent ca8458e commit 9153c00

File tree

3 files changed

+218
-160
lines changed

3 files changed

+218
-160
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
- Show revision hash or local/editing keyword in project panel dependency descriptions ([#1667](https://github.com/swiftlang/vscode-swift/pull/1667))
1010
- Show files generated by build plugins under Target in Project Panel ([#1592](https://github.com/swiftlang/vscode-swift/pull/1592))
1111

12+
### Fixed
13+
14+
- Prepend `/// ` when continuing documentation comments on a new line ([#1703](https://github.com/swiftlang/vscode-swift/pull/1703))
15+
- Respect `files.exclude` setting values set to `false` ([#1696](https://github.com/swiftlang/vscode-swift/pull/1696))
16+
1217
## 2.6.2 - 2025-07-02
1318

1419
### Fixed

src/editor/CommentCompletion.ts

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class CommentCompletion extends vscode.CompletionItem {
3939
* CompletionItem Provider that provides "///" on pressing return if previous line
4040
* contained a "///" documentation comment.
4141
*/
42-
class CommentCompletionProvider implements vscode.CompletionItemProvider {
42+
class DocCommentCompletionProvider implements vscode.CompletionItemProvider {
4343
public async provideCompletionItems(
4444
document: vscode.TextDocument,
4545
position: vscode.Position
@@ -48,22 +48,35 @@ class CommentCompletionProvider implements vscode.CompletionItemProvider {
4848
if (position.line === 0 || isLineComment(document, position.line - 1) === false) {
4949
return undefined;
5050
}
51+
await this.continueExistingDocCommentBlock(document, position);
52+
return [new CommentCompletion("/// ", "///", "Documentation comment")];
53+
}
54+
55+
private async continueExistingDocCommentBlock(
56+
document: vscode.TextDocument,
57+
position: vscode.Position
58+
) {
59+
if (!vscode.window.activeTextEditor) {
60+
return;
61+
}
5162
// Fixes https://github.com/swiftlang/vscode-swift/issues/1648
52-
const match = /^(\s*)\/\/\s(.+)/.exec(document.lineAt(position.line).text);
63+
const match = /^(\s*)(\/\/)?\s?(.+)?/.exec(document.lineAt(position.line).text);
5364
if (match) {
54-
void vscode.window.activeTextEditor?.edit(
65+
await vscode.window.activeTextEditor.edit(
5566
edit => {
56-
void edit.replace(
67+
edit.replace(
5768
new vscode.Range(position.line, 0, position.line, match[0].length),
58-
`${match[1]}///${match[2]}`
69+
`${match[1]}/// ${match[3] ?? ""}`
5970
);
6071
},
6172
{ undoStopBefore: false, undoStopAfter: true }
6273
);
63-
return undefined;
74+
const newPosition = new vscode.Position(position.line, match[1].length + 4);
75+
vscode.window.activeTextEditor.selection = new vscode.Selection(
76+
newPosition,
77+
newPosition
78+
);
6479
}
65-
const completion = new CommentCompletion("/// ", "///", "Documentation comment");
66-
return [completion];
6780
}
6881
}
6982

@@ -237,8 +250,9 @@ class FunctionDocumentationCompletionProvider implements vscode.CompletionItemPr
237250
*/
238251
export class CommentCompletionProviders implements vscode.Disposable {
239252
functionCommentCompletion: FunctionDocumentationCompletionProvider;
253+
docCommentCompletion: DocCommentCompletionProvider;
240254
functionCommentCompletionProvider: vscode.Disposable;
241-
commentCompletionProvider: vscode.Disposable;
255+
docCommentCompletionProvider: vscode.Disposable;
242256

243257
constructor() {
244258
this.functionCommentCompletion = new FunctionDocumentationCompletionProvider();
@@ -247,9 +261,10 @@ export class CommentCompletionProviders implements vscode.Disposable {
247261
this.functionCommentCompletion,
248262
"/"
249263
);
250-
this.commentCompletionProvider = vscode.languages.registerCompletionItemProvider(
264+
this.docCommentCompletion = new DocCommentCompletionProvider();
265+
this.docCommentCompletionProvider = vscode.languages.registerCompletionItemProvider(
251266
"swift",
252-
new CommentCompletionProvider(),
267+
this.docCommentCompletion,
253268
"\n"
254269
);
255270
}
@@ -265,6 +280,6 @@ export class CommentCompletionProviders implements vscode.Disposable {
265280

266281
dispose() {
267282
this.functionCommentCompletionProvider.dispose();
268-
this.commentCompletionProvider.dispose();
283+
this.docCommentCompletionProvider.dispose();
269284
}
270285
}

0 commit comments

Comments
 (0)