Skip to content

Commit 46d0273

Browse files
committed
fix: remove locals for top-level declarations only
1 parent c379c2d commit 46d0273

File tree

4 files changed

+37
-32
lines changed

4 files changed

+37
-32
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.todo.md

README.md

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
11
# 💻 Lua console ![main](https://github.com/yarospace/lua-console.nvim/actions/workflows/test_main.yml/badge.svg?branch=main) ![develop](https://github.com/yarospace/lua-console.nvim/actions/workflows/test_develop.yml/badge.svg?branch=develop) [![LuaRocks](https://img.shields.io/luarocks/v/YaroSpace/lua-console.nvim?logo=lua&color=purple)](https://luarocks.org/modules/YaroSpace/lua-console.nvim)
22

3-
**lua-console.nvim** - is a handy scratch pad / REPL / debug console for Lua development and Neovim exploration and configuration.
4-
Acts as a user friendly replacement of command mode - messages loop and as a handy scratch pad to store and test your code gists.
5-
6-
7-
**Update:** although it originated as a tool for Lua development, it has now evolved into supporting other languages too. See [`evaluating other languages`](#evaluating-other-languages).
3+
**lua-console.nvim** - is a REPL / scratch pad / debug console for Neovim. Supports Lua natively and can be extended for other languages.
84

95
<br/><img src="https://github.com/YaroSpace/assets/blob/main/lua-console.nvim/demo.gif">
106

11-
## 💡 Motivation
12-
13-
After installing Neovim, it took me some time to configure it, learn its settings, structure and API, while learning Lua in the process.
14-
I got fed up of constantly hitting `:`, typing `lua= command`, then typing `:messages` to see the output, only to find out that a made a typo or a
15-
syntax error and retyping the whole thing again, copying the paths from error stacktraces and so on. I needed something better, so there it is.
16-
17-
<br>
18-
197
## ✨ Features
208

219
- Evaluate single line expressions and statements, visually selected lines or characters of code or the whole buffer
@@ -74,7 +62,7 @@ opts = {
7462
autosave = true, -- autosave on console hide / close
7563
load_on_start = true, -- load saved session on start
7664
preserve_context = true, -- preserve results between evaluations
77-
strip_local = true, -- remove local identifier from source code
65+
strip_local = true, -- strip `local` from top-level variable declarations
7866
show_one_line_results = true, -- prints one line results, even if already shown as virtual text
7967
notify_result = false, -- notify result
8068
clear_before_eval = false, -- clear output below result prefix before evaluation of the whole buffer
@@ -112,7 +100,7 @@ opts = {
112100
- Install, press the mapped key `` ` `` and start exploring.
113101
- Enter code as normal, in insert mode.
114102
- Hit `Enter` in normal mode to evaluate a variable, statement or an expression in the current line.
115-
- Visually select a region or a range of lines and press `Enter` to evaluate the code in the range or use `<S-Enter>` to evaluate the whole console.
103+
- Visually select a region or a range of lines and press `Enter` to evaluate the code in the range or use `<S-Enter>` to evaluate the whole buffer.
116104
- The evaluation of the last line is returned and printed, so no `return` is needed in most cases.
117105
To avoid noise, if the return of your execution is `nil`, e.g. from a loop or a function without return, it will not be printed, but shown as virtual text.
118106
The result of assignments on the last line will be also shown as virtual text.
@@ -136,11 +124,13 @@ opts = {
136124
> [!IMPORTANT]
137125
> By default, the option `preserve_context` is on, which means that the execution context is preserved between evaluations.
138126
139-
All the code executed in the console is evaluated in isolated environment. This means that any variables you declare without the `local` keyword will not be persisted
140-
in Neovim's global environment, although all global variables are accessible. If you want purposefully to alter the global state, use `_G.My_variable = ..`.
127+
All the code executed in the console is evaluated in isolated environment, which means that any global variables that you declare will not be persisted
128+
in Neovim's global environment. If you purposefully want to alter the global state, use `_G.My_variable = ..`.
129+
130+
The option `preserve_context` implies that variables without `local` will be stored in the console's local context and preserved between executions.
131+
So, if you first execute `a = 1`, then `a = a + 1` and then `a` - you will get `2`.
141132

142-
The option `preserve_context` means that although you declare variables without `local`, they will be stored in console's local context and preserved between separate executions.
143-
So, if you first execute `a = 1`, then `a = a + 1` and then `a` - you will get `2`. Variables with `local` are also preserved, unless you set the `strip_local` option to `false`.
133+
Also, by default, the option `strip_local` is on, which means that `local` modifier is stripped from top-level variable declarations and these variables are also stored in the context.
144134

145135
If you want the context to be cleared before every execution, set `preserve_context = false`.
146136

@@ -162,8 +152,7 @@ There are two functions available within the console:
162152

163153
#### Setting up
164154

165-
- It is possible to setup external code executors for other languages. Evaluators for `ruby`,`racket` and `python` are working out of the box, support for other languages is coming.
166-
Meanwhile, you can easily setup your own language.
155+
- It is possible to setup external code executors for other languages. Evaluators for `ruby`,`racket` and `python` are working out of the box, support for other can be easily added.
167156
- Below is the default configuration, which can be overridden or extended by your custom config, where `default_process_opts` will be
168157
replaced by language specific opts, e.g. a possible config for `python` could be:
169158

@@ -291,7 +280,6 @@ There are a number of alternatives available, notably:
291280
- [Luadev](https://github.com/bfredl/nvim-luadev)
292281
- [SnipRun](https://github.com/michaelb/sniprun)
293282

294-
Initially, when starting with Lua and Neovim, I tried all the REPLs/code runners I could find. However, I was not satisfied with all of them in one way or another.
295283
Lua-console is an attempt to combine the best features of all of them, like REPL / scratch pad / code runner / debug console, while leaving the UX and config simple.
296284

297285
<br>

lua/lua-console/config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +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-
strip_local = true, -- remove local identifier from source code
10+
strip_local = true, -- strip "local" modifier from top level variables
1111
show_one_line_results = true, -- prints one line results, even if already shown as virtual text
1212
notify_result = false, -- notify result
1313
clear_before_eval = false, -- clear output below result prefix before evaluation of the whole buffer

lua/lua-console/utils.lua

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ end
153153
local get_line_assignment = function(line)
154154
if not line or #line == 0 then return end
155155

156-
local lhs = line[1]:match('^(.-)%s*=')
156+
local lhs = line[1]:gsub('^%s*local%s+', ''):match('^(.-)%s*=')
157157
local ret
158158

159159
if lhs then
@@ -177,6 +177,7 @@ local get_last_assignment = function()
177177

178178
for i = lnum - 1, 0, -1 do
179179
line = vim.api.nvim_buf_get_lines(0, i, i + 1, false)[1]
180+
line = line:gsub('^%s*local%s+', '')
180181

181182
if line:match('^%s*' .. last_var .. '%s*,?[^=]-=') then break end
182183
offset = offset + 1
@@ -265,12 +266,6 @@ local append_current_buffer = function(buf, lines, lnum)
265266
vim.api.nvim_buf_set_lines(buf, lnum, lnum, false, lines)
266267
end
267268

268-
local function remove_empty_lines(tbl)
269-
return vim.tbl_filter(function(el)
270-
return vim.fn.trim(el) ~= ''
271-
end, tbl)
272-
end
273-
274269
local function trim_empty_lines(tbl)
275270
if #tbl == 0 then return tbl end
276271

@@ -358,6 +353,28 @@ local function run_code(code)
358353
return result
359354
end
360355

356+
local function strip_local(lines)
357+
lines = vim.split(lines, '\n')
358+
359+
local ts = vim.treesitter
360+
local ret = {}
361+
362+
local start_row = vim.fn.line('.') - 1 - #lines
363+
364+
for i, line in ipairs(lines) do
365+
local col = line:find('^%s*local%s+')
366+
367+
if col then
368+
local node = ts.get_node { pos = { start_row + i, col } }
369+
line = node and node:parent():type() == 'chunk' and line:gsub('^%s*local%s+', '') or line
370+
end
371+
372+
table.insert(ret, line)
373+
end
374+
375+
return table.concat(ret, '\n')
376+
end
377+
361378
--- Evaluates Lua code and returns pretty printed result with errors if any
362379
--- @param lines string[] table with lines of Lua code
363380
--- @param ctx? table environment to execute code in
@@ -378,7 +395,7 @@ function lua_evaluator(lines, ctx)
378395
end
379396

380397
lines = to_string(lines)
381-
lines = config.buffer.strip_local and lines:gsub('local ', '') or lines
398+
lines = config.buffer.strip_local and strip_local(lines) or lines
382399

383400
local code, errors = load(lines, 'Lua console: ', 't', env)
384401
if errors then return to_table(errors) end
@@ -549,7 +566,6 @@ local eval_code_in_buffer = function(buf, full)
549566
vim.fn.cursor(v_end, 0)
550567

551568
lines = lines or vim.api.nvim_buf_get_lines(buf, v_start - 1, v_end, false)
552-
lines = remove_empty_lines(lines)
553569
if #lines == 0 then return end
554570

555571
local evaluator = get_evaluator(buf, v_start - 1)

0 commit comments

Comments
 (0)