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 function! s:bracket_find(brs_allowable, line, offset)
37 for bracket in a:brs_allowable
38 let found = stridx(a:line, bracket, a:offset)
39 if found != -1 | let possible += [found] | endif
44 function! defsplit#do(brs_allowable, single_line_comma, ...) abort
45 if a:0 == 0 | let skip = 0 | else | let skip = str2nr(a:1) | endif
46 let shift = get(b:, "defsplit_shift", " ")
47 let line = getline(".")
48 for i in range(len(line))
49 if line[i] != shift[0]
50 let prfx = strpart(line, 0, i)
51 if line[i : i+3] ==# "def " ||
52 \line[i : i+5] ==# "class " ||
53 \line[i : i+9] ==# "async def "
59 let brs = {"(": ")", "[": "]", "{": "}"}
60 let brfirst = s:bracket_find(a:brs_allowable, line, 0)
61 let brlast = strridx(line, brs[line[brfirst]])
63 let brfirst = s:bracket_find(a:brs_allowable, line, brfirst + 1)
64 let brlast = strridx(line, brs[line[brfirst]], brlast - 1)
67 let [curly, round, squar, outbuf] = [0, 0, 0, ""]
68 let ready = [strpart(line, 0, brfirst + 1)]
69 let trailing_comma = 1
70 for c in split(line[brfirst + 1 : brlast-1], '\zs')
71 if c ==# "*" | let trailing_comma = 0 | endif
72 if outbuf ==# "" && c ==# " " | continue | endif
74 if c ==# "," && !curly && !round && !squar
75 let ready = add(ready, prfx . shift . outbuf)
77 elseif c ==# "[" | let squar += 1
78 elseif c ==# "]" | let squar -= 1
79 elseif c ==# "(" | let round += 1
80 elseif c ==# ")" | let round -= 1
81 elseif c ==# "{" | let curly += 1
82 elseif c ==# "}" | let curly -= 1
85 if trailing_comma && !(a:single_line_comma == v:true && len(ready) == 1)
86 let outbuf = outbuf . ","
88 let ready = add(ready, prfx . shift . outbuf)
89 let ready = add(ready, prfx . strpart(line, brlast))
90 call append(line("."), ready)