From 6da2b175e20ed3c03b0dfcfc9046de1e0e5d4444 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Sat, 23 Nov 2019 00:05:30 +0800 Subject: =?UTF-8?q?=E6=AD=A3=E8=B7=AF=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script-beta/core/hover/label.lua | 103 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 script-beta/core/hover/label.lua (limited to 'script-beta/core/hover/label.lua') diff --git a/script-beta/core/hover/label.lua b/script-beta/core/hover/label.lua new file mode 100644 index 00000000..72ce60f4 --- /dev/null +++ b/script-beta/core/hover/label.lua @@ -0,0 +1,103 @@ +local buildName = require 'core.hover.name' +local buildArg = require 'core.hover.arg' +local buildReturn = require 'core.hover.return' +local buildTable = require 'core.hover.table' +local vm = require 'vm' +local util = require 'utility' + +local function asFunction(source) + local name = buildName(source) + local arg = buildArg(source) + local rtn = buildReturn(source) + local lines = {} + lines[1] = ('function %s(%s)'):format(name, arg) + lines[2] = rtn + return table.concat(lines, '\n') +end + +local function asLocal(source) + local name = buildName(source) + local type = vm.getType(source) + local literal = vm.getLiteral(source) + if type == 'table' then + type = buildTable(source) + end + if literal == nil then + return ('local %s: %s'):format(name, type) + else + return ('local %s: %s = %s'):format(name, type, util.viewLiteral(literal)) + end +end + +local function asGlobal(source) + local name = buildName(source) + local type = vm.getType(source) + local literal = vm.getLiteral(source) + if type == 'table' then + type = buildTable(source) + end + if literal == nil then + return ('global %s: %s'):format(name, type) + else + return ('global %s: %s = %s'):format(name, type, util.viewLiteral(literal)) + end +end + +local function isGlobalField(source) + if source.type == 'field' + or source.type == 'method' then + source = source.parent + end + if source.type == 'setfield' + or source.type == 'getfield' + or source.type == 'setmethod' + or source.type == 'getmethod' + or source.type == 'tablefield' then + local node = source.node + if node.type == 'setglobal' + or node.type == 'getglobal' then + return true + end + return isGlobalField(node) + else + return false + end +end + +local function asField(source) + if isGlobalField(source) then + return asGlobal(source) + end + local name = buildName(source) + local type = vm.getType(source) + local literal = vm.getLiteral(source) + if type == 'table' then + type = buildTable(source) + end + if literal == nil then + return ('field %s: %s'):format(name, type) + else + return ('field %s: %s = %s'):format(name, type, util.viewLiteral(literal)) + end +end + +return function (source) + if source.type == 'function' then + return asFunction(source) + elseif source.type == 'local' + or source.type == 'getlocal' + or source.type == 'setlocal' then + return asLocal(source) + elseif source.type == 'setglobal' + or source.type == 'getglobal' then + return asGlobal(source) + elseif source.type == 'getfield' + or source.type == 'setfield' + or source.type == 'getmethod' + or source.type == 'setmethod' + or source.type == 'tablefield' + or source.type == 'field' + or source.type == 'method' then + return asField(source) + end +end -- cgit v1.2.3