Selection support for bracket matcher

This commit is contained in:
Mutwin Kraus
2013-04-03 19:03:33 +02:00
parent abce3d7f9b
commit 460c4a0b09
2 changed files with 31 additions and 0 deletions

View File

@@ -30,6 +30,8 @@ module.exports =
editor.on 'cursor:moved.bracket-matcher', => @updateMatch(editor)
editor.command 'editor:go-to-matching-bracket.bracket-matcher', =>
@goToMatchingPair(editor)
editor.command 'editor:select-to-matching-bracket.bracket-matcher', =>
@selectToMatchingPair(editor)
editor.on 'editor:will-be-removed', => editor.off('.bracket-matcher')
editor.startHighlightView = @addHighlightView(editor)
editor.endHighlightView = @addHighlightView(editor)
@@ -57,6 +59,24 @@ module.exports =
else if previousPosition.isEqual(endPosition)
editor.setCursorBufferPosition(startPosition)
selectToMatchingPair: (editor) ->
return unless @pairHighlighted
return unless underlayer = editor.getPane()?.find('.underlayer')
position = editor.getCursorBufferPosition()
previousPosition = position.translate([0, -1])
startPosition = underlayer.find('.bracket-matcher:first').data('bufferPosition')
endPosition = underlayer.find('.bracket-matcher:last').data('bufferPosition')
if position.isEqual(startPosition)
editor.selectToScreenPosition(editor.screenPositionForBufferPosition(endPosition.translate([0, 1])))
else if previousPosition.isEqual(startPosition)
editor.selectToScreenPosition(editor.screenPositionForBufferPosition(endPosition))
else if position.isEqual(endPosition)
editor.selectToScreenPosition(editor.screenPositionForBufferPosition(startPosition.translate([0, 1])))
else if previousPosition.isEqual(endPosition)
editor.selectToScreenPosition(editor.screenPositionForBufferPosition(startPosition))
moveHighlightViews: (editor, bufferRange) ->
{ start, end } = Range.fromObject(bufferRange)
startPixelPosition = editor.pixelPositionForBufferPosition(start)

View File

@@ -86,6 +86,17 @@ describe "bracket matching", ->
editor.trigger "editor:go-to-matching-bracket"
expect(editor.getCursorBufferPosition()).toEqual [0, 28]
describe "when editor:select-to-matching-bracket is triggered", ->
it " selects until the next matched bracket", ->
editor.moveCursorToEndOfLine()
editor.moveCursorLeft()
editor.trigger "editor:select-to-matching-bracket"
expect(editor.getCursorBufferPosition()).toEqual [12, 1]
selections = editor.getSelections()
expect(selections.length).toBe(1)
range = selections[0].getBufferRange()
expect(range).toEqual([[0,28], [12,1]])
describe "matching bracket insertion", ->
beforeEach ->
editSession.buffer.setText("")