Support bundles that have a TM_COMMENT_END variable

The value of TM_COMMENT_END is now considered if present
in the bundle's preferences when commenting and uncommenting
selected lines
This commit is contained in:
Kevin Sawicki 2012-12-18 15:01:29 -08:00
parent 8e4142aa8e
commit 1b8e816d95
4 changed files with 65 additions and 14 deletions

View File

@ -250,3 +250,28 @@ describe "LanguageMode", ->
expect(languageMode.rowRangeForFoldAtBufferRow(1)).toEqual [1, 17]
expect(languageMode.rowRangeForFoldAtBufferRow(2)).toBeNull()
expect(languageMode.rowRangeForFoldAtBufferRow(19)).toEqual [19, 20]
describe "css", ->
beforeEach ->
editSession = fixturesProject.buildEditSessionForPath('css.css', autoIndent: false)
{ buffer, languageMode } = editSession
describe ".toggleLineCommentsForBufferRows(start, end)", ->
it "comments/uncomments lines in the given range", ->
languageMode.toggleLineCommentsForBufferRows(0, 1)
expect(buffer.lineForRow(0)).toBe "/*body {"
expect(buffer.lineForRow(1)).toBe " font-size: 1234px;*/"
expect(buffer.lineForRow(2)).toBe " width: 110%;"
expect(buffer.lineForRow(3)).toBe "}"
languageMode.toggleLineCommentsForBufferRows(2, 2)
expect(buffer.lineForRow(0)).toBe "/*body {"
expect(buffer.lineForRow(1)).toBe " font-size: 1234px;*/"
expect(buffer.lineForRow(2)).toBe "/* width: 110%;*/"
expect(buffer.lineForRow(3)).toBe "}"
languageMode.toggleLineCommentsForBufferRows(0, 1)
expect(buffer.lineForRow(0)).toBe "body {"
expect(buffer.lineForRow(1)).toBe " font-size: 1234px;"
expect(buffer.lineForRow(2)).toBe "/* width: 110%;*/"
expect(buffer.lineForRow(3)).toBe "}"

4
spec/fixtures/css.css vendored Normal file
View File

@ -0,0 +1,4 @@
body {
font-size: 1234px;
width: 110%;
}

View File

@ -67,21 +67,37 @@ class LanguageMode
toggleLineCommentsForBufferRows: (start, end) ->
scopes = @editSession.scopesForBufferPosition([start, 0])
return unless commentString = TextMateBundle.lineCommentStringForScope(scopes[0])
return unless commentStartString = TextMateBundle.lineCommentStartStringForScope(scopes[0])
commentRegexString = _.escapeRegExp(commentString)
commentRegexString = commentRegexString.replace(/(\s+)$/, '($1)?')
commentRegex = new OnigRegExp("^\s*#{commentRegexString}")
buffer = @editSession.buffer
commentStartRegexString = _.escapeRegExp(commentStartString).replace(/(\s+)$/, '($1)?')
commentStartRegex = new OnigRegExp("^\s*#{commentStartRegexString}")
shouldUncomment = commentStartRegex.test(buffer.lineForRow(start))
shouldUncomment = commentRegex.test(@editSession.lineForBufferRow(start))
for row in [start..end]
line = @editSession.lineForBufferRow(row)
if commentEndString = TextMateBundle.lineCommentEndStringForScope(scopes[0])
if shouldUncomment
if match = commentRegex.search(line)
@editSession.buffer.change([[row, 0], [row, match[0].length]], "")
commentEndRegexString = _.escapeRegExp(commentEndString).replace(/^(\s+)/, '($1)?')
commentEndRegex = new OnigRegExp("#{commentEndRegexString}\s*$")
startMatch = commentStartRegex.search(buffer.lineForRow(start))
endMatch = commentEndRegex.search(buffer.lineForRow(end))
if startMatch and endMatch
buffer.transact ->
buffer.change([[start, 0], [start, startMatch[0].length]], "")
endLength = buffer.lineLengthForRow(end)
endColumn = endLength - endMatch[0].length
buffer.change([[end, endColumn], [end, endLength]], "")
else
@editSession.buffer.insert([row, 0], commentString)
buffer.transact ->
buffer.insert([start, 0], commentStartString)
buffer.insert([end, buffer.lineLengthForRow(end)], commentEndString)
else
if shouldUncomment
for row in [start..end]
if match = commentStartRegex.search(buffer.lineForRow(row))
buffer.change([[row, 0], [row, match[0].length]], "")
else
for row in [start..end]
buffer.insert([row, 0], commentStartString)
doesBufferRowStartFold: (bufferRow) ->
return false if @editSession.isBufferRowBlank(bufferRow)

View File

@ -56,9 +56,15 @@ class TextMateBundle
@getPreferenceInScope: (scopeSelector, preferenceName) ->
@preferencesByScopeSelector[scopeSelector]?[preferenceName]
@lineCommentStringForScope: (scope) ->
shellVariables = @getPreferenceInScope(scope, 'shellVariables')
(_.find shellVariables, ({name}) -> name == "TM_COMMENT_START")?['value']
@getPreferenceValueInScope: (scope, preferenceName, valueName) ->
values = @getPreferenceInScope(scope, preferenceName)
(_.find values, ({name}) -> name is valueName)?['value']
@lineCommentStartStringForScope: (scope) ->
@getPreferenceValueInScope(scope, 'shellVariables', 'TM_COMMENT_START')
@lineCommentEndStringForScope: (scope) ->
@getPreferenceValueInScope(scope, 'shellVariables', 'TM_COMMENT_END')
@indentRegexForScope: (scope) ->
if source = @getPreferenceInScope(scope, 'increaseIndentPattern')