Bind ctrl-j to jump to matching bracket

This commit is contained in:
Kevin Sawicki 2013-02-04 20:38:15 -08:00
parent b30fae7a68
commit 39e39afa1b
3 changed files with 58 additions and 6 deletions

View File

@ -22,11 +22,31 @@ class BracketMatcher extends AtomPackage
subscribeToEditor: (editor) ->
editor.on 'cursor:moved.bracket-matcher', => @updateMatch(editor)
editor.command 'editor:go-to-matching-bracket', => @goToMatchingPair(editor)
editor.on 'editor:will-be-removed', => editor.off('.bracket-matcher')
goToMatchingPair: (editor) ->
return unless @pairHighlighted
return unless underlayer = editor.pane()?.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.setCursorBufferPosition(endPosition.translate([0, 1]))
else if previousPosition.isEqual(startPosition)
editor.setCursorBufferPosition(endPosition)
else if position.isEqual(endPosition)
editor.setCursorBufferPosition(startPosition.translate([0, 1]))
else if previousPosition.isEqual(endPosition)
editor.setCursorBufferPosition(startPosition)
createView: (editor, bufferPosition) ->
pixelPosition = editor.pixelPositionForBufferPosition(bufferPosition)
view = $$ -> @div class: 'bracket-matcher'
view.data('bufferPosition', bufferPosition)
view.css('top', pixelPosition.top).css('left', pixelPosition.left)
view.width(editor.charWidth).height(editor.charHeight)
@ -90,6 +110,10 @@ class BracketMatcher extends AtomPackage
matchPosition = @findMatchingStartPair(buffer, position, matchingPair, currentPair)
if position? and matchPosition?
underlayer.append(@createView(editor, position))
underlayer.append(@createView(editor, matchPosition))
if position.isLessThan(matchPosition)
underlayer.append(@createView(editor, position))
underlayer.append(@createView(editor, matchPosition))
else
underlayer.append(@createView(editor, matchPosition))
underlayer.append(@createView(editor, position))
@pairHighlighted = true

View File

@ -0,0 +1,2 @@
'.editor':
'ctrl-j': 'editor:go-to-matching-bracket'

View File

@ -33,16 +33,16 @@ describe "bracket matching", ->
editor.moveCursorLeft()
editor.moveCursorLeft()
expect(editor.underlayer.find('.bracket-matcher').length).toBe 2
expect(editor.underlayer.find('.bracket-matcher:first').position()).toEqual editor.pixelPositionForBufferPosition([12,0])
expect(editor.underlayer.find('.bracket-matcher:last').position()).toEqual editor.pixelPositionForBufferPosition([0,28])
expect(editor.underlayer.find('.bracket-matcher:last').position()).toEqual editor.pixelPositionForBufferPosition([12,0])
expect(editor.underlayer.find('.bracket-matcher:first').position()).toEqual editor.pixelPositionForBufferPosition([0,28])
describe "when the cursor is after an ending pair", ->
it "highlights the starting pair and ending pair", ->
editor.moveCursorToBottom()
editor.moveCursorLeft()
expect(editor.underlayer.find('.bracket-matcher').length).toBe 2
expect(editor.underlayer.find('.bracket-matcher:first').position()).toEqual editor.pixelPositionForBufferPosition([12,0])
expect(editor.underlayer.find('.bracket-matcher:last').position()).toEqual editor.pixelPositionForBufferPosition([0,28])
expect(editor.underlayer.find('.bracket-matcher:last').position()).toEqual editor.pixelPositionForBufferPosition([12,0])
expect(editor.underlayer.find('.bracket-matcher:first').position()).toEqual editor.pixelPositionForBufferPosition([0,28])
describe "when the cursor is moved off a pair", ->
it "removes the starting pair and ending pair highlights", ->
@ -58,3 +58,29 @@ describe "bracket matching", ->
expect(editor.underlayer.find('.bracket-matcher').length).toBe 2
expect(editor.underlayer.find('.bracket-matcher:first').position()).toEqual editor.pixelPositionForBufferPosition([8,42])
expect(editor.underlayer.find('.bracket-matcher:last').position()).toEqual editor.pixelPositionForBufferPosition([8,54])
describe "when editor:go-to-matching-bracket is triggered", ->
describe "when the cursor is before the starting pair", ->
it "moves the cursor to after the ending pair", ->
editor.moveCursorToEndOfLine()
editor.moveCursorLeft()
editor.trigger "editor:go-to-matching-bracket"
expect(editor.getCursorBufferPosition()).toEqual [12, 1]
describe "when the cursor is after the starting pair", ->
it "moves the cursor to before the ending pair", ->
editor.moveCursorToEndOfLine()
editor.trigger "editor:go-to-matching-bracket"
expect(editor.getCursorBufferPosition()).toEqual [12, 0]
describe "when the cursor is before the ending pair", ->
it "moves the cursor to after the starting pair", ->
editor.setCursorBufferPosition([12, 0])
editor.trigger "editor:go-to-matching-bracket"
expect(editor.getCursorBufferPosition()).toEqual [0, 29]
describe "when the cursor is after the ending pair", ->
it "moves the cursor to before the starting pair", ->
editor.setCursorBufferPosition([12, 1])
editor.trigger "editor:go-to-matching-bracket"
expect(editor.getCursorBufferPosition()).toEqual [0, 28]