Skip to content

Commit

Permalink
Show warnings by LuaInspect in location list
Browse files Browse the repository at this point in the history
  • Loading branch information
xolox committed Aug 15, 2010
1 parent cca74e2 commit f0eeca2
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 17 deletions.
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -12,6 +12,8 @@ The Vim plug-in `luainspect.vim` uses the [LuaInspect](http://lua-users.org/wiki

* When luainspect reports a wrong argument count for a function call the text will be highlighted with a green underline. When you hover over the highlighted text a tooltip shows the associated warning message.

* When LuaInspect reports warnings about unused variables, wrong argument counts, etc. they are shown in a [location list window](http://vimdoc.sourceforge.net/htmldoc/quickfix.html#location-list).

* 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.

![Screenshot of semantic highlighting](http://peterodding.com/code/vim/luainspect/screenshot.png)
Expand Down Expand Up @@ -51,6 +53,12 @@ This variable isn't really an option but if you want to avoid loading the `luain

:let g:loaded_luainspect = 1

### The `g:lua_inspect_warnings` option

When LuaInspect reports warnings about unused variables, wrong argument counts, etc. they are automatically shown in a [location list window](http://vimdoc.sourceforge.net/htmldoc/quickfix.html#location-list). If you don't like this add the following to your [vimrc script](http://vimdoc.sourceforge.net/htmldoc/starting.html#vimrc):

:let g:lua_inspect_warnings = 0

### The `g:lua_inspect_events` option

By default semantic highlighting is automatically enabled after a short timeout and when you save a buffer. If you want to disable automatic highlighting altogether add the following to your [vimrc script](http://vimdoc.sourceforge.net/htmldoc/starting.html#vimrc):
Expand Down
14 changes: 3 additions & 11 deletions TODO.md
@@ -1,16 +1,8 @@
# The to-do list

* OMNI completion for in scope variables (including display of library function signatures).
* OMNI completion for in scope variables (including display of library function signatures). This will probably get complicated because most times when you want completion you'll already have typed half a statement, and LuaInspect will find syntax errors when trying to parse the source text. `scite.lua` can just use the last known valid AST but `luainspect4vim.lua` cannot keep this around when executed as an external process…
* Document g:lua_inspect_path option.
* Check whether "core/SciTE: jump to definition now supports functions in different files." is interesting.
* Use the new 'init.get_variable_details' function to replace most of actions.tooltip()? Can't do this until the references to `buffer` and `editor` are removed:
if ast.localmasking then
info = info .. "masking "
local fpos = LA.ast_pos_range(ast.localmasking, buffer.tokenlist)
if fpos then
local linenum0 = editor:LineFromPosition(fpos)
info = info .. "definition at line " .. (linenum0+1) .. " "
end
end
* Bug: Argument count warning tool tips are only shown for parts of the highlighted text.
* Bug: The plug-in warns `Invalid output from luainspect4vim.lua: 'This is an unknown table field.'`. Mixup between tool tip / highlight response parsing?!
* Bug: The plug-in sometimes warns `Invalid output from luainspect4vim.lua: 'This is an unknown table field.'`. Mixup between tool tip / highlight response parsing?!
* Bug: When you add some empty lines to the start of a Lua buffer the highlighting breaks! I haven't tracked this down completely yet but it looks to be a bug somewhere deep down inside of Metalua or LuaInspect `:-|`
42 changes: 40 additions & 2 deletions autoload.vim
Expand Up @@ -26,7 +26,9 @@ endfunction
function! luainspect#highlight_cmd(disable) " {{{1
if a:disable
call s:clear_previous_matches()
unlet! b:luainspect_input b:luainspect_output
unlet! b:luainspect_input
unlet! b:luainspect_output
unlet! b:luainspect_warnings
let b:luainspect_disabled = 1
else
unlet! b:luainspect_disabled
Expand Down Expand Up @@ -173,7 +175,10 @@ endfunction

function! s:highlight_variables() " {{{1
call clearmatches()
for line in b:luainspect_output[1:-1]
let num_warnings = b:luainspect_output[1] + 0
call s:update_warnings(num_warnings > 0 ? b:luainspect_output[2 : num_warnings+1] : [])
let other_output = b:luainspect_output[num_warnings+2 : -1]
for line in other_output
if s:check_output(line, '^\w\+\(\s\+\d\+\)\{4}$')
let [group, l1, c1, l2, c2] = split(line)
let l1 += 0
Expand All @@ -192,6 +197,39 @@ function! s:highlight_variables() " {{{1
endfor
endfunction

function! s:update_warnings(warnings) " {{{1
if !g:lua_inspect_warnings
return
endif
let list = []
for line in a:warnings
if s:check_output(line, '^line\s\+\d\+\s\+column\s\+\d\+\s\+-\s\+\S')
let fields = split(line)
let linenum = fields[1] + 0
let colnum = fields[3] + 0
let message = join(fields[5:-1])
call add(list, { 'bufnr': bufnr('%'), 'lnum': linenum, 'col': colnum, 'text': message })
endif
endfor
" Don't update the location list when it hasn't changed, because Vim will
" reset the highlighting of the current item in the location list!
if !exists('b:luainspect_warnings') || b:luainspect_warnings != list
call setloclist(winnr(), list, 'r')
let b:luainspect_warnings = list
endif
if !empty(list)
lopen
if winheight(winnr()) > 4
resize 4
endif
let warnings = len(list) > 1 ? 'warnings' : 'warning'
let w:quickfix_title = printf('%i %s reported by LuaInspect', len(list), warnings)
wincmd w
else
lclose
endif
endfunction

function! s:rename_variable() " {{{1
" Highlight occurrences of variable before rename.
let highlights = []
Expand Down
7 changes: 6 additions & 1 deletion luainspect.vim
Expand Up @@ -2,7 +2,7 @@
" Author: Peter Odding <peter@peterodding.com>
" Last Change: August 15, 2010
" URL: http://peterodding.com/code/vim/lua-inspect/
" Version: 0.3.11
" Version: 0.4
" License: MIT

" Support for automatic update using the GLVS plug-in.
Expand All @@ -13,6 +13,11 @@ if &cp || exists('g:loaded_luainspect')
finish
endif

if !exists('g:lua_inspect_warnings')
" Change this to disable automatic warning messages.
let g:lua_inspect_warnings = 1
endif

if !exists('g:lua_inspect_events')
" Change this to enable semantic highlighting on your preferred events.
let g:lua_inspect_events = 'CursorHold,CursorHoldI,BufWritePost'
Expand Down
12 changes: 9 additions & 3 deletions luainspect4vim.lua
Expand Up @@ -43,12 +43,17 @@ local function knownvarorfield(token) -- {{{1
return a.definedglobal or v.valueknown and v.value ~= nil
end
function actions.highlight(tokenlist, line, column) -- {{{1
function actions.highlight(tokenlist, line, column, src) -- {{{1
local function dump(token, hlgroup)
local l1, c1 = unpack(token.ast.lineinfo.first, 1, 2)
local l2, c2 = unpack(token.ast.lineinfo.last, 1, 2)
myprint(('%s %i %i %i %i'):format(hlgroup, l1, c1, l2, c2))
end
-- Print any warnings to show in Vim's quick-fix list.
-- FIXME Why does this report argument count warnings in luainspect/init.lua but not in example.lua?!
local warnings = LI.list_warnings(tokenlist, src)
myprint(#warnings)
for i, warning in ipairs(warnings) do myprint(warning) end
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 @@ -119,7 +124,7 @@ function actions.tooltip(tokenlist, line, column, src) -- {{{1
if details ~= '?' then
-- Convert variable type to readable sentence (friendlier to new users IMHO).
details = details:gsub('^[^\n]+', function(vartype)
local vartype = vartype:match '^%s*(.-)%s*$'
vartype = vartype:match '^%s*(.-)%s*$'
if vartype:find 'local$' or vartype:find 'global' then
vartype = vartype .. ' ' .. 'variable'
end
Expand Down Expand Up @@ -178,7 +183,8 @@ end
-- }}}
return function(src)
local action, file, line, column, src = src:match '^(%S+)\n([^\n]+)\n(%d+)\n(%d+)\n(.*)$'
local action, file, line, column
action, file, line, column, src = src:match '^(%S+)\n([^\n]+)\n(%d+)\n(%d+)\n(.*)$'
line = tonumber(line)
column = tonumber(column)
src = LA.remove_shebang(src)
Expand Down

0 comments on commit f0eeca2

Please sign in to comment.