-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmolten.lua
522 lines (465 loc) · 15.4 KB
/
molten.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
if pcall(require, 'image') then
vim.g.molten_image_provider = 'image.nvim'
end
vim.g.molten_auto_init_behavior = 'init'
vim.g.molten_enter_output_behavior = 'open_and_enter'
vim.g.molten_output_win_max_height = 16
vim.g.molten_output_win_cover_gutter = false
vim.g.molten_output_win_border = 'single'
vim.g.molten_output_win_style = 'minimal'
vim.g.molten_auto_open_output = false
vim.g.molten_output_show_more = true
vim.g.molten_virt_text_max_lines = 16
vim.g.molten_wrap_output = true
---Shows a warning message from molten
---@param msg string Content of the notification to show to the user.
---@param level integer|nil One of the values from |vim.log.levels|.
---@param opts table|nil Optional parameters. Unused by default.
---@return nil
local function molten_warn(msg, level, opts)
vim.notify('[Molten] ' .. msg, level or vim.log.levels.WARN, opts)
end
local essentials = {
pynvim = true,
ipykernel = true,
jupyter_client = true,
}
local deps = {
cairosvg = true,
ipykernel = true,
jupyter_client = true,
kaleido = true,
nbformat = true,
plotly = true,
pnglatex = true,
pynvim = true,
pyperclip = true,
pyqt6 = true,
};
---Check all dependencies, install them if they are missing
(function()
if vim.fn.executable('pip') == 0 then
molten_warn('pip not found, skipping python dependency check')
return
end
molten_warn('checking python dependencies...', vim.log.levels.INFO)
for pkg, _ in pairs(deps) do
vim.system(
{ 'pip', 'show', pkg },
{},
vim.schedule_wrap(function(obj)
if obj.code == 0 then
deps[pkg] = nil
return
end
molten_warn('dependency ' .. pkg .. ' not found', vim.log.levels.INFO)
-- Install dependencies automatically only if we are in a virtual
-- environment
if not vim.env.VIRTUAL_ENV then
return
end
vim.system(
{ 'pip', 'install', pkg },
{},
vim.schedule_wrap(function(o)
if o.code == 0 then
molten_warn('installed ' .. pkg, vim.log.levels.INFO)
else
molten_warn(
string.format('failed to install %s: %s', pkg, o.stderr or '')
)
end
deps[pkg] = nil
end)
)
end)
)
end
end)()
-- Block until all dependencies have been checked
-- When in a virtual environment, we need to wait longer for the possible
-- installation of dependencies
local CHECK_DEPS_TIMEOUT = vim.env.VIRTUAL_ENV and 10000 or 4000
vim.wait(CHECK_DEPS_TIMEOUT, function()
return vim.tbl_isempty(deps)
end)
if not vim.tbl_isempty(deps) then
molten_warn(
'missing dependencies: ' .. table.concat(vim.tbl_keys(deps), ', ')
)
local missing_essentials = vim.tbl_filter(function(pkg)
return essentials[pkg]
end, vim.tbl_keys(deps))
if not vim.tbl_isempty(missing_essentials) then
molten_warn(
'missing essential dependencies: '
.. table.concat(missing_essentials, ', ')
)
molten_warn('abort')
return
end
end
-- Molten is lazy-loaded, so we need to re-generate and source rplugin manifest
if not pcall(vim.fn.MoltenStatusLineInit) then
vim.cmd.UpdateRemotePlugins()
local manifest = vim.g.loaded_remote_plugins
if manifest and (vim.uv.fs_stat(manifest) or {}).type == 'file' then
vim.cmd.source(manifest)
end
end
local groupid = vim.api.nvim_create_augroup('MoltenSetup', {})
vim.api.nvim_create_autocmd('BufEnter', {
desc = 'Change the configuration when editing a python file.',
pattern = '*.py',
group = groupid,
callback = function(info)
if info.buf ~= vim.api.nvim_get_current_buf() then
return
end
if require('molten.status').initialized() == 'Molten' then -- this is kinda a hack...
vim.fn.MoltenUpdateOption('output_win_border', 'single')
vim.fn.MoltenUpdateOption('virt_lines_off_by_1', nil)
vim.fn.MoltenUpdateOption('virt_text_output', nil)
else
vim.g.molten_output_win_border = 'single'
vim.g.molten_virt_lines_off_by_1 = nil
vim.g.molten_virt_text_output = nil
end
end,
})
vim.api.nvim_create_autocmd('BufEnter', {
desc = 'Undo config changes when we go back to a markdown or quarto file.',
pattern = { '*.md', '*.ipynb' },
group = groupid,
callback = function(info)
if info.buf ~= vim.api.nvim_get_current_buf() then
return
end
if require('molten.status').initialized() == 'Molten' then
vim.fn.MoltenUpdateOption('output_win_border', { '', '', '', '' })
vim.fn.MoltenUpdateOption('virt_lines_off_by_1', true)
vim.fn.MoltenUpdateOption('virt_text_output', true)
else
vim.g.molten_output_win_border = { '', '', '', '' }
vim.g.molten_virt_lines_off_by_1 = true
vim.g.molten_virt_text_output = true
end
-- Do not show molten cell background in markdown/quarto files
vim.opt_local.winhl:append('MoltenCell:')
end,
})
---Send code cell to molten
---@param cell code_cell_t
---@return nil
local function send(cell)
local range = cell.range
vim.fn.MoltenEvaluateRange(range.from[1] + 1, range.to[1])
end
---Code range, 0-based, end-exclusive
---@class code_range_t
---@field from integer[] 0-based (row, col) array
---@field to integer[] 0-based (row, col) array
---@class code_cell_t
---@field lang string?
---@field text table<string>
---@field range code_range_t
---Check if two ranges are overlapped
---@param r1 code_range_t
---@param r2 code_range_t
---@return boolean
local function is_overlapped(r1, r2)
return r1.from[1] <= r2.to[1] and r2.from[1] <= r1.to[1]
end
---Get the overlap between two (line) ranges
---@param r1 code_range_t
---@param r2 code_range_t
---@return code_range_t?
local function get_overlap(r1, r2)
if is_overlapped(r1, r2) then
return {
from = { math.max(r1.from[1], r2.from[1]), 0 },
to = { math.min(r1.to[1], r2.to[1]), 0 },
}
end
end
---Extract code cells that overlap the given range,
---removes cells with a language that's in the ignore list
---@param lang string
---@param code_chunks table<string, code_cell_t>
---@param range code_range_t
---@param partial boolean?
---@return code_cell_t[]
local function extract_cells(lang, code_chunks, range, partial)
if not code_chunks[lang] then
return {}
end
local chunks = {}
if partial then
for _, chunk in ipairs(code_chunks[lang]) do
local overlap = get_overlap(chunk.range, range)
if overlap then
if vim.deep_equal(overlap, chunk.range) then -- full overlap
table.insert(chunks, chunk)
else -- partial overlap
local text = {}
local lnum_start = overlap.from[1] - chunk.range.from[1] + 1
local lnum_end = lnum_start + overlap.to[1] - overlap.from[1]
for i = lnum_start, lnum_end do
table.insert(text, chunk.text[i])
end
table.insert(
chunks,
vim.tbl_extend('force', chunk, {
text = text,
range = overlap,
})
)
end
end
end
else
for _, chunk in ipairs(code_chunks[lang]) do
if is_overlapped(chunk.range, range) then
table.insert(chunks, chunk)
end
end
end
return chunks
end
local otk
---@type table<string, true>
local not_runnable = {
markdown = true,
markdown_inline = true,
yaml = true,
}
---Find valid language under cursor that can be sent to REPL
---@return string?
local function get_valid_repl_lang()
local lang = otk.get_current_language_context()
if not lang or not_runnable[lang] then
return
end
return lang
end
---Run code for the current language that overlap the given range
---
---Code are run in chunks (cells) , i.e. the whole chunk will be sent to
---REPL even when there are only partial overlap between the chunk and `range`
---@param range code_range_t a range, for with any overlapping code cells are run
---@return nil
local function run_cell(range)
local buf = vim.api.nvim_get_current_buf()
local lang = get_valid_repl_lang() or 'python'
otk.sync_raft(buf)
local otk_buf_info = otk.rafts[buf]
if not otk_buf_info then
molten_warn('code runner not initialized for buffer ' .. buf)
return
end
local filtered = extract_cells(lang, otk_buf_info.code_chunks, range)
if #filtered == 0 then
molten_warn('no code found for ' .. lang)
return
end
for _, chunk in ipairs(filtered) do
send(chunk)
end
end
---Run current cell
---@return nil
local function run_cell_current()
local y = vim.api.nvim_win_get_cursor(0)[1]
local r = { y, 0 }
local range = { from = r, to = r }
run_cell(range)
end
---Run current cell and all above
---@return nil
local function run_cell_above()
local y = vim.api.nvim_win_get_cursor(0)[1]
local range = { from = { 0, 0 }, to = { y, 0 } }
run_cell(range)
end
---Run current cell and all below
---@return nil
local function run_cell_below()
local y = vim.api.nvim_win_get_cursor(0)[1]
local range = { from = { y, 0 }, to = { math.huge, 0 } }
run_cell(range)
end
---Run current line of code
---@return nil
local function run_line()
local lang = get_valid_repl_lang()
if not lang then
return
end
local buf = vim.api.nvim_get_current_buf()
local pos = vim.api.nvim_win_get_cursor(0)
---@type code_cell_t
local cell = {
lang = lang,
range = { from = { pos[1] - 1, 0 }, to = { pos[1], 0 } },
text = vim.api.nvim_buf_get_lines(buf, pos[1] - 1, pos[1], false),
}
send(cell)
end
---Run code in range `range`
---
---Code are run in lines, i.e. only code lines in `range` will be sent to REPL,
---if there is a partial overlap between `range` and a code chunk,
---only the lines inside `range` will be run
---@param range code_range_t
---@return nil
local function run_range(range)
local buf = vim.api.nvim_get_current_buf()
local lang = get_valid_repl_lang() or 'python'
otk.sync_raft(buf)
local otk_buf_info = otk.rafts[buf]
if not otk_buf_info then
molten_warn('code runner not initialized for buffer ' .. buf)
return
end
local filtered = extract_cells(lang, otk_buf_info.code_chunks, range, true)
if #filtered == 0 then
molten_warn('no code found for ' .. lang)
return
end
for _, chunk in ipairs(filtered) do
send(chunk)
end
end
---Run code in previous visual selection
---@return nil
local function run_visual()
local vstart = vim.fn.getpos("'<")
local vend = vim.fn.getpos("'>")
run_range({
from = { vstart[2] - 1, 0 },
to = { vend[2], 0 },
})
end
---Run code covered by operator
---@return nil
local function run_operator()
vim.opt.opfunc = 'v:lua._molten_nb_run_opfunc'
vim.api.nvim_feedkeys('g@', 'n', false)
end
---@param _ 'line'|'char'|'block' operator type, ignored
---@return nil
function _G._molten_nb_run_opfunc(_)
local ostart = vim.fn.getpos("'[")
local oend = vim.fn.getpos("']")
run_range({
from = { ostart[2] - 1, 0 },
to = { oend[2], 0 },
})
end
---Set buffer-local keymaps and commands
---@param buf integer? buffer handler, defaults to current buffer
---@return nil
local function setup_buf_keymaps_and_commands(buf)
buf = buf or vim.api.nvim_get_current_buf()
if not vim.api.nvim_buf_is_valid(buf) then
return
end
local ft = vim.bo[buf].ft
if ft ~= 'markdown' and ft ~= 'python' then
return
end
-- Skip non-notebook markdown files
if
ft == 'markdown'
and vim.fn.fnamemodify(vim.api.nvim_buf_get_name(buf), ':e') ~= 'ipynb'
then
return
end
vim.keymap.set('n', '<C-c>', vim.cmd.MoltenInterrupt, {
buffer = buf,
desc = 'Interrupt kernel',
})
---Enter cell output
local function enter_cell_output()
vim.cmd.MoltenEnterOutput({ mods = { noautocmd = true } })
if vim.bo.ft ~= 'molten_output' then
return
end
if vim.fn.exists('*matchup#loader#bufwinenter') == 1 then
vim.fn['matchup#loader#bufwinenter']()
end
local opts = { buffer = true, desc = 'Exit cell output' }
vim.keymap.set('n', '<C-k>', '<C-w>c', opts)
vim.keymap.set('n', '<C-Up>', '<C-w>c', opts)
local src_win = vim.fn.win_getid(vim.fn.winnr('#'))
local output_win = vim.api.nvim_get_current_win()
vim.api.nvim_create_autocmd('WinScrolled', {
desc = 'Close molten output win when src win is scrolled.',
group = vim.api.nvim_create_augroup('MoltenCloseOutputWin' .. buf, {}),
buffer = buf,
callback = function(info)
if src_win == tonumber(info.match) then
vim.schedule(function()
if vim.api.nvim_win_is_valid(output_win) then
vim.api.nvim_win_close(output_win, false)
end
end)
end
end,
})
end
local opts = { buffer = buf, desc = 'Enter cell output' }
vim.keymap.set('n', '<C-j>', enter_cell_output, opts)
vim.keymap.set('n', '<C-Down>', enter_cell_output, opts)
local otk_ok
otk_ok, otk = pcall(require, 'otter.keeper')
-- Use otter to recognized codeblocks in markdown files,
-- so we can run current codeblock directly without selection
-- using `<CR>`, and other good stuffs
-- stylua: ignore start
if ft == 'markdown' and otk_ok then
vim.api.nvim_buf_create_user_command(buf, 'MoltenNotebookRunLine', run_line, {})
vim.api.nvim_buf_create_user_command(buf, 'MoltenNotebookRunCellAbove', run_cell_above, {})
vim.api.nvim_buf_create_user_command(buf, 'MoltenNotebookRunCellBelow', run_cell_below, {})
vim.api.nvim_buf_create_user_command(buf, 'MoltenNotebookRunCellCurrent', run_cell_current, {})
vim.api.nvim_buf_create_user_command(buf, 'MoltenNotebookRunVisual', run_visual, { range = true })
vim.api.nvim_buf_create_user_command(buf, 'MoltenNotebookRunOperator', run_operator, {})
vim.keymap.set('n', '<LocalLeader><CR>', run_operator, { buffer = buf, desc = 'Run code selected by operator' })
vim.keymap.set('n', '<LocalLeader>k', run_cell_above, { buffer = buf, desc = 'Run current cell and all above' })
vim.keymap.set('n', '<LocalLeader>j', run_cell_below, { buffer = buf, desc = 'Run current cell and all below' })
vim.keymap.set('n', '<CR>', run_cell_current, { buffer = buf, desc = 'Run current cell' })
vim.keymap.set('x', '<CR>', ':<C-u>MoltenNotebookRunVisual<CR>', { buffer = buf, desc = 'Run selected code' })
else -- ft == 'python' or otter.keeper not found
vim.keymap.set('n', '<LocalLeader><CR>', vim.cmd.MoltenEvaluateOperator, { buffer = buf, desc = 'Run code selected by operator' })
vim.keymap.set('n', '<LocalLeader><CR><CR>', vim.cmd.MoltenReevaluateAll, { buffer = buf, desc = 'Rerun all cells' })
vim.keymap.set('n', '<CR>', '<Cmd>MoltenReevaluateCell<CR>', { buffer = buf, desc = 'Rerun current cell' })
vim.keymap.set('x', '<CR>', ':<C-u>MoltenEvaluateVisual<CR>', { buffer = buf, desc = 'Run selected code' })
end
-- stylua: ignore end
end
-- Setup for existing buffers
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
setup_buf_keymaps_and_commands(buf)
end
vim.api.nvim_create_autocmd('FileType', {
desc = 'Set buffer-local keymaps and commands for molten.',
pattern = { 'python', 'markdown' },
group = groupid,
callback = function(info)
setup_buf_keymaps_and_commands(info.buf)
end,
})
---Set default highlight groups for headlines.nvim
---@return nil
local function set_default_hlgroups()
local hl = require('utils.hl')
hl.set(0, 'MoltenCell', { link = 'CursorLine' })
hl.set(0, 'MoltenOutputWin', { link = 'Comment' })
hl.set(0, 'MoltenOutputWinNC', { link = 'Comment' })
end
set_default_hlgroups()
vim.api.nvim_create_autocmd('ColorScheme', {
group = groupid,
desc = 'Set default highlight groups for headlines.nvim.',
callback = set_default_hlgroups,
})