Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Use new LuaInspect API (without incremental parsing for now)

 * %s/notes/token/g following upstream global rename
 * %s/text/src/g because luainspect.typecheck knows about it :-)
 * Use luainspect.ast (also attrs moved from note -> token.ast)
 * Work around _G.editor dependency in ast_from_string()
 * Enable ast.* type checking in luainspect4vim.lua
 * Because incremental parsing is only useful inside if-lua it's not
   used for now. The implementations for if-lua vs. system() will have
   to diverge eventually though.
  • Loading branch information...
commit a14b977c69b6682054e80b9bcceb1c0f797fcd21 1 parent a920570
Peter Odding authored August 07, 2010
26  luainspect.vim
@@ -2,7 +2,8 @@
2 2
 " Author: Peter Odding <peter@peterodding.com>
3 3
 " Last Change: August 7, 2010
4 4
 " URL: http://peterodding.com/code/vim/lua-inspect/
5  
-" Version: 0.1.5
  5
+" Version: 0.1.6
  6
+" License: MIT
6 7
 
7 8
 " Configuration defaults. {{{1
8 9
 
@@ -69,10 +70,25 @@ EOF
69 70
   call s:LoadDefaultStyles()
70 71
   call s:ClearPreviousMatches()
71 72
   " Highlight variables in buffer based on positions.
72  
-  for fields in split(b:luainspect_output, "\n")
73  
-    let [type, lnum, start, end] = split(fields)
74  
-    let command = 'syntax match %s /\%%%il\%%>%ic\<\w\+\>\%%<%ic/'
75  
-    execute printf(command, type, lnum, start - 1, end + 2)
  73
+  let did_warning = 0
  74
+  for line in split(b:luainspect_output, "\n")
  75
+    let fields = split(line, "\t")
  76
+    if len(fields) != 4
  77
+      if !did_warning
  78
+        try
  79
+          echohl WarningMsg
  80
+          echomsg "Invalid output from luainspect4vim.lua:"
  81
+        finally
  82
+          echohl None
  83
+          let did_warning = 1
  84
+        endtry
  85
+      endif
  86
+      echomsg strtrans(line)
  87
+    else
  88
+      let [type, lnum, start, end] = fields
  89
+      let command = 'syntax match %s /\%%%il\%%>%ic\<\w\+\>\%%<%ic/'
  90
+      execute printf(command, type, lnum, start - 1, end + 2)
  91
+    endif
76 92
   endfor
77 93
 endfunction
78 94
 
76  luainspect4vim.lua
@@ -3,8 +3,9 @@
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 29, 2010
  6
+ Last Change: August 7, 2010
7 7
  URL: http://peterodding.com/code/vim/lua-inspect/
  8
+ License: MIT
8 9
 
9 10
 --]]
10 11
 
@@ -23,66 +24,77 @@ else
23 24
   end
24 25
 end
25 26
 
26  
-local function getcurvar(notes, line, column)
27  
-  for i, note in ipairs(notes) do
28  
-    if note.ast.lineinfo then
29  
-      local l1, c1 = unpack(note.ast.lineinfo.first, 1, 2)
30  
-      local l2, c2 = unpack(note.ast.lineinfo.last, 1, 2)
  27
+local function getcurvar(tokenlist, line, column)
  28
+  for i, token in ipairs(tokenlist) do
  29
+    if token.ast.lineinfo then
  30
+      local l1, c1 = unpack(token.ast.lineinfo.first, 1, 2)
  31
+      local l2, c2 = unpack(token.ast.lineinfo.last, 1, 2)
31 32
       if l1 == line and column >= c1 and column <= c2 then
32  
-        if note.ast.id then return note end
  33
+        if token.ast.id then return token end
33 34
       end
34 35
     end
35 36
   end
36 37
 end
37 38
 
38  
-return function(text)
  39
+return function(src)
39 40
   local LI = require 'luainspect.init'
  41
+  local LA = require 'luainspect.ast'
40 42
   -- Split input into current position and source text.
41  
-  local line, column, text = text:match '^(%d+)\n(%d+)\n(.*)$'
  43
+  local line, column, src = src:match '^(%d+)\n(%d+)\n(.*)$'
42 44
   line = tonumber(line)
43 45
   column = tonumber(column)
44  
-  text = LI.remove_shebang(text)
45  
-  local f, err, linenum, colnum, linenum2 = LI.loadstring(text)
  46
+  src = LA.remove_shebang(src)
  47
+  local f, err, linenum, colnum, linenum2 = LA.loadstring(src)
46 48
   if not f then return end -- TODO Highlight syntax errors like spelling errors
47  
-  local ast; ast, err, linenum, colnum, linenum2 = LI.ast_from_string(text, "noname.lua")
  49
+  -- FIXME ast_from_string() references editor.LineCount
  50
+  local numlines = 1
  51
+  for _ in src:gmatch '\n' do numlines = numlines + 1 end
  52
+  local editor_save = _G.editor
  53
+  _G.editor = { lineCount = numlines }
  54
+  local ast; ast, err, linenum, colnum, linenum2 = LA.ast_from_string(src, "noname.lua")
  55
+  _G.editor = editor_save
48 56
   if not ast then return end
49  
-  local notes = LI.inspect(ast)
50  
-  local curvar = getcurvar(notes, line, column)
51  
-  for i, note in ipairs(notes) do
  57
+  local tokenlist = LA.ast_to_tokenlist(ast, src)
  58
+  LI.inspect(ast, tokenlist)
  59
+  local curvar = getcurvar(tokenlist, line, column)
  60
+  for i, token in ipairs(tokenlist) do
52 61
     local kind
53  
-    if curvar and curvar.ast.id == note.ast.id then
  62
+    if curvar and curvar.ast.id == token.ast.id then
54 63
       kind = 'luaInspectSelectedVariable'
55  
-    elseif note.type == 'global' then
56  
-      if note.definedglobal then
57  
-        kind = 'luaInspectGlobalDefined'
58  
-      else
59  
-        kind = 'luaInspectGlobalUndefined'
60  
-      end
61  
-    elseif note.type == 'local' then
62  
-      if not note.ast.localdefinition.isused then
  64
+    elseif token.tag == 'Id' then
  65
+      if not token.ast.localdefinition then
  66
+        if token.ast.definedglobal then
  67
+          kind = 'luaInspectGlobalDefined'
  68
+        else
  69
+          kind = 'luaInspectGlobalUndefined'
  70
+        end
  71
+      elseif not token.ast.localdefinition.isused then
63 72
         kind = 'luaInspectLocalUnused'
64  
-      elseif note.ast.localdefinition.isset then
65  
-        kind = 'luaInspectLocalMutated'
66  
-      elseif note.ast.localdefinition.functionlevel  < note.ast.functionlevel then
  73
+      elseif token.ast.localdefinition.functionlevel < token.ast.functionlevel then
67 74
         kind = 'luaInspectUpValue'
68  
-      elseif note.ast.localdefinition.isparam then
  75
+      elseif token.ast.localdefinition.isset then
  76
+        kind = 'luaInspectLocalMutated'
  77
+      elseif token.ast.localdefinition.isparam then
69 78
         kind = 'luaInspectParam'
70 79
       else
71 80
         kind = 'luaInspectLocal'
72 81
       end
73  
-    elseif note.type == 'field' then
74  
-      if note.definedglobal or note.ast.seevalue.value ~= nil then
  82
+    elseif token.ast.isfield then
  83
+      if token.ast.definedglobal or token.ast.seevalue.valueknown and token.ast.seevalue.value ~= nil then
75 84
         kind = 'luaInspectFieldDefined'
76 85
       else
77 86
         kind = 'luaInspectFieldUndefined'
78 87
       end
79 88
     end
80 89
     if kind then
81  
-      local l1, c1 = unpack(note.ast.lineinfo.first, 1, 2)
82  
-      local l2, c2 = unpack(note.ast.lineinfo.last, 1, 2)
  90
+      local l1, c1 = unpack(token.ast.lineinfo.first, 1, 2)
  91
+      local l2, c2 = unpack(token.ast.lineinfo.last, 1, 2)
83 92
       if l1 == l2 then dumpvar(kind, l1, c1, c2) end
84 93
     end
85 94
   end
86 95
 end
87 96
 
  97
+-- Enable type checking of ast.* expressions.
  98
+--! require 'luainspect.typecheck' (context)
  99
+
88 100
 -- vim: ts=2 sw=2 et

0 notes on commit a14b977

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