Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Highlight occurrences of variable under text cursor

  • Loading branch information...
commit 6a0430cff4f4806a89732037437562ef3c36d0a0 1 parent 13e3597
@xolox authored
Showing with 33 additions and 8 deletions.
  1. +9 −4 luainspect.vim
  2. +24 −4 luainspect4vim.lua
View
13 luainspect.vim
@@ -1,8 +1,8 @@
" Vim plug-in
" Author: Peter Odding <peter@peterodding.com>
-" Last Change: July 28, 2010
+" Last Change: July 29, 2010
" URL: http://peterodding.com/code/vim/lua-inspect/
-" Version: 0.1.4
+" Version: 0.1.5
" Configuration defaults. {{{1
@@ -40,8 +40,11 @@ function! s:AutoEnable()
end
endfunction
-function! s:RunLuaInspect()
- let l:text = join(getline(1, "$"), "\n")
+function! s:RunLuaInspect() abort
+ let lines = getline(1, "$")
+ call insert(lines, col('.'))
+ call insert(lines, line('.'))
+ let l:text = join(lines, "\n")
if has('lua') && g:lua_inspect_internal
" Run LuaInspect using the Lua interface for Vim.
redir => listing
@@ -79,6 +82,7 @@ function! s:ClearPreviousMatches()
if hlexists('luaInspectLocal') | syntax clear luaInspectLocal | endif
if hlexists('luaInspectFieldDefined') | syntax clear luaInspectFieldDefined | endif
if hlexists('luaInspectFieldUndefined') | syntax clear luaInspectFieldUndefined | endif
+ if hlexists('luaInspectSelectedVariable') | syntax clear luaInspectSelectedVariable | endif
endfunction
function! s:LoadDefaultStyles()
@@ -105,6 +109,7 @@ function! s:LoadDefaultStyles()
highlight def link luaInspectLocal luaInspectDefLocal
highlight def link luaInspectFieldDefined luaInspectDefFieldDefined
highlight def link luaInspectFieldUndefined luaInspectDefFieldUndefined
+ highlight def link luaInspectSelectedVariable Folded
endfunction
" vim: ts=2 sw=2 et
View
28 luainspect4vim.lua
@@ -3,7 +3,7 @@
This module is part of the luainspect.vim plug-in for the Vim text editor.
Author: Peter Odding <peter@peterodding.com>
- Last Change: July 28, 2010
+ Last Change: July 29, 2010
URL: http://peterodding.com/code/vim/lua-inspect/
--]]
@@ -23,16 +23,36 @@ else
end
end
+local function getcurvar(notes, line, column)
+ for i, note in ipairs(notes) do
+ if note.ast.lineinfo then
+ local l1, c1 = unpack(note.ast.lineinfo.first, 1, 2)
+ local l2, c2 = unpack(note.ast.lineinfo.last, 1, 2)
+ if l1 == line and column >= c1 and column <= c2 then
+ if note.ast.id then return note end
+ end
+ end
+ end
+end
+
return function(text)
local LI = require 'luainspect.init'
+ -- Split input into current position and source text.
+ local line, column, text = text:match '^(%d+)\n(%d+)\n(.*)$'
+ line = tonumber(line)
+ column = tonumber(column)
text = LI.remove_shebang(text)
local f, err, linenum, colnum, linenum2 = LI.loadstring(text)
- if not f then return end
+ if not f then return end -- TODO Highlight syntax errors like spelling errors
local ast; ast, err, linenum, colnum, linenum2 = LI.ast_from_string(text, "noname.lua")
if not ast then return end
- for i, note in ipairs(LI.inspect(ast)) do
+ local notes = LI.inspect(ast)
+ local curvar = getcurvar(notes, line, column)
+ for i, note in ipairs(notes) do
local kind
- if note.type == 'global' then
+ if curvar and curvar.ast.id == note.ast.id then
+ kind = 'luaInspectSelectedVariable'
+ elseif note.type == 'global' then
if note.definedglobal then
kind = 'luaInspectGlobalDefined'
else
Please sign in to comment.
Something went wrong with that request. Please try again.