@@ -165,37 +165,32 @@ class TokenizedBuffer {
165
165
166
166
toggleLineCommentsForBufferRows ( start , end ) {
167
167
const scope = this . scopeDescriptorForPosition ( [ start , 0 ] )
168
- const commentStrings = this . commentStringsForScopeDescriptor ( scope )
169
- if ( ! commentStrings ) return
170
- const { commentStartString, commentEndString} = commentStrings
168
+ let { commentStartString, commentEndString} = this . commentStringsForScopeDescriptor ( scope )
171
169
if ( ! commentStartString ) return
172
-
173
- const commentStartRegexString = _ . escapeRegExp ( commentStartString ) . replace ( / ( \s + ) $ / , '(?:$1)?' )
174
- const commentStartRegex = new OnigRegExp ( `^(\\s*)(${ commentStartRegexString } )` )
170
+ commentStartString = commentStartString . trim ( )
175
171
176
172
if ( commentEndString ) {
177
- const shouldUncomment = commentStartRegex . testSync ( this . buffer . lineForRow ( start ) )
178
- if ( shouldUncomment ) {
179
- const commentEndRegexString = _ . escapeRegExp ( commentEndString ) . replace ( / ^ ( \s + ) / , '(?:$1)?' )
180
- const commentEndRegex = new OnigRegExp ( `(${ commentEndRegexString } )(\\s*)$` )
181
- const startMatch = commentStartRegex . searchSync ( this . buffer . lineForRow ( start ) )
182
- const endMatch = commentEndRegex . searchSync ( this . buffer . lineForRow ( end ) )
183
- if ( startMatch && endMatch ) {
173
+ commentEndString = commentEndString . trim ( )
174
+ const startDelimiterColumnRange = this . columnRangeForStartDelimiter (
175
+ this . buffer . lineForRow ( start ) ,
176
+ commentStartString
177
+ )
178
+ if ( startDelimiterColumnRange ) {
179
+ const endDelimiterColumnRange = this . columnRangeForEndDelimiter (
180
+ this . buffer . lineForRow ( end ) ,
181
+ commentEndString
182
+ )
183
+ if ( endDelimiterColumnRange ) {
184
184
this . buffer . transact ( ( ) => {
185
- const columnStart = startMatch [ 1 ] . length
186
- const columnEnd = columnStart + startMatch [ 2 ] . length
187
- this . buffer . setTextInRange ( [ [ start , columnStart ] , [ start , columnEnd ] ] , '' )
188
-
189
- const endLength = this . buffer . lineLengthForRow ( end ) - endMatch [ 2 ] . length
190
- const endColumn = endLength - endMatch [ 1 ] . length
191
- return this . buffer . setTextInRange ( [ [ end , endColumn ] , [ end , endLength ] ] , '' )
185
+ this . buffer . delete ( [ [ end , endDelimiterColumnRange [ 0 ] ] , [ end , endDelimiterColumnRange [ 1 ] ] ] )
186
+ this . buffer . delete ( [ [ start , startDelimiterColumnRange [ 0 ] ] , [ start , startDelimiterColumnRange [ 1 ] ] ] )
192
187
} )
193
188
}
194
189
} else {
195
190
this . buffer . transact ( ( ) => {
196
191
const indentLength = this . buffer . lineForRow ( start ) . match ( / ^ \s * / ) [ 0 ] . length
197
- this . buffer . insert ( [ start , indentLength ] , commentStartString )
198
- this . buffer . insert ( [ end , this . buffer . lineLengthForRow ( end ) ] , commentEndString )
192
+ this . buffer . insert ( [ start , indentLength ] , commentStartString + ' ' )
193
+ this . buffer . insert ( [ end , this . buffer . lineLengthForRow ( end ) ] , ' ' + commentEndString )
199
194
} )
200
195
}
201
196
} else {
@@ -204,7 +199,7 @@ class TokenizedBuffer {
204
199
for ( let row = start ; row <= end ; row ++ ) {
205
200
const line = this . buffer . lineForRow ( row )
206
201
if ( NON_WHITESPACE_REGEX . test ( line ) ) {
207
- if ( commentStartRegex . testSync ( line ) ) {
202
+ if ( this . columnRangeForStartDelimiter ( line , commentStartString ) ) {
208
203
hasCommentedLines = true
209
204
} else {
210
205
hasUncommentedLines = true
@@ -216,12 +211,11 @@ class TokenizedBuffer {
216
211
217
212
if ( shouldUncomment ) {
218
213
for ( let row = start ; row <= end ; row ++ ) {
219
- const match = commentStartRegex . searchSync ( this . buffer . lineForRow ( row ) )
220
- if ( match ) {
221
- const columnStart = match [ 1 ] . length
222
- const columnEnd = columnStart + match [ 2 ] . length
223
- this . buffer . setTextInRange ( [ [ row , columnStart ] , [ row , columnEnd ] ] , '' )
224
- }
214
+ const columnRange = this . columnRangeForStartDelimiter (
215
+ this . buffer . lineForRow ( row ) ,
216
+ commentStartString
217
+ )
218
+ if ( columnRange ) this . buffer . delete ( [ [ row , columnRange [ 0 ] ] , [ row , columnRange [ 1 ] ] ] )
225
219
}
226
220
} else {
227
221
let minIndentLevel = Infinity
@@ -247,18 +241,38 @@ class TokenizedBuffer {
247
241
const line = this . buffer . lineForRow ( row )
248
242
if ( NON_WHITESPACE_REGEX . test ( line ) ) {
249
243
const indentColumn = this . columnForIndentLevel ( line , minIndentLevel )
250
- this . buffer . insert ( Point ( row , indentColumn ) , commentStartString )
244
+ this . buffer . insert ( Point ( row , indentColumn ) , commentStartString + ' ' )
251
245
} else {
252
246
this . buffer . setTextInRange (
253
247
new Range ( new Point ( row , 0 ) , new Point ( row , Infinity ) ) ,
254
- indentString + commentStartString
248
+ indentString + commentStartString + ' '
255
249
)
256
250
}
257
251
}
258
252
}
259
253
}
260
254
}
261
255
256
+ columnRangeForStartDelimiter ( line , delimiter ) {
257
+ const startColumn = line . search ( NON_WHITESPACE_REGEX )
258
+ if ( startColumn === - 1 ) return null
259
+ if ( ! line . startsWith ( delimiter , startColumn ) ) return null
260
+
261
+ let endColumn = startColumn + delimiter . length
262
+ if ( line [ endColumn ] === ' ' ) endColumn ++
263
+ return [ startColumn , endColumn ]
264
+ }
265
+
266
+ columnRangeForEndDelimiter ( line , delimiter ) {
267
+ let startColumn = line . lastIndexOf ( delimiter )
268
+ if ( startColumn === - 1 ) return null
269
+
270
+ const endColumn = startColumn + delimiter . length
271
+ if ( NON_WHITESPACE_REGEX . test ( line . slice ( endColumn ) ) ) return null
272
+ if ( line [ startColumn - 1 ] === ' ' ) startColumn --
273
+ return [ startColumn , endColumn ]
274
+ }
275
+
262
276
buildIterator ( ) {
263
277
return new TokenizedBufferIterator ( this )
264
278
}
@@ -844,6 +858,8 @@ class TokenizedBuffer {
844
858
commentStringsForScopeDescriptor ( scopes ) {
845
859
if ( this . scopedSettingsDelegate ) {
846
860
return this . scopedSettingsDelegate . getCommentStrings ( scopes )
861
+ } else {
862
+ return { }
847
863
}
848
864
}
849
865
0 commit comments