Skip to content

Add space after /// when continuing doc comment #1703

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 2 commits into from
Jul 8, 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
- Show revision hash or local/editing keyword in project panel dependency descriptions ([#1667](https://github.com/swiftlang/vscode-swift/pull/1667))
- Show files generated by build plugins under Target in Project Panel ([#1592](https://github.com/swiftlang/vscode-swift/pull/1592))

### Fixed

- Prepend `/// ` when continuing documentation comments on a new line ([#1703](https://github.com/swiftlang/vscode-swift/pull/1703))
- Respect `files.exclude` setting values set to `false` ([#1696](https://github.com/swiftlang/vscode-swift/pull/1696))

## 2.6.2 - 2025-07-02

### Fixed
Expand Down
39 changes: 27 additions & 12 deletions src/editor/CommentCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class CommentCompletion extends vscode.CompletionItem {
* CompletionItem Provider that provides "///" on pressing return if previous line
* contained a "///" documentation comment.
*/
class CommentCompletionProvider implements vscode.CompletionItemProvider {
class DocCommentCompletionProvider implements vscode.CompletionItemProvider {
public async provideCompletionItems(
document: vscode.TextDocument,
position: vscode.Position
Expand All @@ -48,22 +48,35 @@ class CommentCompletionProvider implements vscode.CompletionItemProvider {
if (position.line === 0 || isLineComment(document, position.line - 1) === false) {
return undefined;
}
await this.continueExistingDocCommentBlock(document, position);
return [new CommentCompletion("/// ", "///", "Documentation comment")];
}

private async continueExistingDocCommentBlock(
document: vscode.TextDocument,
position: vscode.Position
) {
if (!vscode.window.activeTextEditor) {
return;
}
// Fixes https://github.com/swiftlang/vscode-swift/issues/1648
const match = /^(\s*)\/\/\s(.+)/.exec(document.lineAt(position.line).text);
const match = /^(\s*)(\/\/)?\s?(.+)?/.exec(document.lineAt(position.line).text);
if (match) {
void vscode.window.activeTextEditor?.edit(
await vscode.window.activeTextEditor.edit(
edit => {
void edit.replace(
edit.replace(
new vscode.Range(position.line, 0, position.line, match[0].length),
`${match[1]}///${match[2]}`
`${match[1]}/// ${match[3] ?? ""}`
);
},
{ undoStopBefore: false, undoStopAfter: true }
);
return undefined;
const newPosition = new vscode.Position(position.line, match[1].length + 4);
vscode.window.activeTextEditor.selection = new vscode.Selection(
newPosition,
newPosition
);
}
const completion = new CommentCompletion("/// ", "///", "Documentation comment");
return [completion];
}
}

Expand Down Expand Up @@ -237,8 +250,9 @@ class FunctionDocumentationCompletionProvider implements vscode.CompletionItemPr
*/
export class CommentCompletionProviders implements vscode.Disposable {
functionCommentCompletion: FunctionDocumentationCompletionProvider;
docCommentCompletion: DocCommentCompletionProvider;
functionCommentCompletionProvider: vscode.Disposable;
commentCompletionProvider: vscode.Disposable;
docCommentCompletionProvider: vscode.Disposable;

constructor() {
this.functionCommentCompletion = new FunctionDocumentationCompletionProvider();
Expand All @@ -247,9 +261,10 @@ export class CommentCompletionProviders implements vscode.Disposable {
this.functionCommentCompletion,
"/"
);
this.commentCompletionProvider = vscode.languages.registerCompletionItemProvider(
this.docCommentCompletion = new DocCommentCompletionProvider();
this.docCommentCompletionProvider = vscode.languages.registerCompletionItemProvider(
"swift",
new CommentCompletionProvider(),
this.docCommentCompletion,
"\n"
);
}
Expand All @@ -265,6 +280,6 @@ export class CommentCompletionProviders implements vscode.Disposable {

dispose() {
this.functionCommentCompletionProvider.dispose();
this.commentCompletionProvider.dispose();
this.docCommentCompletionProvider.dispose();
}
}
Loading