summaryrefslogtreecommitdiff
path: root/server/src/method/initialized.lua
blob: 05abffd7c6a15ea637148a3ea282500debd241ce (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
local rpc = require 'rpc'
local workspace = require 'workspace'

local function initAfterConfig(lsp, firstScope)
    if firstScope then
        lsp.workspace = workspace(lsp, firstScope.name)
        lsp.workspace:init(firstScope.uri)
    end
    -- 必须动态注册的事件:
    rpc:request('client/registerCapability', {
        registrations = {
            -- 监视文件变化
            {
                id = '0',
                method = 'workspace/didChangeWatchedFiles',
                registerOptions = {
                    watchers = {
                        {
                            globPattern = '**/',
                            kind = 1 | 4,
                        }
                    },
                },
            },
            -- 配置变化
            {
                id = '1',
                method = 'workspace/didChangeConfiguration',
            }
        }
    }, function ()
        log.debug('client/registerCapability Success!')
    end)
end

return function (lsp)
    -- 请求工作目录
    rpc:request('workspace/workspaceFolders', nil, function (folders)
        local firstScope
        if folders then
            firstScope = folders[1]
        end
        local uri = firstScope and firstScope.uri
        -- 请求配置
        rpc:request('workspace/configuration', {
            items = {
                {
                    scopeUri = uri,
                    section = 'Lua',
                },
                {
                    scopeUri = uri,
                    section = 'files.associations',
                }
            },
        }, function (configs)
            lsp:onUpdateConfig(configs[1], {
                associations = configs[2],
            })
            initAfterConfig(lsp, firstScope)
        end)
    end)
    return true
end