summaryrefslogtreecommitdiff
path: root/script/config.lua
blob: 1287681c705f7e7b653d9e1a1d840085e8305b77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
local util   = require 'utility'
local define = require 'proto.define'

local m = {}
m.version = 0

local function Boolean(v)
    if type(v) == 'boolean' then
        return true, v
    end
    return false
end

local function Integer(v)
    if type(v) == 'number' then
        return true, math.floor(v)
    end
    return false
end

local function String(v)
    return true, tostring(v)
end

local function Nil(v)
    if type(v) == 'nil' then
        return true, nil
    end
    return false
end

local function Str2Hash(sep)
    return function (v)
        if type(v) == 'string' then
            local t = {}
            for s in v:gmatch('[^'..sep..']+') do
                t[s] = true
            end
            return true, t
        end
        if type(v) == 'table' then
            local t = {}
            for _, s in ipairs(v) do
                if type(s) == 'string' then
                    t[s] = true
                end
            end
            return true, t
        end
        return false
    end
end

local function Array2Hash(checker)
    return function (tbl)
        if type(tbl) ~= 'table' then
            return false
        end
        local t = {}
        if #tbl > 0 then
            for _, k in ipairs(tbl) do
                t[k] = true
            end
        else
            for k, v in pairs(tbl) do
                t[k] = v
            end
        end
        return true, t
    end
end

local function Array(checker)
    return function (tbl)
        if type(tbl) ~= 'table' then
            return false
        end
        local t = {}
        for _, v in ipairs(tbl) do
            local ok, result = checker(v)
            if ok then
                t[#t+1] = result
            end
        end
        return true, t
    end
end

local function Hash(keyChecker, valueChecker)
    return function (tbl)
        if type(tbl) ~= 'table' then
            return false
        end
        local t = {}
        for k, v in pairs(tbl) do
            local ok1, key = keyChecker(k)
            local ok2, value = valueChecker(v)
            if ok1 and ok2 then
                t[key] = value
            end
        end
        if not next(t) then
            return false
        end
        return true, t
    end
end

local function Or(...)
    local checkers = {...}
    return function (obj)
        for _, checker in ipairs(checkers) do
            local suc, res = checker(obj)
            if suc then
                return true, res
            end
        end
        return false
    end
end

local ConfigTemplate = {
    runtime = {
        version           = {'Lua 5.4', String},
        path              = {{
                                  "?.lua",
                                  "?/init.lua",
                                  "?/?.lua"
                              },        Array(String)},
        special           = {{},        Hash(String, String)},
        meta              = {'${version} ${language}', String},
        unicodeName       = {false,     Boolean},
        nonstandardSymbol = {{},        Str2Hash ';'},
        plugin            = {'', String},
        fileEncoding      = {'utf8',    String},
        builtin           = {{},          Hash(String, String)},
    },
    diagnostics = {
        enable          = {true, Boolean},
        globals         = {{},   Str2Hash ';'},
        disable         = {{},   Str2Hash ';'},
        severity        = {
            util.deepCopy(define.DiagnosticDefaultSeverity),
            Hash(String, String),
        },
        neededFileStatus = {
            util.deepCopy(define.DiagnosticDefaultNeededFileStatus),
            Hash(String, String),
        },
        workspaceDelay  = {0,    Integer},
        workspaceRate   = {100,  Integer},
    },
    workspace = {
        ignoreDir       = {{},      Str2Hash ';'},
        ignoreSubmodules= {true,    Boolean},
        useGitIgnore    = {true,    Boolean},
        maxPreload      = {1000,    Integer},
        preloadFileSize = {100,     Integer},
        library         = {{},      Array2Hash(String)},
    },
    completion = {
        enable          = {true,      Boolean},
        callSnippet     = {'Disable', String},
        keywordSnippet  = {'Replace', String},
        displayContext  = {6,         Integer},
        workspaceWord   = {true,      Boolean},
        autoRequire     = {true,      Boolean},
        showParams      = {true,      Boolean},
    },
    signatureHelp = {
        enable          = {true,      Boolean},
    },
    hover = {
        enable          = {true,      Boolean},
        viewString      = {true,      Boolean},
        viewStringMax   = {1000,      Integer},
        viewNumber      = {true,      Boolean},
        previewFields   = {20,        Integer},
        enumsLimit      = {5,         Integer},
    },
    color = {
        mode            = {'Semantic', String},
    },
    hint = {
        enable          = {false,     Boolean},
        paramType       = {true,      Boolean},
        setType         = {false,     Boolean},
        paramName       = {true,      Boolean},
    },
    intelliSense = {
        searchDepth     = {0,         Integer},
    },
    window              = {
        statusBar       = {true,      Boolean},
        progressBar     = {true,      Boolean},
    },
    telemetry = {
        enable          = {nil,       Or(Boolean, Nil)},
    }
}

local OtherTemplate = {
    associations            = {{},   Hash(String, String)},
    exclude                 = {{},   Hash(String, Boolean)},
    semantic                = {'',   Or(Boolean, String)},
    acceptSuggestionOnEnter = {'on', String},
}

local function init()
    if m.config then
        return
    end

    m.config = {}
    for c, t in pairs(ConfigTemplate) do
        m.config[c] = {}
        for k, info in pairs(t) do
            m.config[c][k] = info[1]
        end
    end

    m.other = {}
    for k, info in pairs(OtherTemplate) do
        m.other[k] = info[1]
    end
end

function m.setConfig(config, other)
    m.version = m.version + 1
    xpcall(function ()
        for c, t in pairs(config) do
            for k, v in pairs(t) do
                local region = ConfigTemplate[c]
                if region then
                    local info = region[k]
                    if info then
                        local suc, v = info[2](v)
                        if suc then
                            m.config[c][k] = v
                        else
                            m.config[c][k] = info[1]
                        end
                    end
                end
            end
        end
        for k, v in pairs(other) do
            local info = OtherTemplate[k]
            if info then
                local suc, v = info[2](v)
                if suc then
                    m.other[k] = v
                else
                    m.other[k] = info[1]
                end
            end
        end
        log.debug('Config update: ', util.dump(m.config), util.dump(m.other))
    end, log.error)
end

init()

return m