Skip to content

Commit c0027c3

Browse files
committed
docs: update help and docs
1 parent 17f8b64 commit c0027c3

File tree

7 files changed

+135
-84
lines changed

7 files changed

+135
-84
lines changed

.luacheckrc

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,22 @@ ignore = {
1111
-- Neovim lua API + luacheck thinks variables like `vim.wo.spell = true` is
1212
-- invalid when it actually is valid. So we have to display rule `W122`.
1313
--
14-
"122", -- setting read-only field of vim...
15-
"631", -- max_line_length
16-
"621", -- incostistant indentation
17-
"611" -- line with whitespace
14+
'122', -- setting read-only field of vim...
15+
'631', -- max_line_length
16+
'621', -- incostistant indentation
17+
'611', -- line with whitespace
18+
'111', -- setting non-standard global variable, i.e. _
1819
}
1920

2021
-- Global objects defined by the C code
2122
read_globals = {
22-
"vim",
23-
"Lua_console",
24-
"describe",
25-
"it",
26-
"assert"
23+
'vim',
24+
'Lua_console',
25+
'describe',
26+
'it',
27+
'assert',
2728
}
2829

2930
exclude_files = {
30-
"spec/utfTerminal.lua"
31+
'spec/utfTerminal.lua',
3132
}

README.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,24 +76,28 @@ opts = {
7676
preserve_context = true, -- preserve results between evaluations
7777
strip_local = true, -- remove local identifier from source code
7878
print_one_line_results = true, -- prints one line results, even if already shown as virtual text
79+
notify_result = false, -- notify result
80+
clear_before_eval = false, -- clear output below result prefix before evaluation of the whole buffer
81+
process_timeout = 2 * 1e5, -- number of instructions to process before timeout
7982
},
8083
window = {
8184
border = 'double', -- single|double|rounded
8285
height = 0.6, -- percentage of main window
8386
},
8487
mappings = {
85-
toggle = '`',
86-
attach = '<Leader>`',
87-
quit = 'q',
88-
eval = '<CR>',
89-
eval_buffer = '<S-CR>',
90-
open = 'gf',
91-
messages = 'M',
92-
save = 'S',
93-
load = 'L',
94-
resize_up = '<C-Up>',
95-
resize_down = '<C-Down>',
96-
help = '?'
88+
toggle = '`', -- toggle console
89+
attach = '<Leader>`', -- attach console to a buffer
90+
quit = 'q', -- close console
91+
eval = '<CR>', -- evaluate code
92+
eval_buffer = '<S-CR>', -- evaluate whole buffer
93+
kill_ps = '<Leader>K', -- kill evaluation process
94+
open = 'gf', -- open link
95+
messages = 'M', -- load Neovim messages
96+
save = 'S', -- save session
97+
load = 'L', -- load session
98+
resize_up = '<C-Up>', -- resize up
99+
resize_down = '<C-Down>', -- resize down
100+
help = '?' -- help
97101
},
98102
}
99103
```
@@ -108,7 +112,7 @@ opts = {
108112
- Install, press the mapped key `` ` `` and start exploring.
109113
- Enter code as normal, in insert mode.
110114
- Hit `Enter` in normal mode to evaluate a variable, statement or an expression in the current line.
111-
- Visually select a region or range of lines and press `Enter` to evaluate the code in the range or use `<S-Enter>` to evaluate the whole console.
115+
- Visually select a region or a range of lines and press `Enter` to evaluate the code in the range or use `<S-Enter>` to evaluate the whole console.
112116
- The evaluation of the last line is returned and printed, so no `return` is needed in most cases.
113117
To avoid noise, if the return of your execution is `nil`, e.g. from a loop or a function without return, it will not be printed, but shown as virtual text.
114118
The result of assignments on the last line will be also shown as virtual text.
@@ -142,7 +146,7 @@ If you want the context to be cleared before every execution, set `preserve_cont
142146

143147
There are two functions available within the console:
144148

145-
- `_ctx()` - will print the contents of the context
149+
- `_ctx` - will print the contents of the context
146150
- `_ctx_clear()` - clears the context
147151

148152
<br>
@@ -276,6 +280,9 @@ There are two functions available within the console:
276280

277281
<br>
278282

283+
- If you need to kill the evaluation process, press `<Leader>K`.
284+
- You can use `_ex` command to get a reference to the evaluator process, which can be queried for status with `:is_closing()` and stopped with `:kill()`.
285+
279286
## Alternatives and comparison
280287

281288
There are a number of alternatives available, notably:

lua/lua-console/config.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ local default_config = {
99
preserve_context = true, -- preserve results between evaluations
1010
strip_local = true, -- remove local identifier from source code
1111
print_one_line_results = true, -- prints one line results, even if already shown as virtual text
12+
notify_result = false, -- notify result
1213
clear_before_eval = false, -- clear output below result prefix before evaluation of the whole buffer
14+
process_timeout = 2 * 1e5, -- number of instructions to process before timeout
1315
},
1416
window = {
1517
relative = 'editor',

lua/lua-console/utils.lua

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ end
5050
---@param lnum number line number
5151
---@param position string virtual text position
5252
---@param highlight string higlight group
53-
local show_virtual_text = function(buf, id, text, lnum, position, highlight)
54-
local ns = vim.api.nvim_create_namespace('Lua-console')
53+
local show_virtual_text = function(buf, ns, id, text, lnum, position, highlight)
54+
ns = ns == 0 and vim.api.nvim_create_namespace('Lua-console') or ns
5555

5656
local ext_mark = vim.api.nvim_buf_get_extmark_by_id(0, ns, id, {})
5757
if #ext_mark > 0 then vim.api.nvim_buf_del_extmark(0, ns, id) end
@@ -69,36 +69,42 @@ end
6969

7070
local toggle_help = function(buf)
7171
local cm = config.mappings
72-
local ns = vim.api.nvim_create_namespace('Lua-console')
72+
local ns = vim.api.nvim_create_namespace('Lua-console-help')
7373
local ids = vim.api.nvim_buf_get_extmarks(buf, ns, 0, -1, {})
7474
local message
7575

7676
if #ids == 0 or ids[1][1] == 2 then
77-
vim.api.nvim_buf_del_extmark(buf, ns, 2)
77+
vim.api.nvim_buf_clear_namespace(buf, ns, 0, -1)
7878

7979
message = cm.help .. ' - help '
80-
show_virtual_text(buf, 1, message, 0, 'right_align', 'Comment')
80+
show_virtual_text(buf, ns, 1, message, 0, 'right_align', 'Comment')
8181
elseif ids[1][1] == 1 then
8282
vim.api.nvim_buf_del_extmark(buf, ns, 1)
8383

84-
message =
85-
[[%s - eval a line or selection, %s - eval buffer, %s - kill process, %s - open file, %s - load messages, %s - save console, %s - load console, %s/%s - resize window, %s - toggle help]]
86-
message = string.format(
87-
message,
88-
cm.eval,
89-
cm.eval_buffer,
90-
cm.kill_ps,
91-
cm.open,
92-
cm.messages,
93-
cm.save,
94-
cm.load,
95-
cm.resize_up,
96-
cm.resize_down,
97-
cm.help
98-
)
84+
local keys = {
85+
cm.eval .. ' - eval a line or selection',
86+
cm.eval_buffer .. ' - eval buffer',
87+
cm.kill_ps .. ' - kill process',
88+
cm.open .. ' - open file',
89+
cm.messages .. ' - load messages',
90+
cm.save .. ' - save console',
91+
cm.load .. ' - load console',
92+
cm.resize_up .. ' - resize window up',
93+
cm.resize_down .. ' - resize window down',
94+
cm.help .. ' - toggle help',
95+
' ',
96+
'_ctx - get current context',
97+
'_ctx_clear() - clear current context',
98+
'_ex - get current evaluator process',
99+
'_ex:kill() - kill evaluator process',
100+
'_ex:is_closing() - get process status',
101+
}
99102

100103
local visible_line = vim.fn.line('w0')
101-
show_virtual_text(buf, 2, message, visible_line - 1, 'overlay', 'Comment')
104+
vim.iter(keys):enumerate():each(function(i, key)
105+
_ = vim.api.nvim_buf_line_count(buf) < i and vim.api.nvim_buf_set_lines(buf, i - 1, -1, false, { '' })
106+
show_virtual_text(buf, ns, i + 1, key .. ' ', visible_line + i - 2, 'right_align', 'Comment')
107+
end)
102108
end
103109
end
104110

@@ -208,6 +214,11 @@ local print_to_buffer = function(...)
208214
vim.list_extend(print_buffer, ret)
209215
end
210216

217+
local notify_result = function(ret)
218+
if not config.buffer.notify_result then return end
219+
vim.notify(to_string(ret), vim.log.levels.INFO)
220+
end
221+
211222
---@param buf number
212223
---@param lines string[] Text to append to current buffer after current selection
213224
---@param lnum? number|nil Line number to append from
@@ -227,7 +238,7 @@ local append_current_buffer = function(buf, lines, lnum)
227238
local _, last_assigned_val, last_assignment_lnum = get_last_assignment()
228239
if last_assignment_lnum then
229240
last_assigned_val = to_string(pretty_print(last_assigned_val), '', true)
230-
show_virtual_text(buf, 3, prefix .. last_assigned_val, last_assignment_lnum - 1, 'eol', 'Comment')
241+
show_virtual_text(buf, 0, 3, prefix .. last_assigned_val, last_assignment_lnum - 1, 'eol', 'Comment')
231242
end
232243

233244
if vim.tbl_contains(empty_results, line) then
@@ -236,21 +247,20 @@ local append_current_buffer = function(buf, lines, lnum)
236247
virtual_text = get_line_assignment(vim.fn.getbufline(buf, lnum, lnum)) or line -- ! resets env._last_assignment by calling evaluator
237248

238249
if last_assignment_lnum ~= lnum then
239-
show_virtual_text(buf, 4, prefix .. virtual_text, lnum - 1, 'eol', 'Comment')
250+
show_virtual_text(buf, 0, 4, prefix .. virtual_text, lnum - 1, 'eol', 'Comment')
240251
end
241252
end
242253

243254
if #lines == 0 then return end
244255

245256
if #lines == 1 and last_assignment_lnum ~= lnum and not config.buffer.show_one_line_results then
246257
virtual_text = lines[1]
247-
show_virtual_text(buf, 4, prefix .. virtual_text, lnum - 1, 'eol', 'Comment')
248-
249-
return
258+
return show_virtual_text(buf, 0, 4, prefix .. virtual_text, lnum - 1, 'eol', 'Comment')
259+
and notify_result(virtual_text)
250260
end
251261

252262
lines[1] = prefix .. lines[1]
253-
table.insert(lines, 1, '') -- insert an empty line
263+
_ = not config.buffer.clear_before_eval and table.insert(lines, 1, '') -- insert an empty line
254264

255265
vim.api.nvim_buf_set_lines(buf, lnum, lnum, false, lines)
256266
end
@@ -337,6 +347,17 @@ function get_ctx(buf)
337347
return setmetatable(env, mt)
338348
end
339349

350+
local function run_code(code)
351+
debug.sethook(function()
352+
error('EvaluationTimeout')
353+
end, '', config.buffer.process_timeout)
354+
355+
local result = { xpcall(code, debug.traceback) }
356+
debug.sethook()
357+
358+
return result
359+
end
360+
340361
--- Evaluates Lua code and returns pretty printed result with errors if any
341362
--- @param lines string[] table with lines of Lua code
342363
--- @param ctx? table environment to execute code in
@@ -359,25 +380,20 @@ function lua_evaluator(lines, ctx)
359380
lines = to_string(lines)
360381
lines = config.buffer.strip_local and lines:gsub('local ', '') or lines
361382

362-
local code, error = load(lines, 'Lua console: ', 't', env)
363-
if error then return to_table(error) end
383+
local code, errors = load(lines, 'Lua console: ', 't', env)
384+
if errors then return to_table(errors) end
364385

365386
print_buffer = {}
366387

367-
---@cast code function
368-
local result = { xpcall(code, debug.traceback) }
369-
local status, err = result[1], result[2]
388+
local result = run_code(code)
389+
local status, err = unpack(result)
370390

371391
if status then
372392
table.remove(result, 1)
373-
374-
if #result > 0 then
375-
print_to_buffer(unpack(result))
376-
else
377-
print_to_buffer(nil)
378-
end
393+
_ = #result > 0 and print_to_buffer(unpack(result)) or print_to_buffer(nil)
379394
else
380-
vim.list_extend(print_buffer, clean_stacktrace(err))
395+
err = err:match('EvaluationTimeout') and { 'Process timeout exceeded' } or clean_stacktrace(err)
396+
vim.list_extend(print_buffer, err)
381397
end
382398

383399
return print_buffer
@@ -433,16 +449,16 @@ local get_external_evaluator = function(buf, lang)
433449
opts.on_exit = function(system_completed)
434450
vim.schedule(function()
435451
vim.api.nvim_buf_del_extmark(buf, ns, 10)
452+
_ = fun and fun(system_completed)
436453
end)
437-
_ = fun and fun(system_completed)
438454
end
439455

440456
return function(lines)
441457
local cmd = vim.tbl_extend('force', {}, lang_config.cmd)
442458
local code = (lang_config.code_prefix or '') .. to_string(remove_indentation(lines)) -- some languages, like python are concerned with indentation
443459
table.insert(cmd, code)
444460

445-
ns = show_virtual_text(buf, 10, 'Running...', 0, 'right_align', 'Comment')
461+
ns = show_virtual_text(buf, 0, 10, 'Running... ', vim.fn.line('w0'), 'right_align', 'Comment')
446462

447463
local status, id = pcall(vim.system, cmd, opts, opts.on_exit)
448464

@@ -491,7 +507,7 @@ end
491507
local clear_buffer = function(buf)
492508
for i = vim.api.nvim_buf_line_count(buf), 0, -1 do
493509
if vim.fn.getline(i):match('^' .. config.buffer.result_prefix) then
494-
vim.api.nvim_buf_set_lines(buf, i - 1, -1, false, { '' })
510+
vim.api.nvim_buf_set_lines(buf, i - 1, -1, false, {})
495511
break
496512
end
497513
end

spec/unit/external_evals_spec.lua

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ describe('external evaluators', function()
211211
text = 'Test 5',
212212
timeout = 20,
213213
detach = 'Test 7',
214-
on_exit = 'Test 8',
215214
},
216215
},
217216
}
@@ -233,20 +232,21 @@ describe('external evaluators', function()
233232
end)
234233

235234
it('uses removes indentation from code', function()
236-
config.setup {
237-
external_evaluators = { ruby = { code_prefix = '' }, }, }
238-
239-
content = {
240-
' a = [1, 3, 5, 7, 9]',
241-
' for val in a:',
242-
' print(val)'
243-
}
244-
245-
expected = {
246-
'a = [1, 3, 5, 7, 9]',
247-
' for val in a:',
248-
' print(val)'
249-
}
235+
config.setup {
236+
external_evaluators = { ruby = { code_prefix = '' } },
237+
}
238+
239+
content = {
240+
' a = [1, 3, 5, 7, 9]',
241+
' for val in a:',
242+
' print(val)',
243+
}
244+
245+
expected = {
246+
'a = [1, 3, 5, 7, 9]',
247+
' for val in a:',
248+
' print(val)',
249+
}
250250

251251
h.set_buffer(buf, content)
252252

spec/unit/lua-console_spec.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('lua-console.nvim', function()
2323
it('sets up with custom config', function()
2424
config = {
2525
buffer = {
26-
result_prefix = '$$ '
26+
result_prefix = '$$ ',
2727
},
2828
window = {
2929
border = 'single',
@@ -32,6 +32,7 @@ describe('lua-console.nvim', function()
3232
toggle = '!#',
3333
attach = '!`',
3434
eval = '$#',
35+
kill_ps = '!K',
3536
},
3637
}
3738

@@ -107,6 +108,7 @@ describe('lua-console.nvim', function()
107108
local mappings = config.mappings
108109
mappings.toggle = nil
109110
mappings.attach = nil
111+
mappings.kill_ps = nil
110112

111113
local maps = h.get_maps(buf)
112114

0 commit comments

Comments
 (0)