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
54 changes: 54 additions & 0 deletions autoload/sj/elixir.vim
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,57 @@ function! sj#elixir#JoinDef()
call sj#ReplaceLines(def_lineno, end_lineno, joined_line)
return 1
endfunction

function! sj#elixir#SplitArray()
let [from, to] = sj#LocateBracesAroundCursor('[', ']', [
\ 'elixirInterpolationDelimiter',
\ 'elixirString',
\ 'elixirStringDelimiter',
\ 'elixirSigilDelimiter',
\ ])

if from < 0
return 0
endif

let items = sj#ParseJsonObjectBody(from + 1, to - 1)
Comment thread
frm marked this conversation as resolved.

if len(items) == 0 || to - from < 2
return 1
endif

" substitute [1, 2, | tail]
let items[-1] = substitute(items[-1], "\\(|[^>].*\\)", "\n\\1", "")
Comment thread
frm marked this conversation as resolved.

let body = "[\n" . join(items, ",\n") . "\n]"

call sj#ReplaceMotion('Va[', body)

return 1
endfunction

function! sj#elixir#JoinArray()
normal! $

if getline('.')[col('.') - 1] != '['
return 0
endif

let body = sj#Trim(sj#GetMotion('Vi['))
" remove trailing comma
let body = substitute(body, ',\ze\_s*$', '', '')

let items = split(body, ",\s*\n")

if len(items) == 0
return 1
endif

" join isolated | tail on the last line
let items[-1] = substitute(items[-1], "[[:space:]]*\\(|[^>].*\\)", " \\1", "")
Comment thread
frm marked this conversation as resolved.

let body = join(sj#TrimList(items), ', ')
call sj#ReplaceMotion('Va[', '['.body.']')

return 1
endfunction
2 changes: 2 additions & 0 deletions ftplugin/elixir/splitjoin.vim
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
let b:splitjoin_split_callbacks = [
\ 'sj#elixir#SplitDef',
\ 'sj#elixir#SplitArray',
\ ]

let b:splitjoin_join_callbacks = [
\ 'sj#elixir#JoinDef',
\ 'sj#elixir#JoinArray',
\ ]
62 changes: 62 additions & 0 deletions spec/plugin/elixir_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,66 @@ def bar(foo) do
EOF
end
end

specify "arrays" do
set_file_contents <<~EOF
[a, b, c]
EOF

split

assert_file_contents <<~EOF
[
a,
b,
c
]
EOF

vim.search('[')
join

assert_file_contents <<~EOF
[a, b, c]
EOF

set_file_contents <<~EOF
[a: 1, b: 2, c: %{a: 1, b: 2}]
EOF

split

assert_file_contents <<~EOF
[
a: 1,
b: 2,
c: %{a: 1, b: 2}
]
EOF

vim.search('[')
join

assert_file_contents <<~EOF
[a: 1, b: 2, c: %{a: 1, b: 2}]
EOF

set_file_contents <<~EOF
[]
EOF

vim.search('[')
split

assert_file_contents <<~EOF
[]
EOF

vim.search('[')
join

assert_file_contents <<~EOF
[]
EOF
end
end