Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

(Performance) improvements suggested by Luis Carvalho

  • Loading branch information...
commit 7de26617d1f659a3e173f8be5ed0305fee6a83a9 1 parent 59637bd
Peter Odding authored July 28, 2010

Showing 1 changed file with 50 additions and 49 deletions. Show diff stats Hide diff stats

  1. 99  luainspect4vim.lua
99  luainspect4vim.lua
@@ -3,7 +3,7 @@
3 3
  This module is part of the luainspect.vim plug-in for the Vim text editor.
4 4
 
5 5
  Author: Peter Odding <peter@peterodding.com>
6  
- Last Change: July 27, 2010
  6
+ Last Change: July 28, 2010
7 7
  URL: http://peterodding.com/code/vim/lua-inspect/
8 8
 
9 9
 --]]
@@ -14,64 +14,65 @@ local function offset2lineinfo(text, offset)
14 14
   local curlnum = 1
15 15
   local lastlineoffset = 0
16 16
   for i in text:gmatch '()\n' do
17  
-    if i < offset then
18  
-      curlnum = curlnum + 1
19  
-      lastlineoffset = i
20  
-    else
21  
-      break
22  
-    end
  17
+    if i >= offset then break end
  18
+    curlnum = curlnum + 1
  19
+    lastlineoffset = i
23 20
   end
24 21
   return curlnum, offset - lastlineoffset
25 22
 end
26 23
 
  24
+local dumpvar
  25
+if type(vim) == 'table' and vim.eval then
  26
+  -- The Lua interface for Vim redefines print() so it prints inside Vim.
  27
+  dumpvar = function(text, kind, firstbyte, lastbyte)
  28
+    local line1, offset1 = offset2lineinfo(text, firstbyte)
  29
+    print(kind, line1, offset1, offset1 + (lastbyte - firstbyte))
  30
+  end
  31
+else
  32
+  -- My $LUA_INIT script redefines print() to enable pretty printing in the
  33
+  -- interactive prompt, which means strings are printed with surrounding
  34
+  -- quotes. This would break the communication between Vim and this script.
  35
+  dumpvar = function(text, kind, firstbyte, lastbyte)
  36
+    local line1, offset1 = offset2lineinfo(text, firstbyte)
  37
+    io.write(kind, '\t', line1, '\t', offset1, '\t', offset1 + (lastbyte - firstbyte), '\n')
  38
+  end
  39
+end
  40
+
27 41
 return function(text)
28  
-  -- Load the LuaInspect core module.
29 42
   local LI = require 'luainspect.init'
30 43
   text = LI.remove_shebang(text)
31 44
   local f, err, linenum, colnum, linenum2 = LI.loadstring(text)
32  
-  local function dumpvar(kind, firstbyte, lastbyte)
33  
-    local line1, offset1 = offset2lineinfo(text, firstbyte)
34  
-    if type(vim) == 'table' and vim.eval then
35  
-      -- The Lua interface for Vim redefines print() so it prints inside Vim.
36  
-      print(kind, line1, offset1, offset1 + (lastbyte - firstbyte))
37  
-    else
38  
-      -- My $LUA_INIT script redefines print() to enable pretty printing in the
39  
-      -- interactive prompt, which means strings are printed with surrounding
40  
-      -- quotes. This would break the communication between Vim and this script.
41  
-      io.write(kind, '\t', line1, '\t', offset1, '\t', offset1 + (lastbyte - firstbyte), '\n')
42  
-    end
43  
-  end
44  
-  if f then
45  
-    local ast; ast, err, linenum, colnum, linenum2 = LI.ast_from_string(text, "noname.lua")
46  
-    if ast then
47  
-      for i, note in ipairs(LI.inspect(ast)) do
48  
-        if note.type == 'global' then
49  
-          if note.definedglobal then
50  
-            dumpvar('luaInspectGlobalDefined', note[1], note[2])
51  
-          else
52  
-            dumpvar('luaInspectGlobalUndefined', note[1], note[2])
53  
-          end
54  
-        elseif note.type == 'local' then
55  
-          if not note.ast.localdefinition.isused then
56  
-            dumpvar('luaInspectLocalUnused', note[1], note[2])
57  
-          elseif note.ast.localdefinition.isset then
58  
-            dumpvar('luaInspectLocalMutated', note[1], note[2])
59  
-          elseif note.ast.localdefinition.functionlevel  < note.ast.functionlevel then
60  
-            dumpvar('luaInspectUpValue', note[1], note[2])
61  
-          elseif note.ast.localdefinition.isparam then
62  
-            dumpvar('luaInspectParam', note[1], note[2])
63  
-          else
64  
-            dumpvar('luaInspectLocal', note[1], note[2])
65  
-          end
66  
-        elseif note.type == 'field' then
67  
-          if note.definedglobal or note.ast.seevalue.value ~= nil then
68  
-            dumpvar('luaInspectFieldDefined', note[1], note[2])
69  
-          else
70  
-            dumpvar('luaInspectFieldUndefined', note[1], note[2])
71  
-          end
72  
-        end
  45
+  if not f then return end
  46
+  local ast; ast, err, linenum, colnum, linenum2 = LI.ast_from_string(text, "noname.lua")
  47
+  if not ast then return end
  48
+  for i, note in ipairs(LI.inspect(ast)) do
  49
+    local kind
  50
+    if note.type == 'global' then
  51
+      if note.definedglobal then
  52
+        kind = 'luaInspectGlobalDefined'
  53
+      else
  54
+        kind = 'luaInspectGlobalUndefined'
  55
+      end
  56
+    elseif note.type == 'local' then
  57
+      if not note.ast.localdefinition.isused then
  58
+        kind = 'luaInspectLocalUnused'
  59
+      elseif note.ast.localdefinition.isset then
  60
+        kind = 'luaInspectLocalMutated'
  61
+      elseif note.ast.localdefinition.functionlevel  < note.ast.functionlevel then
  62
+        kind = 'luaInspectUpValue'
  63
+      elseif note.ast.localdefinition.isparam then
  64
+        kind = 'luaInspectParam'
  65
+      else
  66
+        kind = 'luaInspectLocal'
  67
+      end
  68
+    elseif note.type == 'field' then
  69
+      if note.definedglobal or note.ast.seevalue.value ~= nil then
  70
+        kind = 'luaInspectFieldDefined'
  71
+      else
  72
+        kind = 'luaInspectFieldUndefined'
73 73
       end
74 74
     end
  75
+    if kind then dumpvar(text, kind, note[1], note[2]) end
75 76
   end
76 77
 end
77 78
 

0 notes on commit 7de2661

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