Skip to content

Commit 311567e

Browse files
committed
Simplify .toggleLineComments method to avoid using oniguruma
1 parent c266276 commit 311567e

File tree

2 files changed

+56
-40
lines changed

2 files changed

+56
-40
lines changed

spec/tokenized-buffer-spec.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -692,38 +692,38 @@ describe('TokenizedBuffer', () => {
692692

693693
it('comments/uncomments lines in the given range', () => {
694694
tokenizedBuffer.toggleLineCommentsForBufferRows(0, 1)
695-
expect(buffer.lineForRow(0)).toBe('/*body {')
696-
expect(buffer.lineForRow(1)).toBe(' font-size: 1234px;*/')
695+
expect(buffer.lineForRow(0)).toBe('/* body {')
696+
expect(buffer.lineForRow(1)).toBe(' font-size: 1234px; */')
697697
expect(buffer.lineForRow(2)).toBe(' width: 110%;')
698698
expect(buffer.lineForRow(3)).toBe(' font-weight: bold !important;')
699699

700700
tokenizedBuffer.toggleLineCommentsForBufferRows(2, 2)
701-
expect(buffer.lineForRow(0)).toBe('/*body {')
702-
expect(buffer.lineForRow(1)).toBe(' font-size: 1234px;*/')
703-
expect(buffer.lineForRow(2)).toBe(' /*width: 110%;*/')
701+
expect(buffer.lineForRow(0)).toBe('/* body {')
702+
expect(buffer.lineForRow(1)).toBe(' font-size: 1234px; */')
703+
expect(buffer.lineForRow(2)).toBe(' /* width: 110%; */')
704704
expect(buffer.lineForRow(3)).toBe(' font-weight: bold !important;')
705705

706706
tokenizedBuffer.toggleLineCommentsForBufferRows(0, 1)
707707
expect(buffer.lineForRow(0)).toBe('body {')
708708
expect(buffer.lineForRow(1)).toBe(' font-size: 1234px;')
709-
expect(buffer.lineForRow(2)).toBe(' /*width: 110%;*/')
709+
expect(buffer.lineForRow(2)).toBe(' /* width: 110%; */')
710710
expect(buffer.lineForRow(3)).toBe(' font-weight: bold !important;')
711711
})
712712

713713
it('uncomments lines with leading whitespace', () => {
714-
buffer.setTextInRange([[2, 0], [2, Infinity]], ' /*width: 110%;*/')
714+
buffer.setTextInRange([[2, 0], [2, Infinity]], ' /* width: 110%; */')
715715
tokenizedBuffer.toggleLineCommentsForBufferRows(2, 2)
716716
expect(buffer.lineForRow(2)).toBe(' width: 110%;')
717717
})
718718

719719
it('uncomments lines with trailing whitespace', () => {
720-
buffer.setTextInRange([[2, 0], [2, Infinity]], '/*width: 110%;*/ ')
720+
buffer.setTextInRange([[2, 0], [2, Infinity]], '/* width: 110%; */ ')
721721
tokenizedBuffer.toggleLineCommentsForBufferRows(2, 2)
722722
expect(buffer.lineForRow(2)).toBe('width: 110%; ')
723723
})
724724

725725
it('uncomments lines with leading and trailing whitespace', () => {
726-
buffer.setTextInRange([[2, 0], [2, Infinity]], ' /*width: 110%;*/ ')
726+
buffer.setTextInRange([[2, 0], [2, Infinity]], ' /* width: 110%; */ ')
727727
tokenizedBuffer.toggleLineCommentsForBufferRows(2, 2)
728728
expect(buffer.lineForRow(2)).toBe(' width: 110%; ')
729729
})

src/tokenized-buffer.js

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -165,37 +165,32 @@ class TokenizedBuffer {
165165

166166
toggleLineCommentsForBufferRows (start, end) {
167167
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)
171169
if (!commentStartString) return
172-
173-
const commentStartRegexString = _.escapeRegExp(commentStartString).replace(/(\s+)$/, '(?:$1)?')
174-
const commentStartRegex = new OnigRegExp(`^(\\s*)(${commentStartRegexString})`)
170+
commentStartString = commentStartString.trim()
175171

176172
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) {
184184
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]]])
192187
})
193188
}
194189
} else {
195190
this.buffer.transact(() => {
196191
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)
199194
})
200195
}
201196
} else {
@@ -204,7 +199,7 @@ class TokenizedBuffer {
204199
for (let row = start; row <= end; row++) {
205200
const line = this.buffer.lineForRow(row)
206201
if (NON_WHITESPACE_REGEX.test(line)) {
207-
if (commentStartRegex.testSync(line)) {
202+
if (this.columnRangeForStartDelimiter(line, commentStartString)) {
208203
hasCommentedLines = true
209204
} else {
210205
hasUncommentedLines = true
@@ -216,12 +211,11 @@ class TokenizedBuffer {
216211

217212
if (shouldUncomment) {
218213
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]]])
225219
}
226220
} else {
227221
let minIndentLevel = Infinity
@@ -247,18 +241,38 @@ class TokenizedBuffer {
247241
const line = this.buffer.lineForRow(row)
248242
if (NON_WHITESPACE_REGEX.test(line)) {
249243
const indentColumn = this.columnForIndentLevel(line, minIndentLevel)
250-
this.buffer.insert(Point(row, indentColumn), commentStartString)
244+
this.buffer.insert(Point(row, indentColumn), commentStartString + ' ')
251245
} else {
252246
this.buffer.setTextInRange(
253247
new Range(new Point(row, 0), new Point(row, Infinity)),
254-
indentString + commentStartString
248+
indentString + commentStartString + ' '
255249
)
256250
}
257251
}
258252
}
259253
}
260254
}
261255

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+
262276
buildIterator () {
263277
return new TokenizedBufferIterator(this)
264278
}
@@ -844,6 +858,8 @@ class TokenizedBuffer {
844858
commentStringsForScopeDescriptor (scopes) {
845859
if (this.scopedSettingsDelegate) {
846860
return this.scopedSettingsDelegate.getCommentStrings(scopes)
861+
} else {
862+
return {}
847863
}
848864
}
849865

0 commit comments

Comments
 (0)