From 4e63ce41147f68b25282c375b2c067c2663e9aaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Fri, 28 Dec 2018 18:32:52 +0800 Subject: =?UTF-8?q?=E6=9A=82=E6=97=B6=E5=85=88=E8=BF=99=E6=A0=B7=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E5=90=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/core/document_symbol.lua | 19 ++++++++++++++++--- server/src/core/env.lua | 19 +++++++++++++++++-- server/src/core/vm.lua | 3 ++- 3 files changed, 35 insertions(+), 6 deletions(-) (limited to 'server/src/core') diff --git a/server/src/core/document_symbol.lua b/server/src/core/document_symbol.lua index 8e6fd01c..3902f8a3 100644 --- a/server/src/core/document_symbol.lua +++ b/server/src/core/document_symbol.lua @@ -30,7 +30,7 @@ local SymbolKind = { TypeParameter = 26, } -local function buildFunc(vm, func) +local function buildFunction(vm, func) local source = func.source local declarat = func.declarat local name @@ -75,10 +75,23 @@ local function buildFunc(vm, func) } end +local function buildChunk(vm, chunk) + local symbol = buildFunction(vm, chunk.func) + local childs = {} + for i, child in vm.chunk:childs(chunk) do + childs[i] = buildChunk(vm, child) + end + if #childs ~= 0 then + symbol.children = childs + end + return symbol +end + return function (vm) local symbols = {} - for _, func in ipairs(vm.results.funcs) do - symbols[#symbols+1] = buildFunc(vm, func) + + for i, chunk in vm.chunk:childs() do + symbols[i] = buildChunk(vm, chunk) end return symbols diff --git a/server/src/core/env.lua b/server/src/core/env.lua index ada26145..bee90012 100644 --- a/server/src/core/env.lua +++ b/server/src/core/env.lua @@ -16,7 +16,13 @@ return function (root) local mt = { _env = env } function mt:push() - env[#env+1] = { _next = env[#env], _cut = {} } + local current = env[#env] + if not current._child then + current._child = {} + end + local new = { _next = env[#env], _cut = {} } + current._child[#current._child+1] = new + env[#env+1] = new end function mt:pop() env[#env] = nil @@ -24,6 +30,15 @@ return function (root) function mt:cut(key) env[#env]._cut[key] = true end + function mt:childs(obj) + if not obj then + obj = env[#env] + end + if not obj._child then + return function () end + end + return ipairs(obj._child) + end function mt:__index(key) local origin = env[#env] if is_table[key] then @@ -99,7 +114,7 @@ return function (root) cuted[key] = true end for key, value in pairs(o) do - if key == '_cut' or key == '_next' then + if key == '_cut' or key == '_next' or key == '_child' then goto CONTINUE end if cuted[key] then diff --git a/server/src/core/vm.lua b/server/src/core/vm.lua index fc00b581..f60fcb57 100644 --- a/server/src/core/vm.lua +++ b/server/src/core/vm.lua @@ -401,6 +401,7 @@ function mt:buildFunction(exp, object) self.chunk:cut 'dots' self.chunk:cut 'labels' self.chunk.func = func + self.chunk.locals = {} if object then local var = self:createArg('self', object.source, self:getValue(object)) @@ -1435,6 +1436,7 @@ function mt:createEnvironment() self.scope.block = { start = 0, finish = math.maxinteger } -- 整个文件是一个函数 self.chunk.func = self:buildFunction() + self.chunk.locals = {} self.results.main = self.chunk.func -- 隐藏的上值`_ENV` local parent = self:createLocal('_ENV') @@ -1464,7 +1466,6 @@ local function compile(ast, lsp, uri) }, chunk = env { labels = {}, - locals = {}, }, results = { locals = {}, -- cgit v1.2.3