mirror of
https://github.com/davidhalter/jedi-vim.git
synced 2025-12-07 03:04:30 +08:00
huge functions refactoring
This commit is contained in:
56
jedi.vim
56
jedi.vim
@@ -16,10 +16,10 @@ if exists("g:loaded_jedi") || &cp
|
|||||||
endif
|
endif
|
||||||
let g:loaded_jedi = 1
|
let g:loaded_jedi = 1
|
||||||
|
|
||||||
|
|
||||||
" ------------------------------------------------------------------------
|
" ------------------------------------------------------------------------
|
||||||
" completion
|
" completion
|
||||||
" ------------------------------------------------------------------------
|
" ------------------------------------------------------------------------
|
||||||
|
|
||||||
function! jedi#complete(findstart, base)
|
function! jedi#complete(findstart, base)
|
||||||
python << PYTHONEOF
|
python << PYTHONEOF
|
||||||
if 1:
|
if 1:
|
||||||
@@ -32,7 +32,6 @@ if 1:
|
|||||||
count += 1
|
count += 1
|
||||||
vim.command('return %i' % (column - count))
|
vim.command('return %i' % (column - count))
|
||||||
else:
|
else:
|
||||||
buf_path = vim.current.buffer.name
|
|
||||||
base = vim.eval('a:base')
|
base = vim.eval('a:base')
|
||||||
source = ''
|
source = ''
|
||||||
for i, line in enumerate(vim.current.buffer):
|
for i, line in enumerate(vim.current.buffer):
|
||||||
@@ -45,7 +44,10 @@ if 1:
|
|||||||
# here again, the hacks, because jedi has a different interface than vim
|
# here again, the hacks, because jedi has a different interface than vim
|
||||||
column += len(base)
|
column += len(base)
|
||||||
try:
|
try:
|
||||||
completions, call_def = functions.complete(source, row, column, buf_path)
|
script = get_script(source=source, column=column)
|
||||||
|
completions = script.complete()
|
||||||
|
call_def = script.get_in_function_call()
|
||||||
|
|
||||||
out = []
|
out = []
|
||||||
for c in completions:
|
for c in completions:
|
||||||
d = dict(word=c.word[:len(base)] + c.complete,
|
d = dict(word=c.word[:len(base)] + c.complete,
|
||||||
@@ -72,21 +74,16 @@ if 1:
|
|||||||
PYTHONEOF
|
PYTHONEOF
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
" ------------------------------------------------------------------------
|
" ------------------------------------------------------------------------
|
||||||
" func_def
|
" func_def
|
||||||
" ------------------------------------------------------------------------
|
" ------------------------------------------------------------------------
|
||||||
function jedi#show_func_def()
|
function jedi#show_func_def()
|
||||||
python << PYTHONEOF
|
python show_func_def(get_script().get_in_function_call())
|
||||||
if 1:
|
|
||||||
row, column = vim.current.window.cursor
|
|
||||||
source = '\n'.join(vim.current.buffer)
|
|
||||||
buf_path = vim.current.buffer.name
|
|
||||||
call_def = functions.get_in_function_call(source, row, column, buf_path)
|
|
||||||
show_func_def(call_def)
|
|
||||||
PYTHONEOF
|
|
||||||
return ''
|
return ''
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
function jedi#clear_func_def()
|
function jedi#clear_func_def()
|
||||||
python << PYTHONEOF
|
python << PYTHONEOF
|
||||||
if 1:
|
if 1:
|
||||||
@@ -106,6 +103,7 @@ function! jedi#goto()
|
|||||||
python _goto()
|
python _goto()
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
" ------------------------------------------------------------------------
|
" ------------------------------------------------------------------------
|
||||||
" get_definition
|
" get_definition
|
||||||
" ------------------------------------------------------------------------
|
" ------------------------------------------------------------------------
|
||||||
@@ -113,6 +111,7 @@ function! jedi#get_definition()
|
|||||||
python _goto(is_definition=True)
|
python _goto(is_definition=True)
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
" ------------------------------------------------------------------------
|
" ------------------------------------------------------------------------
|
||||||
" related_names
|
" related_names
|
||||||
" ------------------------------------------------------------------------
|
" ------------------------------------------------------------------------
|
||||||
@@ -120,6 +119,7 @@ function! jedi#related_names()
|
|||||||
python _goto(is_related_name=True)
|
python _goto(is_related_name=True)
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
" ------------------------------------------------------------------------
|
" ------------------------------------------------------------------------
|
||||||
" rename
|
" rename
|
||||||
" ------------------------------------------------------------------------
|
" ------------------------------------------------------------------------
|
||||||
@@ -143,7 +143,6 @@ if 1:
|
|||||||
# reset autocommand
|
# reset autocommand
|
||||||
vim.command('autocmd! jedi_rename InsertLeave')
|
vim.command('autocmd! jedi_rename InsertLeave')
|
||||||
|
|
||||||
current_buf = vim.current.buffer.name
|
|
||||||
replace = vim.eval("expand('<cword>')")
|
replace = vim.eval("expand('<cword>')")
|
||||||
vim.command('normal! u') # undo new word
|
vim.command('normal! u') # undo new word
|
||||||
vim.command('normal! u') # 2u didn't work...
|
vim.command('normal! u') # 2u didn't work...
|
||||||
@@ -167,6 +166,7 @@ if 1:
|
|||||||
PYTHONEOF
|
PYTHONEOF
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
" ------------------------------------------------------------------------
|
" ------------------------------------------------------------------------
|
||||||
" show_pydoc
|
" show_pydoc
|
||||||
" ------------------------------------------------------------------------
|
" ------------------------------------------------------------------------
|
||||||
@@ -231,6 +231,7 @@ PYTHONEOF
|
|||||||
let b:current_syntax = "rst"
|
let b:current_syntax = "rst"
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
" ------------------------------------------------------------------------
|
" ------------------------------------------------------------------------
|
||||||
" helper functions
|
" helper functions
|
||||||
" ------------------------------------------------------------------------
|
" ------------------------------------------------------------------------
|
||||||
@@ -245,6 +246,7 @@ function! jedi#new_buffer(path)
|
|||||||
endif
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
function! jedi#tabnew(path)
|
function! jedi#tabnew(path)
|
||||||
python << PYTHONEOF
|
python << PYTHONEOF
|
||||||
if 1:
|
if 1:
|
||||||
@@ -271,6 +273,7 @@ if 1:
|
|||||||
PYTHONEOF
|
PYTHONEOF
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
function! s:add_goto_window()
|
function! s:add_goto_window()
|
||||||
set lazyredraw
|
set lazyredraw
|
||||||
cclose
|
cclose
|
||||||
@@ -283,6 +286,7 @@ function! s:add_goto_window()
|
|||||||
redraw!
|
redraw!
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
function! jedi#goto_window_on_enter()
|
function! jedi#goto_window_on_enter()
|
||||||
let l:list = getqflist()
|
let l:list = getqflist()
|
||||||
let l:data = l:list[line('.') - 1]
|
let l:data = l:list[line('.') - 1]
|
||||||
@@ -296,6 +300,7 @@ function! jedi#goto_window_on_enter()
|
|||||||
endif
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
function! jedi#syn_stack()
|
function! jedi#syn_stack()
|
||||||
if !exists("*synstack")
|
if !exists("*synstack")
|
||||||
return []
|
return []
|
||||||
@@ -303,6 +308,7 @@ function! jedi#syn_stack()
|
|||||||
return map(synstack(line('.'), col('.') - 1), 'synIDattr(v:val, "name")')
|
return map(synstack(line('.'), col('.') - 1), 'synIDattr(v:val, "name")')
|
||||||
endfunc
|
endfunc
|
||||||
|
|
||||||
|
|
||||||
function! jedi#do_popup_on_dot()
|
function! jedi#do_popup_on_dot()
|
||||||
let highlight_groups = jedi#syn_stack()
|
let highlight_groups = jedi#syn_stack()
|
||||||
for a in highlight_groups
|
for a in highlight_groups
|
||||||
@@ -435,21 +441,31 @@ class PythonToVimStr(str):
|
|||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '"%s"' % self.replace('"', r'\"')
|
return '"%s"' % self.replace('"', r'\"')
|
||||||
|
|
||||||
|
|
||||||
def echo_highlight(msg):
|
def echo_highlight(msg):
|
||||||
vim.command('echohl WarningMsg | echo "%s" | echohl None' % msg)
|
vim.command('echohl WarningMsg | echo "%s" | echohl None' % msg)
|
||||||
|
|
||||||
|
|
||||||
|
def get_script(source=None, column=None):
|
||||||
|
if source is None:
|
||||||
|
source = '\n'.join(vim.current.buffer)
|
||||||
|
row = vim.current.window.cursor[0]
|
||||||
|
if column is None:
|
||||||
|
column = vim.current.window.cursor[1]
|
||||||
|
buf_path = vim.current.buffer.name
|
||||||
|
return functions.Script(source, row, column, buf_path)
|
||||||
|
|
||||||
|
|
||||||
def _goto(is_definition=False, is_related_name=False, no_output=False):
|
def _goto(is_definition=False, is_related_name=False, no_output=False):
|
||||||
definitions = []
|
definitions = []
|
||||||
row, column = vim.current.window.cursor
|
script = get_script()
|
||||||
buf_path = vim.current.buffer.name
|
|
||||||
source = '\n'.join(vim.current.buffer)
|
|
||||||
try:
|
try:
|
||||||
if is_related_name:
|
if is_related_name:
|
||||||
definitions = functions.related_names(source, row, column, buf_path)
|
definitions = script.related_names()
|
||||||
elif is_definition:
|
elif is_definition:
|
||||||
definitions = functions.get_definition(source, row, column, buf_path)
|
definitions = script.get_definition()
|
||||||
else:
|
else:
|
||||||
definitions = functions.goto(source, row, column, buf_path)
|
definitions = script.goto()
|
||||||
except functions.NotFoundError:
|
except functions.NotFoundError:
|
||||||
echo_highlight("Cannot follow nothing. Put your cursor on a valid name.")
|
echo_highlight("Cannot follow nothing. Put your cursor on a valid name.")
|
||||||
except Exception:
|
except Exception:
|
||||||
@@ -512,7 +528,7 @@ def show_func_def(call_def, completion_lines=0):
|
|||||||
params = [p.get_code().replace('\n', '') for p in call_def.params]
|
params = [p.get_code().replace('\n', '') for p in call_def.params]
|
||||||
try:
|
try:
|
||||||
params[call_def.index] = '*%s*' % params[call_def.index]
|
params[call_def.index] = '*%s*' % params[call_def.index]
|
||||||
except IndexError:
|
except (IndexError, TypeError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
text = " (%s) " % ', '.join(params)
|
text = " (%s) " % ', '.join(params)
|
||||||
@@ -524,8 +540,6 @@ def show_func_def(call_def, completion_lines=0):
|
|||||||
repl = ("%s" + regex + "%s") % (line[:insert_column],
|
repl = ("%s" + regex + "%s") % (line[:insert_column],
|
||||||
line[insert_column:end_column], text, line[end_column:])
|
line[insert_column:end_column], text, line[end_column:])
|
||||||
vim.eval('setline(%s, "%s")' % (row_to_replace, repl))
|
vim.eval('setline(%s, "%s")' % (row_to_replace, repl))
|
||||||
#vim.command(r"%ss/^.\{%s\}/\1%s/g" % (row_to_replace, column, text))
|
|
||||||
|
|
||||||
PYTHONEOF
|
PYTHONEOF
|
||||||
|
|
||||||
" vim: set et ts=4:
|
" vim: set et ts=4:
|
||||||
|
|||||||
Reference in New Issue
Block a user