aboutsummaryrefslogtreecommitdiff
path: root/atom/packages/vim-surround/lib/command
diff options
context:
space:
mode:
authorBen Beltran <ben@nsovocal.com>2015-07-10 11:12:25 -0500
committerBen Beltran <ben@nsovocal.com>2015-07-10 11:12:25 -0500
commit24c7594d62d8d7fbbcdb64b11ce4adc5d8e6991a (patch)
treeded312222bb108923da1820ba40b04d710d20e5b /atom/packages/vim-surround/lib/command
parenteb786e82d170e2abc351a432ade616d6ecdeeb6b (diff)
Adds atom
Diffstat (limited to 'atom/packages/vim-surround/lib/command')
-rw-r--r--atom/packages/vim-surround/lib/command/base.coffee59
-rw-r--r--atom/packages/vim-surround/lib/command/change.coffee74
-rw-r--r--atom/packages/vim-surround/lib/command/delete.coffee33
-rw-r--r--atom/packages/vim-surround/lib/command/selector.coffee22
-rw-r--r--atom/packages/vim-surround/lib/command/surround.coffee18
5 files changed, 206 insertions, 0 deletions
diff --git a/atom/packages/vim-surround/lib/command/base.coffee b/atom/packages/vim-surround/lib/command/base.coffee
new file mode 100644
index 0000000..b40e644
--- /dev/null
+++ b/atom/packages/vim-surround/lib/command/base.coffee
@@ -0,0 +1,59 @@
+{CompositeDisposable} = require 'atom'
+
+module.exports = class Base
+
+ constructor: (config) ->
+ @disposables = new CompositeDisposable
+
+ @curPairs = []
+ @registerPairs config.pairs
+
+ registerPairs: (pairs) ->
+ pairs = (x for x in pairs when x.length > 0 and x.length %2 == 0)
+
+ for pair in pairs
+ if pair not in @curPairs
+ @registerPair pair
+ @curPairs.push(pair)
+
+ registerPair: (pair) ->
+ [left, right] = @splitPair(pair)
+
+ if left != right
+ @createPairBindings left, "#{left} ", " #{right}"
+ @createPairBindings right, left, right
+
+ createPairBindings: (key, left, right) ->
+ name = "vim-surround:#{@getName key}"
+
+ # First, we add a command to the system to actually perform the surround.
+ # We attach the disposable to our object's list.
+ @disposables.add atom.commands.add @context, name, @getRunner left, right
+
+ # Next, we build up keybindings for our command. First, we build a
+ # space-delineated version of our key that's passed in. This breaks up
+ # double keys like `{%` into the seperate keystroke form: `{ %`
+ keys = ""
+ for i in [0..key.length-1]
+ if i == 0
+ keys = key[i]
+ else
+ keys = "#{keys} #{key[i]}"
+
+ # Making a one-command keymap data structure here. Basically:
+ # "atom-text-editor.vim-mode.visual-mode":
+ # "s (":
+ # "vim-surround:surround-("
+
+ keymapArg = {}
+ fullCommand = "#{@command} #{keys}"
+ keymapArg[fullCommand] = name
+
+ contextArg = {}
+ contextArg[@context] = keymapArg
+
+ # Capture the disposable heretom test!
+ @disposables.add atom.keymaps.add name, contextArg
+
+ splitPair: (pair) ->
+ return [pair[..(pair.length/2)-1], pair[pair.length/2..]]
diff --git a/atom/packages/vim-surround/lib/command/change.coffee b/atom/packages/vim-surround/lib/command/change.coffee
new file mode 100644
index 0000000..b1860f5
--- /dev/null
+++ b/atom/packages/vim-surround/lib/command/change.coffee
@@ -0,0 +1,74 @@
+{CompositeDisposable} = require 'atom'
+
+Base = require './base'
+Selector = require './selector'
+
+module.exports = class Change
+ constructor: (config) ->
+ @command = config.changeSurroundCommand
+ @context = "atom-text-editor.vim-mode.command-mode"
+ @disposables = new CompositeDisposable
+ @curPairs = []
+ @curPairsWithTarget = []
+ @registerPairs config.pairs
+
+ getName: (key, targetKey) -> "change-#{key}-to-#{targetKey}"
+
+ registerPairs: (pairs) ->
+ pairs = (x for x in pairs when x.length > 0 and x.length %2 == 0)
+
+ for pair in pairs
+ for target in pairs
+ if "#{pair}#{target}" not in @curPairs
+ @registerPair pair, target
+ @curPairs.push("#{pair}#{target}")
+
+ registerPair: (pair, target) ->
+ [left, right] = @splitPair(pair)
+ [target_left, target_right] = @splitPair(target)
+
+ for key in [left, right]
+ for targetKey in [target_left, target_right]
+ if "#{key}#{targetKey}" not in @curPairsWithTarget
+ name = "vim-surround:#{@getName(key, targetKey)}"
+
+ unless pair == target
+ @disposables.add atom.commands.add @context, name, @getRunner pair, target
+
+ keymapArg = {}
+ fullCommand = "#{@command} #{key} #{targetKey}"
+ keymapArg[fullCommand] = name
+
+ contextArg = {}
+ contextArg[@context] = keymapArg
+
+ # Capture the disposable heretom test!
+ unless pair == target
+ @disposables.add atom.keymaps.add name, contextArg
+ @curPairsWithTarget.push("#{key}#{targetKey}")
+
+ splitPair: (pair) ->
+ return [pair[..(pair.length/2)-1], pair[pair.length/2..]]
+
+ getRunner: (from, to) -> ->
+ [left, right] = [from[0], from[1]]
+ [target_left, target_right] = [to[0], to[1]]
+ editor = atom.workspace.getActiveTextEditor()
+ selector = new Selector(editor, left, right)
+
+ editor.transact ->
+ cursorPos = editor.getCursorBufferPosition()
+
+ selector.inside().select()
+ editor.selections.forEach (selection) ->
+ text = selection.getText()
+
+ # restore cursore and select text with surrounding keys
+ editor.setCursorBufferPosition(cursorPos)
+ selector.outside().select()
+
+ editor.selections.forEach (selection) ->
+ selection.insertText "#{target_left}#{text}#{target_right}"
+
+ # restore cursore
+ editor.setCursorBufferPosition(cursorPos)
diff --git a/atom/packages/vim-surround/lib/command/delete.coffee b/atom/packages/vim-surround/lib/command/delete.coffee
new file mode 100644
index 0000000..aa5eec0
--- /dev/null
+++ b/atom/packages/vim-surround/lib/command/delete.coffee
@@ -0,0 +1,33 @@
+{CompositeDisposable} = require 'atom'
+
+Base = require './base'
+Selector = require './selector'
+
+module.exports = class Delete extends Base
+ constructor: (config) ->
+ @command = config.deleteSurroundCommand
+ @context = "atom-text-editor.vim-mode.command-mode"
+ super config
+
+ getName: (key) -> "delete-#{key}"
+
+ getRunner: (left, right) -> ->
+ editor = atom.workspace.getActiveTextEditor()
+ selector = new Selector(editor, left, right)
+
+ editor.transact ->
+ cursorPos = editor.getCursorBufferPosition()
+
+ selector.inside().select()
+ editor.selections.forEach (selection) ->
+ text = selection.getText()
+
+ # restore cursore and select text with surrounding keys
+ editor.setCursorBufferPosition(cursorPos)
+ selector.outside().select()
+
+ editor.selections.forEach (selection) ->
+ selection.insertText text
+
+ # restore cursore
+ editor.setCursorBufferPosition(cursorPos)
diff --git a/atom/packages/vim-surround/lib/command/selector.coffee b/atom/packages/vim-surround/lib/command/selector.coffee
new file mode 100644
index 0000000..aae3541
--- /dev/null
+++ b/atom/packages/vim-surround/lib/command/selector.coffee
@@ -0,0 +1,22 @@
+vimModePath = atom.packages.resolvePackagePath('vim-mode')
+{SelectInsideQuotes, SelectInsideBrackets} = require "#{vimModePath}/lib/text-objects"
+
+module.exports = class Selector
+ constructor: (@editor, left, right) ->
+ @left = left.trim()
+ @right = right.trim()
+
+ inside: ->
+ if @isBraket()
+ new SelectInsideBrackets(@editor, @left, @right, false)
+ else
+ new SelectInsideQuotes(@editor, @left, false)
+
+ outside: ->
+ if @isBraket()
+ new SelectInsideBrackets(@editor, @left, @right, true)
+ else
+ new SelectInsideQuotes(@editor, @left, true)
+
+ isBraket: ->
+ ['[', ']', '{', '}', '<', '>', '(', ')'].indexOf?(@left.trim()) >= 0
diff --git a/atom/packages/vim-surround/lib/command/surround.coffee b/atom/packages/vim-surround/lib/command/surround.coffee
new file mode 100644
index 0000000..e939f78
--- /dev/null
+++ b/atom/packages/vim-surround/lib/command/surround.coffee
@@ -0,0 +1,18 @@
+{CompositeDisposable} = require 'atom'
+
+Base = require './base'
+
+module.exports = class Surround extends Base
+ constructor: (config) ->
+ @command = config.surroundCommand
+ @context = "atom-text-editor.vim-mode.visual-mode"
+ super config
+
+ getName: (key) -> "surround-#{key}"
+
+ getRunner: (left, right) -> ->
+ editor = atom.workspace.getActiveTextEditor()
+ editor.transact ->
+ editor.selections.forEach (selection) ->
+ text = selection.getText()
+ selection.insertText "#{left}#{text}#{right}"