summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2020-12-07 09:55:24 +0800
committer最萌小汐 <sumneko@hotmail.com>2020-12-07 09:55:31 +0800
commit3238be9f9828d3728fb81e2284ac2a5c2c9b3208 (patch)
tree704453298c91800648ac580a97dec332884981be
parent48c7a420e07fff03a88542c3b22f6f61b3cfc95b (diff)
downloadlua-language-server-3238be9f9828d3728fb81e2284ac2a5c2c9b3208.zip
update auto require
-rw-r--r--script/core/completion.lua11
-rw-r--r--script/workspace/require-path.lua10
-rw-r--r--script/workspace/workspace.lua2
-rw-r--r--test/crossfile/completion.lua37
4 files changed, 49 insertions, 11 deletions
diff --git a/script/core/completion.lua b/script/core/completion.lua
index bac4f673..924a9691 100644
--- a/script/core/completion.lua
+++ b/script/core/completion.lua
@@ -271,7 +271,7 @@ local function buildFunction(results, source, oop, data)
end
end
-local function buildInsertRequire(ast, targetName, targetUri)
+local function buildInsertRequire(ast, targetUri, stemName)
local uri = guide.getUri(ast.ast)
local lines = files.getLines(uri)
local text = files.getText(uri)
@@ -285,7 +285,7 @@ local function buildInsertRequire(ast, targetName, targetUri)
end
end
local path = furi.decode(targetUri)
- local visiblePaths = rpath.getVisiblePath(path, config.config.runtime.path)
+ local visiblePaths = rpath.getVisiblePath(path, config.config.runtime.path, true)
if #visiblePaths == 0 then
return nil
end
@@ -296,7 +296,7 @@ local function buildInsertRequire(ast, targetName, targetUri)
{
start = start,
finish = start - 1,
- newText = ('local %s = require %q\n'):format(targetName, visiblePaths[1].expect)
+ newText = ('local %s = require %q\n'):format(stemName, visiblePaths[1].expect)
}
}
end
@@ -389,10 +389,11 @@ local function checkModule(ast, word, offset, results)
return {
detail = buildDetail(targetSource),
description = lang.script('COMPLETION_IMPORT_FROM', ('[%s](%s)'):format(
- fileName, originUri
+ workspace.getRelativePath(originUri),
+ originUri
))
.. '\n' .. buildDesc(targetSource),
- additionalTextEdits = buildInsertRequire(ast, stemName, uri),
+ additionalTextEdits = buildInsertRequire(ast, uri, stemName),
}
end)
}
diff --git a/script/workspace/require-path.lua b/script/workspace/require-path.lua
index 3ec2d6d1..f1dc2fb9 100644
--- a/script/workspace/require-path.lua
+++ b/script/workspace/require-path.lua
@@ -1,6 +1,7 @@
local platform = require 'bee.platform'
local files = require 'files'
local furi = require 'file-uri'
+local workspace = require "workspace"
local m = {}
m.cache = {}
@@ -24,7 +25,7 @@ local function getOnePath(path, searcher)
return nil
end
-function m.getVisiblePath(path, searchers)
+function m.getVisiblePath(path, searchers, strict)
path = path:gsub('^[/\\]+', '')
local uri = furi.encode(path)
local libraryPath = files.getLibraryPath(uri)
@@ -35,6 +36,8 @@ function m.getVisiblePath(path, searchers)
if libraryPath then
libraryPath = libraryPath:gsub('^[/\\]+', '')
pos = #libraryPath + 2
+ else
+ path = workspace.getRelativePath(uri)
end
repeat
local cutedPath = path:sub(pos)
@@ -62,10 +65,7 @@ function m.getVisiblePath(path, searchers)
}
end
end
- if not pos then
- break
- end
- until not pos
+ until not pos or strict
end
return m.cache[path]
end
diff --git a/script/workspace/workspace.lua b/script/workspace/workspace.lua
index a6425a8b..c76eec55 100644
--- a/script/workspace/workspace.lua
+++ b/script/workspace/workspace.lua
@@ -6,7 +6,6 @@ local config = require 'config'
local glob = require 'glob'
local platform = require 'bee.platform'
local await = require 'await'
-local rpath = require 'workspace.require-path'
local proto = require 'proto.proto'
local lang = require 'language'
local library = require 'library'
@@ -359,6 +358,7 @@ function m.getRelativePath(uri)
end
function m.reload()
+ local rpath = require 'workspace.require-path'
files.flushAllLibrary()
files.removeAllClosed()
files.flushCache()
diff --git a/test/crossfile/completion.lua b/test/crossfile/completion.lua
index f2487b68..ead28fe2 100644
--- a/test/crossfile/completion.lua
+++ b/test/crossfile/completion.lua
@@ -639,5 +639,42 @@ function (a: any, b: any)
}
}
+
+TEST {
+ {
+ path = 'dir/myfunc.lua',
+ content = [[
+ return function (a, b)
+ end
+ ]]
+ },
+ {
+ path = 'main.lua',
+ main = true,
+ content = [[
+ myfun$
+ ]],
+ },
+ completion = {
+ {
+ label = 'myfunc',
+ kind = CompletionItemKind.Variable,
+ detail = 'function',
+ description = [[
+从 [dir\myfunc.lua](file:///dir/myfunc.lua) 中导入
+```lua
+function (a: any, b: any)
+```]],
+ additionalTextEdits = {
+ {
+ start = 1,
+ finish = 0,
+ newText = 'local myfunc = require "dir.myfunc"\n'
+ }
+ }
+ }
+ }
+}
+
Cared['detail'] = nil
Cared['additionalTextEdits'] = nil