Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Bug fix: Show error highlighting only in Lua buffers

When luainspect.vim requests a tool tip text from luainspect4vim.lua but
a syntax error is found then no tool tips are available, however the
syntax error is reported so that it can be highlighted as such. This
feature and its sequence of events introduced the following bug:

 1. Open a Lua buffer.
 2. Create a syntax error in the Lua buffer.
 2. Create a split window showing a non-Lua buffer.
 3. With the focus on the non-Lua buffer, hover over the split window
    showing the Lua buffer and wait for the tool tip to appear.
 4. The tool tip will be correct but the syntax error highlighting is
    now applied in the non-Lua buffer: Clearly not the intention :-)

This commit fixes the problem. I've also added calls to my xolox#timer
functions so that, when you execute the following command:

    :verbose LuaInspect

You get a message with the time spent inside luainspect4vim.lua and the
overhead imposed by the communication between Vim and Lua.
  • Loading branch information...
commit 2d671c489bdf42dfe68b289588c09e0a18c7ce0b 1 parent c6a8ecf
Peter Odding authored August 12, 2010
2  README.md
Source Rendered
@@ -10,7 +10,7 @@ The Vim plug-in `luainspect.vim` uses the [LuaInspect](http://lua-users.org/wiki
10 10
 
11 11
  * 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).
12 12
 
13  
- * When luainspect reports a wrong argument count for a function call the call will be highlighted with a green underline. When you hover over the highlighted text a tooltip shows the associated warning message.
  13
+ * 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.
14 14
 
15 15
  * 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.
16 16
 
1  TODO.md
Source Rendered
@@ -4,4 +4,3 @@
4 4
  * Document g:lua_inspect_path option.
5 5
  * Check whether "core/SciTE: jump to definition now supports functions in different files." is interesting.
6 6
  * Use the new 'init.get_variable_details' function to replace most of actions.tooltip()?
7  
- * BUG: With a Lua script in one split window and a different file type in another split window, where the active window contains the non-Lua file, if you hover over the Lua window syntax error highlighting is applied to the non-Lua buffer! Hint: getbufline() / assert(v:beval_bufnr==bufnr('%'))
145  luainspect.vim
@@ -13,6 +13,8 @@ if &cp || exists('g:loaded_luainspect')
13 13
   finish
14 14
 endif
15 15
 
  16
+let s:script = expand('<sfile>:p:~')
  17
+
16 18
 " Configuration defaults. {{{1
17 19
 
18 20
 if !exists('g:lua_inspect_events')
@@ -56,37 +58,54 @@ let s:groups['WrongArgCount'] = 'SpellLocal'
56 58
 
57 59
 " (Automatic) command definitions. {{{1
58 60
 
59  
-command! -bar -bang LuaInspect call s:run_lua_inspect('highlight', 1, <q-bang> != '!')
  61
+command! -bar -bang LuaInspect call s:luainspect_command(<q-bang> == '!')
60 62
 
61 63
 augroup PluginLuaInspect
62 64
   " Clear existing automatic commands.
63 65
   autocmd!
64  
-  " Disable easytags.vim because it doesn't play nice with luainspect.vim!
65  
-  autocmd BufNewFile,BufReadPost,BufWritePost * call s:init_lua_buffer()
66  
-  " Define the configured automatic commands.
  66
+  " Automatically enable the plug-in on these events.
  67
+  autocmd BufNewFile,BufReadPost,BufWritePost * call s:auto_enable_plugin()
  68
+  " Define the configured automatic commands for highlighting.
67 69
   for s:event in split(g:lua_inspect_events, ',')
68  
-    execute 'autocmd' s:event '* if s:check_plugin_enabled() | LuaInspect | endif'
  70
+    execute 'autocmd' s:event '* if s:check_plugin_valid() | LuaInspect | endif'
69 71
   endfor
  72
+  unlet s:event
70 73
 augroup END
71 74
 
72 75
 " Script local functions. {{{1
73 76
 
74  
-function! s:check_plugin_enabled()
  77
+function! s:check_plugin_valid() " {{{2
75 78
   return &ft == 'lua' && !&diff && !exists('b:luainspect_disabled')
76 79
 endfunction
77 80
 
78  
-function! s:init_lua_buffer()
79  
-  if s:check_plugin_enabled()
  81
+function! s:luainspect_command(disable) " {{{2
  82
+  if a:disable
  83
+    call s:clear_previous_matches()
  84
+    unlet! b:luainspect_input b:luainspect_output
  85
+    let b:luainspect_disabled = 1
  86
+  else
  87
+    unlet! b:luainspect_disabled
  88
+    let starttime = xolox#timer#start()
  89
+    call s:run_lua_inspect('highlight')
  90
+    let bufname = expand('%:p:~')
  91
+    let msg = "%s: Highlighted %s in %s."
  92
+    call xolox#timer#stop(msg, s:script, bufname, starttime)
  93
+  endif
  94
+endfunction
  95
+
  96
+function! s:auto_enable_plugin() " {{{2
  97
+  if s:check_plugin_valid()
  98
+    " Disable easytags.vim because it doesn't play nice with luainspect.vim!
80 99
     let b:easytags_nohl = 1
81  
-    inoremap <buffer> <silent> <F2> <C-o>:call <Sid>run_lua_inspect('rename', 0, 1)<CR>
82  
-    nnoremap <buffer> <silent> <F2> :call <Sid>run_lua_inspect('rename', 0, 1)<CR>
83  
-    nnoremap <buffer> <silent> gd :call <Sid>run_lua_inspect('goto', 0, 1)<CR>
  100
+    inoremap <buffer> <silent> <F2> <C-o>:call <Sid>run_lua_inspect('rename')<CR>
  101
+    nnoremap <buffer> <silent> <F2> :call <Sid>run_lua_inspect('rename')<CR>
  102
+    nnoremap <buffer> <silent> gd :call <Sid>run_lua_inspect('goto')<CR>
84 103
     setlocal ballooneval balloonexpr=LuaInspectToolTip()
85 104
   endif
86 105
 endfunction
87 106
 
88 107
 function! LuaInspectToolTip() " {{{2
89  
-  let text = s:run_lua_inspect('tooltip', 0, 1)
  108
+  let text = s:run_lua_inspect('tooltip')
90 109
   if exists('b:luainspect_syntax_error')
91 110
     return b:luainspect_syntax_error
92 111
   else
@@ -94,71 +113,63 @@ function! LuaInspectToolTip() " {{{2
94 113
   endif
95 114
 endfunction
96 115
 
97  
-function! s:run_lua_inspect(action, toggle, enabled) " {{{2
98  
-  if !a:toggle || s:set_plugin_enabled(a:enabled)
  116
+function! s:run_lua_inspect(action) " {{{2
  117
+  if a:action == 'tooltip'
  118
+    let lines = getbufline(v:beval_bufnr, 1, "$")
  119
+    call insert(lines, v:beval_col)
  120
+    call insert(lines, v:beval_lnum)
  121
+  else
99 122
     let lines = getline(1, "$")
100  
-    if a:action == 'tooltip'
101  
-      call insert(lines, v:beval_col)
102  
-      call insert(lines, v:beval_lnum)
103  
-    else
104  
-      call insert(lines, col('.'))
105  
-      call insert(lines, line('.'))
  123
+    call insert(lines, col('.'))
  124
+    call insert(lines, line('.'))
  125
+  endif
  126
+  call insert(lines, a:action)
  127
+  call s:parse_text(join(lines, "\n"), s:prepare_search_path())
  128
+  if !empty(b:luainspect_output)
  129
+    let response = b:luainspect_output[0]
  130
+    if response == 'syntax_error' && len(b:luainspect_output) >= 4
  131
+      " Never perform syntax error highlighting in non-Lua buffers!
  132
+      let linenum = b:luainspect_output[1] + 0
  133
+      let colnum = b:luainspect_output[2] + 0
  134
+      let linenum2 = b:luainspect_output[3] + 0
  135
+      let b:luainspect_syntax_error = b:luainspect_output[4]
  136
+      if a:action != 'tooltip' || v:beval_bufnr == bufnr('%')
  137
+        let error_cmd = 'syntax match luaInspectSyntaxError /\%%>%il\%%<%il.*/ containedin=ALLBUT,lua*Comment*'
  138
+        execute printf(error_cmd, linenum - 1, (linenum2 ? linenum2 : line('$')) + 1)
  139
+      endif
  140
+      " But always let the user know that a syntax error exists.
  141
+      let bufname = fnamemodify(bufname(a:action != 'tooltip' ? '%' : v:beval_bufnr), ':p:~')
  142
+      call xolox#warning("Syntax error around line %i in %s: %s", linenum, bufname, b:luainspect_syntax_error)
  143
+      return
106 144
     endif
107  
-    call insert(lines, a:action)
108  
-    call s:parse_text(join(lines, "\n"), s:prepare_search_path())
109  
-    if !empty(b:luainspect_output)
110  
-      let response = b:luainspect_output[0]
111  
-      if response == 'syntax_error' && len(b:luainspect_output) >= 4
  145
+    unlet! b:luainspect_syntax_error
  146
+    if response == 'highlight'
  147
+      call s:define_default_styles()
  148
+      call s:clear_previous_matches()
  149
+      call s:highlight_variables()
  150
+    elseif response == 'goto'
  151
+      if len(b:luainspect_output) < 3
  152
+        call xolox#warning("No variable under cursor!")
  153
+      else
112 154
         let linenum = b:luainspect_output[1] + 0
113 155
         let colnum = b:luainspect_output[2] + 0
114  
-        let linenum2 = b:luainspect_output[3] + 0
115  
-        let b:luainspect_syntax_error = b:luainspect_output[4]
116  
-        let error_cmd = 'syntax match luaInspectSyntaxError /\%%>%il\%%<%il.*/ containedin=ALLBUT,lua*Comment*'
117  
-        execute printf(error_cmd, linenum - 1, (linenum2 ? linenum2 : line('$')) + 1)
118  
-        call xolox#warning("Syntax error around line %i: %s", linenum, b:luainspect_syntax_error)
119  
-        return
  156
+        call setpos('.', [0, linenum, colnum, 0])
  157
+        call xolox#message("") " Clear any previous message to avoid confusion.
120 158
       endif
121  
-      unlet! b:luainspect_syntax_error
122  
-      if response == 'highlight'
123  
-        call s:define_default_styles()
124  
-        call s:clear_previous_matches()
125  
-        call s:highlight_variables()
126  
-      elseif response == 'goto'
127  
-        if len(b:luainspect_output) < 3
128  
-          call xolox#warning("No variable under cursor!")
129  
-        else
130  
-          let linenum = b:luainspect_output[1] + 0
131  
-          let colnum = b:luainspect_output[2] + 0
132  
-          call setpos('.', [0, linenum, colnum, 0])
133  
-          call xolox#message("") " Clear any previous message to avoid confusion.
134  
-        endif
135  
-      elseif response == 'tooltip'
136  
-        if len(b:luainspect_output) > 1
137  
-          return join(b:luainspect_output[1:-1], "\n")
138  
-        endif
139  
-      elseif response == 'rename'
140  
-        if len(b:luainspect_output) == 1
141  
-          call xolox#warning("No variable under cursor!")
142  
-        else
143  
-          call s:rename_variable()
144  
-        endif
  159
+    elseif response == 'tooltip'
  160
+      if len(b:luainspect_output) > 1
  161
+        return join(b:luainspect_output[1:-1], "\n")
  162
+      endif
  163
+    elseif response == 'rename'
  164
+      if len(b:luainspect_output) == 1
  165
+        call xolox#warning("No variable under cursor!")
  166
+      else
  167
+        call s:rename_variable()
145 168
       endif
146 169
     endif
147 170
   endif
148 171
 endfunction
149 172
 
150  
-function! s:set_plugin_enabled(enabled) " {{{2
151  
-  if a:enabled
152  
-    unlet! b:luainspect_disabled
153  
-    return 1
154  
-  else
155  
-    call s:clear_previous_matches()
156  
-    unlet! b:luainspect_input b:luainspect_output
157  
-    let b:luainspect_disabled = 1
158  
-    return 0
159  
-  endif
160  
-endfunction
161  
-
162 173
 function! s:prepare_search_path() " {{{2
163 174
   let code = ''
164 175
   if !(has('lua') && g:lua_inspect_internal && exists('s:changed_path'))
33  luainspect4vim.lua
@@ -83,25 +83,34 @@ end
83 83
 
84 84
 function actions.tooltip(tokenlist, line, column) -- {{{1
85 85
   local did_details = false
  86
+  local note
86 87
   for i, token in ipairs(tokenlist) do
87 88
     if token.ast.lineinfo then
88 89
       local l1, c1 = unpack(token.ast.lineinfo.first, 1, 2)
89 90
       local l2, c2 = unpack(token.ast.lineinfo.last, 1, 2)
90 91
       if l1 == line then
91 92
         local ast = token.ast
92  
-        if ast and ast.id and column >= c1 and column <= c2 and not did_details then
93  
-          printvartype(token)
94  
-          printsignature(ast)
95  
-          printvalue(ast)
96  
-          did_details = true
97  
-        end
98  
-        if ast.note then
99  
-          if ast.note:find '[Tt]oo%s+%w+%s+arguments' then
100  
-            myprint("Warning: " .. ast.note)
101  
-          else
102  
-            myprint("Note: " .. ast.note)
  93
+        if ast and column >= c1 and column <= c2 then
  94
+          if ast.id and not did_details then
  95
+            printvartype(token)
  96
+            printsignature(ast)
  97
+            printvalue(ast)
  98
+            if note then
  99
+              myprint(note)
  100
+              break
  101
+            end
  102
+          end
  103
+          if ast.note then
  104
+            -- This is complicated by the fact that I don't really understand
  105
+            -- the metalua/lua-inspect abstract syntax tree and apparently notes
  106
+            -- are not always available on the identifier token printed above.
  107
+            local iswarning = ast.note:find '[Tt]oo%s+%w+%s+arguments'
  108
+            note = (iswarning and "Warning: " or "Note: ") .. ast.note
  109
+            if did_details then
  110
+              myprint(note)
  111
+              break
  112
+            end
103 113
           end
104  
-          break
105 114
         end
106 115
       end
107 116
     end

0 notes on commit 2d671c4

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