Skip to content

Commit 6bba5fe

Browse files
committed
fix: virtual text double output
1 parent 8d1ab9e commit 6bba5fe

5 files changed

Lines changed: 76 additions & 38 deletions

File tree

lua/lua-console/config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ local default_config = {
1616
title = ' Lua console ',
1717
title_pos = 'left',
1818
height = 0.6, -- percentage of main window
19-
zindex = 1,
19+
zindex = 100,
2020
},
2121
mappings = {
2222
toggle = '`',

lua/lua-console/injections.lua

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,36 @@ M.set_highlighting = function()
66
local lang_prefix = config.external_evaluators.lang_prefix
77
local lang_pattern = ('^%s([^\\n]-)\\n.+$'):format(lang_prefix)
88

9-
vim.treesitter.query.add_directive('deindent!', function(_, _, _, predicate, metadata) -- remove indentaion in the region
10-
local capture_id = predicate[2]
11-
if not metadata[capture_id].range then return end
9+
vim.treesitter.query.add_directive(
10+
'deindent!',
11+
function(_, _, _, predicate, metadata) -- remove indentaion in the region
12+
local capture_id = predicate[2]
13+
if not metadata[capture_id].range then return end
1214

13-
metadata[capture_id].range[2] = tonumber(predicate[3]) -- set indent col to 0
14-
end, { all = true, force = true })
15+
metadata[capture_id].range[2] = tonumber(predicate[3]) -- set indent col to 0
16+
end,
17+
{ all = true, force = true }
18+
)
1519

1620
local function extend_query(query)
1721
local extended = ''
1822
vim.tbl_map(function(path)
19-
extended = extended .. io.open(path):read("*a") .. '\n'
23+
extended = extended .. io.open(path):read('*a') .. '\n'
2024
end, vim.treesitter.query.get_files('lua', 'injections'))
2125

2226
return extended .. query
2327
end
2428

25-
local query = ([[ ;query
29+
local query_string = ([[ ;query
2630
((string_content) @injection.language @injection.content
27-
(#lua-match? @injection.language "^@1")
28-
(#gsub! @injection.language "@2" "%1")
29-
(#offset! @injection.content 1 0 0 0)
30-
(#deindent! @injection.content 0))
31+
(#lua-match? @injection.language "^@1")
32+
(#gsub! @injection.language "@2" "%1")
33+
(#offset! @injection.content 1 0 0 0)
34+
(#deindent! @injection.content 0))
3135
]]):gsub('@1', lang_prefix):gsub('@2', lang_pattern)
3236

33-
query = extend_query(query)
34-
vim.treesitter.query.set('lua', 'injections', query)
37+
query_string = extend_query(query_string)
38+
vim.treesitter.query.set('lua', 'injections', query_string)
3539
end
3640

3741
return M

lua/lua-console/utils.lua

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@ end
2626
local to_table = function(obj)
2727
obj = type(obj) == 'string' and { obj } or obj
2828

29-
return vim.iter(obj):map(function(line)
30-
return vim.split(line or '', '\n', { trimempty = true })
31-
end):flatten():totable()
29+
return vim
30+
.iter(obj)
31+
:map(function(line)
32+
return vim.split(line or '', '\n', { trimempty = true })
33+
end)
34+
:flatten()
35+
:totable()
3236
end
3337

3438
local function remove_indentation(tbl)
@@ -47,8 +51,8 @@ end
4751
---@param highlight string higlight group
4852
local show_virtual_text = function(buf, id, text, lnum, position, highlight)
4953
local ns = vim.api.nvim_create_namespace('Lua-console')
50-
local ext_mark = vim.api.nvim_buf_get_extmark_by_id(0, ns, id, {})
5154

55+
local ext_mark = vim.api.nvim_buf_get_extmark_by_id(0, ns, id, {})
5256
if #ext_mark > 0 then vim.api.nvim_buf_del_extmark(0, ns, id) end
5357

5458
vim.api.nvim_buf_set_extmark(buf, ns, lnum, 0, {
@@ -77,8 +81,18 @@ local toggle_help = function(buf)
7781

7882
message =
7983
[[%s - eval a line or selection, %s - eval buffer, %s - open file, %s - load messages, %s - save console, %s - load console, %s/%s - resize window, %s - toggle help]]
80-
message =
81-
string.format(message, cm.eval, cm.eval_buffer, cm.open, cm.messages, cm.save, cm.load, cm.resize_up, cm.resize_down, cm.help)
84+
message = string.format(
85+
message,
86+
cm.eval,
87+
cm.eval_buffer,
88+
cm.open,
89+
cm.messages,
90+
cm.save,
91+
cm.load,
92+
cm.resize_up,
93+
cm.resize_down,
94+
cm.help
95+
)
8296

8397
local visible_line = vim.fn.line('w0')
8498
show_virtual_text(buf, 2, message, visible_line - 1, 'overlay', 'Comment')
@@ -159,7 +173,9 @@ local get_last_assignment = function()
159173
offset = offset + 1
160174
end
161175

162-
return last_var, last_val, offset
176+
lnum = (lnum - offset) > 0 and lnum - offset or nil
177+
178+
return last_var, last_val, lnum
163179
end
164180

165181
---Pretty prints objects
@@ -191,27 +207,32 @@ end
191207

192208
---@param buf number
193209
---@param lines string[] Text to append to current buffer after current selection
194-
local append_current_buffer = function(buf, lines)
210+
---@param lnum? number|nil Line number to append from
211+
local append_current_buffer = function(buf, lines, lnum)
195212
if not lines or #lines == 0 then return end
213+
lnum = lnum or vim.fn.line('.')
214+
215+
local ns = vim.api.nvim_create_namespace('Lua-console')
216+
vim.api.nvim_buf_clear_namespace(buf, ns, 0, -1)
196217

197-
local lnum = vim.fn.line('.')
198218
local prefix = config.buffer.result_prefix
199219
local empty_results = { 'nil', '', '""', "''" }
200220

201221
local virtual_text
202222
local line = lines[#lines]
203223

204-
local last_assigned_var, last_assigned_val, last_assignment_offset = get_last_assignment()
205-
if last_assigned_var then
206-
virtual_text = to_string(pretty_print(last_assigned_val), '', true)
207-
show_virtual_text(buf, 3, prefix .. virtual_text, lnum - last_assignment_offset - 1, 'eol', 'Comment')
224+
local _, last_assigned_val, last_assignment_lnum = get_last_assignment()
225+
if last_assignment_lnum then
226+
last_assigned_val = to_string(pretty_print(last_assigned_val), '', true)
227+
show_virtual_text(buf, 3, prefix .. last_assigned_val, last_assignment_lnum - 1, 'eol', 'Comment')
208228
end
209229

210230
if vim.tbl_contains(empty_results, line) then
211231
table.remove(lines)
212232

213-
virtual_text = get_line_assignment(vim.fn.getbufline(buf, lnum, lnum)) or line
214-
show_virtual_text(buf, 4, prefix .. virtual_text, lnum - 1, 'eol', 'Comment')
233+
virtual_text = get_line_assignment(vim.fn.getbufline(buf, lnum, lnum)) or line -- ! resets env._last_assignment by calling evaluator
234+
235+
if not last_assignment_lnum then show_virtual_text(buf, 4, prefix .. virtual_text, lnum - 1, 'eol', 'Comment') end
215236
end
216237

217238
if #lines == 0 then return end
@@ -293,7 +314,7 @@ function get_ctx(buf)
293314
end,
294315
_reset_last_assignment = function()
295316
mt._last_assignment = nil
296-
end
317+
end,
297318
}
298319

299320
lc.ctx[buf] = env
@@ -314,7 +335,7 @@ function lua_evaluator(lines, ctx)
314335
local lines_with_return_last_line = add_return(lines, #lines)
315336

316337
if not select(2, load(to_string(lines_with_return_first_line), '', 't', env)) then
317-
lines = lines_with_return_first_line
338+
lines = lines_with_return_first_line
318339
elseif not select(2, load(to_string(lines_with_return_last_line), '', 't', env)) then
319340
lines = lines_with_return_last_line
320341
end
@@ -409,7 +430,7 @@ end
409430
---@param range number[]
410431
---@return string
411432
local function get_lang(buf, range)
412-
local pattern = ('^.*' .. config.external_evaluators.lang_prefix .. '(.-)%s*$')
433+
local pattern = ('^.*' .. config.external_evaluators.lang_prefix .. '(%w+)%s*$')
413434
local line, lang
414435

415436
line = vim.api.nvim_buf_get_lines(buf, math.max(0, range[1] - 2), range[2], false)[1]
@@ -442,7 +463,10 @@ local eval_code_in_buffer = function(buf, full)
442463
buf = buf or vim.fn.bufnr()
443464
local win = vim.fn.bufwinid(buf)
444465

445-
if vim.api.nvim_get_mode().mode == 'V' then vim.api.nvim_input('<Esc>') end
466+
if vim.api.nvim_get_mode().mode == 'V' then
467+
LOG('here')
468+
vim.api.nvim_input('<Esc>')
469+
end
446470

447471
local v_start, v_end
448472
if full then
@@ -472,6 +496,7 @@ end
472496
---Load messages into console
473497
local load_messages = function(buf)
474498
local ns = vim.api.nvim_create_namespace('Lua-console')
499+
local lnum = vim.fn.line('.')
475500

476501
---This way we catch the output of messages command, in case it was overriden by some other plugin, like Noice
477502
vim.ui_attach(ns, { ext_messages = true }, function(event, entries) ---@diagnostic disable-line
@@ -484,8 +509,8 @@ local load_messages = function(buf)
484509
if #messages == 0 then return end
485510

486511
vim.schedule(function()
487-
vim.api.nvim_input('<Down>') -- forcing to redraw buffer
488-
append_current_buffer(buf, to_table(messages))
512+
append_current_buffer(buf, to_table(messages), lnum)
513+
vim.api.nvim__redraw { flush = true, buf = buf }
489514
end)
490515
end)
491516

spec/log.lua

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,26 @@ local inspect = require('inspect')
33

44
local log = function(...) --luacheck: ignore
55
local caller = debug.getinfo(2)
6+
local caller_path = caller.short_src
67

78
local time = os.date('*t', os.time())
89
time.min, time.sec = 0, 0
10+
11+
---@diagnostic disable-next-line
912
time = os.time() - os.time(time)
1013

14+
if caller_path then
15+
local path_dirs = vim.split(vim.fs.dirname(caller_path), '/')
16+
caller_path = path_dirs[#path_dirs] .. '/' .. vim.fs.basename(caller_path)
17+
end
18+
1119
local result = ('\nLOG #%s (%s:%s:%s) => '):format(
1220
time,
21+
caller_path or '',
1322
caller.name or '',
14-
caller.short_src or '',
1523
caller.currentline or ''
1624
)
25+
1726
local nargs = select('#', ...)
1827
local var_no = ''
1928

spec/spec_helper.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ end
1919
M.to_string = function(tbl)
2020
tbl = tbl or {}
2121

22-
if type(tbl) == 'string' then tbl = { tbl } end
22+
if type(tbl) ~= 'table' then tbl = { tbl } end
2323
return table.concat(tbl, '\n'):clean()
2424
end
2525

@@ -62,7 +62,7 @@ end
6262

6363
M.get_virtual_text = function(buf, line_start, line_end)
6464
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 0, -1 }, {})
65+
local ids = vim.api.nvim_buf_get_extmarks(buf, ns, { line_start or 0, 0 }, { line_end or -1, -1 }, {})
6666

6767
if vim.tbl_isempty(ids) then
6868
_G.LOG('No extmarks found')

0 commit comments

Comments
 (0)