Meta-/ comments out selected lines

This commit is contained in:
Nathan Sobo
2012-06-01 19:38:33 -06:00
parent 9219a1f30b
commit 14e399fdf0
6 changed files with 41 additions and 3 deletions

View File

@@ -274,6 +274,24 @@ describe "Selection", ->
expect(editor.buffer.lineForRow(2)).toBe " if (items.length <= 1) return items;"
expect(selection.getBufferRange()).toEqual [[0, 1], [3, 15 - tabLength]]
describe ".toggleLineComments()", ->
it "toggles comments on the selected lines", ->
selection.setBufferRange([[4, 5], [7, 5]])
selection.toggleLineComments()
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(selection.getBufferRange()).toEqual [[4, 7], [7, 7]]
selection.toggleLineComments()
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 " }"
describe "when the selection ends on the begining of a fold line", ->
beforeEach ->
editor.createFold(2,4)

View File

@@ -113,6 +113,9 @@ class CompositeSeleciton
deleteToEndOfWord: ->
@mutateSelectedText (selection) -> selection.deleteToEndOfWord()
toggleLineComments: ->
@mutateSelectedText (selection) -> selection.toggleLineComments()
selectToScreenPosition: (position) ->
@getLastSelection().selectToScreenPosition(position)

View File

@@ -38,7 +38,6 @@ class Editor extends View
cursor: null
selection: null
buffer: null
highlighter: null
renderer: null
autoIndent: null
lineCache: null
@@ -146,6 +145,7 @@ class Editor extends View
'select-to-end-of-word': @selectToEndOfWord
'select-to-beginning-of-word': @selectToBeginningOfWord
'select-all': @selectAll
'toggle-line-comments': @toggleLineCommentsInSelection
for name, method of editorBindings
do (name, method) =>
@@ -820,6 +820,12 @@ class Editor extends View
logLines: (start, end) ->
@renderer.logLines(start, end)
toggleLineCommentsInSelection: ->
@compositeSelection.toggleLineComments()
toggleLineCommentsInRange: (range) ->
@renderer.toggleLineCommentsInRange(range)
logRenderedLines: ->
@visibleLines.find('.line').each (n) ->
console.log n, $(this).text()

View File

@@ -30,3 +30,4 @@ window.keymap.bindKeys '.editor',
'meta-}': 'show-next-buffer'
'meta-+': 'increase-font-size'
'meta--': 'decrease-font-size'
'meta-/': 'toggle-line-comments'

View File

@@ -8,7 +8,7 @@ Range = require 'range'
Fold = require 'fold'
ScreenLine = require 'screen-line'
Token = require 'token'
foldPlaceholderLength = 3
LineCommenter = require 'line-commenter'
module.exports =
class Renderer
@@ -16,13 +16,14 @@ class Renderer
lineMap: null
highlighter: null
activeFolds: null
lineCommenter: null
foldsById: null
lastHighlighterChangeEvent: null
foldPlaceholderLength: 3
constructor: (@buffer, options={}) ->
@id = @constructor.idCounter++
@highlighter = new Highlighter(@buffer, options.tabText ? ' ')
@lineCommenter = new LineCommenter(@highlighter)
@foldSuggester = new FoldSuggester(@highlighter)
@maxLineLength = options.maxLineLength ? Infinity
@activeFolds = {}
@@ -246,4 +247,8 @@ class Renderer
logLines: (start, end) ->
@lineMap.logLines(start, end)
toggleLineCommentsInRange: (range) ->
console.log range.inspect()
@lineCommenter.toggleLineCommentsInRange(range)
_.extend Renderer.prototype, EventEmitter

View File

@@ -84,6 +84,7 @@ class Selection extends View
new Range(@cursor.getScreenPosition(), @cursor.getScreenPosition())
setScreenRange: (range, {reverse}={}) ->
range = Range.fromObject(range)
{ start, end } = range
[start, end] = [end, start] if reverse
@@ -125,6 +126,10 @@ class Selection extends View
if leadingTabRegex.test buffer.lineForRow(row)
buffer.delete [[row, 0], [row, @editor.tabText.length]]
toggleLineComments: ->
@modifySelection =>
@editor.toggleLineCommentsInRange(@getBufferRange())
autoIndentText: (text) ->
if @editor.autoIndent
mode = @editor.getCurrentMode()