15
15
import * as vscode from "vscode" ;
16
16
import { DocumentParser } from "./DocumentParser" ;
17
17
18
+ function isLineComment ( document : vscode . TextDocument , line : number ) : boolean {
19
+ // test if line consists of just '///'
20
+ return / ^ \s * \/ \/ \/ / . test ( document . lineAt ( line ) . text ) ;
21
+ }
22
+
18
23
/** CompletionItem for Swift Comments */
19
24
class CommentCompletion extends vscode . CompletionItem {
20
25
constructor (
@@ -40,20 +45,26 @@ class CommentCompletionProvider implements vscode.CompletionItemProvider {
40
45
position : vscode . Position
41
46
) : Promise < vscode . CompletionItem [ ] | undefined > {
42
47
// Is line a '///' comment
43
- if ( position . line === 0 || this . isLineComment ( document , position . line - 1 ) === false ) {
48
+ if ( position . line === 0 || isLineComment ( document , position . line - 1 ) === false ) {
49
+ return undefined ;
50
+ }
51
+ // Fixes https://github.com/swiftlang/vscode-swift/issues/1648
52
+ const match = / ^ ( \s * ) \/ \/ \s ( .+ ) / . exec ( document . lineAt ( position . line ) . text ) ;
53
+ if ( match ) {
54
+ void vscode . window . activeTextEditor ?. edit (
55
+ edit => {
56
+ void edit . replace (
57
+ new vscode . Range ( position . line , 0 , position . line , match [ 0 ] . length ) ,
58
+ `${ match [ 1 ] } ///${ match [ 2 ] } `
59
+ ) ;
60
+ } ,
61
+ { undoStopBefore : false , undoStopAfter : true }
62
+ ) ;
44
63
return undefined ;
45
64
}
46
65
const completion = new CommentCompletion ( "/// " , "///" , "Documentation comment" ) ;
47
66
return [ completion ] ;
48
67
}
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
- }
57
68
}
58
69
59
70
interface FunctionDetails {
@@ -73,7 +84,7 @@ class FunctionDocumentationCompletionProvider implements vscode.CompletionItemPr
73
84
position : vscode . Position
74
85
) : Promise < vscode . CompletionItem [ ] | undefined > {
75
86
// Is line a '///' comment
76
- const isComment = this . isLineComment ( document , position . line ) ;
87
+ const isComment = isLineComment ( document , position . line ) ;
77
88
if ( isComment === false ) {
78
89
return undefined ;
79
90
}
@@ -117,14 +128,6 @@ class FunctionDocumentationCompletionProvider implements vscode.CompletionItemPr
117
128
}
118
129
}
119
130
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
-
128
131
/**
129
132
* Extract function details from line below. Inspiration for this code can be found
130
133
* here https://github.com/fappelman/swift-add-documentation
0 commit comments