Skip to content

Commit 671f1f4

Browse files
committed
add tests
1 parent 3fba691 commit 671f1f4

File tree

5 files changed

+212
-14
lines changed

5 files changed

+212
-14
lines changed

script/client.lua

+8-2
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,15 @@ end
204204
---@field global? boolean
205205
---@field uri? uri
206206

207-
---@param uri uri
207+
---@param uri uri?
208208
---@param changes config.change[]
209209
---@return config.change[]
210210
local function getValidChanges(uri, changes)
211-
local scp = scope.getScope(uri)
212211
local newChanges = {}
212+
if not uri then
213+
return changes
214+
end
215+
local scp = scope.getScope(uri)
213216
for _, change in ipairs(changes) do
214217
if scp:isChildUri(change.uri)
215218
or scp:isLinkedUri(change.uri) then
@@ -429,6 +432,9 @@ function m.setConfig(changes, onlyMemory)
429432
xpcall(function ()
430433
local ws = require 'workspace'
431434
if #ws.folders == 0 then
435+
if tryModifySpecifiedConfig(nil, finalChanges) then
436+
return
437+
end
432438
tryModifyClient(nil, finalChanges)
433439
return
434440
end

script/json-edit.lua

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
---@diagnostic disable: param-type-mismatch
2+
13
local type = type
24
local next = next
35
local error = error

script/provider/provider.lua

+14-12
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@ local fs = require 'bee.filesystem'
2323

2424
require 'library'
2525

26+
---@class provider
27+
local m = {}
28+
29+
m.attributes = {}
30+
2631
---@async
27-
local function updateConfig(uri)
32+
function m.updateConfig(uri)
2833
config.addNullSymbol(json.null)
2934
local specified = cfgLoader.loadLocalConfig(uri, CONFIGPATH)
3035
if specified then
@@ -58,11 +63,6 @@ local function updateConfig(uri)
5863
config.update(scope.fallback, global)
5964
end
6065

61-
---@class provider
62-
local m = {}
63-
64-
m.attributes = {}
65-
6666
function m.register(method)
6767
return function (attrs)
6868
m.attributes[method] = attrs
@@ -81,23 +81,23 @@ filewatch.event(function (ev, path) ---@async
8181
for _, scp in ipairs(workspace.folders) do
8282
local configPath = workspace.getAbsolutePath(scp.uri, CONFIGPATH)
8383
if path == configPath then
84-
updateConfig(scp.uri)
84+
m.updateConfig(scp.uri)
8585
end
8686
end
8787
end
8888
if util.stringEndWith(path, '.luarc.json') then
8989
for _, scp in ipairs(workspace.folders) do
9090
local rcPath = workspace.getAbsolutePath(scp.uri, '.luarc.json')
9191
if path == rcPath then
92-
updateConfig(scp.uri)
92+
m.updateConfig(scp.uri)
9393
end
9494
end
9595
end
9696
if util.stringEndWith(path, '.luarc.jsonc') then
9797
for _, scp in ipairs(workspace.folders) do
9898
local rcPath = workspace.getAbsolutePath(scp.uri, '.luarc.jsonc')
9999
if path == rcPath then
100-
updateConfig(scp.uri)
100+
m.updateConfig(scp.uri)
101101
end
102102
end
103103
end
@@ -136,7 +136,7 @@ m.register 'initialized'{
136136
function (params)
137137
files.init()
138138
local _ <close> = progress.create(workspace.getFirstScope().uri, lang.script.WINDOW_INITIALIZING, 0.5)
139-
updateConfig()
139+
m.updateConfig()
140140
local registrations = {}
141141

142142
if client.getAbility 'workspace.didChangeConfiguration.dynamicRegistration' then
@@ -177,7 +177,7 @@ m.register 'workspace/didChangeConfiguration' {
177177
if CONFIGPATH then
178178
return
179179
end
180-
updateConfig()
180+
m.updateConfig()
181181
end
182182
}
183183

@@ -251,7 +251,7 @@ m.register 'workspace/didChangeWorkspaceFolders' {
251251
log.debug('workspace/didChangeWorkspaceFolders', inspect(params))
252252
for _, folder in ipairs(params.event.added) do
253253
workspace.create(folder.uri)
254-
updateConfig()
254+
m.updateConfig()
255255
workspace.reload(scope.getScope(folder.uri))
256256
end
257257
for _, folder in ipairs(params.event.removed) do
@@ -1576,3 +1576,5 @@ files.watch(function (ev, uri)
15761576
end
15771577
end
15781578
end)
1579+
1580+
return m

test/tclient/init.lua

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require 'tclient.tests.load-relative-library'
1212
require 'tclient.tests.hover-set-local'
1313
require 'tclient.tests.same-prefix'
1414
require 'tclient.tests.recursive-runner'
15+
require 'tclient.tests.modify-luarc'
1516

1617
require 'tclient.tests.performance-jass-common'
1718
require 'tclient.tests.build-meta'

test/tclient/tests/modify-luarc.lua

+187
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
local lclient = require 'lclient'
2+
local util = require 'utility'
3+
local ws = require 'workspace'
4+
local jsonc = require 'jsonc'
5+
local jsonb = require 'json-beautify'
6+
local client = require 'client'
7+
local provider = require 'provider'
8+
local json = require 'json'
9+
10+
local configPath = LOGPATH .. '/modify-luarc.json'
11+
12+
---@async
13+
lclient():start(function (languageClient)
14+
languageClient:registerFakers()
15+
16+
CONFIGPATH = configPath
17+
18+
languageClient:initialize()
19+
20+
ws.awaitReady()
21+
22+
-------------------------------
23+
24+
util.saveFile(configPath, jsonb.beautify {
25+
['xxxx'] = 1, -- TODO: bug of json-edit, can not be an empty json
26+
})
27+
28+
provider.updateConfig()
29+
30+
client.setConfig({
31+
{
32+
action = 'set',
33+
key = 'Lua.runtime.version',
34+
value = 'LuaJIT',
35+
}
36+
})
37+
38+
assert(util.equal(jsonc.decode_jsonc(util.loadFile(configPath)), {
39+
['xxxx'] = 1,
40+
['Lua.runtime.version'] = 'LuaJIT',
41+
}))
42+
43+
-------------------------------
44+
45+
util.saveFile(configPath, jsonb.beautify {
46+
['xxxx'] = 1, -- TODO: bug of json-edit, can not be an empty json
47+
})
48+
49+
provider.updateConfig()
50+
51+
client.setConfig({
52+
{
53+
action = 'add',
54+
key = 'Lua.diagnostics.disable',
55+
value = 'undefined-global',
56+
}
57+
})
58+
59+
assert(util.equal(jsonc.decode_jsonc(util.loadFile(configPath)), {
60+
['xxxx'] = 1,
61+
['Lua.diagnostics.disable'] = {
62+
'undefined-global',
63+
}
64+
}))
65+
66+
-------------------------------
67+
68+
util.saveFile(configPath, jsonb.beautify {
69+
['Lua.diagnostics.disable'] = {}
70+
})
71+
72+
provider.updateConfig()
73+
74+
client.setConfig({
75+
{
76+
action = 'add',
77+
key = 'Lua.diagnostics.disable',
78+
value = 'undefined-global',
79+
}
80+
})
81+
82+
assert(util.equal(jsonc.decode_jsonc(util.loadFile(configPath)), {
83+
['Lua.diagnostics.disable'] = {
84+
'undefined-global',
85+
}
86+
}))
87+
88+
-------------------------------
89+
90+
util.saveFile(configPath, jsonb.beautify {
91+
['Lua.diagnostics.disable'] = {
92+
'unused-local'
93+
}
94+
})
95+
96+
provider.updateConfig()
97+
98+
client.setConfig({
99+
{
100+
action = 'add',
101+
key = 'Lua.diagnostics.disable',
102+
value = 'undefined-global',
103+
}
104+
})
105+
106+
assert(util.equal(jsonc.decode_jsonc(util.loadFile(configPath)), {
107+
['Lua.diagnostics.disable'] = {
108+
'unused-local',
109+
'undefined-global',
110+
}
111+
}))
112+
113+
-------------------------------
114+
115+
util.saveFile(configPath, jsonb.beautify {
116+
['xxxx'] = 1, -- TODO: bug of json-edit, can not be an empty json
117+
})
118+
119+
provider.updateConfig()
120+
121+
client.setConfig({
122+
{
123+
action = 'prop',
124+
key = 'Lua.runtime.special',
125+
prop = 'include',
126+
value = 'require',
127+
}
128+
})
129+
130+
assert(util.equal(jsonc.decode_jsonc(util.loadFile(configPath)), {
131+
['xxxx'] = 1,
132+
['Lua.runtime.special'] = {
133+
['include'] = 'require',
134+
}
135+
}))
136+
137+
-------------------------------
138+
139+
util.saveFile(configPath, jsonb.beautify {
140+
['Lua.runtime.special'] = json.createEmptyObject()
141+
})
142+
143+
provider.updateConfig()
144+
145+
client.setConfig({
146+
{
147+
action = 'prop',
148+
key = 'Lua.runtime.special',
149+
prop = 'include',
150+
value = 'require',
151+
}
152+
})
153+
154+
-- TODO: bug of json-edit, can not be an empty json
155+
-- assert(util.equal(jsonc.decode_jsonc(util.loadFile(configPath)), {
156+
-- ['Lua.runtime.special'] = {
157+
-- ['include'] = 'require',
158+
-- }
159+
-- }))
160+
161+
-------------------------------
162+
163+
util.saveFile(configPath, jsonb.beautify {
164+
['Lua.runtime.special'] = {
165+
['import'] = 'require',
166+
}
167+
})
168+
169+
provider.updateConfig()
170+
171+
client.setConfig({
172+
{
173+
action = 'prop',
174+
key = 'Lua.runtime.special',
175+
prop = 'include',
176+
value = 'require',
177+
}
178+
})
179+
180+
assert(util.equal(jsonc.decode_jsonc(util.loadFile(configPath)), {
181+
['Lua.runtime.special'] = {
182+
['import'] = 'require',
183+
['include'] = 'require',
184+
}
185+
}))
186+
187+
end)

0 commit comments

Comments
 (0)