]> Sergey Matveev's repositories - dotfiles.git/blob - vim/.vim/autoload/defsplit.vim
Vim scripts refactoring
[dotfiles.git] / vim / .vim / autoload / defsplit.vim
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
4 "
5 " This plugin splits Python function call on several lines.
6 "
7 "   def foobar(self, foo: str, bar: Some[thing, too]) -> None:
8 " to
9 "   def foobar(
10 "           self,
11 "           foo: str,
12 "           bar: Some[thing, too],
13 "   ) -> None:
14 "
15 "   foo(bar, baz)[0]
16 " to
17 "   foo(
18 "       bar,
19 "       baz,
20 "   )[0]
21 "
22 " You can un-split it using :Undefsplit command on the line where
23 " splitting starts.
24 "
25 " :Defsplit has optional argument specifying how many opening round
26 " parenthesis must be skipped.
27 " :Defsplit 1 on foo(baz(baz(...))) produces
28 "    foo(baz(
29 "        baz(...),
30 "    ))
31 "
32 " Also there is :Brsplit command behaving similarly, but it splits other
33 " types of brackets: "{}", "[]".
34
35 function! s:bracket_find(brs_allowable, line, offset)
36     let possible = []
37     for bracket in a:brs_allowable
38         let found = stridx(a:line, bracket, a:offset)
39         if found != -1 | let possible += [found] | endif
40     endfor
41     return min(possible)
42 endfunction
43
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 "
54                 let shift .= shift
55             endif
56             break
57         endif
58     endfor
59     let brs = {"(": ")", "[": "]", "{": "}"}
60     let brfirst = s:bracket_find(a:brs_allowable, line, 0)
61     let brlast = strridx(line, brs[line[brfirst]])
62     while skip > 0
63         let brfirst = s:bracket_find(a:brs_allowable, line, brfirst + 1)
64         let brlast = strridx(line, brs[line[brfirst]], brlast - 1)
65         let skip -= 1
66     endwhile
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
73         let outbuf .= c
74         if c ==# "," && !curly && !round && !squar
75             let ready = add(ready, prfx . shift . outbuf)
76             let 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
83         endif
84     endfor
85     if trailing_comma && !(a:single_line_comma == v:true && len(ready) == 1)
86         let outbuf = outbuf . ","
87     endif
88     let ready = add(ready, prfx . shift . outbuf)
89     let ready = add(ready, prfx . strpart(line, brlast))
90     call append(line("."), ready)
91     normal "_dd
92 endfunction