Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
vim-argument-swapper
==========================
# vim-argument-swapper

A VIM plugin to swap 2 arguments in a method call/definition.

Usage
=====
# Usage

Put cursor over the first argument, `:ArgumentSwapperSwap`, done.

Expand All @@ -14,8 +12,7 @@ Map it to a leader shortcut, for example:
map <leader>as :ArgumentSwapperSwap<cr>
```

Why?
====
# Why?

To make it easy to turn:

Expand All @@ -29,9 +26,14 @@ into:
in_array($needle, $haystack);
```

Installation
============
# Installation

Using [vim-plug](https://github.com/junegunn/vim-plug):

`Plug 'robertbasic/vim-argument-swapper'`

# Configuration

By default the plugin uses Python 3.

`let g:argumentswapper_python_version = 2` in your `.vimrc` file to use Python 2.
21 changes: 21 additions & 0 deletions autoload/argumentswapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import os
import sys
import vim

def swap():
from argumentswapper import swapper

(row, column) = vim.current.window.cursor
line = vim.current.line
new_line = swapper.swap(line, column)
vim.current.buffer[row - 1] = new_line

def setup_py_imports():
python_plugin_path_loaded = int(vim.eval('exists("g:argumentswapper_plugin_path_loaded")'))

if python_plugin_path_loaded == 0:
vim.command("let g:argumentswapper_plugin_path_loaded=1")

plugin_path = vim.eval("g:argumentswapper_plugin_path")
python_plugin_path = os.path.abspath('%s/../lib' % (plugin_path))
sys.path.append(python_plugin_path)
35 changes: 10 additions & 25 deletions autoload/argumentswapper.vim
Original file line number Diff line number Diff line change
@@ -1,30 +1,15 @@
function! argumentswapper#ArgumentSwapperSwap()
python << endpython
import vim
from argumentswapper import swapper

(row, column) = vim.current.window.cursor
line = vim.current.line
new_line = swapper.swap(line, column)
vim.current.buffer[row - 1] = new_line

endpython
if g:argumentswapper_python_version == 3
python3 swap()
else
python swap()
endif
endfunction

function! argumentswapper#SetupPyImports()
python << endpython
import os
import sys
import vim

python_plugin_path_loaded = int(vim.eval('exists("g:argumentswapper_plugin_path_loaded")'))

if python_plugin_path_loaded == 0:
vim.command("let g:argumentswapper_plugin_path_loaded=1")

plugin_path = vim.eval("g:argumentswapper_plugin_path")
python_plugin_path = os.path.abspath('%s/../lib' % (plugin_path))
sys.path.append(python_plugin_path)

endpython
if g:argumentswapper_python_version == 3
python3 setup_py_imports()
else
python setup_py_imports()
endif
endfunction
10 changes: 10 additions & 0 deletions plugin/argumentswapper.vim
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ let g:argumentswapper_plugin_loaded = 1

let g:argumentswapper_plugin_path = expand('<sfile>:p:h')

if !exists('g:argumentswapper_python_version')
let g:argumentswapper_python_version = 3
endif

if g:argumentswapper_python_version == 3
exe 'py3file ' . g:argumentswapper_plugin_path . '/../autoload/argumentswapper.py'
else
exe 'pyfile ' . g:argumentswapper_plugin_path . '/../autoload/argumentswapper.py'
endif

" To setup Python imports
call argumentswapper#SetupPyImports()

Expand Down