Wrap selection in brackets

Enclose the selection in brackets when an opening
bracket is typed and the selection is non-empty

Closes #41
This commit is contained in:
Kevin Sawicki
2013-01-23 16:21:43 -08:00
parent bc3646f180
commit 6324a60d72
2 changed files with 27 additions and 0 deletions

View File

@@ -26,6 +26,10 @@ class LanguageMode
previousCharacter = @editSession.getTextInBufferRange([cursorBufferPosition.add([0, -1]), cursorBufferPosition])
nextCharacter = @editSession.getTextInBufferRange([cursorBufferPosition, cursorBufferPosition.add([0,1])])
if @isOpeningBracket(text) and not @editSession.getSelection().isEmpty()
@wrapSelectionInBrackets(text)
return false
hasWordAfterCursor = /\w/.test(nextCharacter)
hasWordBeforeCursor = /\w/.test(previousCharacter)
@@ -61,6 +65,18 @@ class LanguageMode
@editSession.delete()
false
wrapSelectionInBrackets: (bracket) ->
pair = @pairedCharacters[bracket]
@editSession.mutateSelectedText (selection) =>
return if selection.isEmpty()
range = selection.getBufferRange()
options = reverse: selection.isReversed()
wrappedText = "#{bracket}#{selection.getText()}#{pair}"
selection.insertText(wrappedText)
newRange = [range.start.add([0, 1]), range.end.add([0, 1])]
selection.setBufferRange(newRange, options)
reloadGrammar: ->
path = @buffer.getPath()
pathContents = @buffer.cachedDiskContents