Skip to content

Commit 2757f33

Browse files
committed
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
1 parent 269d6f2 commit 2757f33

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

src/editor/CommentCompletion.ts

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
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+
if (/^\s*\/\/\//.test(document.lineAt(line).text)) {
21+
return true;
22+
}
23+
return false;
24+
}
25+
1826
/** CompletionItem for Swift Comments */
1927
class CommentCompletion extends vscode.CompletionItem {
2028
constructor(
@@ -40,20 +48,26 @@ class CommentCompletionProvider implements vscode.CompletionItemProvider {
4048
position: vscode.Position
4149
): Promise<vscode.CompletionItem[] | undefined> {
4250
// Is line a '///' comment
43-
if (position.line === 0 || this.isLineComment(document, position.line - 1) === false) {
51+
if (position.line === 0 || isLineComment(document, position.line - 1) === false) {
52+
return undefined;
53+
}
54+
// Fixes https://github.com/swiftlang/vscode-swift/issues/1648
55+
const match = /^(\s*)\/\/\s(.+)/.exec(document.lineAt(position.line).text);
56+
if (match) {
57+
void vscode.window.activeTextEditor?.edit(
58+
edit => {
59+
void edit.replace(
60+
new vscode.Range(position.line, 0, position.line, match[0].length),
61+
`${match[1]}///${match[2]}`
62+
);
63+
},
64+
{ undoStopBefore: false, undoStopAfter: true }
65+
);
4466
return undefined;
4567
}
4668
const completion = new CommentCompletion("/// ", "///", "Documentation comment");
4769
return [completion];
4870
}
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-
}
5771
}
5872

5973
interface FunctionDetails {
@@ -73,7 +87,7 @@ class FunctionDocumentationCompletionProvider implements vscode.CompletionItemPr
7387
position: vscode.Position
7488
): Promise<vscode.CompletionItem[] | undefined> {
7589
// Is line a '///' comment
76-
const isComment = this.isLineComment(document, position.line);
90+
const isComment = isLineComment(document, position.line);
7791
if (isComment === false) {
7892
return undefined;
7993
}
@@ -117,14 +131,6 @@ class FunctionDocumentationCompletionProvider implements vscode.CompletionItemPr
117131
}
118132
}
119133

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-
128134
/**
129135
* Extract function details from line below. Inspiration for this code can be found
130136
* here https://github.com/fappelman/swift-add-documentation

0 commit comments

Comments
 (0)