mirror of
https://github.com/atom/atom.git
synced 2026-02-14 08:35:11 -05:00
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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user