Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Highlight occurrences of variable under text cursor

  • Loading branch information...
commit 6a0430cff4f4806a89732037437562ef3c36d0a0 1 parent 13e3597
Peter Odding authored July 29, 2010
13  luainspect.vim
... ...
@@ -1,8 +1,8 @@
1 1
 " Vim plug-in
2 2
 " Author: Peter Odding <peter@peterodding.com>
3  
-" Last Change: July 28, 2010
  3
+" Last Change: July 29, 2010
4 4
 " URL: http://peterodding.com/code/vim/lua-inspect/
5  
-" Version: 0.1.4
  5
+" Version: 0.1.5
6 6
 
7 7
 " Configuration defaults. {{{1
8 8
 
@@ -40,8 +40,11 @@ function! s:AutoEnable()
40 40
   end
41 41
 endfunction
42 42
 
43  
-function! s:RunLuaInspect()
44  
-  let l:text = join(getline(1, "$"), "\n")
  43
+function! s:RunLuaInspect() abort
  44
+  let lines = getline(1, "$")
  45
+  call insert(lines, col('.'))
  46
+  call insert(lines, line('.'))
  47
+  let l:text = join(lines, "\n")
45 48
   if has('lua') && g:lua_inspect_internal
46 49
     " Run LuaInspect using the Lua interface for Vim.
47 50
     redir => listing
@@ -79,6 +82,7 @@ function! s:ClearPreviousMatches()
79 82
   if hlexists('luaInspectLocal') | syntax clear luaInspectLocal | endif
80 83
   if hlexists('luaInspectFieldDefined') | syntax clear luaInspectFieldDefined | endif
81 84
   if hlexists('luaInspectFieldUndefined') | syntax clear luaInspectFieldUndefined | endif
  85
+  if hlexists('luaInspectSelectedVariable') | syntax clear luaInspectSelectedVariable | endif
82 86
 endfunction
83 87
 
84 88
 function! s:LoadDefaultStyles()
@@ -105,6 +109,7 @@ function! s:LoadDefaultStyles()
105 109
   highlight def link luaInspectLocal luaInspectDefLocal
106 110
   highlight def link luaInspectFieldDefined luaInspectDefFieldDefined
107 111
   highlight def link luaInspectFieldUndefined luaInspectDefFieldUndefined
  112
+  highlight def link luaInspectSelectedVariable Folded
108 113
 endfunction
109 114
 
110 115
 " vim: ts=2 sw=2 et
28  luainspect4vim.lua
@@ -3,7 +3,7 @@
3 3
  This module is part of the luainspect.vim plug-in for the Vim text editor.
4 4
 
5 5
  Author: Peter Odding <peter@peterodding.com>
6  
- Last Change: July 28, 2010
  6
+ Last Change: July 29, 2010
7 7
  URL: http://peterodding.com/code/vim/lua-inspect/
8 8
 
9 9
 --]]
@@ -23,16 +23,36 @@ else
23 23
   end
24 24
 end
25 25
 
  26
+local function getcurvar(notes, line, column)
  27
+  for i, note in ipairs(notes) do
  28
+    if note.ast.lineinfo then
  29
+      local l1, c1 = unpack(note.ast.lineinfo.first, 1, 2)
  30
+      local l2, c2 = unpack(note.ast.lineinfo.last, 1, 2)
  31
+      if l1 == line and column >= c1 and column <= c2 then
  32
+        if note.ast.id then return note end
  33
+      end
  34
+    end
  35
+  end
  36
+end
  37
+
26 38
 return function(text)
27 39
   local LI = require 'luainspect.init'
  40
+  -- Split input into current position and source text.
  41
+  local line, column, text = text:match '^(%d+)\n(%d+)\n(.*)$'
  42
+  line = tonumber(line)
  43
+  column = tonumber(column)
28 44
   text = LI.remove_shebang(text)
29 45
   local f, err, linenum, colnum, linenum2 = LI.loadstring(text)
30  
-  if not f then return end
  46
+  if not f then return end -- TODO Highlight syntax errors like spelling errors
31 47
   local ast; ast, err, linenum, colnum, linenum2 = LI.ast_from_string(text, "noname.lua")
32 48
   if not ast then return end
33  
-  for i, note in ipairs(LI.inspect(ast)) do
  49
+  local notes = LI.inspect(ast)
  50
+  local curvar = getcurvar(notes, line, column)
  51
+  for i, note in ipairs(notes) do
34 52
     local kind
35  
-    if note.type == 'global' then
  53
+    if curvar and curvar.ast.id == note.ast.id then
  54
+      kind = 'luaInspectSelectedVariable'
  55
+    elseif note.type == 'global' then
36 56
       if note.definedglobal then
37 57
         kind = 'luaInspectGlobalDefined'
38 58
       else

0 notes on commit 6a0430c

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