mirror of
https://github.com/atom/atom.git
synced 2026-02-08 05:35:04 -05:00
Improve brackets auto closing.
When an open bracket is inserted, an anchorRange is created. When a closing bracket is inserted, and its position matches the end of one of the anchorRanges, the closing bracket is not inserted and the cursor moves right.
This commit is contained in:
@@ -15,20 +15,29 @@ class LanguageMode
|
||||
constructor: (@editSession) ->
|
||||
@buffer = @editSession.buffer
|
||||
@grammar = TextMateBundle.grammarForFileName(@buffer.getBaseName())
|
||||
@bracketAnchorRanges = []
|
||||
|
||||
_.adviseBefore @editSession, 'insertText', (text) =>
|
||||
return true if @editSession.hasMultipleCursors()
|
||||
|
||||
cursorBufferPosition = @editSession.getCursorBufferPosition()
|
||||
nextCharacter = @editSession.getTextInBufferRange([cursorBufferPosition, cursorBufferPosition.add([0, 1])])
|
||||
nextCharachter = @editSession.getTextInBufferRange([cursorBufferPosition, cursorBufferPosition.add([0,1])])
|
||||
|
||||
if @isClosingBracket(text) and text == nextCharacter
|
||||
@editSession.moveCursorRight()
|
||||
false
|
||||
else if /^\s*$/.test(nextCharacter) and pairedCharacter = @pairedCharacters[text]
|
||||
@editSession.insertText text + pairedCharacter
|
||||
if @isOpeningBracket(text) and /\W|^$/.test(nextCharachter)
|
||||
@editSession.insertText(text + @pairedCharacters[text])
|
||||
@editSession.moveCursorLeft()
|
||||
range = [cursorBufferPosition, cursorBufferPosition.add([0, text.length])]
|
||||
@bracketAnchorRanges.push @editSession.addAnchorRange(range)
|
||||
false
|
||||
else if @isClosingBracket(text)
|
||||
return true if nextCharachter != text
|
||||
anchorRange = @bracketAnchorRanges.filter((anchorRange) -> anchorRange.getBufferRange().end.isEqual(cursorBufferPosition))[0]
|
||||
|
||||
if anchorRange
|
||||
anchorRange.destroy()
|
||||
@bracketAnchorRanges = _.without(@bracketAnchorRanges, anchorRange)
|
||||
@editSession.moveCursorRight()
|
||||
false
|
||||
|
||||
isOpeningBracket: (string) ->
|
||||
@pairedCharacters[string]?
|
||||
|
||||
Reference in New Issue
Block a user