-
Notifications
You must be signed in to change notification settings - Fork 11
/
keeper.lua
591 lines (540 loc) · 19.3 KB
/
keeper.lua
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
local M = {}
local extensions = require("otter.tools.extensions")
local fn = require("otter.tools.functions")
local api = vim.api
local ts = vim.treesitter
local config = require("otter.config")
M._otters_attached = {}
local injectable_languages = {}
for key, _ in pairs(extensions) do
table.insert(injectable_languages, key)
end
local function determine_language(main_nr, name, node, metadata, current_language)
local injection_language = metadata["injection.language"]
if injection_language ~= nil then
-- chunks using the metadata to specify the injected language
-- e.g. html script tags
if injection_language ~= "comment" then
-- don't use comment as language,
-- comments with language inside are handled in injection.combined
return injection_language
end
elseif metadata["injection.combined"] == true then
-- chunks where the injected language is specified in the text of a comment
local lang_capture = metadata[2]["text"]
if lang_capture ~= nil then
return lang_capture
end
elseif name == "_lang" or name == "injection.language" then
-- chunks where the name of the injected language is dynamic
-- e.g. markdown code chunks
return ts.get_node_text(node, main_nr, metadata)
else
return current_language
end
end
---trims the leading whitespace from text
---@param text string
---@param bufnr number host buffer number
---@param starting_ln number
---@return string, number
local function trim_leading_witespace(text, bufnr, starting_ln)
if not config.cfg.handle_leading_whitespace then
return text, 0
end
-- Assume the first line is least indented
-- the first line in the capture doesn't have its leading indent, so we grab from the buffer
local split = vim.split(text, "\n", { trimempty = false })
if #split == 0 then
return text, 0
end
local first_line = vim.api.nvim_buf_get_lines(bufnr, starting_ln, starting_ln + 1, false)
local leading = first_line[1]:match("^%s+")
if not leading then
return text, 0
end
for i, line in ipairs(split) do
split[i] = line:gsub("^" .. leading, "")
end
return table.concat(split, "\n"), #leading
end
---@class CodeChunk
---@field range table
---@field lang string
---@field node any
---@field text string
---@field leading_offset number
---Extract code chunks from the specified buffer.
---Updates M._otters_attached[main_nr].code_chunks
---@param main_nr integer The main buffer number
---@param lang string|nil language to extract. All languages if nil.
---@return CodeChunk[]
M.extract_code_chunks = function(main_nr, lang, exclude_eval_false, row_from, row_to)
local query = M._otters_attached[main_nr].query
local parser = M._otters_attached[main_nr].parser
local tree = parser:parse()
local root = tree[1]:root()
local code_chunks = {}
local lang_capture = nil
for _, match, metadata in query:iter_matches(root, main_nr, 0, -1, { all = true }) do
for id, nodes in pairs(match) do
local name = query.captures[id]
-- TODO: maybe can be removed with nvim v0.10
if type(nodes) ~= "table" then
nodes = { nodes }
end
for _, node in ipairs(nodes) do
local text
lang_capture = determine_language(main_nr, name, node, metadata, lang_capture)
if
lang_capture
and (name == "content" or name == "injection.content")
and (lang == nil or lang_capture == lang)
then
-- the actual code content
text = ts.get_node_text(node, main_nr, { metadata = metadata[id] })
-- remove surrounding quotes (workaround for treesitter offsets
-- not properly processed)
text, _ = fn.strip_wrapping_quotes(text)
if exclude_eval_false and string.find(text, "| *eval: *false") then
text = ""
end
local row1, col1, row2, col2 = node:range()
if row_from ~= nil and row_to ~= nil and ((row1 >= row_to and row_to > 0) or row2 < row_from) then
goto continue
end
local leading_offset
text, leading_offset = trim_leading_witespace(text, main_nr, row1)
local result = {
range = { from = { row1, col1 }, to = { row2, col2 } },
lang = lang_capture,
node = node,
text = fn.lines(text),
leading_offset = leading_offset,
}
if code_chunks[lang_capture] == nil then
code_chunks[lang_capture] = {}
end
table.insert(code_chunks[lang_capture], result)
-- reset current language
lang_capture = nil
elseif fn.contains(injectable_languages, name) then
-- chunks where the name of the language is the name of the capture
if lang == nil or name == lang then
text = ts.get_node_text(node, main_nr, { metadata = metadata[id] })
text, _ = fn.strip_wrapping_quotes(text)
local row1, col1, row2, col2 = node:range()
-- if was_stripped then
-- col1 = col1 + 1
-- col2 = col2 - 1
-- end
local leading_offset
text, leading_offset = trim_leading_witespace(text, main_nr, row1)
local result = {
range = { from = { row1, col1 }, to = { row2, col2 } },
lang = name,
node = node,
text = fn.lines(text),
leading_offset = leading_offset,
}
if code_chunks[name] == nil then
code_chunks[name] = {}
end
table.insert(code_chunks[name], result)
end
end
::continue::
end
end
end
return code_chunks
end
M.get_current_language_context = function(main_nr)
main_nr = main_nr or api.nvim_get_current_buf()
if M._otters_attached[main_nr] == nil then
return nil
end
local row, col = unpack(api.nvim_win_get_cursor(0))
row = row - 1
col = col
local query = M._otters_attached[main_nr].query
local parser = M._otters_attached[main_nr].parser
local tree = parser:parse()
local root = tree[1]:root()
local lang_capture = nil
for _, match, metadata in query:iter_matches(root, main_nr, 0, -1, { all = true }) do
for id, nodes in pairs(match) do
local name = query.captures[id]
-- TODO: maybe can be removed with nvim v0.10
if type(nodes) ~= "table" then
nodes = { nodes }
end
for _, node in ipairs(nodes) do
lang_capture = determine_language(main_nr, name, node, metadata, lang_capture)
local start_row, start_col, end_row, end_col = node:range()
end_row = end_row - 1
local language = nil
if lang_capture and (name == "content" or name == "injection.content") then
-- chunks where the name of the injected language is dynamic
-- e.g. markdown code chunks
if ts.is_in_node_range(node, row, col) then
language = lang_capture
end
-- chunks where the name of the language is the name of the capture
elseif fn.contains(injectable_languages, name) then
if ts.is_in_node_range(node, row, col) then
language = name
end
end
if language then
if config.cfg.handle_leading_whitespace then
local buf = M._otters_attached[main_nr].buffers[language]
if buf then
local lines = vim.api.nvim_buf_get_lines(buf, end_row - 1, end_row, false)
if lines[1] then
end_col = #lines[1]
end
end
end
return language, start_row, start_col, end_row, end_col
end
end
end
end
return nil
end
---find the leading_offset of the given line number, and buffer number. Returns 0 if the line number
---isn't in a chunk.
---@param line_nr number
---@param main_nr number
M.get_leading_offset = function(line_nr, main_nr)
if not config.cfg.handle_leading_whitespace then
return 0
end
local lang_chunks = M._otters_attached[main_nr].code_chunks
for _, chunks in pairs(lang_chunks) do
for _, chunk in ipairs(chunks) do
if line_nr >= chunk.range.from[1] and line_nr <= chunk.range.to[1] then
return chunk.leading_offset
end
end
end
return 0
end
---adjusts IN PLACE the position to include the start and end
---@param obj table
---@param main_nr number
---@param invert boolean?
---@param exclude_end boolean?
---@param known_offset number?
M.modify_position = function(obj, main_nr, invert, exclude_end, known_offset)
if not config.cfg.handle_leading_whitespace or known_offset == 0 then
return
end
local sign = invert and -1 or 1
local offset = known_offset
-- there are apparently a lot of ranges that different language servers can use
local ranges = { "range", "targetSelectionRange", "targetRange", "originSelectionRange" }
for _, range in ipairs(ranges) do
if obj[range] then
local start = obj[range].start
local end_ = obj[range]["end"]
offset = offset or M.get_leading_offset(start.line, main_nr) * sign
obj[range].start.character = start.character + offset
if not exclude_end then
obj[range]["end"].character = end_.character + offset
end
end
end
if obj.position then
local pos = obj.position
offset = offset or M.get_leading_offset(pos.line, main_nr) * sign
obj.position.character = pos.character + offset
end
if obj.documentChanges then
for _, change in ipairs(obj.documentChanges) do
if change.edits then
for _, edit in ipairs(change.edits) do
M.modify_position(edit, main_nr, invert, exclude_end, offset)
end
end
end
end
if obj.changes then
for _, change in pairs(obj.changes) do
for _, edit in ipairs(change) do
M.modify_position(edit, main_nr, invert, exclude_end, offset)
end
end
end
if obj.newText then
offset = offset or M.get_leading_offset(obj.range.start, main_nr) * sign
local str = ""
for _ = 1, offset, 1 do
str = str .. " "
end
-- Put indents in front of newline, but ignore newlines that are followed by newlines
obj.newText = string.gsub(obj.newText, "(\n)([^\n])", "%1" .. str .. "%2")
obj.newText = string.gsub(obj.newText, "\n$", "\n" .. str) -- match a potential newline at the end
end
end
--- Synchronize the raft of otters attached to a buffer
---@param main_nr integer bufnr of the parent buffer
---@param lang string|nil only sync one otter buffer matching a language
M.sync_raft = function(main_nr, lang)
if M._otters_attached[main_nr] ~= nil then
local all_code_chunks
local changetick = api.nvim_buf_get_changedtick(main_nr)
if M._otters_attached[main_nr].last_changetick == changetick then
all_code_chunks = M._otters_attached[main_nr].code_chunks
return
else
all_code_chunks = M.extract_code_chunks(main_nr)
end
M._otters_attached[main_nr].last_changetick = changetick
M._otters_attached[main_nr].code_chunks = all_code_chunks
local langs
if lang == nil then
langs = M._otters_attached[main_nr].languages
else
langs = { lang }
end
for _, lang in ipairs(langs) do
local otter_nr = M._otters_attached[main_nr].buffers[lang]
if otter_nr ~= nil then
local code_chunks = all_code_chunks[lang]
if code_chunks ~= nil then
local nmax = code_chunks[#code_chunks].range["to"][1] -- last code line
-- create list with empty lines the length of the buffer
local ls = fn.empty_lines(nmax)
-- collect language lines
for _, t in ipairs(code_chunks) do
local start_index = t.range["from"][1]
for i, l in ipairs(t.text) do
local index = start_index + i
table.remove(ls, index)
table.insert(ls, index, l)
end
end
-- replace language lines
api.nvim_buf_set_lines(otter_nr, 0, -1, false, ls)
else -- no code chunks so we wipe the otter buffer
api.nvim_buf_set_lines(otter_nr, 0, -1, false, {})
end
end
end
end
end
--- Send a request to the otter buffers and handle the response.
--- The response can optionally be filtered through a function.
---@param main_nr integer bufnr of main buffer
---@param request string lsp request
---@param filter function|nil function to process the response
---@param fallback function|nil optional function to call if not in an otter context
---@param handler function|nil optional function to handle the filtered lsp request for cases in which the default handler does not suffice
---@param conf table|nil optional config to pass to the handler.
M.send_request = function(main_nr, request, filter, fallback, handler, conf)
fallback = fallback or nil
filter = filter or function(x)
return x
end
M.sync_raft(main_nr)
local lang, start_row, start_col, end_row, end_col = M.get_current_language_context()
if not fn.contains(M._otters_attached[main_nr].languages, lang) then
if fallback then
fallback()
end
return
end
local otter_nr = M._otters_attached[main_nr].buffers[lang]
local otter_uri = vim.uri_from_bufnr(otter_nr)
local params
if request == "textDocument/documentSymbol" then
params = vim.lsp.util.make_text_document_params()
params.uri = otter_uri
else
params = vim.lsp.util.make_position_params()
end
-- general
params.textDocument = {
uri = otter_uri,
}
if request == "textDocument/references" then
params.context = {
includeDeclaration = true,
}
end
-- for 'textDocument/rename'
if request == "textDocument/rename" then
local cword = vim.fn.expand("<cword>")
local prompt_opts = {
prompt = "New Name: ",
default = cword,
}
vim.ui.input(prompt_opts, function(input)
params.newName = input
end)
end
if request == "textDocument/rangeFormatting" then
params = vim.lsp.util.make_formatting_params()
params.textDocument = {
uri = otter_uri,
}
params.range = {
start = { line = start_row, character = start_col },
["end"] = { line = end_row, character = end_col },
}
assert(end_row)
local line = vim.api.nvim_buf_get_lines(otter_nr, end_row, end_row + 1, false)[1]
if line then
params.range["end"].character = #line
end
M.modify_position(params, main_nr, true, true)
else
-- formatting gets its own special treatment, everything else gets the same
M.modify_position(params, main_nr, true)
end
vim.lsp.buf_request(otter_nr, request, params, function(err, response, ctx, ...)
if response == nil then
return
end
-- if response is a list of responses, filter every response
if #response > 0 then
local responses = {}
for _, res in ipairs(response) do
local filtered_res = filter(res)
if filtered_res then
M.modify_position(filtered_res, main_nr)
table.insert(responses, filtered_res)
end
end
response = responses
else
-- otherwise apply the filter to the one response
response = filter(response)
M.modify_position(response, main_nr)
end
if response == nil then
return
end
if handler ~= nil then
handler(err, response, ctx, conf)
else
vim.lsp.handlers[request](err, response, ctx, ...)
end
end)
end
--- Export the raft of otters as files.
--- Asks for filename for each language.
---@param force boolean
M.export_raft = function(force)
local main_nr = api.nvim_get_current_buf()
M.sync_raft(main_nr)
for _, otter_nr in pairs(M._otters_attached[main_nr].buffers) do
local path = api.nvim_buf_get_name(otter_nr)
local lang = M._otters_attached[main_nr].otter_nr_to_lang[otter_nr]
local extension = extensions[lang] or lang
path = fn.otterpath_to_plain_path(path) .. "." .. extension
vim.notify("Exporting otter: " .. lang)
local new_path = vim.fn.input({ prompt = "New path: ", default = path, completion = "file" })
if new_path ~= "" then
api.nvim_set_current_buf(otter_nr)
vim.lsp.buf.format({ bufnr = otter_nr })
vim.cmd.write({ new_path, bang = force })
end
api.nvim_set_current_buf(main_nr)
end
end
--- Export only one language to a pre-specified filename
---@param language string
---@param fname string
---@param force boolean
M.export_otter_as = function(language, fname, force)
local main_nr = api.nvim_get_current_buf()
M.sync_raft(main_nr)
for _, otter_nr in pairs(M._otters_attached[main_nr].buffers) do
local path = api.nvim_buf_get_name(otter_nr)
local lang = M._otters_attached[main_nr].otter_nr_to_lang[otter_nr]
if lang ~= language then
return
end
path = path:match("(.*[/\\])") .. fname
api.nvim_set_current_buf(otter_nr)
vim.lsp.buf.format()
vim.cmd.write({ path, bang = force })
api.nvim_set_current_buf(main_nr)
end
end
M.get_curent_language_lines = function(exclude_eval_false, row_start, row_end)
local main_nr = vim.api.nvim_get_current_buf()
M.sync_raft(main_nr)
local lang = M.get_current_language_context()
if lang == nil then
return
end
local chunks = M.extract_code_chunks(main_nr, lang, exclude_eval_false, row_start, row_end)[lang]
if not chunks or next(chunks) == nil then
return
end
local code = {}
for _, c in ipairs(chunks) do
table.insert(code, fn.concat(c.text))
end
return code
end
M.get_language_lines_around_cursor = function()
local main_nr = vim.api.nvim_get_current_buf()
local row, col = unpack(api.nvim_win_get_cursor(0))
row = row - 1
col = col
local query = M._otters_attached[main_nr].query
local parser = M._otters_attached[main_nr].parser
local tree = parser:parse()
local root = tree[1]:root()
for pattern, match, metadata in query:iter_matches(root, main_nr, 0, -1, { all = true }) do
for id, nodes in pairs(match) do
local name = query.captures[id]
-- TODO: maybe can be removed with nvim v0.10
if type(nodes) ~= "table" then
nodes = { nodes }
end
for _, node in ipairs(nodes) do
if name == "content" then
if ts.is_in_node_range(node, row, col) then
return ts.get_node_text(node, main_nr, metadata)
end
-- chunks where the name of the language is the name of the capture
elseif fn.contains(injectable_languages, name) then
if ts.is_in_node_range(node, row, col) then
return ts.get_node_text(node, main_nr, metadata)
end
end
end
end
end
end
M.get_language_lines_to_cursor = function(exclude_eval_false)
local row, _ = unpack(vim.api.nvim_win_get_cursor(0))
row = row + 1
return M.get_curent_language_lines(exclude_eval_false, 0, row)
end
M.get_language_lines_from_cursor = function(exclude_eval_false)
local row, _ = unpack(vim.api.nvim_win_get_cursor(0))
row = row + 1
return M.get_curent_language_lines(exclude_eval_false, row, -1)
end
M.get_language_lines = function(exclude_eval_false)
return M.get_curent_language_lines(exclude_eval_false)
end
M.get_language_lines_in_visual_selection = function(exclude_eval_false)
local lang = M.get_current_language_context()
if lang == nil then
return
end
local row_start, _ = unpack(api.nvim_buf_get_mark(0, "<"))
local row_end, _ = unpack(api.nvim_buf_get_mark(0, ">"))
row_start = row_start - 1
row_end = row_end - 1
return M.get_curent_language_lines(exclude_eval_false, row_start, row_end)
end
return M