summaryrefslogtreecommitdiff
path: root/server-beta/src/proto/provider.lua
blob: 4eb1a66978c85032c9d25101c499246631e4592e (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
local util      = require 'utility'
local cap       = require 'proto.capability'
local pub       = require 'pub'
local task      = require 'task'
local files     = require 'files'
local proto     = require 'proto.proto'
local inte      = require 'proto.interface'
local workspace = require 'workspace'
local config    = require 'config'

local function updateConfig()
    local configs = proto.request('workspace/configuration', {
        items = {
            {
                scopeUri = workspace.uri,
                section = 'Lua',
            },
            {
                scopeUri = workspace.uri,
                section = 'files.associations',
            },
            {
                scopeUri = workspace.uri,
                section = 'files.exclude',
            }
        },
    })

    local updated = configs[1]
    local other   = {
        associations = configs[2],
        exclude      = configs[3],
    }

    local oldConfig = util.deepCopy(config.config)
    local oldOther  = util.deepCopy(config.other)
    config.setConfig(updated, other)
    local newConfig = config.config
    local newOther  = config.other
    if not util.equal(oldConfig.runtime, newConfig.runtime) then
    end
    if not util.equal(oldConfig.diagnostics, newConfig.diagnostics) then
    end
    if newConfig.completion.enable then
    else
    end
    if not util.equal(oldConfig.plugin, newConfig.plugin) then
    end
    if not util.equal(oldConfig.workspace, newConfig.workspace)
    or not util.equal(oldConfig.plugin, newConfig.plugin)
    or not util.equal(oldOther.associations, newOther.associations)
    or not util.equal(oldOther.exclude, newOther.exclude)
    then
    end
end

proto.on('initialize', function (params)
    --log.debug(util.dump(params))
    if params.workspaceFolders then
        local name = params.workspaceFolders[1].name
        local uri  = params.workspaceFolders[1].uri
        workspace.init(name, uri)
    end
    return {
        capabilities = cap.initer,
    }
end)

proto.on('initialized', function (params)
    updateConfig()
    proto.request('client/registerCapability', {
        registrations = {
            -- 监视文件变化
            {
                id = '0',
                method = 'workspace/didChangeWatchedFiles',
                registerOptions = {
                    watchers = {
                        {
                            globPattern = '**/',
                            kind = 1 | 2 | 4,
                        }
                    },
                },
            },
            -- 配置变化
            {
                id = '1',
                method = 'workspace/didChangeConfiguration',
            }
        }
    })
    task.create(function ()
        workspace.preload()
    end)
    return true
end)

proto.on('exit', function ()
    log.info('Server exited.')
    os.exit(true)
end)

proto.on('shutdown', function ()
    log.info('Server shutdown.')
    return true
end)

proto.on('workspace/configuration', function ()
    updateConfig()
end)

proto.on('workspace/didChangeWatchedFiles', function (params)
end)

proto.on('textDocument/didOpen', function (params)
    local doc   = params.textDocument
    local uri   = doc.uri
    local text  = doc.text
    files.open(uri)
    files.setText(uri, text)
end)

proto.on('textDocument/didClose', function (params)
    local doc   = params.textDocument
    local uri   = doc.uri
    files.close(uri)
    if not files.isLua(uri) then
        files.remove(uri)
    end
end)

proto.on('textDocument/didChange', function (params)
    local doc    = params.textDocument
    local change = params.contentChanges
    local uri    = doc.uri
    local text   = change[1].text
    files.setText(uri, text)
end)

proto.on('textDocument/hover', function ()
    return {
        contents = {
            value = 'Hello loli!',
            kind  = 'markdown',
        }
    }
end)

proto.on('textDocument/definition', function (params)
    local core   = require 'core.definition'
    local uri    = params.textDocument.uri
    local ast    = files.getAst(uri)
    if not ast then
        return nil
    end
    local text   = files.getText(uri)
    local offset = inte.offset(ast.lines, text, params.position)
    local result = core(ast, text, offset)
    if not result then
        return nil
    end
    local response = {}
    for i, info in ipairs(result) do
        response[i] = inte.locationLink(info.uri
            , inte.range(ast.lines, text, info.target.start - 1, info.target.finish)
            , inte.range(ast.lines, text, info.target.start - 1, info.target.finish)
            , inte.range(ast.lines, text, info.source.start - 1, info.source.finish)
        )
    end
    return response
end)