When an identical closing bracket is inserted, don't insert a new character and move cursor to the right.

This commit is contained in:
Corey Johnson & Nathan Sobo 2012-08-01 09:33:42 -07:00
parent dc50959af1
commit 97b492edfc
2 changed files with 30 additions and 1 deletions

View File

@ -39,6 +39,16 @@ describe "LanguageMode", ->
editSession.insertText "'"
expect(buffer.lineForRow(0)).toMatch /^''/
describe "when ) is inserted before a )", ->
describe "when there is whitespace after the )", ->
it "moves the cursor one column to the right instead of inserting a new )", ->
editSession.buffer.setText("")
editSession.insertText '() '
editSession.setCursorBufferPosition([0, 1])
editSession.insertText ')'
expect(buffer.lineForRow(0)).toBe "() "
expect(editSession.getCursorBufferPosition().column).toBe 2
describe "javascript", ->
beforeEach ->
editSession = fixturesProject.buildEditSessionForPath('sample.js', autoIndent: false)

View File

@ -17,7 +17,13 @@ class LanguageMode
@aceAdaptor = new AceAdaptor(@editSession)
_.adviseBefore @editSession, 'insertText', (text) =>
if matchingCharacter = @matchingCharacters[text]
cursorBufferPosition = @editSession.getCursorBufferPosition()
nextCharachter = @editSession.getTextInBufferRange([cursorBufferPosition, cursorBufferPosition.add([0, 1])])
if @isCloseBracket(text) and text == nextCharachter
@editSession.moveCursorRight()
false
else if matchingCharacter = @matchingCharacters[text]
@editSession.insertText text + matchingCharacter
@editSession.moveCursorLeft()
false
@ -35,6 +41,19 @@ class LanguageMode
else 'text'
new (require("ace/mode/#{modeName}").Mode)
isOpenBracket: (string) ->
@pairedCharacters[string]?
isCloseBracket: (string) ->
@getInvertedPairedCharacters()[string]?
getInvertedPairedCharacters: ->
return @invertedPairedCharacters if @invertedPairedCharacters
@invertedPairedCharacters = {}
for open, close of @matchingCharacters
@invertedPairedCharacters[close] = open
@invertedPairedCharacters
toggleLineCommentsInRange: (range) ->
range = Range.fromObject(range)
@aceMode.toggleCommentLines(@tokenizedBuffer.stateForRow(range.start.row), @aceAdaptor, range.start.row, range.end.row)