From bb99531941ca10ffd9b77a2a9dcd83a354a022cf Mon Sep 17 00:00:00 2001 From: Peter Odding Date: Wed, 11 Aug 2010 15:53:57 +0200 Subject: [PATCH] 'gd' mapping to jump to variable declaration --- README.md | 4 +++- TODO.md | 2 +- luainspect.vim | 22 ++++++++++++++++------ luainspect4vim.lua | 30 ++++++++++++++++++++---------- 4 files changed, 40 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index c5f6224..8b482c4 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,9 @@ The Vim plug-in `luainspect.vim` uses the [LuaInspect](http://lua-users.org/wiki * If the text cursor is on a variable while the highlighting is refreshed then all occurrences of the variable will be marked in the style of [Vim's cursorline option](http://vimdoc.sourceforge.net/htmldoc/options.html#%27cursorline%27). - * If you press `` with the text cursor on a variable then the plug-in will prompt you to rename the variable. + * Press `` with the text cursor on a variable and the plug-in will prompt you to rename the variable. + + * Press `gd` (in normal mode) with the text cursor on a variable and you'll jump to its declaration / first occurrence. * When a syntax error is found (during highlighting or using the rename functionality) the lines where the error is reported will be marked like a spelling error. diff --git a/TODO.md b/TODO.md index 678440d..35a5b76 100644 --- a/TODO.md +++ b/TODO.md @@ -2,6 +2,6 @@ * Right now the highlighting styles used by `luainspect.vim` are the same as those used by the SciTE plug-in and they don't work well on dark backgrounds. As soon as I get around to picking some alternate colors I'll include those in the plug-in. - * Bindings for other features of LuaInspect such as showing tooltips for variables, goto definition (`gd` mapping) for variables, omni completion for in scope variables (including display of library function signatures), etc. This might be a lot of work but could prove to be really useful in making Lua easy to use in Vim. + * Bindings for other features of LuaInspect such as showing tooltips for variables, omni completion for in scope variables (including display of library function signatures), etc. This might be a lot of work but could prove to be really useful in making Lua easy to use in Vim. * Document the g:lua_inspect_path option. diff --git a/luainspect.vim b/luainspect.vim index 2898359..f70e4e8 100644 --- a/luainspect.vim +++ b/luainspect.vim @@ -2,7 +2,7 @@ " Author: Peter Odding " Last Change: August 11, 2010 " URL: http://peterodding.com/code/vim/lua-inspect/ -" Version: 0.3 +" Version: 0.3.2 " License: MIT " Support for automatic update using the GLVS plug-in. @@ -55,7 +55,7 @@ let s:groups['SyntaxError'] = 'SpellBad' " (Automatic) command definitions. {{{1 -command! -bar -bang LuaInspect call s:run_lua_inspect('highlight', != '!') +command! -bar -bang LuaInspect call s:run_lua_inspect('highlight', 1, != '!') augroup PluginLuaInspect " Clear existing automatic commands. @@ -77,13 +77,14 @@ endfunction function! s:init_lua_buffer() if s:check_plugin_enabled() let b:easytags_nohl = 1 - inoremap :call run_lua_inspect('rename', 1) - nnoremap :call run_lua_inspect('rename', 1) + inoremap :call run_lua_inspect('rename', 0, 1) + nnoremap :call run_lua_inspect('rename', 0, 1) + nnoremap gd :call run_lua_inspect('goto', 0, 1) endif endfunction -function! s:run_lua_inspect(action, enable) " {{{2 - if s:set_plugin_enabled(a:enable) +function! s:run_lua_inspect(action, toggle, enabled) " {{{2 + if !a:toggle || s:set_plugin_enabled(a:enabled) let lines = getline(1, "$") call insert(lines, col('.')) call insert(lines, line('.')) @@ -104,6 +105,15 @@ function! s:run_lua_inspect(action, enable) " {{{2 call s:define_default_styles() call s:clear_previous_matches() call s:highlight_variables() + elseif response == 'goto' + if len(b:luainspect_output) < 3 + call xolox#warning("No variable under cursor!") + else + let linenum = b:luainspect_output[1] + 0 + let colnum = b:luainspect_output[2] + 0 + call setpos('.', [0, linenum, colnum, 0]) + call xolox#message("") " Clear any previous message to avoid confusion. + endif elseif response == 'rename' if len(b:luainspect_output) == 1 call xolox#warning("No variable under cursor!") diff --git a/luainspect4vim.lua b/luainspect4vim.lua index a572933..ca26c9c 100644 --- a/luainspect4vim.lua +++ b/luainspect4vim.lua @@ -11,7 +11,7 @@ local LI = require 'luainspect.init' local LA = require 'luainspect.ast' -local myprint, getcurvar, highlight, rename +local actions, myprint, getcurvar = {} if type(vim) == 'table' and vim.eval then -- The Lua interface for Vim redefines print() so it prints inside Vim. @@ -35,8 +35,7 @@ function getcurvar(tokenlist, line, column) end end -function highlight(tokenlist, line, column) - myprint 'highlight' +function actions.highlight(tokenlist, line, column) local curvar = getcurvar(tokenlist, line, column) for i, token in ipairs(tokenlist) do local kind @@ -75,8 +74,20 @@ function highlight(tokenlist, line, column) end end -function rename(tokenlist, line, column) - myprint 'rename' +function actions.goto(tokenlist, line, column) + -- FIXME This only jumps to declaration of local / 1st occurrence of global. + local curvar = getcurvar(tokenlist, line, column) + for i, token in ipairs(tokenlist) do + if curvar and curvar.ast.id == token.ast.id then + local l1, c1 = unpack(token.ast.lineinfo.first, 1, 2) + myprint(l1) + myprint(c1) + break + end + end +end + +function actions.rename(tokenlist, line, column) local curvar = getcurvar(tokenlist, line, column) for i, token in ipairs(tokenlist) do if curvar and curvar.ast.id == token.ast.id then @@ -108,11 +119,10 @@ return function(src) -- Create a list of tokens from the AST and decorate it using luainspect. local tokenlist = LA.ast_to_tokenlist(ast, src) LI.inspect(ast, tokenlist) - -- Branch on the requested action. - if action == 'highlight' then - highlight(tokenlist, line, column) - elseif action == 'rename' then - rename(tokenlist, line, column) + -- Branch on requested action. + if actions[action] then + myprint(action) + actions[action](tokenlist, line, column) end end