Permalink
Browse files

Version 0.0.1: Initial upload

  • Loading branch information...
AndrewRadev authored and vim-scripts committed Aug 15, 2012
0 parents commit 1a0097ad8346b0403d99564d1c88ba40644631ae
Showing with 518 additions and 0 deletions.
  1. +41 −0 README
  2. +108 −0 autoload/sideways.vim
  3. +141 −0 autoload/sideways/parsing.vim
  4. +127 −0 autoload/sideways/util.vim
  5. +88 −0 doc/sideways.txt
  6. +13 −0 plugin/sideways.vim
41 README
@@ -0,0 +1,41 @@
+This is a mirror of http://www.vim.org/scripts/script.php?script_id=4171
+
+Github project: https://github.com/AndrewRadev/sideways.vim
+
+The plugin defines two commands, ":SidewaysLeft" and ":SidewaysRight", which move the item under the cursor left or right, where an "item" is defined by a delimiter. As an example:
+
+ def function(one, two, three):
+ pass
+
+Placing the cursor on "two" and executing `:SidewaysLeft`, the "one" and "two" arguments will switch their places, resulting in this:
+
+ def function(two, one, three):
+ pass
+
+In this case, the delimiter is a comma. The plugin currently works with various other cases and it's intended to make the process configurable. While this particular example is in python, this should work for arguments in many different languages that use round braces to denote function calls.
+
+Apart from functions, it works for square-bracket lists in dynamic languages:
+
+ list = [one, [two, four, five], three]
+
+Notice that, if you experiment with this example, you'll find that you can move the entire second list around. The plugin takes into consideration nested structures.
+
+Apart from functions, it works for lists in CSS declarations:
+
+ border-radius: 20px 0 0 20px;
+
+And, it also works for cucumber tables (see docs for better table formatting):
+
+ Examples:
+ | input_1 | input_2 | button | output |
+ | 20 | 30 | add | 50 |
+ | 2 | 5 | add | 7 |
+ | 0 | 40 | add | 40 |
+
+It's highly suggested to map the two commands to convenient keys. For example, mapping them to <c-h> and <c-l> would look like this:
+
+ nnoremap <c-h> :SidewaysLeft<cr>
+ nnoremap <c-l> :SidewaysRight<cr>
+
+The plugin is intended to be highly customizable, eventually. In the future, it should be able to work with ruby function arguments and it may also contain an "argument" text object (since the machinery to detect arguments is already there).
+
@@ -0,0 +1,108 @@
+function! sideways#Left()
+ let items = sideways#parsing#Parse()
+ if empty(items)
+ return 0
+ end
+
+ let last_index = len(items) - 1
+ let active_index = s:FindActiveItem(items)
+ if active_index < 0
+ return 0
+ endif
+
+ if active_index == 0
+ let first = items[active_index]
+ let second = items[last_index]
+ let new_cursor_column = second[0] + s:Delta(second, first)
+ else
+ let first = items[active_index - 1]
+ let second = items[active_index]
+ let new_cursor_column = first[0]
+ endif
+
+ call s:Swap(first, second, new_cursor_column)
+ silent! call repeat#set(":call sideways#Left()\<cr>")
+ return 1
+endfunction
+
+function! sideways#Right()
+ let items = sideways#parsing#Parse()
+ if empty(items)
+ return 0
+ end
+
+ let last_index = len(items) - 1
+ let active_index = s:FindActiveItem(items)
+ if active_index < 0
+ return 0
+ endif
+
+ if active_index == last_index
+ let first = items[0]
+ let second = items[last_index]
+ let new_cursor_column = first[0]
+ else
+ let first = items[active_index]
+ let second = items[active_index + 1]
+ let new_cursor_column = second[0] + s:Delta(second, first)
+ endif
+
+ call s:Swap(first, second, new_cursor_column)
+ silent! call repeat#set(":call sideways#Right()\<cr>")
+ return 1
+endfunction
+
+" Swaps the a:first and a:second items in the buffer. Both first arguments are
+" expected to be pairs of start and end columns. The last argument is a
+" number, the new column to position the cursor on.
+"
+" In order to avoid having to consider eventual changes in column positions,
+" a:first is expected to be positioned before a:second. Assuming that, the
+" function first places the second item and then the first one, ensuring that
+" the column number remain consistent until it's done.
+function! s:Swap(first, second, new_cursor_column)
+ let [first_start, first_end] = a:first
+ let [second_start, second_end] = a:second
+
+ let first_body = sideways#util#GetCols(first_start, first_end)
+ let second_body = sideways#util#GetCols(second_start, second_end)
+
+ let position = getpos('.')
+
+ call sideways#util#ReplaceCols(second_start, second_end, first_body)
+ call sideways#util#ReplaceCols(first_start, first_end, second_body)
+
+ let position[2] = a:new_cursor_column
+ call setpos('.', position)
+endfunction
+
+" Finds an item in the given list of column pairs, which the cursor is
+" currently positioned in.
+"
+" Returns the index of the found item, or -1 if it's not found.
+function! s:FindActiveItem(items)
+ let column = col('.')
+
+ let index = 0
+ for item in a:items
+ let [start, end] = item
+
+ if start <= column && column <= end
+ return index
+ endif
+
+ let index += 1
+ endfor
+
+ return -1
+endfunction
+
+" Return the difference in length between the first start-end column pair and
+" the second one.
+"
+" It is assumed that a:first is positioned before a:second. This is used to
+" account for the column positions becoming inconsistent after replacing text
+" in the current line.
+function! s:Delta(first, second)
+ return (a:first[1] - a:first[0]) - (a:second[1] - a:second[0])
+endfunction
@@ -0,0 +1,141 @@
+" Extract column positions for "arguments" on the current line. Returns a list
+" of pairs, each pair contains the start and end columns of the item
+"
+" Example:
+"
+" On the following line:
+"
+" def function(one, two):
+"
+" The result would be:
+"
+" [ [14, 16], [19, 21] ]
+"
+function! sideways#parsing#Parse()
+ let definitions =
+ \ [
+ \ {
+ \ 'start': '\k(',
+ \ 'end': ')',
+ \ 'delimiter': '^,\s*',
+ \ 'skip': '^\s',
+ \ 'brackets': ['([''"', ')]''"']
+ \ },
+ \ {
+ \ 'start': '\[',
+ \ 'end': '\]',
+ \ 'delimiter': '^,\s*',
+ \ 'skip': '^\s',
+ \ 'brackets': ['([''"', ')]''"']
+ \ },
+ \ {
+ \ 'start': '\k:\s*',
+ \ 'end': ';',
+ \ 'delimiter': '^\s',
+ \ 'skip': '^\s',
+ \ 'brackets': ['(''"', ')''"']
+ \ },
+ \ {
+ \ 'start': '^\s*|',
+ \ 'end': '|$',
+ \ 'delimiter': '^|',
+ \ 'skip': '^$',
+ \ 'brackets': ['(''"', ')''"']
+ \ },
+ \ ]
+
+ let items = []
+
+ for definition in definitions
+ let start_pattern = definition.start
+ let end_pattern = definition.end
+ let delimiter_pattern = definition.delimiter
+ let skip_pattern = definition.skip
+
+ let [opening_brackets, closing_brackets] = definition.brackets
+
+ normal! zR
+ call sideways#util#PushCursor()
+
+ if searchpair(start_pattern, '', end_pattern, 'bW', '', line('.')) <= 0
+ call sideways#util#PopCursor()
+ continue
+ else
+ call search(start_pattern, 'Wce', line('.'))
+ endif
+
+ normal! l
+
+ let current_item = [col('.'), -1]
+
+ let remainder_of_line = s:RemainderOfLine()
+
+ while s:RemainderOfLine() !~ '^'.end_pattern
+ let remainder_of_line = s:RemainderOfLine()
+ let bracket_match = s:BracketMatch(remainder_of_line, opening_brackets)
+
+ if bracket_match >= 0
+ let closing_bracket = closing_brackets[bracket_match]
+
+ if search('\V'.closing_bracket, 'W', line('.')) <= 0
+ continue
+ else
+ normal! l
+ endif
+ elseif remainder_of_line =~ delimiter_pattern
+ let current_item[1] = col('.') - 1
+ call add(items, current_item)
+
+ normal! l
+ while s:RemainderOfLine() =~ skip_pattern
+ normal! l
+ endwhile
+ let current_item = [col('.'), -1]
+ elseif col('.') == col('$') - 1
+ let current_item[1] = col('$') - 1
+ break
+ else
+ normal! l
+ endif
+ endwhile
+
+ if current_item[1] < 0
+ let current_item[1] = col('.') - 1
+ endif
+ call add(items, current_item)
+
+ if !empty(items)
+ call sideways#util#PopCursor()
+ break
+ endif
+
+ call sideways#util#PopCursor()
+ endfor
+
+ return items
+endfunction
+
+function! s:BracketMatch(text, brackets)
+ let index = 0
+ for char in split(a:brackets, '\zs')
+ if a:text[0] ==# char
+ return index
+ else
+ let index += 1
+ endif
+ endfor
+
+ return -1
+endfunction
+
+" Returns the remainder of the line from the current cursor position to the
+" end.
+function! s:RemainderOfLine()
+ return strpart(getline('.'), col('.') - 1)
+endfunction
+
+" Simple debugging
+function! s:DebugItems(items)
+ Decho a:items
+ Decho map(copy(a:items), 'sideways#util#GetCols(v:val[0], v:val[1])')
+endfunction
Oops, something went wrong.

0 comments on commit 1a0097a

Please sign in to comment.