Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Fixed

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

## 2.6.0 - 2025-06-26

Expand Down
39 changes: 21 additions & 18 deletions src/editor/CommentCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
import * as vscode from "vscode";
import { DocumentParser } from "./DocumentParser";

function isLineComment(document: vscode.TextDocument, line: number): boolean {
// test if line consists of just '///'
return /^\s*\/\/\//.test(document.lineAt(line).text);
}

/** CompletionItem for Swift Comments */
class CommentCompletion extends vscode.CompletionItem {
constructor(
Expand All @@ -40,20 +45,26 @@ class CommentCompletionProvider implements vscode.CompletionItemProvider {
position: vscode.Position
): Promise<vscode.CompletionItem[] | undefined> {
// Is line a '///' comment
if (position.line === 0 || this.isLineComment(document, position.line - 1) === false) {
if (position.line === 0 || isLineComment(document, position.line - 1) === false) {
return undefined;
}
// Fixes https://github.com/swiftlang/vscode-swift/issues/1648
const match = /^(\s*)\/\/\s(.+)/.exec(document.lineAt(position.line).text);
if (match) {
void vscode.window.activeTextEditor?.edit(
edit => {
void edit.replace(
new vscode.Range(position.line, 0, position.line, match[0].length),
`${match[1]}///${match[2]}`
);
},
{ undoStopBefore: false, undoStopAfter: true }
);
return undefined;
}
const completion = new CommentCompletion("/// ", "///", "Documentation comment");
return [completion];
}

private isLineComment(document: vscode.TextDocument, line: number): boolean {
// test if line starts with '///'
if (/^\s*\/\/\//.test(document.lineAt(line).text)) {
return true;
}
return false;
}
}

interface FunctionDetails {
Expand All @@ -73,7 +84,7 @@ class FunctionDocumentationCompletionProvider implements vscode.CompletionItemPr
position: vscode.Position
): Promise<vscode.CompletionItem[] | undefined> {
// Is line a '///' comment
const isComment = this.isLineComment(document, position.line);
const isComment = isLineComment(document, position.line);
if (isComment === false) {
return undefined;
}
Expand Down Expand Up @@ -117,14 +128,6 @@ class FunctionDocumentationCompletionProvider implements vscode.CompletionItemPr
}
}

private isLineComment(document: vscode.TextDocument, line: number): boolean {
// test if line consists of just '///'
if (/^\s*\/\/\/\s*$/.test(document.lineAt(line).text)) {
return true;
}
return false;
}

/**
* Extract function details from line below. Inspiration for this code can be found
* here https://github.com/fappelman/swift-add-documentation
Expand Down
Loading