Skip to content

Commit

Permalink
Highlight occurrences of variable under text cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
xolox committed Jul 29, 2010
1 parent 13e3597 commit 6a0430c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
13 changes: 9 additions & 4 deletions 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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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
28 changes: 24 additions & 4 deletions luainspect4vim.lua
Expand Up @@ -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/
--]]
Expand All @@ -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
Expand Down

0 comments on commit 6a0430c

Please sign in to comment.