@@ -32,9 +32,33 @@ export function deactivate() { }
32
32
33
33
type RegExpMatchArrayWithIndex = RegExpMatchArray & { index : number } ;
34
34
35
- // these two variables gonna fill later
35
+ // these variables will fill later
36
36
let autoSemicolonFormatsIncluded = true ;
37
37
let autoMoveFormatsIncluded = false ;
38
+ let commentDelimiter = '//' ;
39
+
40
+
41
+ type StringDictionary = {
42
+ [ key : string ] : string ;
43
+ } ;
44
+
45
+ const languagesDelimiter :StringDictionary = {
46
+ "dotenv" : '#' ,
47
+ "dockerfile" : '#' ,
48
+ "snippets" : '#' ,
49
+ "python" : '#' ,
50
+ "ruby" : '#' ,
51
+ "perl" : '#' ,
52
+ "bash" : '#' ,
53
+ "shellscript" : '#' ,
54
+ "r" : '#' ,
55
+ "julia" : '#' ,
56
+ "coffeescript" : '#' ,
57
+ "ini" : ';' ,
58
+ "bat" : ':' ,
59
+ "sql" : '%' ,
60
+ "tex" : '%' ,
61
+ } ;
38
62
39
63
function getConfig ( ) {
40
64
return vscode . workspace . getConfiguration ( 'autoSemicolon' ) ;
@@ -48,8 +72,18 @@ function isForStatementIgnored() {
48
72
return getConfig ( ) . supportedLanguageId . ignores . theForStatement ;
49
73
}
50
74
51
- function isUnallowdEndsIncluded ( lastCharacter : string ) {
75
+ function isUnallowdEndsIncluded ( lineText : string ) {
76
+ let pos = lineText . lastIndexOf ( commentDelimiter ) ;
77
+
78
+ // it it commented
79
+ if ( pos >= 0 ) {
80
+ lineText = lineText . substring ( 0 , pos ) ;
81
+ }
82
+
83
+ let lastCharacter = lineText [ lineText . length - 1 ] ;
84
+
52
85
let unallowedEnds = getConfig ( ) . autoInsertSemicolon . unallowedEnds . split ( "," ) ;
86
+
53
87
if ( ! Array . isArray ( unallowedEnds ) )
54
88
return false ;
55
89
@@ -110,6 +144,7 @@ function autoSemicolonCommand(editor: vscode.TextEditor, textEdit: vscode.TextEd
110
144
111
145
autoSemicolonFormatsIncluded = isAutoSemicolonLanguageIdIncluded ( languageId ) ;
112
146
autoMoveFormatsIncluded = isAutoMoveLanguageIdIncluded ( languageId ) ;
147
+ commentDelimiter = getCommentDelimiter ( languageId ) ;
113
148
114
149
position = newPosition ( line , selection . active . character + 1 ) ;
115
150
@@ -139,8 +174,8 @@ function autoSemicolonCommand(editor: vscode.TextEditor, textEdit: vscode.TextEd
139
174
let length = line . range . end . character + 1 ;
140
175
if ( isBetweenTags ( '{' , '}' , lineText , currentPos ) ) {
141
176
length = putSemicolonBefore ( '}' , textEdit , selection , line ) ;
142
- } else if ( ! isUnallowdEndsIncluded ( lineText [ lineText . length - 1 ] ) || currentPos === line . text . length ) {
143
- length = putSemicolonBefore ( '//' , textEdit , selection , line ) + 1 ;
177
+ } else if ( ! isUnallowdEndsIncluded ( lineText ) || currentPos === line . text . length ) {
178
+ length = autoSemicolonBeforeComment ( textEdit , selection , line ) + 1 ;
144
179
}
145
180
146
181
position = newPosition ( line , length ) ;
@@ -165,12 +200,12 @@ function autoSemicolonCommand(editor: vscode.TextEditor, textEdit: vscode.TextEd
165
200
} ) ;
166
201
}
167
202
168
- function putSemicolonAfterPos ( position : number , textEdit : vscode . TextEditorEdit , selection : vscode . Selection , line : vscode . TextLine , justMove : boolean = false ) : number {
203
+ function putSemicolonAfterPos ( position : number , textEdit : vscode . TextEditorEdit , selection : vscode . Selection , line : vscode . TextLine , justMove : boolean = false ) : number {
169
204
position = position >= 0 ? position : 0 ;
170
- return putSemicolonBefore ( '//' , textEdit , selection , line , justMove ) + 1 ;
205
+ return autoSemicolonBeforeComment ( textEdit , selection , line , justMove ) + 1 ;
171
206
}
172
207
173
- function putSemicolonBefore ( tag : string , textEdit : vscode . TextEditorEdit , selection : vscode . Selection , line : vscode . TextLine , justMove : boolean = false ) : number {
208
+ function putSemicolonBefore ( tag : string , textEdit : vscode . TextEditorEdit , selection : vscode . Selection , line : vscode . TextLine , justMove : boolean = false ) : number {
174
209
let lineText = line . text . trimEnd ( ) ;
175
210
const currentPos = selection . active . character ;
176
211
const posClose = lineText . indexOf ( tag , currentPos ) ;
@@ -180,7 +215,7 @@ function putSemicolonBefore(tag: string, textEdit: vscode.TextEditorEdit, select
180
215
const lineTextTrimmed = lineText . substring ( 0 , posClose ) . trimEnd ( ) ;
181
216
length = lineTextTrimmed . length ;
182
217
183
- if ( ! isUnallowdEndsIncluded ( lineTextTrimmed [ lineTextTrimmed . length - 1 ] ) || currentPos === line . text . length ) {
218
+ if ( ! isUnallowdEndsIncluded ( lineTextTrimmed ) || currentPos === line . text . length ) {
184
219
lineText = lineText . replace ( lineTextTrimmed , lineTextTrimmed + ';' ) ;
185
220
length += 1 ;
186
221
textEdit . delete ( new vscode . Selection ( newPosition ( line , 0 ) , newPosition ( line , line . text . length ) ) ) ;
@@ -196,6 +231,32 @@ function putSemicolonBefore(tag: string, textEdit: vscode.TextEditorEdit, select
196
231
return length ;
197
232
}
198
233
234
+ function autoSemicolonBeforeComment ( textEdit : vscode . TextEditorEdit , selection : vscode . Selection , line : vscode . TextLine , justMove : boolean = false ) : number {
235
+ let lineText = line . text . trimEnd ( ) ;
236
+ const currentPos = selection . active . character ;
237
+ const posClose = lineText . indexOf ( commentDelimiter , currentPos ) ;
238
+ let length = lineText . length ;
239
+
240
+ if ( posClose >= 0 ) {
241
+ const lineTextTrimmed = lineText . substring ( 0 , posClose ) . trimEnd ( ) ;
242
+ length = lineTextTrimmed . length ;
243
+
244
+ if ( ! isUnallowdEndsIncluded ( lineTextTrimmed ) || currentPos === length ) {
245
+ lineText = lineText . replace ( lineTextTrimmed , lineTextTrimmed + ';' ) ;
246
+ length += 1 ;
247
+ textEdit . delete ( new vscode . Selection ( newPosition ( line , 0 ) , newPosition ( line , line . text . length ) ) ) ;
248
+ textEdit . insert ( newPosition ( line , 0 ) , lineText ) ;
249
+ }
250
+
251
+ return length - commentDelimiter . length + 1 ;
252
+ }
253
+
254
+ if ( ! justMove ) {
255
+ textEdit . insert ( newPosition ( line , length ) , ';' ) ;
256
+ }
257
+ return length ;
258
+ }
259
+
199
260
function newPosition ( line : vscode . TextLine , position : number ) : vscode . Position {
200
261
return new vscode . Position ( line . lineNumber , position ) ;
201
262
}
@@ -210,17 +271,31 @@ function isBetweenTags(open: string, close: string, lineText: string, currentPos
210
271
function isBetweenTagsB ( open : string , close : string , lineText : string , currentPos : number ) : boolean {
211
272
const posOpen = lineText . lastIndexOf ( open , currentPos ) ;
212
273
const posClose = lineText . indexOf ( close , currentPos ) ;
213
-
274
+
214
275
return (
215
276
posOpen >= 0 && // open tag exists before current position
216
277
posOpen < currentPos && // open tag is before current position
217
278
posClose >= currentPos // close tag is after current position
218
279
) ;
219
280
}
220
281
282
+ function getCommentDelimiter ( languageId : string | undefined ) : string {
283
+ let delimiterDefault = '//' ;
284
+
285
+ if ( ! languageId )
286
+ return delimiterDefault ;
287
+
288
+ for ( const key in languagesDelimiter ) {
289
+ if ( key == languageId ) {
290
+ return languagesDelimiter [ key ] ;
291
+ }
292
+ }
293
+
294
+ return delimiterDefault ;
295
+ }
296
+
221
297
function isCommented ( lineText : string , currentPos : number ) : boolean {
222
- const pos = lineText . lastIndexOf ( '//' , currentPos ) ;
223
- return pos >= 0 ;
298
+ return lineText . lastIndexOf ( commentDelimiter , currentPos ) >= 0 ;
224
299
}
225
300
226
301
function findTheForStatement ( lineText : string , currentPos : number ) : string [ ] | null {
0 commit comments