2626local 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 ()
3236end
3337
3438local function remove_indentation (tbl )
4751--- @param highlight string higlight group
4852local 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
163179end
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
409430--- @param range number[]
410431--- @return string
411432local 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
472496--- Load messages into console
473497local 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
0 commit comments