Skip to content

Commit 9da1388

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

6 files changed

Lines changed: 101 additions & 59 deletions

File tree

README.md

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,24 +76,27 @@ 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
7981
},
8082
window = {
8183
border = 'double', -- single|double|rounded
8284
height = 0.6, -- percentage of main window
8385
},
8486
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 = '?'
87+
toggle = '`', -- toggle console
88+
attach = '<Leader>`', -- attach console to a buffer
89+
quit = 'q', -- close console
90+
eval = '<CR>', -- evaluate code
91+
eval_buffer = '<S-CR>', -- evaluate whole buffer
92+
kill_ps = '<Leader>K', -- kill evaluation process
93+
open = 'gf', -- open link
94+
messages = 'M', -- load Neovim messages
95+
save = 'S', -- save session
96+
load = 'L', -- load session
97+
resize_up = '<C-Up>', -- resize up
98+
resize_down = '<C-Down>', -- resize down
99+
help = '?' -- help
97100
},
98101
}
99102
```
@@ -108,7 +111,7 @@ opts = {
108111
- Install, press the mapped key `` ` `` and start exploring.
109112
- Enter code as normal, in insert mode.
110113
- 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.
114+
- 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.
112115
- The evaluation of the last line is returned and printed, so no `return` is needed in most cases.
113116
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.
114117
The result of assignments on the last line will be also shown as virtual text.
@@ -142,7 +145,7 @@ If you want the context to be cleared before every execution, set `preserve_cont
142145

143146
There are two functions available within the console:
144147

145-
- `_ctx()` - will print the contents of the context
148+
- `_ctx` - will print the contents of the context
146149
- `_ctx_clear()` - clears the context
147150

148151
<br>
@@ -276,6 +279,9 @@ There are two functions available within the console:
276279

277280
<br>
278281

282+
- If you need to kill the evaluation process, press `<Leader>K`.
283+
- 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()`.
284+
279285
## Alternatives and comparison
280286

281287
There are a number of alternatives available, notably:

lua/lua-console/config.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ 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
1314
},
1415
window = {

lua/lua-console/utils.lua

Lines changed: 37 additions & 27 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,17 +247,16 @@ 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]
@@ -442,7 +452,7 @@ local get_external_evaluator = function(buf, lang)
442452
local code = (lang_config.code_prefix or '') .. to_string(remove_indentation(lines)) -- some languages, like python are concerned with indentation
443453
table.insert(cmd, code)
444454

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

447457
local status, id = pcall(vim.system, cmd, opts, opts.on_exit)
448458

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

spec/unit/utils_spec.lua

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,19 +147,42 @@ describe('lua-console.utils', function()
147147
end)
148148

149149
it('provides access to context', function()
150-
config.setup { buffer = { preserve_context = true } }
150+
config.setup { buffer = { preserve_context = true, strip_local = false } }
151+
152+
code = { 'test_1 = 5; b = 10; local c = 100' }
153+
lua_evaluator(code)
154+
155+
code = { 'test_1 = test_1 + 5; b = b * 10; c = c - 50' }
156+
lua_evaluator(code)
157+
158+
code = { '_ctx' }
159+
160+
expected = h.to_string([[
161+
{
162+
b = 100,
163+
test_1 = 10
164+
}
165+
]])
166+
167+
result = lua_evaluator(code)
168+
assert.has_string(result, expected)
169+
end)
170+
171+
it('strips local declaration', function()
172+
config.setup { buffer = { preserve_context = true, strip_local = true } }
151173

152174
code = { 'test_1 = 5; b = 10; local c = 100' }
153175
lua_evaluator(code)
154176

155177
code = { 'test_1 = test_1 + 5; b = b * 10; c = c - 50' }
156178
lua_evaluator(code)
157179

158-
code = { '_ctx()' }
180+
code = { '_ctx' }
159181

160182
expected = h.to_string([[
161183
{
162184
b = 100,
185+
c = 50,
163186
test_1 = 10
164187
}
165188
]])

0 commit comments

Comments
 (0)