summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2021-08-24 21:23:09 +0800
committer最萌小汐 <sumneko@hotmail.com>2021-08-24 21:23:09 +0800
commit6cbc089f83eec21a33c792943e323a0f50a56e0a (patch)
tree4df28def4aae951a96705c99991a3c82b2683470
parent061c9b03686c7ae8b3dc70d491ff003258b13bf6 (diff)
downloadlua-language-server-6cbc089f83eec21a33c792943e323a0f50a56e0a.zip
command test
-rw-r--r--script/core/command/autoRequire.lua2
-rw-r--r--script/utility.lua14
-rw-r--r--script/vm/eachDef.lua3
-rw-r--r--script/vm/eachRef.lua3
-rw-r--r--script/vm/getDocs.lua3
-rw-r--r--script/vm/getGlobals.lua3
-rw-r--r--script/vm/getLibrary.lua1
-rw-r--r--script/vm/getLinks.lua3
-rw-r--r--script/vm/vm.lua4
-rw-r--r--test.lua1
-rw-r--r--test/command/auto-require.lua36
-rw-r--r--test/command/init.lua1
12 files changed, 60 insertions, 14 deletions
diff --git a/script/core/command/autoRequire.lua b/script/core/command/autoRequire.lua
index 63ff784e..2cb6a8f8 100644
--- a/script/core/command/autoRequire.lua
+++ b/script/core/command/autoRequire.lua
@@ -107,7 +107,6 @@ return function (data)
return
end
- local offset, fmt = findInsertOffset(uri)
local path = furi.decode(target)
local visiblePaths = rpath.getVisiblePath(path, config.get 'Lua.runtime.path')
if not visiblePaths or #visiblePaths == 0 then
@@ -122,5 +121,6 @@ return function (data)
return
end
+ local offset, fmt = findInsertOffset(uri)
applyAutoRequire(uri, offset, name, result, fmt)
end
diff --git a/script/utility.lua b/script/utility.lua
index f62c7af3..8d377708 100644
--- a/script/utility.lua
+++ b/script/utility.lua
@@ -17,6 +17,7 @@ local mathRandom = math.random
local ioOpen = io.open
local utf8Len = utf8.len
local getenv = os.getenv
+local getupvalue = debug.getupvalue
local mathHuge = math.huge
local inf = 1 / 0
local nan = 0 / 0
@@ -684,4 +685,17 @@ function m.switch()
return obj
end
+function m.getUpvalue(f, name)
+ for i = 1, 999 do
+ local uname, value = getupvalue(f, i)
+ if not uname then
+ break
+ end
+ if name == uname then
+ return value, true
+ end
+ end
+ return nil, false
+end
+
return m
diff --git a/script/vm/eachDef.lua b/script/vm/eachDef.lua
index 65cae73e..2bfad4bf 100644
--- a/script/vm/eachDef.lua
+++ b/script/vm/eachDef.lua
@@ -1,5 +1,4 @@
----@diagnostic disable-next-line
----@class vm
+---@type vm
local vm = require 'vm.vm'
local searcher = require 'core.searcher'
diff --git a/script/vm/eachRef.lua b/script/vm/eachRef.lua
index 6e008e2a..35425818 100644
--- a/script/vm/eachRef.lua
+++ b/script/vm/eachRef.lua
@@ -1,5 +1,4 @@
----@diagnostic disable-next-line
----@class vm
+---@type vm
local vm = require 'vm.vm'
local searcher = require 'core.searcher'
diff --git a/script/vm/getDocs.lua b/script/vm/getDocs.lua
index 8ee942d1..c0205654 100644
--- a/script/vm/getDocs.lua
+++ b/script/vm/getDocs.lua
@@ -1,7 +1,6 @@
local files = require 'files'
local guide = require 'parser.guide'
----@diagnostic disable-next-line
----@class vm
+---@type vm
local vm = require 'vm.vm'
local config = require 'config'
local collector = require 'core.collector'
diff --git a/script/vm/getGlobals.lua b/script/vm/getGlobals.lua
index d794a394..6dacda43 100644
--- a/script/vm/getGlobals.lua
+++ b/script/vm/getGlobals.lua
@@ -1,7 +1,6 @@
local collector = require 'core.collector'
local guide = require 'parser.guide'
----@diagnostic disable-next-line
----@class vm
+---@type vm
local vm = require 'vm.vm'
local noder = require 'core.noder'
diff --git a/script/vm/getLibrary.lua b/script/vm/getLibrary.lua
index 1002babb..1a8d8ffd 100644
--- a/script/vm/getLibrary.lua
+++ b/script/vm/getLibrary.lua
@@ -1,4 +1,3 @@
----@diagnostic disable-next-line
---@class vm
local vm = require 'vm.vm'
diff --git a/script/vm/getLinks.lua b/script/vm/getLinks.lua
index 8df79dca..161396f6 100644
--- a/script/vm/getLinks.lua
+++ b/script/vm/getLinks.lua
@@ -1,6 +1,5 @@
local guide = require 'parser.guide'
----@diagnostic disable-next-line
----@class vm
+---@type vm
local vm = require 'vm.vm'
local files = require 'files'
diff --git a/script/vm/vm.lua b/script/vm/vm.lua
index f51def71..6abaaa0e 100644
--- a/script/vm/vm.lua
+++ b/script/vm/vm.lua
@@ -13,8 +13,8 @@ local mathHuge = math.huge
local weakMT = { __mode = 'kv' }
_ENV = nil
----@diagnostic disable-next-line
----@class vm
+
+---@type vm
local m = {}
function m.getArgInfo(source)
diff --git a/test.lua b/test.lua
index 70993a70..d2e2c38a 100644
--- a/test.lua
+++ b/test.lua
@@ -74,6 +74,7 @@ local function testAll()
test 'highlight'
test 'rename'
test 'signature'
+ test 'command'
test 'document_symbol'
test 'code_action'
test 'type_formatting'
diff --git a/test/command/auto-require.lua b/test/command/auto-require.lua
new file mode 100644
index 00000000..94bb5069
--- /dev/null
+++ b/test/command/auto-require.lua
@@ -0,0 +1,36 @@
+local util = require 'utility'
+local files = require 'files'
+local autoRequire = require 'core.command.autoRequire'
+local client = require 'client'
+
+local findInsertOffset = util.getUpvalue(autoRequire, 'findInsertOffset')
+local applyAutoRequire = util.getUpvalue(autoRequire, 'applyAutoRequire')
+
+local originEditText = client.editText
+local EditResult
+
+client.editText = function (uri, edits)
+ EditResult = edits[1]
+end
+
+function TEST(text)
+ return function (name)
+ return function (expect)
+ files.removeAll()
+ files.setText('', text)
+ EditResult = nil
+ local offset, fmt = findInsertOffset('')
+ applyAutoRequire('', offset, name, name, fmt)
+ assert(util.equal(EditResult, expect))
+ end
+ end
+end
+
+-- TODO change to position
+TEST '' 'test' {
+ start = 0,
+ finish = -1,
+ text = '\nlocal test = require "test"\n'
+}
+
+client.editText = originEditText
diff --git a/test/command/init.lua b/test/command/init.lua
new file mode 100644
index 00000000..85d7f0d8
--- /dev/null
+++ b/test/command/init.lua
@@ -0,0 +1 @@
+require 'command.auto-require'