Skip to content

Commit

Permalink
'gd' mapping to jump to variable declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
xolox committed Aug 11, 2010
1 parent 0c52221 commit bb99531
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -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 `<F2>` with the text cursor on a variable then the plug-in will prompt you to rename the variable.
* Press `<F2>` 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.

Expand Down
2 changes: 1 addition & 1 deletion TODO.md
Expand Up @@ -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.
22 changes: 16 additions & 6 deletions luainspect.vim
Expand Up @@ -2,7 +2,7 @@
" Author: Peter Odding <peter@peterodding.com>
" 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.
Expand Down Expand Up @@ -55,7 +55,7 @@ let s:groups['SyntaxError'] = 'SpellBad'

" (Automatic) command definitions. {{{1

command! -bar -bang LuaInspect call s:run_lua_inspect('highlight', <q-bang> != '!')
command! -bar -bang LuaInspect call s:run_lua_inspect('highlight', 1, <q-bang> != '!')

augroup PluginLuaInspect
" Clear existing automatic commands.
Expand All @@ -77,13 +77,14 @@ endfunction
function! s:init_lua_buffer()
if s:check_plugin_enabled()
let b:easytags_nohl = 1
inoremap <buffer> <silent> <F2> <C-o>:call <Sid>run_lua_inspect('rename', 1)<CR>
nnoremap <buffer> <silent> <F2> :call <Sid>run_lua_inspect('rename', 1)<CR>
inoremap <buffer> <silent> <F2> <C-o>:call <Sid>run_lua_inspect('rename', 0, 1)<CR>
nnoremap <buffer> <silent> <F2> :call <Sid>run_lua_inspect('rename', 0, 1)<CR>
nnoremap <buffer> <silent> gd :call <Sid>run_lua_inspect('goto', 0, 1)<CR>
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('.'))
Expand All @@ -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!")
Expand Down
30 changes: 20 additions & 10 deletions luainspect4vim.lua
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bb99531

Please sign in to comment.