Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Use LuaInspect's get_value_details() function

This eliminates a lot of code from luainspect4vim.lua
  • Loading branch information...
commit cca74e2556c351e91e150bcf33b69f537b4a2644 1 parent 17bb37c
Peter Odding authored August 15, 2010
2  autoload.vim
@@ -126,7 +126,7 @@ function! s:parse_text(input, search_path) " {{{1
126 126
         " Ignore missing shell.vim plug-in.
127 127
         let b:luainspect_output = split(system(command, a:input), "\n")
128 128
         if v:shell_error
129  
-          let msg = "Failed to execute luainspect as external process! %s"
  129
+          let msg = "Failed to execute LuaInspect as external process! %s"
130 130
           throw printf(msg, strtrans(join(b:luainspect_output, "\n")))
131 131
         endif
132 132
       endtry
2  luainspect.vim
@@ -2,7 +2,7 @@
2 2
 " Author: Peter Odding <peter@peterodding.com>
3 3
 " Last Change: August 15, 2010
4 4
 " URL: http://peterodding.com/code/vim/lua-inspect/
5  
-" Version: 0.3.10
  5
+" Version: 0.3.11
6 6
 " License: MIT
7 7
 
8 8
 " Support for automatic update using the GLVS plug-in.
177  luainspect4vim.lua
@@ -9,13 +9,11 @@
9 9
 
10 10
 --]]
11 11
 
12  
-local MAX_PREVIEW_KEYS = 20
13  
-
14 12
 local LI = require 'luainspect.init'
15 13
 local LA = require 'luainspect.ast'
16  
-local LS = require 'luainspect.signatures'
17  
-local actions, myprint, getcurvar, knownvarorfield = {}
18  
-local printvartype, printsignature, printvalue
  14
+local MAX_PREVIEW_KEYS = 20
  15
+local actions = {}
  16
+local myprint
19 17
 
20 18
 if type(vim) == 'table' and vim.eval then
21 19
   -- The Lua interface for Vim redefines print() so it prints inside Vim.
@@ -27,7 +25,7 @@ else
27 25
   function myprint(text) io.write(text, '\n') end
28 26
 end
29 27
 
30  
-function getcurvar(tokenlist, line, column) -- {{{1
  28
+local function getcurvar(tokenlist, line, column) -- {{{1
31 29
   for i, token in ipairs(tokenlist) do
32 30
     if token.ast.lineinfo then
33 31
       local l1, c1 = unpack(token.ast.lineinfo.first, 1, 2)
@@ -39,7 +37,7 @@ function getcurvar(tokenlist, line, column) -- {{{1
39 37
   end
40 38
 end
41 39
 
42  
-function knownvarorfield(token) -- {{{1
  40
+local function knownvarorfield(token) -- {{{1
43 41
   local a = token.ast
44 42
   local v = a.seevalue or a
45 43
   return a.definedglobal or v.valueknown and v.value ~= nil
@@ -81,20 +79,56 @@ function actions.highlight(tokenlist, line, column) -- {{{1
81 79
   end
82 80
 end
83 81
 
84  
-function actions.tooltip(tokenlist, line, column) -- {{{1
  82
+local function previewtable(ast) -- {{{1
  83
+  -- Print a preview of a table's fields.
  84
+  local value = (ast.seevalue or ast).value
  85
+  if type(value) == 'table' then
  86
+    -- Print at most MAX_PREVIEW_KEYS of the table's keys.
  87
+    local keys = {}
  88
+    local count = 0
  89
+    for k, v in pairs(value) do
  90
+      if #keys < MAX_PREVIEW_KEYS then
  91
+        if type(k) == 'string' and k:find '^[A-Za-z_][A-Za-z0-9_]*$' then
  92
+          keys[#keys+1] = k .. (type(v) == 'function' and '()' or '')
  93
+        end
  94
+      end
  95
+      count = count + 1
  96
+    end
  97
+    table.sort(keys)
  98
+    if count > 0 then
  99
+      local fields = #keys == 1 and ' field' or ' fields'
  100
+      local including = count ~= #keys and ' including' or ''
  101
+      myprint('This table contains ' .. count .. fields .. including .. ':')
  102
+      for i, k in ipairs(keys) do myprint(' - ' .. k) end
  103
+    end
  104
+  end
  105
+end
  106
+
  107
+function actions.tooltip(tokenlist, line, column, src) -- {{{1
85 108
   local did_details = false
86 109
   local note
87 110
   for i, token in ipairs(tokenlist) do
88  
-    if token.ast.lineinfo then
89  
-      local l1, c1 = unpack(token.ast.lineinfo.first, 1, 2)
90  
-      local l2, c2 = unpack(token.ast.lineinfo.last, 1, 2)
  111
+    local ast = token.ast
  112
+    if ast.lineinfo then
  113
+      local l1, c1 = unpack(ast.lineinfo.first, 1, 2)
  114
+      local l2, c2 = unpack(ast.lineinfo.last, 1, 2)
91 115
       if l1 == line then
92  
-        local ast = token.ast
93  
-        if ast and column >= c1 and column <= c2 then
  116
+        if column >= c1 and column <= c2 then
94 117
           if ast.id and not did_details then
95  
-            printvartype(token)
96  
-            printsignature(ast)
97  
-            printvalue(ast)
  118
+            local details = LI.get_value_details(ast, tokenlist, src)
  119
+            if details ~= '?' then
  120
+              -- Convert variable type to readable sentence (friendlier to new users IMHO).
  121
+              details = details:gsub('^[^\n]+', function(vartype)
  122
+                local vartype = vartype:match '^%s*(.-)%s*$'
  123
+                if vartype:find 'local$' or vartype:find 'global' then
  124
+                  vartype = vartype .. ' ' .. 'variable'
  125
+                end
  126
+                local article = details:find '^[aeiou]' and 'an' or 'a'
  127
+                return "This is " .. article .. ' ' .. vartype .. '.'
  128
+              end)
  129
+              myprint(details)
  130
+            end
  131
+            previewtable(ast)
98 132
             if note then
99 133
               myprint(note)
100 134
               break
@@ -117,115 +151,6 @@ function actions.tooltip(tokenlist, line, column) -- {{{1
117 151
   end
118 152
 end
119 153
 
120  
-function printvartype(token) -- {{{1
121  
-  local ast = token.ast
122  
-  local text = {}
123  
-  if ast.localdefinition then
124  
-    if not ast.localdefinition.isused then text[#text+1] = "unused" end
125  
-    if ast.localdefinition.isset then text[#text+1] = "mutable" end
126  
-    if ast.localmasking then text[#text+1] = "masking" end
127  
-    if ast.localmasked then text[#text+1] = "masked" end
128  
-    if ast.localdefinition.functionlevel < ast.functionlevel then
129  
-      text[#text+1] = "upvalue"
130  
-    elseif ast.localdefinition.isparam then
131  
-      text[#text+1]  = "function parameter"
132  
-    else
133  
-      text[#text+1] = "local variable"
134  
-    end
135  
-  elseif ast.tag == 'Id' then
136  
-    text[#text+1] = knownvarorfield(token) and "known" or "unknown"
137  
-    text[#text+1] = "global variable"
138  
-  elseif ast.isfield then
139  
-    text[#text+1] = knownvarorfield(token) and "known" or "unknown"
140  
-    text[#text+1] = "table field"
141  
-  else
142  
-    return
143  
-  end
144  
-  -- TODO Bug in LuaInspect's static analysis? text:find() below is marked as
145  
-  -- an unknown table field even though table.concat() returns a string?!
146  
-  text = table.concat(text, ' ')
147  
-  myprint("This is " .. (text:find '^[aeiou]' and 'an' or 'a') .. ' ' .. text .. '.')
148  
-end
149  
-
150  
-function printsignature(ast) -- {{{1
151  
-  -- Display signatures for standard library functions.
152  
-  local name = ast.resolvedname
153  
-  local signature = name and LS.global_signatures[name]
154  
-  if not signature then
155  
-    local value = (ast.seevalue or ast).value
156  
-    signature = value and LS.value_signatures[value]
157  
-  end
158  
-  if signature then
159  
-    if not signature:find '%w %b()$' then
160  
-      myprint 'Its description is:'
161  
-      myprint('    ' .. signature)
162  
-    else
163  
-      myprint 'Its signature is as follows:'
164  
-      myprint('    ' .. signature)
165  
-    end
166  
-  end
167  
-end
168  
-
169  
-function printvalue(ast) -- {{{1
170  
-  -- Try to represent the value as a string.
171  
-  local value = (ast.seevalue or ast).value
172  
-  if type(value) == 'table' then
173  
-    -- Print at most MAX_PREVIEW_KEYS of the table's keys.
174  
-    local keys = {}
175  
-    for k, v in pairs(value) do
176  
-      if type(k) == 'string' then
177  
-        keys[#keys+1] = k .. (type(v) == 'function' and '()' or '')
178  
-      elseif type(k) == 'number' then
179  
-        keys[#keys+1] = '[' .. k .. ']'
180  
-      else
181  
-        keys[#keys+1] = tostring(k)
182  
-      end
183  
-    end
184  
-    table.sort(keys)
185  
-    if #keys > MAX_PREVIEW_KEYS then
186  
-      myprint('Its value is a table with ' .. #keys .. ' fields including:')
187  
-      for i, k in ipairs(keys) do
188  
-        myprint(' - ' .. k)
189  
-        if i == MAX_PREVIEW_KEYS then break end
190  
-      end
191  
-    elseif #keys >= 1 then
192  
-      myprint("Its value is a table with the following field" .. (#keys > 1 and "s" or '') .. ":")
193  
-      for i, k in ipairs(keys) do myprint(' - ' .. k) end
194  
-    else
195  
-      myprint 'Its value is a table.'
196  
-    end
197  
-  elseif type(value) == 'string' then
198  
-    -- Print string value.
199  
-    if value ~= '' then
200  
-      myprint("Its value is the string " .. string.format('%q', value) .. ".")
201  
-    else
202  
-      myprint "Its value is a string."
203  
-    end
204  
-  elseif type(value) == 'function' then
205  
-    -- Print function details.
206  
-    local text = { "Its value is a" }
207  
-    local info = debug.getinfo(value)
208  
-    text[#text+1] = info.what
209  
-    text[#text+1] = "function"
210  
-    -- Try to find out where the function was defined.
211  
-    local source = (info.source or ''):match '^@(.+)$'
212  
-    if source and not source:find '[\\/]+luainspect[\\/]+.-%.lua$' then
213  
-      source = source:gsub('^/home/[^/]+/', '~/')
214  
-      text[#text+1] = "defined in"
215  
-      text[#text+1] = source
216  
-      if info.linedefined then
217  
-        text[#text+1] = "on line"
218  
-        text[#text+1] = info.linedefined
219  
-      end
220  
-    end
221  
-    myprint(table.concat(text, ' ') .. '.')
222  
-  elseif type(value) == 'userdata' then
223  
-    myprint("Its value is a " .. type(value) .. '.')
224  
-  elseif value ~= nil then
225  
-    myprint("Its value is the " .. type(value) .. ' ' .. tostring(value) .. '.')
226  
-  end
227  
-end
228  
-
229 154
 function actions.goto(tokenlist, line, column) -- {{{1
230 155
   -- FIXME This only jumps to declaration of local / 1st occurrence of global.
231 156
   local curvar = getcurvar(tokenlist, line, column)
@@ -278,7 +203,7 @@ return function(src)
278 203
   -- Branch on the requested action.
279 204
   if actions[action] then
280 205
     myprint(action)
281  
-    actions[action](tokenlist, line, column)
  206
+    actions[action](tokenlist, line, column, src)
282 207
   end
283 208
 end
284 209
 

0 notes on commit cca74e2

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