Skip to content

Commit 971d8f1

Browse files
committed
feat: evaluate char selection
1 parent 6bba5fe commit 971d8f1

6 files changed

Lines changed: 84 additions & 26 deletions

File tree

lua/lua-console/config.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ local default_config = {
77
autosave = true, -- autosave on console hide / close
88
load_on_start = true, -- load saved session on start
99
preserve_context = true, -- preserve results between evaluations
10+
print_one_line_results = true,
1011
},
1112
window = {
1213
relative = 'editor',

lua/lua-console/mappings.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ M.set_evaluator_mappings = function(buf, toggle)
129129
callback = function()
130130
utils.eval_code_in_buffer(buf)
131131
end,
132-
}, { 'n', 'v' })
132+
}, { 'n', 'x' })
133133

134134
set_map(buf, m.eval_buffer, {
135135
desc = 'Eval code in current buffer',

lua/lua-console/utils.lua

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ local get_last_assignment = function()
169169
for i = lnum - 1, 0, -1 do
170170
line = vim.api.nvim_buf_get_lines(0, i, i + 1, false)[1]
171171

172-
if line:match('^%s*' .. last_var .. '%s*=') then break end
172+
if line:match('^%s*' .. last_var .. '%s*,?[^=]-=') then break end
173173
offset = offset + 1
174174
end
175175

@@ -232,11 +232,20 @@ local append_current_buffer = function(buf, lines, lnum)
232232

233233
virtual_text = get_line_assignment(vim.fn.getbufline(buf, lnum, lnum)) or line -- ! resets env._last_assignment by calling evaluator
234234

235-
if not last_assignment_lnum then show_virtual_text(buf, 4, prefix .. virtual_text, lnum - 1, 'eol', 'Comment') end
235+
if last_assignment_lnum ~= lnum then
236+
show_virtual_text(buf, 4, prefix .. virtual_text, lnum - 1, 'eol', 'Comment')
237+
end
236238
end
237239

238240
if #lines == 0 then return end
239241

242+
if #lines == 1 and last_assignment_lnum ~= lnum and not config.buffer.show_one_line_results then
243+
virtual_text = lines[1]
244+
show_virtual_text(buf, 4, prefix .. virtual_text, lnum - 1, 'eol', 'Comment')
245+
246+
return
247+
end
248+
240249
lines[1] = prefix .. lines[1]
241250
table.insert(lines, 1, '') -- insert an empty line
242251

@@ -429,23 +438,23 @@ end
429438
---@param buf number
430439
---@param range number[]
431440
---@return string
432-
local function get_lang(buf, range)
441+
local function get_lang(buf, lnum)
433442
local pattern = ('^.*' .. config.external_evaluators.lang_prefix .. '(%w+)%s*$')
434443
local line, lang
435444

436-
line = vim.api.nvim_buf_get_lines(buf, math.max(0, range[1] - 2), range[2], false)[1]
437-
lang = line:match(pattern)
445+
line = vim.api.nvim_buf_get_lines(buf, math.max(0, lnum - 1), lnum, false)[1]
446+
lang = line and line:match(pattern)
438447
if lang then return lang end
439448

440449
line = vim.api.nvim_buf_get_lines(buf, 0, 1, false)[1]
441-
lang = line:match(pattern)
450+
lang = line and line:match(pattern)
442451
if lang then return lang end
443452

444453
return vim.bo[buf].filetype
445454
end
446455

447-
local get_evaluator = function(buf, range)
448-
local lang = get_lang(buf, range)
456+
local get_evaluator = function(buf, lnum)
457+
local lang = get_lang(buf, lnum)
449458

450459
if lang == '' then
451460
vim.notify('Plese specify the language to evaluate or set the filetype', vim.log.levels.WARN)
@@ -461,30 +470,35 @@ end
461470
---@param full? boolean evaluate full buffer
462471
local eval_code_in_buffer = function(buf, full)
463472
buf = buf or vim.fn.bufnr()
464-
local win = vim.fn.bufwinid(buf)
465473

466-
if vim.api.nvim_get_mode().mode == 'V' then
467-
LOG('here')
468-
vim.api.nvim_input('<Esc>')
469-
end
474+
local mode = vim.api.nvim_get_mode().mode
475+
if mode == 'V' or mode == 'v' then vim.api.nvim_input('<Esc>') end
476+
477+
local v_start, v_end, lines
470478

471-
local v_start, v_end
472479
if full then
473480
v_start, v_end = 1, vim.api.nvim_buf_line_count(buf)
474-
else
475-
v_start, v_end = vim.fn.line('.', win), vim.fn.line('v', win)
481+
elseif mode == 'v' or mode == 'V' then
482+
v_start, v_end = vim.fn.getpos('.'), vim.fn.getpos('v')
483+
lines = vim.fn.getregion(v_start, v_end, { type = mode })
484+
485+
v_start, v_end = v_start[2], v_end[2]
486+
476487
if v_start > v_end then
477488
v_start, v_end = v_end, v_start
478489
end
490+
else
491+
v_start = vim.fn.line('.')
492+
v_end = v_start
479493
end
480494

481-
vim.api.nvim_win_set_cursor(win, { v_end, 0 })
495+
vim.fn.cursor(v_end, 0)
482496

483-
local lines = vim.api.nvim_buf_get_lines(buf, v_start - 1, v_end, false)
497+
lines = lines or vim.api.nvim_buf_get_lines(buf, v_start - 1, v_end, false)
484498
lines = remove_empty_lines(lines)
485499
if #lines == 0 then return end
486500

487-
local evaluator = get_evaluator(buf, { v_start, v_end })
501+
local evaluator = get_evaluator(buf, v_start - 1)
488502
if not evaluator then return end
489503

490504
local result = evaluator(lines)

spec/spec_helper.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,18 @@ M.send_keys = function(keys)
6161
end
6262

6363
M.get_virtual_text = function(buf, line_start, line_end)
64-
local ns = vim.api.nvim_create_namespace('Lua-console')
65-
local ids = vim.api.nvim_buf_get_extmarks(buf, ns, { line_start or 0, 0 }, { line_end or -1, -1 }, {})
64+
local ids = vim.api.nvim_buf_get_extmarks(buf, -1, { line_start or 0, 0 }, { line_end or -1, -1 }, { details = true })
6665

6766
if vim.tbl_isempty(ids) then
6867
_G.LOG('No extmarks found')
6968
return ''
7069
end
7170

72-
local mark = vim.api.nvim_buf_get_extmark_by_id(buf, ns, ids[1][1], { details = true })
71+
local marks = vim.tbl_map(function(mark)
72+
return mark[4].virt_text[1][1]
73+
end, ids)
7374

74-
return mark[3].virt_text[1][1]
75+
return marks
7576
end
7677

7778
---Collects paths for nested keys

spec/unit/mappings_spec.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ describe('lua-console.nvim - mappings', function()
2929
buffer = {
3030
load_on_start = false,
3131
save_path = vim.fn.stdpath('state') .. '/lua-console-test.lua',
32+
show_one_line_results = true,
3233
},
3334
}
3435

@@ -264,6 +265,7 @@ describe('lua-console.nvim - mappings', function()
264265
utils.attach_toggle(other_buf)
265266
end)
266267

268+
--TODO: finish spec
267269
it('cretes mappings for attached buffer', function()
268270
-- code
269271
end)
@@ -328,6 +330,7 @@ describe('lua-console.nvim - mappings', function()
328330
-- code
329331
end)
330332

333+
--TODO: finish spec
331334
it('removes mappings for dettached buffer', function()
332335
-- code
333336
end)

spec/unit/utils_spec.lua

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
local assert = require('luassert.assert')
22
local h = require('spec_helper')
33

4+
--TODO: add multiple assigmnets andchar selection
5+
46
describe('lua-console.utils', function()
57
_G.Lua_console = {}
68
local buf, config, utils
79

810
setup(function()
911
utils = require('lua-console.utils')
1012
config = require('lua-console.config')
11-
config.setup()
13+
config.setup { buffer = { show_one_line_results = true } }
1214
end)
1315

1416
before_each(function()
@@ -332,6 +334,23 @@ describe('lua-console.utils', function()
332334
assert.has_string(result, expected)
333335
end)
334336

337+
it('single line - char selection', function()
338+
content = h.to_table([[
339+
Some text
340+
LOG(vim.bo.filetype)
341+
Some text
342+
]])
343+
344+
h.set_buffer(buf, content)
345+
346+
vim.api.nvim_win_set_cursor(win, { 2, 4 })
347+
h.send_keys('v14l')
348+
utils.eval_code_in_buffer()
349+
350+
result = h.get_buffer(buf)
351+
assert.has_string(result, 'lua')
352+
end)
353+
335354
it('multiline', function()
336355
vim.api.nvim_win_set_cursor(win, { 2, 0 })
337356
vim.cmd.exe("'normal V3j'")
@@ -495,7 +514,27 @@ describe('lua-console.utils', function()
495514

496515
expected = config.buffer.result_prefix .. '5'
497516

498-
result = h.get_virtual_text(buf, 0, 0)
517+
result = h.get_virtual_text(buf)
518+
assert.has_string(result, expected)
519+
end)
520+
521+
it('shows value of the multiple assignments as virtual text', function()
522+
vim.api.nvim_win_set_cursor(win, { 1, 0 })
523+
h.send_keys('V4j')
524+
525+
content = h.to_table([[
526+
a = 5
527+
for i = 1, 5 do
528+
i = i + 5
529+
end
530+
vim.bo.filetype, c = tostring(a + 5) .. 'test', 200
531+
]])
532+
h.set_buffer(buf, content)
533+
utils.eval_code_in_buffer()
534+
535+
expected = config.buffer.result_prefix .. '[1]"10test", [2] 200'
536+
537+
result = h.get_virtual_text(buf)
499538
assert.has_string(result, expected)
500539
end)
501540

0 commit comments

Comments
 (0)