Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

'gd' mapping to jump to variable declaration

  • Loading branch information...
commit bb99531941ca10ffd9b77a2a9dcd83a354a022cf 1 parent 0c52221
Peter Odding authored August 11, 2010
4  README.md
Source Rendered
@@ -4,7 +4,9 @@ The Vim plug-in `luainspect.vim` uses the [LuaInspect](http://lua-users.org/wiki
4 4
 
5 5
  * 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).
6 6
 
7  
- * If you press `<F2>` with the text cursor on a variable then the plug-in will prompt you to rename the variable.
  7
+ * Press `<F2>` with the text cursor on a variable and the plug-in will prompt you to rename the variable.
  8
+
  9
+ * Press `gd` (in normal mode) with the text cursor on a variable and you'll jump to its declaration / first occurrence.
8 10
 
9 11
  * 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.
10 12
 
2  TODO.md
Source Rendered
@@ -2,6 +2,6 @@
2 2
 
3 3
  * 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.
4 4
 
5  
- * 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.
  5
+ * 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.
6 6
 
7 7
  * Document the g:lua_inspect_path option.
22  luainspect.vim
@@ -2,7 +2,7 @@
2 2
 " Author: Peter Odding <peter@peterodding.com>
3 3
 " Last Change: August 11, 2010
4 4
 " URL: http://peterodding.com/code/vim/lua-inspect/
5  
-" Version: 0.3
  5
+" Version: 0.3.2
6 6
 " License: MIT
7 7
 
8 8
 " Support for automatic update using the GLVS plug-in.
@@ -55,7 +55,7 @@ let s:groups['SyntaxError'] = 'SpellBad'
55 55
 
56 56
 " (Automatic) command definitions. {{{1
57 57
 
58  
-command! -bar -bang LuaInspect call s:run_lua_inspect('highlight', <q-bang> != '!')
  58
+command! -bar -bang LuaInspect call s:run_lua_inspect('highlight', 1, <q-bang> != '!')
59 59
 
60 60
 augroup PluginLuaInspect
61 61
   " Clear existing automatic commands.
@@ -77,13 +77,14 @@ endfunction
77 77
 function! s:init_lua_buffer()
78 78
   if s:check_plugin_enabled()
79 79
     let b:easytags_nohl = 1
80  
-    inoremap <buffer> <silent> <F2> <C-o>:call <Sid>run_lua_inspect('rename', 1)<CR>
81  
-    nnoremap <buffer> <silent> <F2> :call <Sid>run_lua_inspect('rename', 1)<CR>
  80
+    inoremap <buffer> <silent> <F2> <C-o>:call <Sid>run_lua_inspect('rename', 0, 1)<CR>
  81
+    nnoremap <buffer> <silent> <F2> :call <Sid>run_lua_inspect('rename', 0, 1)<CR>
  82
+    nnoremap <buffer> <silent> gd :call <Sid>run_lua_inspect('goto', 0, 1)<CR>
82 83
   endif
83 84
 endfunction
84 85
 
85  
-function! s:run_lua_inspect(action, enable) " {{{2
86  
-  if s:set_plugin_enabled(a:enable)
  86
+function! s:run_lua_inspect(action, toggle, enabled) " {{{2
  87
+  if !a:toggle || s:set_plugin_enabled(a:enabled)
87 88
     let lines = getline(1, "$")
88 89
     call insert(lines, col('.'))
89 90
     call insert(lines, line('.'))
@@ -104,6 +105,15 @@ function! s:run_lua_inspect(action, enable) " {{{2
104 105
         call s:define_default_styles()
105 106
         call s:clear_previous_matches()
106 107
         call s:highlight_variables()
  108
+      elseif response == 'goto'
  109
+        if len(b:luainspect_output) < 3
  110
+          call xolox#warning("No variable under cursor!")
  111
+        else
  112
+          let linenum = b:luainspect_output[1] + 0
  113
+          let colnum = b:luainspect_output[2] + 0
  114
+          call setpos('.', [0, linenum, colnum, 0])
  115
+          call xolox#message("") " Clear any previous message to avoid confusion.
  116
+        endif
107 117
       elseif response == 'rename'
108 118
         if len(b:luainspect_output) == 1
109 119
           call xolox#warning("No variable under cursor!")
30  luainspect4vim.lua
@@ -11,7 +11,7 @@
11 11
 
12 12
 local LI = require 'luainspect.init'
13 13
 local LA = require 'luainspect.ast'
14  
-local myprint, getcurvar, highlight, rename
  14
+local actions, myprint, getcurvar = {}
15 15
 
16 16
 if type(vim) == 'table' and vim.eval then
17 17
   -- The Lua interface for Vim redefines print() so it prints inside Vim.
@@ -35,8 +35,7 @@ function getcurvar(tokenlist, line, column)
35 35
   end
36 36
 end
37 37
 
38  
-function highlight(tokenlist, line, column)
39  
-  myprint 'highlight'
  38
+function actions.highlight(tokenlist, line, column)
40 39
   local curvar = getcurvar(tokenlist, line, column)
41 40
   for i, token in ipairs(tokenlist) do
42 41
     local kind
@@ -75,8 +74,20 @@ function highlight(tokenlist, line, column)
75 74
   end
76 75
 end
77 76
 
78  
-function rename(tokenlist, line, column)
79  
-  myprint 'rename'
  77
+function actions.goto(tokenlist, line, column)
  78
+  -- FIXME This only jumps to declaration of local / 1st occurrence of global.
  79
+  local curvar = getcurvar(tokenlist, line, column)
  80
+  for i, token in ipairs(tokenlist) do
  81
+    if curvar and curvar.ast.id == token.ast.id then
  82
+      local l1, c1 = unpack(token.ast.lineinfo.first, 1, 2)
  83
+      myprint(l1)
  84
+      myprint(c1)
  85
+      break
  86
+    end
  87
+  end
  88
+end
  89
+
  90
+function actions.rename(tokenlist, line, column)
80 91
   local curvar = getcurvar(tokenlist, line, column)
81 92
   for i, token in ipairs(tokenlist) do
82 93
     if curvar and curvar.ast.id == token.ast.id then
@@ -108,11 +119,10 @@ return function(src)
108 119
   -- Create a list of tokens from the AST and decorate it using luainspect.
109 120
   local tokenlist = LA.ast_to_tokenlist(ast, src)
110 121
   LI.inspect(ast, tokenlist)
111  
-  -- Branch on the requested action.
112  
-  if action == 'highlight' then
113  
-    highlight(tokenlist, line, column)
114  
-  elseif action == 'rename' then
115  
-    rename(tokenlist, line, column)
  122
+  -- Branch on requested action.
  123
+  if actions[action] then
  124
+    myprint(action)
  125
+    actions[action](tokenlist, line, column)
116 126
   end
117 127
 end
118 128
 

0 notes on commit bb99531

Please sign in to comment.
Something went wrong with that request. Please try again.