@@ -5,6 +5,8 @@ local to_string = function(tbl, sep, trim)
5
5
tbl = tbl or {}
6
6
sep = sep or ' \n '
7
7
8
+ if type (tbl ) ~= ' table' then tbl = { tbl } end
9
+
8
10
local line = table.concat (tbl , sep )
9
11
local patterns = { ' \\ r' , ' \\ t' , ' \\ n' }
10
12
@@ -123,9 +125,9 @@ local get_path_lnum = function(path)
123
125
return path , lnum
124
126
end
125
127
126
- --- Determines if there is an assigment on the line and returns its value
127
- --- @param line string[]
128
- 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 )
129
131
if not line or # line == 0 then return end
130
132
131
133
local lhs = line [1 ]:match (' ^(.-)%s*=' )
@@ -138,39 +140,29 @@ local get_assignment = function(line)
138
140
end
139
141
end
140
142
141
- local print_buffer = {}
142
-
143
- --- @param buf number
144
- --- @param lines string[] Text to append to current buffer after current selection
145
- local append_current_buffer = function (buf , lines )
146
- if not lines or # lines == 0 then return end
147
-
143
+ local get_last_assignment = function ()
144
+ local ctx = get_ctx ()
148
145
local lnum = vim .fn .line (' .' )
149
- local prefix = config .buffer .result_prefix
150
- local empty_results = { ' nil' , ' ' , ' ""' , " ''" }
151
146
152
- local virtual_text
153
- local line = lines [# lines ]
154
-
155
- if vim .tbl_contains (empty_results , line ) then
156
- table.remove (lines )
157
- virtual_text = line
158
- end
147
+ local last_var = ctx ._last_assignment
148
+ local last_val = ctx [ctx ._last_assignment ]
159
149
160
- local assignment_value = get_assignment (vim .fn .getbufline (buf , lnum , lnum ))
161
- if assignment_value ~= nil then virtual_text = assignment_value end
150
+ if not last_var then return end
162
151
163
- if virtual_text then show_virtual_text (buf , 3 , prefix .. virtual_text , lnum - 1 , ' eol' , ' Comment' ) end
152
+ local line
153
+ local offset = 0
164
154
165
- 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 ]
166
157
167
- lines [1 ] = prefix .. lines [1 ]
168
- 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
169
161
170
- vim . api . nvim_buf_set_lines ( buf , lnum , lnum , false , lines )
162
+ return last_var , last_val , offset
171
163
end
172
164
173
- --- Pretty prints objects and appends to print_buffer
165
+ --- Pretty prints objects
174
166
--- @param ... any []
175
167
--- @return string[]
176
168
local pretty_print = function (...)
@@ -187,10 +179,47 @@ local pretty_print = function(...)
187
179
result = result .. var_no .. vim .inspect (o )
188
180
end
189
181
190
- result = to_table (result )
191
- 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
218
+
219
+ lines [1 ] = prefix .. lines [1 ]
220
+ table.insert (lines , 1 , ' ' ) -- insert an empty line
192
221
193
- -- return result
222
+ vim . api . nvim_buf_set_lines ( buf , lnum , lnum , false , lines )
194
223
end
195
224
196
225
local function remove_empty_lines (tbl )
@@ -227,11 +256,11 @@ local function clean_stacktrace(error)
227
256
return lines
228
257
end
229
258
230
- local function add_return (tbl )
231
- if vim . fn . trim (tbl [# tbl ]) == ' end' then return tbl end
259
+ local function add_return (tbl , lnum )
260
+ if to_string (tbl [lnum ], ' ' , true ): match ( ' ^%s* end' ) then return tbl end
232
261
233
262
local ret = vim .deepcopy (tbl )
234
- ret [# ret ] = ' return ' .. ret [# ret ]
263
+ ret [lnum ] = ' return ' .. ret [lnum ]
235
264
236
265
return ret
237
266
end
@@ -245,19 +274,26 @@ function get_ctx(buf)
245
274
local ctx = lc .ctx [buf ]
246
275
if config .buffer .preserve_context and ctx then return ctx end
247
276
248
- local env , mt = {}, {}
277
+ local env , mt , values = {}, {}, {}
249
278
250
279
mt = {
251
- print = pretty_print ,
280
+ print = print_to_buffer ,
252
281
_ctx = function ()
253
- return vim .tbl_extend ( ' force ' , {}, env )
282
+ return vim .deepcopy ( values )
254
283
end ,
255
284
_ctx_clear = function ()
256
285
lc .ctx [buf ] = nil
257
286
end ,
258
287
__index = function (_ , key )
259
- return mt [key ] and mt [key ] or _G [key ]
288
+ return mt [key ] and mt [key ] or values [ key ] or _G [key ]
260
289
end ,
290
+ __newindex = function (_ , k , v )
291
+ values [k ] = v
292
+ mt ._last_assignment = k
293
+ end ,
294
+ _reset_last_assignment = function ()
295
+ mt ._last_assignment = nil
296
+ end
261
297
}
262
298
263
299
lc .ctx [buf ] = env
@@ -271,10 +307,17 @@ end
271
307
function lua_evaluator (lines , ctx )
272
308
vim .validate { lines = { lines , ' table' } }
273
309
274
- local lines_with_return = add_return (lines )
275
310
local env = ctx or get_ctx ()
311
+ env ._reset_last_assignment ()
276
312
277
- if not select (2 , load (to_string (lines_with_return ), ' ' , ' t' , env )) then lines = lines_with_return end
313
+ local lines_with_return_first_line = add_return (lines , 1 )
314
+ local lines_with_return_last_line = add_return (lines , # lines )
315
+
316
+ if not select (2 , load (to_string (lines_with_return_first_line ), ' ' , ' t' , env )) then
317
+ lines = lines_with_return_first_line
318
+ elseif not select (2 , load (to_string (lines_with_return_last_line ), ' ' , ' t' , env )) then
319
+ lines = lines_with_return_last_line
320
+ end
278
321
279
322
local code , error = load (to_string (lines ), ' Lua console: ' , ' t' , env )
280
323
if error then return to_table (error ) end
@@ -289,9 +332,9 @@ function lua_evaluator(lines, ctx)
289
332
table.remove (result , 1 )
290
333
291
334
if # result > 0 then
292
- pretty_print (unpack (result ))
335
+ print_to_buffer (unpack (result ))
293
336
else
294
- pretty_print (nil )
337
+ print_to_buffer (nil )
295
338
end
296
339
else
297
340
vim .list_extend (print_buffer , clean_stacktrace (err ))
@@ -476,6 +519,8 @@ local attach_toggle = function(buf)
476
519
end
477
520
478
521
mappings .set_evaluator_mappings (buf , toggle )
522
+ vim .api .nvim_set_option_value (' syntax' , ' on' , { buf = buf })
523
+
479
524
vim .notify (
480
525
(' Evaluator %s for buffer [%s:%s]' ):format (toggle and ' attached' or ' dettached' , name , buf ),
481
526
vim .log .levels .INFO
0 commit comments