From d1ca1c8add2b122eb9b558d3c185f34de001e971 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 9 Dec 2019 17:29:00 +0800 Subject: =?UTF-8?q?hover=20=E6=94=AF=E6=8C=81=20require=20=E8=B7=AF?= =?UTF-8?q?=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script-beta/core/definition.lua | 11 +++++---- script-beta/core/hover/description.lua | 43 ++++++++++++++++++++++++++++++++++ script-beta/core/hover/init.lua | 7 ++++-- 3 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 script-beta/core/hover/description.lua (limited to 'script-beta/core') diff --git a/script-beta/core/definition.lua b/script-beta/core/definition.lua index 865fc7cb..5f16ad84 100644 --- a/script-beta/core/definition.lua +++ b/script-beta/core/definition.lua @@ -58,14 +58,17 @@ local function checkRequire(source, offset, callback) if type(literal) ~= 'string' then return end - local name = func.special - if name == 'require' then + local lib = vm.getLibrary(func) + if not lib then + return + end + if lib.name == 'require' then local result = workspace.findUrisByRequirePath(literal, true) for _, uri in ipairs(result) do callback(uri) end - elseif name == 'dofile' - or name == 'loadfile' then + elseif lib.name == 'dofile' + or lib.name == 'loadfile' then local result = workspace.findUrisByFilePath(literal, true) for _, uri in ipairs(result) do callback(uri) diff --git a/script-beta/core/hover/description.lua b/script-beta/core/hover/description.lua new file mode 100644 index 00000000..1a6f19ca --- /dev/null +++ b/script-beta/core/hover/description.lua @@ -0,0 +1,43 @@ +local vm = require 'vm' +local ws = require 'workspace' +local furi = require 'file-uri' +local files = require 'files' + +local function asString(source) + local literal = vm.getLiteral(source) + if type(literal) ~= 'string' then + return nil + end + local parent = source.parent + if parent and parent.type == 'callargs' then + local result + local call = parent.parent + local node = call.node + local lib = vm.getLibrary(node) + if not lib then + return + end + if lib.name == 'require' then + result = ws.findUrisByRequirePath(literal, true) + elseif lib.name == 'dofile' + or lib.name == 'loadfile' then + result = ws.findUrisByFilePath(literal, true) + end + if result and #result > 0 then + for i, uri in ipairs(result) do + local path = furi.decode(uri) + if files.eq(path:sub(1, #ws.path), ws.path) then + path = path:sub(#ws.path + 1):gsub('^[/\\]*', '') + end + result[i] = ('[%s](%s)'):format(path, uri) + end + return table.concat(result, '\n') + end + end +end + +return function (source) + if source.type == 'string' then + return asString(source) + end +end diff --git a/script-beta/core/hover/init.lua b/script-beta/core/hover/init.lua index 34a1afae..63129c3e 100644 --- a/script-beta/core/hover/init.lua +++ b/script-beta/core/hover/init.lua @@ -2,6 +2,7 @@ local files = require 'files' local guide = require 'parser.guide' local vm = require 'vm' local getLabel = require 'core.hover.label' +local getDesc = require 'core.hover.description' local util = require 'utility' local function getHoverAsFunction(source) @@ -54,9 +55,11 @@ end local function getHoverAsValue(source) local label = getLabel(source, source) + local desc = getDesc(source) return { - label = label, - source = source, + label = label, + description = desc, + source = source, } end -- cgit v1.2.3