Skip to content

Commit 022d0b8

Browse files
committed
feat: improved virtual text for assignments
1 parent fe60a91 commit 022d0b8

2 files changed

Lines changed: 88 additions & 34 deletions

File tree

lua/lua-console/utils.lua

Lines changed: 68 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ local get_path_lnum = function(path)
125125
return path, lnum
126126
end
127127

128-
---Determines if there is an assigment on the line and returns its value
129-
---@param line string[]
130-
local get_assignment = function(line)
128+
----Determines if there is an assigment on the line and returns its value
129+
----@param line string[]
130+
local get_line_assignment = function(line)
131131
if not line or #line == 0 then return end
132132

133133
local lhs = line[1]:match('^(.-)%s*=')
@@ -140,39 +140,29 @@ local get_assignment = function(line)
140140
end
141141
end
142142

143-
local print_buffer = {}
144-
145-
---@param buf number
146-
---@param lines string[] Text to append to current buffer after current selection
147-
local append_current_buffer = function(buf, lines)
148-
if not lines or #lines == 0 then return end
149-
143+
local get_last_assignment = function()
144+
local ctx = get_ctx()
150145
local lnum = vim.fn.line('.')
151-
local prefix = config.buffer.result_prefix
152-
local empty_results = { 'nil', '', '""', "''" }
153-
154-
local virtual_text
155-
local line = lines[#lines]
156146

157-
if vim.tbl_contains(empty_results, line) then
158-
table.remove(lines)
159-
virtual_text = line
160-
end
147+
local last_var = ctx._last_assignment
148+
local last_val = ctx[ctx._last_assignment]
161149

162-
local assignment_value = get_assignment(vim.fn.getbufline(buf, lnum, lnum))
163-
if assignment_value ~= nil then virtual_text = assignment_value end
150+
if not last_var then return end
164151

165-
if virtual_text then show_virtual_text(buf, 3, prefix .. virtual_text, lnum - 1, 'eol', 'Comment') end
152+
local line
153+
local offset = 0
166154

167-
if #lines == 0 then return end
155+
for i = lnum - 1, 0, -1 do
156+
line = vim.api.nvim_buf_get_lines(0, i, i + 1, false)[1]
168157

169-
lines[1] = prefix .. lines[1]
170-
table.insert(lines, 1, '') -- insert an empty line
158+
if line:match('^%s*' .. last_var .. '%s*=') then break end
159+
offset = offset + 1
160+
end
171161

172-
vim.api.nvim_buf_set_lines(buf, lnum, lnum, false, lines)
162+
return last_var, last_val, offset
173163
end
174164

175-
---Pretty prints objects and appends to print_buffer
165+
---Pretty prints objects
176166
---@param ... any[]
177167
---@return string[]
178168
local pretty_print = function(...)
@@ -189,10 +179,47 @@ local pretty_print = function(...)
189179
result = result .. var_no .. vim.inspect(o)
190180
end
191181

192-
result = to_table(result)
193-
vim.list_extend(print_buffer, result)
182+
return to_table(result)
183+
end
184+
185+
local print_buffer = {}
186+
187+
local print_to_buffer = function(...)
188+
local ret = pretty_print(...)
189+
vim.list_extend(print_buffer, ret)
190+
end
191+
192+
---@param buf number
193+
---@param lines string[] Text to append to current buffer after current selection
194+
local append_current_buffer = function(buf, lines)
195+
if not lines or #lines == 0 then return end
196+
197+
local lnum = vim.fn.line('.')
198+
local prefix = config.buffer.result_prefix
199+
local empty_results = { 'nil', '', '""', "''" }
200+
201+
local virtual_text
202+
local line = lines[#lines]
203+
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')
208+
end
209+
210+
if vim.tbl_contains(empty_results, line) then
211+
table.remove(lines)
212+
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')
215+
end
216+
217+
if #lines == 0 then return end
194218

195-
-- return result
219+
lines[1] = prefix .. lines[1]
220+
table.insert(lines, 1, '') -- insert an empty line
221+
222+
vim.api.nvim_buf_set_lines(buf, lnum, lnum, false, lines)
196223
end
197224

198225
local function remove_empty_lines(tbl)
@@ -247,19 +274,26 @@ function get_ctx(buf)
247274
local ctx = lc.ctx[buf]
248275
if config.buffer.preserve_context and ctx then return ctx end
249276

250-
local env, mt = {}, {}
277+
local env, mt, values = {}, {}, {}
251278

252279
mt = {
253-
print = pretty_print,
280+
print = print_to_buffer,
254281
_ctx = function()
255-
return vim.tbl_extend('force', {}, env)
282+
return vim.deepcopy(values)
256283
end,
257284
_ctx_clear = function()
258285
lc.ctx[buf] = nil
259286
end,
260287
__index = function(_, key)
261-
return mt[key] and mt[key] or _G[key]
288+
return mt[key] and mt[key] or values[key] or _G[key]
289+
end,
290+
__newindex = function(_, k, v)
291+
values[k] = v
292+
mt._last_assignment = k
262293
end,
294+
_reset_last_assignment = function()
295+
mt._last_assignment = nil
296+
end
263297
}
264298

265299
lc.ctx[buf] = env

spec/unit/utils_spec.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,26 @@ describe('lua-console.utils', function()
479479
assert.has_string(result, expected)
480480
end)
481481

482+
it('shows value of the last assignment on the corresponging line as virtual text', function()
483+
vim.api.nvim_win_set_cursor(win, { 1, 0 })
484+
h.send_keys('V4j')
485+
486+
content = h.to_table([[
487+
a = 5
488+
for i = 1, 5 do
489+
i = i + 5
490+
end
491+
vim.bo.filetype = tostring(a + 5) .. 'test'
492+
]])
493+
h.set_buffer(buf, content)
494+
utils.eval_code_in_buffer()
495+
496+
expected = config.buffer.result_prefix .. '5'
497+
498+
result = h.get_virtual_text(buf, 0, 0)
499+
assert.has_string(result, expected)
500+
end)
501+
482502
it('gives syntax error message', function()
483503
vim.api.nvim_win_set_cursor(win, { 1, 0 })
484504

0 commit comments

Comments
 (0)