1515import * as vscode from "vscode" ;
1616import { 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 */
1927class 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
5973interface 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