Permalink
Browse files

[vim] splitjoin update

  • Loading branch information...
1 parent 2173467 commit 1490b997fabd8c24a2ba5719be83eb2590d49c17 @guns committed Sep 13, 2013
@@ -2,5 +2,5 @@ source 'http://rubygems.org'
gem 'rake'
gem 'rspec'
-gem 'vimrunner'
+gem 'vimrunner', '0.3.0'
gem 'pry'
@@ -18,7 +18,7 @@ GEM
diff-lcs (~> 1.1.3)
rspec-mocks (2.10.1)
slop (2.4.4)
- vimrunner (0.2.0)
+ vimrunner (0.3.0)
PLATFORMS
ruby
@@ -27,4 +27,4 @@ DEPENDENCIES
pry
rake
rspec
- vimrunner
+ vimrunner (= 0.3.0)
@@ -40,7 +40,7 @@ This currently works for:
* Javascript object literals and functions
* Tags in HTML/XML
* CSS, SCSS, LESS style declarations.
- * YAML arrays
+ * YAML arrays and maps
* Lua functions and tables
* Vimscript line continuations
@@ -64,16 +64,19 @@ endfunction
" Note that the motion needs to include a visual mode key, like "V", "v" or
" "gv"
function! sj#ReplaceMotion(motion, text)
- let register = s:DefaultRegister()
+ " reset clipboard to avoid problems with 'unnamed' and 'autoselect'
+ let saved_clipboard = &clipboard
+ set clipboard=
- let saved_register_text = getreg(register, 1)
- let saved_register_type = getregtype(register)
+ let saved_register_text = getreg('"', 1)
+ let saved_register_type = getregtype('"')
- call setreg(register, a:text, 'v')
- exec 'silent normal! '.a:motion.'"'.register.'p'
+ call setreg('"', a:text, 'v')
+ exec 'silent normal! '.a:motion.'p'
silent normal! gv=
- call setreg(register, saved_register_text, saved_register_type)
+ call setreg('"', saved_register_text, saved_register_type)
+ let &clipboard = saved_clipboard
endfunction
" function! sj#ReplaceLines(start, end, text) {{{2
@@ -482,18 +485,6 @@ function! sj#ParseJsonObjectBody(from, to)
return parser.args
endfunction
-" Finds the configuration's default paste register based on the 'clipboard'
-" option.
-function! s:DefaultRegister()
- if &clipboard =~ 'unnamedplus'
- return '+'
- elseif &clipboard =~ 'unnamed'
- return '*'
- else
- return '"'
- endif
-endfunction
-
function! s:SkipSyntax(...)
let syntax_groups = a:000
let skip_pattern = '\%('.join(syntax_groups, '\|').'\)'
@@ -1,11 +1,13 @@
function! sj#coffee#SplitFunction()
- let line = getline('.')
+ let lineno = line('.')
+ let line = getline('.')
+ let indent = indent(lineno)
if line !~ '[-=]>'
return 0
else
s/\([-=]\)>\s*/\1>\r/
- normal! ==
+ call s:SetBaseIndent(lineno + 1, lineno + 1, indent + &sw)
return 1
endif
endfunction
@@ -29,11 +31,11 @@ function! sj#coffee#SplitIfClause()
if line =~ suffix_pattern
call sj#ReplaceMotion('V', substitute(line, suffix_pattern, '\2 \3\n\1', ''))
- call s:SetBaseIndent(line('.'), line('.') + 1, base_indent)
+ call s:SetBaseIndent(line('.') + 1, line('.') + 1, base_indent + &sw)
return 1
elseif line =~ postfix_pattern
call sj#ReplaceMotion('V', substitute(line, postfix_pattern, '\1 \2\n\3', ''))
- call s:SetBaseIndent(line('.'), line('.') + 1, base_indent)
+ call s:SetBaseIndent(line('.') + 1, line('.') + 1, base_indent + &sw)
return 1
else
return 0
@@ -63,14 +65,18 @@ function! sj#coffee#JoinIfClause()
endfunction
function! sj#coffee#SplitTernaryClause()
- let line = getline('.')
- let pattern = '\v^(.*)if (.*) then (.*) else ([^)]*)(.*)$'
+ let lineno = line('.')
+ let indent = indent('.')
+ let line = getline('.')
+ let pattern = '\v^(\s*)(.*)if (.*) then (.*) else ([^)]*)(.*)$'
if line =~ pattern
- let body_when_true = sj#ExtractRx(line, pattern, '\3')
- let body_when_false = sj#ExtractRx(line, pattern, '\4')
- let replacement = "if \\2\r\\1".body_when_true."\\5\relse\r\\1".body_when_false."\\5"
+ let body_when_true = sj#ExtractRx(line, pattern, '\4')
+ let body_when_false = sj#ExtractRx(line, pattern, '\5')
+ let replacement = "if \\3\r\\2".body_when_true."\\6\relse\r\\2".body_when_false."\\6"
exe 's/'.pattern.'/'.escape(replacement, '/')
+
+ call s:SetBaseIndent(lineno, lineno + 3, indent)
normal! >>kk>>
return 1
@@ -85,13 +91,16 @@ function! sj#coffee#SplitObjectLiteral()
if from < 0 && to < 0
return 0
else
- let pairs = sj#ParseJsonObjectBody(from + 1, to - 1)
- let body = "\n".join(pairs, "\n")
+ let indent = indent('.')
+ let pairs = sj#ParseJsonObjectBody(from + 1, to - 1)
+ let body = "\n".join(pairs, "\n")
call sj#ReplaceMotion('Va{', body)
" clean the remaining whitespace
s/\s\+$//e
+ call s:SetIndent(line('.') + 1, line('.') + len(pairs), indent + &sw)
+
if g:splitjoin_align
let body_start = line('.') + 1
let body_end = body_start + len(pairs) - 1
@@ -187,9 +196,18 @@ function! s:IndentedLinesBelow(line)
return [first_line, current_line]
endfunction
+" Looks at the first line, considers it a baseline for the next ones, and
+" changes that baseline to the given indent.
function! s:SetBaseIndent(from, to, indent)
let current_whitespace = matchstr(getline(a:from), '^\s*')
let new_whitespace = repeat(' ', a:indent)
exe a:from.','.a:to.'s/^'.current_whitespace.'/'.new_whitespace
endfunction
+
+" Sets the absolute indent of the given range of lines to the given indent
+function! s:SetIndent(from, to, indent)
+ let new_whitespace = repeat(' ', a:indent)
+
+ exe a:from.','.a:to.'s/^\s*/'.new_whitespace
+endfunction
@@ -0,0 +1,38 @@
+" TODO (2013-05-09) Only works for very simple things, needs work
+function! sj#haml#SplitInterpolation()
+ let lineno = line('.')
+ let line = getline('.')
+ let indent = indent('.')
+ let pattern = '^\s*%.\{-}\zs='
+
+ if line !~ pattern
+ return 0
+ endif
+
+ exe 's/'.pattern.'/\r=/'
+ call s:SetIndent(lineno + 1, lineno + 1, indent + &sw)
+
+ return 1
+endfunction
+
+function! sj#haml#JoinInterpolation()
+ if line('.') == line('$')
+ return 0
+ endif
+
+ let line = getline('.')
+ let next_line = getline(line('.') + 1)
+
+ if !(line =~ '^\s*%\k\+\s*$' && next_line =~ '^\s*=')
+ return 0
+ end
+
+ s/\n\s*//
+ return 1
+endfunction
+
+" Sets the absolute indent of the given range of lines to the given indent
+function! s:SetIndent(from, to, indent)
+ let new_whitespace = repeat(' ', a:indent)
+ exe a:from.','.a:to.'s/^\s*/'.new_whitespace
+endfunction
@@ -21,36 +21,78 @@ function! sj#yaml#SplitArray()
endfunction
function! sj#yaml#JoinArray()
- let line_no = line('.')
- let line = getline(line_no)
- let next_line_no = line_no + 1
- let indent = indent(line_no)
- let whitespace = s:GetIndentWhitespace(line_no)
+ let line_no = line('.')
+ let line = getline(line_no)
+ let whitespace = s:GetIndentWhitespace(line_no)
- if line !~ ':\s*\(#.*\)\?$' || next_line_no > line('$')
+ if line !~ ':\s*\(#.*\)\?$' || line_no + 1 > line('$')
" then there's nothing to join
return 0
else
- let next_line = getline(next_line_no)
+ let [lines, last_line_no] = s:GetChildren(line_no)
- if next_line !~ '^\s*-'
+ if empty(lines) || lines[0] !~ '^\s*-'
return 0
- endif
+ end
- while next_line_no <= line('$') &&
- \ (sj#BlankString(next_line) || indent(next_line_no) > indent)
- let next_line_no = next_line_no + 1
- let next_line = getline(next_line_no)
- endwhile
- let next_line_no = next_line_no - 1
-
- let lines = sj#GetLines(line_no + 1, next_line_no)
let lines = map(lines, 'sj#Trim(substitute(v:val, "^\\s*-", "", ""))')
let lines = filter(lines, 'v:val !~ "^\s*$"')
let first_line = substitute(line, '\s*#.*$', '', '')
let replacement = first_line.' ['.join(lines, ', ').']'
- call sj#ReplaceLines(line_no, next_line_no, replacement)
+ call sj#ReplaceLines(line_no, last_line_no, replacement)
+ silent! normal! zO
+ call s:SetIndentWhitespace(line_no, whitespace)
+
+ return 1
+ endif
+endfunction
+
+function! sj#yaml#SplitMap()
+ let [from, to] = sj#LocateBracesOnLine('{', '}')
+
+ if from < 0 && to < 0
+ return 0
+ else
+ let line_no = line('.')
+ let whitespace = s:GetIndentWhitespace(line_no)
+ let pairs = sj#ParseJsonObjectBody(from + 1, to - 1)
+ let body = "\n".join(pairs, "\n")
+
+ call sj#ReplaceMotion('Va{', body)
+ silent! normal! zO
+ call s:SetIndentWhitespace(line_no, whitespace)
+ call s:IncreaseIndentWhitespace(line_no + 1, line_no + len(pairs), whitespace)
+ exe line_no.'s/\s*$//e'
+
+ if g:splitjoin_align
+ let body_start = line_no + 1
+ let body_end = body_start + len(pairs) - 1
+ call sj#Align(body_start, body_end, 'json_object')
+ endif
+
+ return 1
+ endif
+endfunction
+
+function! sj#yaml#JoinMap()
+ let line_no = line('.')
+ let line = getline(line_no)
+ let whitespace = s:GetIndentWhitespace(line_no)
+
+ if line !~ '\k\+:\s*$' || line_no + 1 > line('$')
+ return 0
+ else
+ let [lines, last_line_no] = s:GetChildren(line_no)
+ let lines = sj#TrimList(lines)
+
+ if g:splitjoin_normalize_whitespace
+ let lines = map(lines, 'substitute(v:val, ":\\s\\+", ": ", "")')
+ endif
+
+ let replacement = sj#Trim(line) . ' { '. join(lines, ', ') . ' }'
+
+ call sj#ReplaceLines(line_no, last_line_no, replacement)
silent! normal! zO
call s:SetIndentWhitespace(line_no, whitespace)
@@ -77,3 +119,19 @@ function! s:IncreaseIndentWhitespace(from, to, whitespace)
call s:SetIndentWhitespace(line_no, new_whitespace)
endfor
endfunction
+
+function! s:GetChildren(line_no)
+ let line_no = a:line_no
+ let next_line_no = line_no + 1
+ let indent = indent(line_no)
+ let next_line = getline(next_line_no)
+
+ while next_line_no <= line('$') &&
+ \ (sj#BlankString(next_line) || indent(next_line_no) > indent)
+ let next_line_no = next_line_no + 1
+ let next_line = getline(next_line_no)
+ endwhile
+ let next_line_no = next_line_no - 1
+
+ return [sj#GetLines(line_no + 1, next_line_no), next_line_no]
+endfunction
@@ -1,3 +1,5 @@
+*splitjoin.txt* Switch between single-line and multiline forms of code
+
==============================================================================
CONTENTS *splitjoin* *splitjoin-contents*
@@ -533,6 +535,19 @@ Arrays ~
- 'three'
- 'four'
<
+Maps ~
+>
+ root:
+ one: { foo: bar }
+ two: { three: ['four', 'five'], six: seven }
+
+ root:
+ one:
+ foo: bar
+ two:
+ three: ['four', 'five']
+ six: seven
+<
==============================================================================
ERUBY *splitjoin-eruby*
@@ -31,3 +31,4 @@ splitjoin-settings splitjoin.txt /*splitjoin-settings*
splitjoin-usage splitjoin.txt /*splitjoin-usage*
splitjoin-vimscript splitjoin.txt /*splitjoin-vimscript*
splitjoin-yaml splitjoin.txt /*splitjoin-yaml*
+splitjoin.txt splitjoin.txt /*splitjoin.txt*
@@ -0,0 +1,3 @@
+%div
+ %span
+ = 1 + 2
@@ -8,3 +8,6 @@ root:
three: # comment
- four
- five
+
+ one: { foo: bar, bar: baz }
+ two: { three: ['four', 'five'], six: seven }
@@ -0,0 +1,7 @@
+let b:splitjoin_split_callbacks = [
+ \ 'sj#haml#SplitInterpolation'
+ \ ]
+
+let b:splitjoin_join_callbacks = [
+ \ 'sj#haml#JoinInterpolation'
+ \ ]
@@ -1,11 +1,13 @@
if !exists('b:splitjoin_split_callbacks')
let b:splitjoin_split_callbacks = [
- \ 'sj#yaml#SplitArray'
+ \ 'sj#yaml#SplitArray',
+ \ 'sj#yaml#SplitMap'
\ ]
endif
if !exists('b:splitjoin_join_callbacks')
let b:splitjoin_join_callbacks = [
- \ 'sj#yaml#JoinArray'
+ \ 'sj#yaml#JoinArray',
+ \ 'sj#yaml#JoinMap'
\ ]
endif
Oops, something went wrong.

0 comments on commit 1490b99

Please sign in to comment.