"============================================================================
" File: pythontextobj.vim
" Author: Nat Williams
" License: I dunno
" Description: Adds text objects for Python classes and functions.
" Credits: Most code pretty much copied straight out of Alfredo Deza's
" chapa.vim (https://github.com/alfredodeza/chapa.vim)
" Also Austin Taylor's indentobj
" (https://github.com/austintaylor/vim-indentobject)
"============================================================================
if (exists("g:loaded_pythontextobj") && g:loaded_pythontextobj)
finish
endif
let g:loaded_pythontextobj = 1
onoremap af :call FunctionTextObject(0)
onoremap if :call FunctionTextObject(1)
vnoremap af :call FunctionTextObject(0)gv
vnoremap if :call FunctionTextObject(1)gv
onoremap ac :call ClassTextObject(0)
onoremap ic :call ClassTextObject(1)
vnoremap ac :call ClassTextObject(0)gv
vnoremap ic :call ClassTextObject(1)gv
" Select an object ("class"/"function")
function! s:PythonSelectObject(obj, inner)
" find definition line
let start_line = s:FindPythonObjectStart(a:obj)
if (! start_line)
return
endif
" get end (w/ or w/out whitespace)
let until = s:ObjectEnd(start_line, a:inner)
" include decorators
if (! a:inner)
let start_line = s:StartDecorators(start_line)
endif
" select range
let line_moves = until - start_line
exec start_line
if line_moves > 0
execute "normal V" . line_moves . "j"
else
execute "normal VG"
endif
endfunction
function! s:ObjectEnd(start, inner)
let objend = s:NextIndent(a:start)
if a:inner
let objend = prevnonblank(objend)
endif
return objend
endfunction
function! s:NextIndent(start)
let line = a:start
let lastline = line('$')
let indent = indent(line)
while (line > 0 && line 1)
exec line(".") - 1
endwhile
let cursor_indent = indent(line("."))
if (a:obj == "class")
let objregexp = "^\\s*class\\s\\+[a-zA-Z0-9_]\\+"
\ . "\\s*\\((\\([a-zA-Z0-9_,. \\t\\n]\\)*)\\)\\=\\s*:"
else
let objregexp = "^\\s*def\\s\\+[a-zA-Z0-9_]\\+\\s*(\\_[^:#]*)\\s*:"
endif
let found = 0
while (! found)
normal $
let result = search(objregexp, "Wbcn")
if (! result)
return
endif
if indent(result)