]> git.madduck.net Git - etc/vim.git/blob - autoload/ale/fixers/generic_python.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Squashed '.vim/bundle/ale/' content from commit 22185c4c
[etc/vim.git] / autoload / ale / fixers / generic_python.vim
1 " Author: w0rp <devw0rp@gmail.com>
2 " Description: Generic fixer functions for Python.
3
4 " Add blank lines before control statements.
5 function! ale#fixers#generic_python#AddLinesBeforeControlStatements(buffer, lines) abort
6     let l:new_lines = []
7     let l:last_indent_size = 0
8     let l:last_line_is_blank = 0
9     let l:in_docstring = 0
10
11     for l:line in a:lines
12         let l:indent_size = len(matchstr(l:line, '^ *'))
13
14         if !l:in_docstring
15             " Make sure it is not just a single line docstring and then verify
16             " it's starting a new docstring
17             if match(l:line, '\v^ *("""|'''''').*("""|'''''')') == -1
18             \&& match(l:line, '\v^ *("""|'''''')') >= 0
19                 let l:in_docstring = 1
20             endif
21         else
22             if match(l:line, '\v^ *.*("""|'''''')') >= 0
23                 let l:in_docstring = 0
24             endif
25         endif
26
27         if !l:last_line_is_blank
28         \&& !l:in_docstring
29         \&& l:indent_size <= l:last_indent_size
30         \&& match(l:line, '\v^ *(return|if|for|while|break|continue)(\(| |$)') >= 0
31             call add(l:new_lines, '')
32         endif
33
34         call add(l:new_lines, l:line)
35         let l:last_indent_size = l:indent_size
36         let l:last_line_is_blank = empty(split(l:line))
37     endfor
38
39     return l:new_lines
40 endfunction
41
42 " This function breaks up long lines so that autopep8 or other tools can
43 " fix the badly-indented code which is produced as a result.
44 function! ale#fixers#generic_python#BreakUpLongLines(buffer, lines) abort
45     " Default to a maximum line length of 79
46     let l:max_line_length = 79
47     let l:conf = ale#path#FindNearestFile(a:buffer, 'setup.cfg')
48
49     " Read the maximum line length from setup.cfg
50     if !empty(l:conf)
51         for l:match in ale#util#GetMatches(
52         \   readfile(l:conf),
53         \   '\v^ *max-line-length *\= *(\d+)',
54         \)
55             let l:max_line_length = str2nr(l:match[1])
56         endfor
57     endif
58
59     let l:new_list = []
60
61     for l:line in a:lines
62         if len(l:line) > l:max_line_length && l:line !~# '# *noqa'
63             let l:line = substitute(l:line, '\v([(,])([^)])', '\1\n\2', 'g')
64             let l:line = substitute(l:line, '\v([^(])([)])', '\1,\n\2', 'g')
65
66             for l:split_line in split(l:line, "\n")
67                 call add(l:new_list, l:split_line)
68             endfor
69         else
70             call add(l:new_list, l:line)
71         endif
72     endfor
73
74     return l:new_list
75 endfunction