Use TextMate to toggle comments

This commit is contained in:
Corey Johnson
2012-08-09 10:33:46 -07:00
parent 92d706b52b
commit c95ca04340
2 changed files with 27 additions and 13 deletions

View File

@@ -68,16 +68,16 @@ describe "LanguageMode", ->
describe ".toggleLineCommentsInRange(range)", ->
it "comments/uncomments lines in the given range", ->
languageMode.toggleLineCommentsInRange([[4, 5], [7, 8]])
expect(buffer.lineForRow(4)).toBe "// while(items.length > 0) {"
expect(buffer.lineForRow(5)).toBe "// current = items.shift();"
expect(buffer.lineForRow(6)).toBe "// current < pivot ? left.push(current) : right.push(current);"
expect(buffer.lineForRow(7)).toBe "// }"
expect(buffer.lineForRow(4)).toBe "// while(items.length > 0) {"
expect(buffer.lineForRow(5)).toBe "// current = items.shift();"
expect(buffer.lineForRow(6)).toBe "// current < pivot ? left.push(current) : right.push(current);"
expect(buffer.lineForRow(7)).toBe "// }"
languageMode.toggleLineCommentsInRange([[4, 5], [5, 8]])
expect(buffer.lineForRow(4)).toBe " while(items.length > 0) {"
expect(buffer.lineForRow(5)).toBe " current = items.shift();"
expect(buffer.lineForRow(6)).toBe "// current < pivot ? left.push(current) : right.push(current);"
expect(buffer.lineForRow(7)).toBe "// }"
expect(buffer.lineForRow(6)).toBe "// current < pivot ? left.push(current) : right.push(current);"
expect(buffer.lineForRow(7)).toBe "// }"
describe "fold suggestion", ->
describe ".doesBufferRowStartFold(bufferRow)", ->
@@ -102,16 +102,16 @@ describe "LanguageMode", ->
describe ".toggleLineCommentsInRange(range)", ->
it "comments/uncomments lines in the given range", ->
languageMode.toggleLineCommentsInRange([[4, 5], [7, 8]])
expect(buffer.lineForRow(4)).toBe " #pivot = items.shift()"
expect(buffer.lineForRow(5)).toBe " #left = []"
expect(buffer.lineForRow(6)).toBe " #right = []"
expect(buffer.lineForRow(7)).toBe "#"
expect(buffer.lineForRow(4)).toBe "# pivot = items.shift()"
expect(buffer.lineForRow(5)).toBe "# left = []"
expect(buffer.lineForRow(6)).toBe "# right = []"
expect(buffer.lineForRow(7)).toBe "# "
languageMode.toggleLineCommentsInRange([[4, 5], [5, 8]])
expect(buffer.lineForRow(4)).toBe " pivot = items.shift()"
expect(buffer.lineForRow(5)).toBe " left = []"
expect(buffer.lineForRow(6)).toBe " #right = []"
expect(buffer.lineForRow(7)).toBe "#"
expect(buffer.lineForRow(6)).toBe "# right = []"
expect(buffer.lineForRow(7)).toBe "# "
describe "fold suggestion", ->
describe ".doesBufferRowStartFold(bufferRow)", ->

View File

@@ -2,6 +2,7 @@ AceAdaptor = require 'ace-adaptor'
Range = require 'range'
TextMateBundle = require 'text-mate-bundle'
_ = require 'underscore'
require 'underscore-extensions'
module.exports =
class LanguageMode
@@ -62,7 +63,20 @@ class LanguageMode
toggleLineCommentsInRange: (range) ->
range = Range.fromObject(range)
@aceMode.toggleCommentLines(@tokenizedBuffer.stackForRow(range.start.row), @aceAdaptor, range.start.row, range.end.row)
range = new Range([range.start.row, 0], [range.end.row, Infinity])
scopes = @tokenizedBuffer.scopesForPosition(range.start)
commentString = TextMateBundle.lineCommentStringForScope(scopes[0])
commentSource = "^(\s*)" + _.escapeRegExp(commentString)
text = @editSession.getTextInBufferRange(range)
isCommented = new RegExp(commentSource).test text
if isCommented
text = text.replace(new RegExp(commentSource, "gm"), "$1")
else
text = text.replace(/^/gm, commentString)
@editSession.setTextInBufferRange(range, text)
doesBufferRowStartFold: (bufferRow) ->
return false if @editSession.isBufferRowBlank(bufferRow)