1- local util = require ' utility'
2- local define = require ' proto.define '
3- local timer = require ' timer '
4- local scope = require ' workspace.scope '
1+ local util = require ' utility'
2+ local timer = require ' timer '
3+ local scope = require ' workspace.scope '
4+ local template = require ' config.template '
55
66--- @alias config.source ' "client"' | ' "path"' | ' "local"'
77
8- --- @class config.unit
9- --- @field caller function
10- local mt = {}
11- mt .__index = mt
12-
13- function mt :__call (...)
14- self :caller (... )
15- return self
16- end
17-
18- function mt :__shr (default )
19- self .default = default
20- return self
21- end
22-
23- local units = {}
24-
25- local function register (name , default , checker , loader , caller )
26- units [name ] = {
27- default = default ,
28- checker = checker ,
29- loader = loader ,
30- caller = caller ,
31- }
32- end
33-
34- local Type = setmetatable ({}, { __index = function (_ , name )
35- local unit = {}
36- for k , v in pairs (units [name ]) do
37- unit [k ] = v
38- end
39- return setmetatable (unit , mt )
40- end })
41-
42- register (' Boolean' , false , function (self , v )
43- return type (v ) == ' boolean'
44- end , function (self , v )
45- return v
46- end )
47-
48- register (' Integer' , 0 , function (self , v )
49- return type (v ) == ' number'
50- end , function (self , v )
51- return math.floor (v )
52- end )
53-
54- register (' String' , ' ' , function (self , v )
55- return type (v ) == ' string'
56- end , function (self , v )
57- return tostring (v )
58- end )
59-
60- register (' Nil' , nil , function (self , v )
61- return type (v ) == ' nil'
62- end , function (self , v )
63- return nil
64- end )
65-
66- register (' Array' , {}, function (self , value )
67- return type (value ) == ' table'
68- end , function (self , value )
69- local t = {}
70- for _ , v in ipairs (value ) do
71- if self .sub :checker (v ) then
72- t [# t + 1 ] = self .sub :loader (v )
73- end
74- end
75- return t
76- end , function (self , sub )
77- self .sub = sub
78- end )
79-
80- register (' Hash' , {}, function (self , value )
81- if type (value ) == ' table' then
82- if # value == 0 then
83- for k , v in pairs (value ) do
84- if not self .subkey :checker (k )
85- or not self .subvalue :checker (v ) then
86- return false
87- end
88- end
89- else
90- if not self .subvalue :checker (true ) then
91- return false
92- end
93- for _ , v in ipairs (value ) do
94- if not self .subkey :checker (v ) then
95- return false
96- end
97- end
98- end
99- return true
100- end
101- if type (value ) == ' string' then
102- return self .subkey :checker (' ' )
103- and self .subvalue :checker (true )
104- end
105- end , function (self , value )
106- if type (value ) == ' table' then
107- local t = {}
108- if # value == 0 then
109- for k , v in pairs (value ) do
110- t [k ] = v
111- end
112- else
113- for _ , k in pairs (value ) do
114- t [k ] = true
115- end
116- end
117- return t
118- end
119- if type (value ) == ' string' then
120- local t = {}
121- for s in value :gmatch (' [^' .. self .sep .. ' ]+' ) do
122- t [s ] = true
123- end
124- return t
125- end
126- end , function (self , subkey , subvalue , sep )
127- self .subkey = subkey
128- self .subvalue = subvalue
129- self .sep = sep
130- end )
131-
132- register (' Or' , nil , function (self , value )
133- for _ , sub in ipairs (self .subs ) do
134- if sub :checker (value ) then
135- return true
136- end
137- end
138- return false
139- end , function (self , value )
140- for _ , sub in ipairs (self .subs ) do
141- if sub :checker (value ) then
142- return sub :loader (value )
143- end
144- end
145- end , function (self , ...)
146- self .subs = { ... }
147- end )
148-
149- local Template = {
150- [' Lua.runtime.version' ] = Type .String >> ' Lua 5.4' ,
151- [' Lua.runtime.path' ] = Type .Array (Type .String ) >> {
152- " ?.lua" ,
153- " ?/init.lua" ,
154- },
155- [' Lua.runtime.pathStrict' ] = Type .Boolean >> false ,
156- [' Lua.runtime.special' ] = Type .Hash (Type .String , Type .String ),
157- [' Lua.runtime.meta' ] = Type .String >> ' ${version} ${language} ${encoding}' ,
158- [' Lua.runtime.unicodeName' ] = Type .Boolean ,
159- [' Lua.runtime.nonstandardSymbol' ] = Type .Hash (Type .String , Type .Boolean , ' ;' ),
160- [' Lua.runtime.plugin' ] = Type .String ,
161- [' Lua.runtime.fileEncoding' ] = Type .String >> ' utf8' ,
162- [' Lua.runtime.builtin' ] = Type .Hash (Type .String , Type .String ),
163- [' Lua.diagnostics.enable' ] = Type .Boolean >> true ,
164- [' Lua.diagnostics.globals' ] = Type .Hash (Type .String , Type .Boolean , ' ;' ),
165- [' Lua.diagnostics.disable' ] = Type .Hash (Type .String , Type .Boolean , ' ;' ),
166- [' Lua.diagnostics.severity' ] = Type .Hash (Type .String , Type .String )
167- >> util .deepCopy (define .DiagnosticDefaultSeverity ),
168- [' Lua.diagnostics.neededFileStatus' ] = Type .Hash (Type .String , Type .String )
169- >> util .deepCopy (define .DiagnosticDefaultNeededFileStatus ),
170- [' Lua.diagnostics.disableScheme' ] = Type .Hash (Type .String , Type .Boolean , ' ;' ) >> {
171- [' git' ] = true ,
172- },
173- [' Lua.diagnostics.workspaceDelay' ] = Type .Integer >> 5 ,
174- [' Lua.diagnostics.workspaceRate' ] = Type .Integer >> 100 ,
175- [' Lua.diagnostics.libraryFiles' ] = Type .String >> ' Opened' ,
176- [' Lua.diagnostics.ignoredFiles' ] = Type .String >> ' Opened' ,
177- [' Lua.workspace.ignoreDir' ] = Type .Array (Type .String ),
178- [' Lua.workspace.ignoreSubmodules' ] = Type .Boolean >> true ,
179- [' Lua.workspace.useGitIgnore' ] = Type .Boolean >> true ,
180- [' Lua.workspace.maxPreload' ] = Type .Integer >> 3000 ,
181- [' Lua.workspace.preloadFileSize' ] = Type .Integer >> 500 ,
182- [' Lua.workspace.library' ] = Type .Hash (Type .String , Type .Boolean , ' ;' ),
183- [' Lua.workspace.checkThirdParty' ] = Type .Boolean >> true ,
184- [' Lua.workspace.userThirdParty' ] = Type .Array (Type .String ),
185- [' Lua.workspace.supportScheme' ] = Type .Hash (Type .String , Type .Boolean , ' ;' ) >> {
186- [' file' ] = true ,
187- [' untitled' ] = true ,
188- [' git' ] = true ,
189- },
190- [' Lua.completion.enable' ] = Type .Boolean >> true ,
191- [' Lua.completion.callSnippet' ] = Type .String >> ' Disable' ,
192- [' Lua.completion.keywordSnippet' ] = Type .String >> ' Replace' ,
193- [' Lua.completion.displayContext' ] = Type .Integer >> 0 ,
194- [' Lua.completion.workspaceWord' ] = Type .Boolean >> true ,
195- [' Lua.completion.showWord' ] = Type .String >> ' Fallback' ,
196- [' Lua.completion.autoRequire' ] = Type .Boolean >> true ,
197- [' Lua.completion.showParams' ] = Type .Boolean >> true ,
198- [' Lua.completion.requireSeparator' ] = Type .String >> ' .' ,
199- [' Lua.completion.postfix' ] = Type .String >> ' @' ,
200- [' Lua.signatureHelp.enable' ] = Type .Boolean >> true ,
201- [' Lua.hover.enable' ] = Type .Boolean >> true ,
202- [' Lua.hover.viewString' ] = Type .Boolean >> true ,
203- [' Lua.hover.viewStringMax' ] = Type .Integer >> 1000 ,
204- [' Lua.hover.viewNumber' ] = Type .Boolean >> true ,
205- [' Lua.hover.previewFields' ] = Type .Integer >> 20 ,
206- [' Lua.hover.enumsLimit' ] = Type .Integer >> 5 ,
207- [' Lua.hover.expandAlias' ] = Type .Boolean >> true ,
208- [' Lua.semantic.enable' ] = Type .Boolean >> true ,
209- [' Lua.semantic.variable' ] = Type .Boolean >> true ,
210- [' Lua.semantic.annotation' ] = Type .Boolean >> true ,
211- [' Lua.semantic.keyword' ] = Type .Boolean >> false ,
212- [' Lua.hint.enable' ] = Type .Boolean >> false ,
213- [' Lua.hint.paramType' ] = Type .Boolean >> true ,
214- [' Lua.hint.setType' ] = Type .Boolean >> false ,
215- [' Lua.hint.paramName' ] = Type .String >> ' All' ,
216- [' Lua.hint.await' ] = Type .Boolean >> true ,
217- [' Lua.hint.arrayIndex' ] = Type .Boolean >> ' Auto' ,
218- [' Lua.window.statusBar' ] = Type .Boolean >> true ,
219- [' Lua.window.progressBar' ] = Type .Boolean >> true ,
220- [' Lua.format.enable' ] = Type .Boolean >> true ,
221- [' Lua.format.defaultConfig' ] = Type .Hash (Type .String , Type .String )
222- >> {},
223- [' Lua.spell.dict' ] = Type .Array (Type .String ),
224- [' Lua.telemetry.enable' ] = Type .Or (Type .Boolean >> false , Type .Nil ) >> nil ,
225-
226- -- VSCode
227- [' files.associations' ] = Type .Hash (Type .String , Type .String ),
228- [' files.exclude' ] = Type .Hash (Type .String , Type .Boolean ),
229- [' editor.semanticHighlighting.enabled' ] = Type .Or (Type .Boolean , Type .String ),
230- [' editor.acceptSuggestionOnEnter' ] = Type .String >> ' on' ,
231- }
232-
2338--- @class config.api
2349local m = {}
23510m .watchList = {}
27954--- @param key string
28055--- @param value any
28156function m .setByScope (scp , key , value )
282- local unit = Template [key ]
57+ local unit = template [key ]
28358 if not unit then
28459 return false
28560 end
@@ -311,7 +86,7 @@ function m.set(uri, key, value)
31186end
31287
31388function m .add (uri , key , value )
314- local unit = Template [key ]
89+ local unit = template [key ]
31590 if not unit then
31691 return false
31792 end
@@ -338,7 +113,7 @@ function m.add(uri, key, value)
338113end
339114
340115function m .prop (uri , key , prop , value )
341- local unit = Template [key ]
116+ local unit = template [key ]
342117 if not unit then
343118 return false
344119 end
@@ -371,7 +146,7 @@ function m.get(uri, key)
371146 local scp = getScope (uri , key )
372147 local value = m .getNowTable (scp )[key ]
373148 if value == nil then
374- value = Template [key ].default
149+ value = template [key ].default
375150 end
376151 if value == m .NULL then
377152 value = nil
@@ -386,7 +161,7 @@ function m.getRaw(uri, key)
386161 local scp = getScope (uri , key )
387162 local value = m .getRawTable (scp )[key ]
388163 if value == nil then
389- value = Template [key ].default
164+ value = template [key ].default
390165 end
391166 if value == m .NULL then
392167 value = nil
@@ -423,9 +198,9 @@ function m.update(scp, ...)
423198 if m .nullSymbols [value ] then
424199 value = m .NULL
425200 end
426- if Template [fullKey ] then
201+ if template [fullKey ] then
427202 m .setByScope (scp , fullKey , value )
428- elseif Template [' Lua.' .. fullKey ] then
203+ elseif template [' Lua.' .. fullKey ] then
429204 m .setByScope (scp , ' Lua.' .. fullKey , value )
430205 elseif type (value ) == ' table' then
431206 expand (value , fullKey )
0 commit comments