summaryrefslogtreecommitdiff
path: root/script/config/loader.lua
blob: 391424d531dd47f181e43692594ab133ffbc7f37 (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
local proto     = require 'proto'
local lang      = require 'language'
local util      = require 'utility'
local workspace = require 'workspace'
local scope     = require 'workspace.scope'
local inspect   = require 'inspect'
local jsonc     = require 'jsonc'

local function errorMessage(msg)
    proto.notify('window/showMessage', {
        type = 3,
        message = msg,
    })
    log.error(msg)
end

---@class config.loader
local m = {}

---@return table?
function m.loadRCConfig(uri, filename)
    local scp  = scope.getScope(uri)
    local path = workspace.getAbsolutePath(uri, filename)
    if not path then
        scp:set('lastRCConfig', nil)
        return nil
    end
    local buf = util.loadFile(path)
    if not buf then
        scp:set('lastRCConfig', nil)
        return nil
    end
    local suc, res = pcall(jsonc.decode_jsonc, buf)
    if not suc then
        errorMessage(lang.script('CONFIG_LOAD_ERROR', res))
        return scp:get('lastRCConfig')
    end
    ---@cast res table
    scp:set('lastRCConfig', res)
    return res
end

---@return table?
function m.loadLocalConfig(uri, filename)
    if not filename then
        return nil
    end
    local scp  = scope.getScope(uri)
    local path = workspace.getAbsolutePath(uri, filename)
    if not path then
        scp:set('lastLocalConfig', nil)
        scp:set('lastLocalType', nil)
        return nil
    end
    local buf = util.loadFile(path)
    if not buf then
        --errorMessage(lang.script('CONFIG_LOAD_FAILED', path))
        scp:set('lastLocalConfig', nil)
        scp:set('lastLocalType', nil)
        return nil
    end
    local firstChar = buf:match '%S'
    if firstChar == '{' then
        local suc, res = pcall(jsonc.decode_jsonc, buf)
        if not suc then
            errorMessage(lang.script('CONFIG_LOAD_ERROR', res))
            return scp:get('lastLocalConfig')
        end
        ---@cast res table
        scp:set('lastLocalConfig', res)
        scp:set('lastLocalType', 'json')
        return res
    else
        local suc, res = pcall(function ()
            return assert(load(buf, '@' .. path, 't'))()
        end)
        if not suc then
            errorMessage(lang.script('CONFIG_LOAD_ERROR', res))
            scp:set('lastLocalConfig', res)
        end
        scp:set('lastLocalConfig', res)
        scp:set('lastLocalType', 'lua')
        return res
    end
end

---@async
---@param uri? uri
---@return table?
function m.loadClientConfig(uri)
    local configs = proto.awaitRequest('workspace/configuration', {
        items = {
            {
                scopeUri = uri,
                section = 'Lua',
            },
            {
                scopeUri = uri,
                section = 'files.associations',
            },
            {
                scopeUri = uri,
                section = 'files.exclude',
            },
            {
                scopeUri = uri,
                section = 'editor.semanticHighlighting.enabled',
            },
            {
                scopeUri = uri,
                section = 'editor.acceptSuggestionOnEnter',
            },
        },
    })
    if not configs or not configs[1] then
        log.warn('No config?', inspect(configs))
        return nil
    end

    local newConfig = {
        ['Lua']                                 = configs[1],
        ['files.associations']                  = configs[2],
        ['files.exclude']                       = configs[3],
        ['editor.semanticHighlighting.enabled'] = configs[4],
        ['editor.acceptSuggestionOnEnter']      = configs[5],
    }

    return newConfig
end

return m