-
-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathinit.lua
More file actions
229 lines (200 loc) · 5.25 KB
/
init.lua
File metadata and controls
229 lines (200 loc) · 5.25 KB
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
local api, lsp = vim.api, vim.lsp
---@diagnostic disable-next-line: deprecated
local uv = vim.version().minor >= 10 and vim.uv or vim.loop
local config = require('lspsaga').config
local symbol = {}
local cache = {}
symbol.__index = symbol
function symbol.__newindex(t, k, v)
rawset(t, k, v)
end
local function clean_buf_cache(buf)
buf = buf or api.nvim_get_current_buf()
if buf and cache[buf] then
for k, _ in pairs(cache[buf]) do
cache[buf][k] = nil
end
cache[buf] = nil
end
end
local function timer_clear(t)
t:stop()
t:close()
end
function symbol:buf_watcher(buf, client_id)
local buf_request_state = {}
local function defer_request(b)
if not self[b] or not api.nvim_buf_is_valid(b) then
return
end
local client = lsp.get_client_by_id(client_id)
if not client then
return
end
if buf_request_state[b] then
timer_clear(buf_request_state[b])
buf_request_state[b] = nil
end
buf_request_state[b] = uv.new_timer()
buf_request_state[b]:start(1000, 0, function()
timer_clear(buf_request_state[b])
buf_request_state[b] = nil
vim.schedule(function()
self:do_request(b, client_id)
end)
end)
end
api.nvim_buf_attach(buf, false, {
on_lines = function(_, b, changedtick)
if not self[b] or not changedtick then
return
end
self[b].changedtick = changedtick
if self[b] and not self[b].pending_request then
defer_request(b)
end
end,
on_changedtick = function(_, b, changedtick)
if not self[b] or not changedtick then
return
end
self[b].changedtick = changedtick
end,
})
api.nvim_create_autocmd('BufDelete', {
buffer = buf,
callback = function(args)
clean_buf_cache(args.buf)
if buf_request_state[args.buf] then
if not buf_request_state[args.buf]:is_closing() then
buf_request_state[args.buf]:stop()
buf_request_state[args.buf]:close()
end
buf_request_state[args.buf] = nil
end
end,
})
end
function symbol:do_request(buf, client_id)
if not vim.api.nvim_buf_is_valid(buf) then
return
end
local params = { textDocument = {
uri = vim.uri_from_bufnr(buf),
} }
local client = vim.lsp.get_client_by_id(client_id)
if not client then
return
end
if not self[buf] then
self[buf] = {}
self:buf_watcher(buf, client.id)
end
self[buf].pending_request = true
client.request('textDocument/documentSymbol', params, function(err, result, ctx)
if not api.nvim_buf_is_loaded(ctx.bufnr) or not self[ctx.bufnr] then
return
end
self[ctx.bufnr].pending_request = false
if not result or err then
return
end
self[ctx.bufnr].symbols = result
api.nvim_exec_autocmds('User', {
pattern = 'SagaSymbolUpdate',
modeline = false,
data = {
symbols = result or {},
client_id = ctx.client_id,
bufnr = ctx.bufnr,
},
})
end, buf)
end
function symbol:get_buf_symbols(buf)
buf = buf or api.nvim_get_current_buf()
local res = {}
if not self[buf] then
return
end
if self[buf].pending_request then
res.pending_request = self[buf].pending_request
return res
end
res.symbols = self[buf].symbols
res.pending_request = self[buf].pending_request
return res
end
function symbol:node_is_keyword(buf, node)
local lang = vim.treesitter.language.get_lang(vim.bo[buf].filetype)
local ok = pcall(vim.treesitter.get_parser, buf, lang)
if not ok then
return
end
if not node.selectionRange then
return false
end
local tnode = vim.treesitter.get_node({
bufnr = buf,
pos = {
node.selectionRange.start.line,
node.selectionRange.start.character,
},
})
if not tnode then
return
end
local keylist = {
'if_statement',
'for_statement',
'while_statement',
'repeat_statement',
'do_statement',
}
if vim.tbl_contains(keylist, tnode:type()) then
return true
end
return false
end
function symbol:register_module()
local group = api.nvim_create_augroup('LspsagaSymbols', { clear = true })
api.nvim_create_autocmd('FileType', {
group = group,
callback = function(args)
if config.symbol_in_winbar.enable then
require('lspsaga.symbol.winbar').init_winbar(args.buf)
end
end,
})
api.nvim_create_autocmd('LspAttach', {
group = group,
callback = function(args)
if self[args.buf] then
return
end
local client = lsp.get_client_by_id(args.data.client_id)
if not client or not client.supports_method('textDocument/documentSymbol') then
return
end
self:do_request(args.buf, args.data.client_id)
if config.implement.enable and client.supports_method('textDocument/implementation') then
require('lspsaga.implement').start()
end
end,
})
api.nvim_create_autocmd({ 'LspDetach', 'BufDelete' }, {
group = group,
callback = function(args)
if self[args.buf] then
self[args.buf] = nil
if config.symbol_in_winbar.enable then
pcall(api.nvim_del_augroup_by_name, 'SagaWinbar' .. args.buf)
end
end
end,
})
end
function symbol:outline()
require('lspsaga.symbol.outline'):outline()
end
return setmetatable(cache, symbol)