mirror of
https://github.com/neovim/neovim.git
synced 2026-04-20 22:35:33 +00:00
committed by
Christian Clason
parent
b23e99e051
commit
0aabe7ae76
@@ -47,6 +47,40 @@ local char = string.char
|
|||||||
local gsub = string.gsub
|
local gsub = string.gsub
|
||||||
local fmt = string.format
|
local fmt = string.format
|
||||||
|
|
||||||
|
local sbavailable, stringbuffer = pcall(require, 'string.buffer')
|
||||||
|
local buffnew
|
||||||
|
local puts
|
||||||
|
local render
|
||||||
|
|
||||||
|
if sbavailable then
|
||||||
|
buffnew = stringbuffer.new
|
||||||
|
puts = function(buf, str)
|
||||||
|
buf:put(str)
|
||||||
|
end
|
||||||
|
render = function(buf)
|
||||||
|
return buf:get()
|
||||||
|
end
|
||||||
|
else
|
||||||
|
buffnew = function()
|
||||||
|
return { n = 0 }
|
||||||
|
end
|
||||||
|
puts = function(buf, str)
|
||||||
|
buf.n = buf.n + 1
|
||||||
|
buf[buf.n] = str
|
||||||
|
end
|
||||||
|
render = function(buf)
|
||||||
|
return table.concat(buf)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local _rawget
|
||||||
|
if rawget then
|
||||||
|
_rawget = rawget
|
||||||
|
else
|
||||||
|
_rawget = function(t, k)
|
||||||
|
return t[k]
|
||||||
|
end
|
||||||
|
end
|
||||||
local function rawpairs(t)
|
local function rawpairs(t)
|
||||||
return next, t, nil
|
return next, t, nil
|
||||||
end
|
end
|
||||||
@@ -90,31 +124,14 @@ local function escape(str)
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- List of lua keywords
|
local luaKeywords = {}
|
||||||
local luaKeywords = {
|
for k in
|
||||||
['and'] = true,
|
([[ and break do else elseif end false for function goto if
|
||||||
['break'] = true,
|
in local nil not or repeat return then true until while
|
||||||
['do'] = true,
|
]]):gmatch('%w+')
|
||||||
['else'] = true,
|
do
|
||||||
['elseif'] = true,
|
luaKeywords[k] = true
|
||||||
['end'] = true,
|
end
|
||||||
['false'] = true,
|
|
||||||
['for'] = true,
|
|
||||||
['function'] = true,
|
|
||||||
['goto'] = true,
|
|
||||||
['if'] = true,
|
|
||||||
['in'] = true,
|
|
||||||
['local'] = true,
|
|
||||||
['nil'] = true,
|
|
||||||
['not'] = true,
|
|
||||||
['or'] = true,
|
|
||||||
['repeat'] = true,
|
|
||||||
['return'] = true,
|
|
||||||
['then'] = true,
|
|
||||||
['true'] = true,
|
|
||||||
['until'] = true,
|
|
||||||
['while'] = true,
|
|
||||||
}
|
|
||||||
|
|
||||||
local function isIdentifier(str)
|
local function isIdentifier(str)
|
||||||
return type(str) == 'string'
|
return type(str) == 'string'
|
||||||
@@ -157,7 +174,7 @@ end
|
|||||||
|
|
||||||
local function getKeys(t)
|
local function getKeys(t)
|
||||||
local seqLen = 1
|
local seqLen = 1
|
||||||
while rawget(t, seqLen) ~= nil do
|
while _rawget(t, seqLen) ~= nil do
|
||||||
seqLen = seqLen + 1
|
seqLen = seqLen + 1
|
||||||
end
|
end
|
||||||
seqLen = seqLen - 1
|
seqLen = seqLen - 1
|
||||||
@@ -173,17 +190,19 @@ local function getKeys(t)
|
|||||||
return keys, keysLen, seqLen
|
return keys, keysLen, seqLen
|
||||||
end
|
end
|
||||||
|
|
||||||
local function countCycles(x, cycles)
|
local function countCycles(x, cycles, depth)
|
||||||
if type(x) == 'table' then
|
if type(x) == 'table' then
|
||||||
if cycles[x] then
|
if cycles[x] then
|
||||||
cycles[x] = cycles[x] + 1
|
cycles[x] = cycles[x] + 1
|
||||||
else
|
else
|
||||||
cycles[x] = 1
|
cycles[x] = 1
|
||||||
for k, v in rawpairs(x) do
|
if depth > 0 then
|
||||||
countCycles(k, cycles)
|
for k, v in rawpairs(x) do
|
||||||
countCycles(v, cycles)
|
countCycles(k, cycles, depth - 1)
|
||||||
|
countCycles(v, cycles, depth - 1)
|
||||||
|
end
|
||||||
|
countCycles(getmetatable(x), cycles, depth - 1)
|
||||||
end
|
end
|
||||||
countCycles(getmetatable(x), cycles)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -234,11 +253,6 @@ local function processRecursive(process, item, path, visited)
|
|||||||
return processed
|
return processed
|
||||||
end
|
end
|
||||||
|
|
||||||
local function puts(buf, str)
|
|
||||||
buf.n = buf.n + 1
|
|
||||||
buf[buf.n] = str
|
|
||||||
end
|
|
||||||
|
|
||||||
local Inspector = {}
|
local Inspector = {}
|
||||||
|
|
||||||
local Inspector_mt = { __index = Inspector }
|
local Inspector_mt = { __index = Inspector }
|
||||||
@@ -354,10 +368,10 @@ function inspect.inspect(root, options)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local cycles = {}
|
local cycles = {}
|
||||||
countCycles(root, cycles)
|
countCycles(root, cycles, depth)
|
||||||
|
|
||||||
local inspector = setmetatable({
|
local inspector = setmetatable({
|
||||||
buf = { n = 0 },
|
buf = buffnew(),
|
||||||
ids = {},
|
ids = {},
|
||||||
cycles = cycles,
|
cycles = cycles,
|
||||||
depth = depth,
|
depth = depth,
|
||||||
@@ -368,7 +382,7 @@ function inspect.inspect(root, options)
|
|||||||
|
|
||||||
inspector:putValue(root)
|
inspector:putValue(root)
|
||||||
|
|
||||||
return table.concat(inspector.buf)
|
return render(inspector.buf)
|
||||||
end
|
end
|
||||||
|
|
||||||
setmetatable(inspect, {
|
setmetatable(inspect, {
|
||||||
|
|||||||
Reference in New Issue
Block a user