vim_config/ftplugin/python.vim

88 lines
2.5 KiB
VimL
Raw Permalink Normal View History

2021-06-11 19:35:22 +02:00
setlocal expandtab
setlocal colorcolumn=88
setlocal textwidth=88
" Enable comment continuation.
" setlocal formatoptions+=cro
" setlocal foldmethod=indent
" setlocal foldminlines=10
" setlocal textwidth=0
" Multi-line commenting and uncommenting.
" vmap <buffer> <C-m> :s/^\(\s*\)/\1#/<Return>
" vmap <buffer> <C-,> :s/^\(\s*\)#/\1/<Return>
" Use the AutoPythonImport tool.
" map <buffer> <C-n> :call AutoPythonImport(expand("<cword>"))<Return>
" Change the line length for Python files based on configuration files.
function! ChangePythonLineLength() abort
let l:conf = ale#path#FindNearestFile(bufnr(''), 'setup.cfg')
" Reset settings back to defaults when configuration files are not found
let l:line_length = 79
if !empty(l:conf)
for l:match in ale#util#GetMatches(
\ readfile(l:conf),
\ '\v^ *max-line-length *\= *(\d+)',
\)
let l:line_length = str2nr(l:match[1])
endfor
endif
let &l:colorcolumn = l:line_length + 1
endfunction
" call ChangePythonLineLength()
let b:ale_linters = ['flake8', 'mypy']
let b:ale_fixers = [
\ 'autopep8',
\ 'yapf',
\ 'black',
\]
let b:ale_completion_excluded_words = ['and', 'or', 'if']
" let g:ale_pattern_options = {'suite_py': {'ale_fixers': []}}
if expand('%:e') is# 'pyi'
let b:ale_linters = ['mypy']
endif
map <buffer> <silent> <F9> :TestFile<CR>
let s:virtualenv = ale#python#FindVirtualenv(bufnr(''))
if !empty(s:virtualenv)
if executable(s:virtualenv . '/bin/pytest')
let g:test#python#runner = 'pytest'
let g:test#python#pytest#executable =
\ ale#path#CdString(ale#path#Dirname(s:virtualenv))
\ . ale#Escape(s:virtualenv . '/bin/pytest')
else
let g:test#python#runner = 'djangotest'
let g:test#python#djangotest#executable =
\ ale#Escape(s:virtualenv . '/bin/python')
\ . ' ' . ale#Escape(ale#path#Dirname(s:virtualenv) . '/manage.py') . ' test'
endif
endif
if !exists("g:python_debugger")
let g:python_debugger = 'ipdb'
endif
let g:python_debug_line = 'import ' . g:python_debugger . '; ' . g:python_debugger . '.set_trace()'
function! InsertBreakpoint() abort
:normal o *
let l:curr_indent = indent(line(".")) - 1
let l:line = ''
:delete
:normal k
while l:curr_indent > 0
let l:line .= ' '
let l:curr_indent -= 1
endwhile
let l:line = l:line . g:python_debug_line
:put =l:line
endfunction
nnoremap <leader>d :call InsertBreakpoint()<CR>