1 " Python function call splitter
2 " Maintainer: Sergey Matveev <stargrave@stargrave.org>
3 " License: GNU General Public License version 3 of the License or later
5 " This plugin splits Python function call on several lines.
7 " def foobar(self, foo: str, bar: Some[thing, too]) -> None:
12 " bar: Some[thing, too],
22 " You can un-split it using :Undefsplit command on the line where
25 " :Defsplit has optional argument specifying how many opening round
26 " parenthesis must be skipped.
27 " :Defsplit 1 on foo(baz(baz(...))) produces
32 " Also there is :Brsplit command behaving similarly, but it splits other
33 " types of brackets: "{}", "[]".
35 if !exists("g:defsplit_shift") | let g:defsplit_shift = " " | endif
36 if exists('*<SID>Defsplit') | finish | endif
38 function! s:bracket_find(brs_allowable, line, offset)
40 for bracket in a:brs_allowable
41 let found = stridx(a:line, bracket, a:offset)
42 if found != -1 | let possible += [found] | endif
47 function! s:Defsplit(brs_allowable, single_line_comma, ...)
48 if a:0 == 0 | let skip = 0 | else | let skip = str2nr(a:1) | endif
49 let shift = g:defsplit_shift
50 let line = getline(".")
51 for i in range(len(line))
52 if line[i] != g:defsplit_shift[0]
53 let prfx = strpart(line, 0, i)
54 if line[i : i+3] ==# "def " ||
55 \line[i : i+5] ==# "class " ||
56 \line[i : i+9] ==# "async def "
62 let brs = {"(": ")", "[": "]", "{": "}"}
63 let brfirst = s:bracket_find(a:brs_allowable, line, 0)
64 let brlast = strridx(line, brs[line[brfirst]])
66 let brfirst = s:bracket_find(a:brs_allowable, line, brfirst + 1)
67 let brlast = strridx(line, brs[line[brfirst]], brlast - 1)
70 let [curly, round, squar, outbuf] = [0, 0, 0, ""]
71 let ready = [strpart(line, 0, brfirst + 1)]
72 let trailing_comma = 1
73 for c in split(line[brfirst + 1 : brlast-1], '\zs')
74 if c ==# "*" | let trailing_comma = 0 | endif
75 if outbuf ==# "" && c ==# " " | continue | endif
77 if c ==# "," && !curly && !round && !squar
78 let ready = add(ready, prfx . shift . outbuf)
80 elseif c ==# "[" | let squar += 1
81 elseif c ==# "]" | let squar -= 1
82 elseif c ==# "(" | let round += 1
83 elseif c ==# ")" | let round -= 1
84 elseif c ==# "{" | let curly += 1
85 elseif c ==# "}" | let curly -= 1
88 if trailing_comma && !(a:single_line_comma == v:true && len(ready) == 1)
89 let outbuf = outbuf . ","
91 let ready = add(ready, prfx . shift . outbuf)
92 let ready = add(ready, prfx . strpart(line, brlast))
93 call append(line("."), ready)
97 command! -nargs=? Defsplit call s:Defsplit(["("], v:false, <f-args>)
98 command! -nargs=? Brsplit call s:Defsplit(["(", "[", "{"], v:false, <f-args>)
99 command! -nargs=? Defsplits call s:Defsplit(["("], v:true, <f-args>)
100 command! -nargs=? Brsplits call s:Defsplit(["(", "[", "{"], v:true, <f-args>)
102 command! Undefsplit normal ^v%$J:keepp s/^\(.*\)\([([{]\) \(.*[^,]\),\?\([)\]}]\)\(.*\)$/\1\2\3\4\5<CR>:keepp s/, \?\([)\]}]\+\)$/\1/e<CR>:<CR>