Skip to content

Commit 5e12038

Browse files
authored
Merge pull request #4 from YaroSpace/develop
Release 1.2.0
2 parents 7742a1c + d922f58 commit 5e12038

27 files changed

Lines changed: 2097 additions & 1002 deletions

.busted

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ return {
33
coverage = false,
44
lpath = "lua/?.lua;lua/?/init.lua;spec/?.lua",
55
lua = 'spec/nvim-shim.sh',
6-
-- lua = 'nlua',
7-
ROOT = {'spec'}
6+
ROOT = {'spec'},
7+
output = 'spec/utfTerminal.lua',
8+
verbose = true
89
},
910
default = {
10-
verbose = true
1111
},
1212
unit = {
1313
ROOT = {'spec/unit'},
14-
verbose = true,
1514
},
1615
}

.github/actions/build/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ runs:
7171
run: |
7272
luarocks install busted --local
7373
luarocks install luacheck --local
74-
# luarocks install nlua --local
74+
luarocks install inspect --local
7575
shell: bash
7676

7777
- run: make test

.github/workflows/documentation.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Documentation
33
on:
44
push:
55
branches:
6-
- develop
6+
- main
77

88
jobs:
99
documentation:

.luacheckrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ ignore = {
1111
-- Neovim lua API + luacheck thinks variables like `vim.wo.spell = true` is
1212
-- invalid when it actually is valid. So we have to display rule `W122`.
1313
--
14-
"122",
14+
"122", -- setting read-only field of vim...
1515
"631", -- max_line_length
1616
"621", -- incostistant indentation
1717
"611" -- line with whitespace

Makefile

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,36 @@
1-
.PHONY: api_documentation llscheck luacheck stylua test
1+
set_lua_paths = eval $$(luarocks path --lua-version 5.1 --bin)
2+
busted = $$(find /usr/local/lib/luarocks/*/busted/* -name busted)
3+
4+
test_unit = busted --run=unit
5+
test_nvim = nvim --headless -i NONE -n -u spec/minimal_init.lua -l $(busted) --run=unit
6+
7+
tag ?= wip
8+
watch = '*.lua'
9+
10+
.PHONY: api_documentation luacheck stylua test
11+
12+
all: test luacheck stylua
213

314
api_documentation:
4-
nvim -u scripts/make_api_documentation/minimal_init.lua -l scripts/make_api_documentation/main.lua
15+
nvim -u scripts/make_api_documentation/init.lua -l scripts/make_api_documentation/main.lua
516

617
llscheck:
718
llscheck --configpath .luarc.json .
819

920
luacheck:
10-
luacheck lua spec # plugin scripts
21+
luacheck lua spec scripts
1122

1223
stylua:
13-
stylua lua plugin scripts spec
24+
stylua --check lua scripts spec
1425

1526
test:
16-
eval $(luarocks path --lua-version 5.1 --bin)
17-
busted -o spec/utfTerminal.lua --run unit
27+
@$(set_lua_paths); $(test_unit)
1828

1929
watch:
20-
while sleep 0.1; do ls -d spec/**/*.lua | entr -d -c busted -o spec/utfTerminal.lua --run unit; done
30+
@$(set_lua_paths); while sleep 0.1; do find . -name $(watch) | entr -d -c $(test_unit); done
31+
32+
watch_tag:
33+
@$(set_lua_paths); while sleep 0.1; do find . -name $(watch) | entr -d -c $(test_unit) --tags=$(tag); done
2134

22-
watch_current:
23-
while sleep 0.1; do ls -d spec/**/*.lua | entr -d -c busted -o spec/utfTerminal.lua --run unit -t=current; done
35+
test_nvim:
36+
@$(test_nvim) spec/unit

README.md

Lines changed: 150 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
**lua-console.nvim** - is a handy scratch pad / REPL / debug console for Lua development and Neovim exploration and configuration.
44
Acts as a user friendly replacement of command mode - messages loop and as a handy scratch pad to store and test your code gists.
55

6-
<img src="doc/demo.gif">
6+
***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).***
7+
8+
<br/><img src="doc/demo.gif">
79

810
## 💡 Motivation
911

@@ -13,15 +15,15 @@ syntax error and retyping the whole thing again, copying the paths from error st
1315

1416
## ✨ Features
1517

16-
- Evaluate single line expressions
17-
- Evaluate visually selected lines of code
18+
- Evaluate single line expressions and statements, visually selected lines of code or the whole buffer
1819
- Pretty print Lua objects, including function details and their source paths
19-
- Show normal and error output in the console, including output of `print()`, errors and stacktraces.
20+
- Show normal and error output in the console/buffer, including output of `print()`, errors and stacktraces.
2021
- Syntax highlighting and autocompletion
2122
- Load Neovim’s messages into console for inspection and copy/paste
2223
- Open links from stacktraces and function sources
23-
- Save / Load console session
24-
- Use as scratch pad for code gists
24+
- Save / Load / Autosave console session
25+
- Use as a scratch pad for code gists
26+
- Attach code evaluators to any buffer
2527

2628

2729
## 📦 Installation
@@ -35,91 +37,212 @@ return {
3537
}
3638
```
3739
otherwise, install with your favorite package manager and add
38-
`require('lua-console').setup({opts})` somewhere in your config.
40+
`require('lua-console').setup { custom_options }` somewhere in your config.
3941

4042

4143
## ⚙️ Configuration
4244

4345
> [!NOTE]
44-
> All settings are very straight forward, but please read below about [`preserve_context`](#-notes-on-globals-locals-and-preserving-execution-context) option.
46+
> All settings are self explanatory, but please read below about [`preserve_context`](#-notes-on-globals-locals-and-preserving-execution-context) option.
4547
46-
Mappings are local to the console, except the one for toggling, which is - `` ` `` by default.
48+
Mappings are local to the console, except the ones for toggling the console - `` ` `` and attaching to a buffer - ``<Leader>` ``. All mappings can be overridden in your custom
49+
config. If you want to delete a mapping - set its value to `false`.
4750

4851
<details><summary>Default Settings</summary>
4952

5053
<!-- config:start -->
51-
54+
`config.lua`
5255
```lua
5356
opts = {
5457
buffer = {
55-
prepend_result_with = '=> ',
58+
result_prefix = '=> ',
5659
save_path = vim.fn.stdpath('state') .. '/lua-console.lua',
57-
load_on_start = true, -- load saved session on first entry
58-
preserve_context = true -- preserve context between executions
60+
autosave = true, -- autosave on console hide / close
61+
load_on_start = true, -- load saved session on start
62+
preserve_context = true, -- preserve results between evaluations
5963
},
6064
window = {
61-
border = 'double', -- single|double|rounded
65+
border = 'double', -- single|double|rounded
6266
height = 0.6, -- percentage of main window
6367
},
6468
mappings = {
6569
toggle = '`',
70+
attach = '<Leader>`',
6671
quit = 'q',
6772
eval = '<CR>',
68-
clear = 'C',
73+
eval_buffer = '<S-CR>',
74+
open = 'gf',
6975
messages = 'M',
7076
save = 'S',
7177
load = 'L',
7278
resize_up = '<C-Up>',
7379
resize_down = '<C-Down>',
7480
help = '?'
75-
}
81+
},
7682
}
7783
```
7884
<!-- config:end -->
7985

8086
</details>
8187

8288

83-
## 🚀 Usage (with default mappings)
89+
## 🚀 Basic usage (with default mappings)
8490

8591
- Install, press the mapped key `` ` `` and start exploring.
8692
- Enter code as normal, in insert mode.
8793
- Hit `Enter` in normal mode to evaluate a variable, statement or an expression in the current line.
88-
- Visually select a range of lines and press `Enter` to evaluate the code in the range.
94+
- Visually select a range of lines and press `Enter` to evaluate the code in the range or use `<S-Enter>` to evaluate the whole console.
8995
- The evaluation of the last line is returned and printed, so no `return` is needed in most cases.
90-
To avoid noise, if the only return of your execution is `nil`, e.g. from an assignment, like `a = 1`, it will not be printed, but shown as virtual text.
91-
- Use `print()` in your code to output the results into the console. Accepts variable number of arguments, e.g. `print(var_1, var_2, ...)`.
92-
- Objects and functions are pretty printed, with function source paths.
96+
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.
97+
The result of assignments on the last line will be also shown as virtual text.
98+
- Use `print()` in your code to output the results into the console. It accepts variable number of arguments, e.g. `print(var_1, var_2, ...)`.
99+
- Objects and functions are pretty printed, with function details and their source paths.
93100
- Press `gf` to follow the paths in stack traces and to function sources. Truncated paths work too.
94101

95102
> [!NOTE]
96103
> This is especially useful when you want to see where a function was redefined at runtime. So, if you evaluate `vim.lsp.handlers['textDocument/hover']` for
97104
example, you can jump to its current definition, while Lsp/tags would take you to the original one.
98105

99106
- Press `M` to load Neovim messages into the console.
100-
- Use `S` and `L` to save / load the console session to preserve history of your hacking.
107+
- Use `S` and `L` to save / load the console session to preserve history of your hacking. If the `autosave` option is on, the contents of the console will be
108+
saved whenever it is toggled or closed.
101109
- You can resize the console with `<C-Up>` and `<C-Down>`.
102110

103111

104112
## 📓 Notes on globals, locals and preserving execution context
105113

106114
> [!IMPORTANT]
107-
> By default, the option `preserve_context` is on, which means that the context is preserved between executions.
115+
> By default, the option `preserve_context` is on, which means that the execution context is preserved between evaluations.
108116
109-
All the code executed in the console is evaluated in isolated environment. This means that any variables you declare will not be persisted in Neovim's global
110-
environment, although all global variables are accessible. If you want purposefully to alter the global state, use `_G.My_variable = ..`.
117+
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
118+
in Neovim's global environment, although all global variables are accessible. If you want purposefully to alter the global state, use `_G.My_variable = ..`.
111119

112-
The option `preserve_context` means that if you assign variables without `local`, they will be stored in console's local context and preserved between separate executions.
120+
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.
113121
So, if you first execute `a = 1`, then `a = a + 1` and then `a` - you will get `2`. Variables with `local` are not preserved.
114122

115-
If you want a classic REPL experience, when the context is cleared on every execution, set `preserve_context = false`.
123+
If you want the context to be cleared before every execution, set `preserve_context = false`.
116124

117125
There are two functions available within the console:
118126

119127
- `_ctx()` - will print the contents of the context
120128
- `_ctx_clear()` - clears the context
121129

122130

131+
## ⭐ Extra features
132+
133+
### Attaching code evaluator to other buffers
134+
135+
- The evaluator behind the console can be attached/detached to any buffer by pressing ``<Leader>` `` or executing command `LuaConsole AttachToggle`.
136+
You will be able to evaluate the code in the buffer as in the console and follow links. The evaluators and their contexts are isolated for each attached buffer.
137+
138+
### Evaluating other languages
139+
140+
#### Setting up
141+
142+
- It is possible to setup external code executors for other languages. Evaluators for `ruby` and `racket` are working out of the box, support for other languages is coming.
143+
Meanwhile, you can easily setup your own language.
144+
- Below is the default configuration which can be overridden or extended by your custom config (`default_process_opts` will be
145+
replaced by language specific opts), e.g. a possible config for `python` could be:
146+
147+
```lua
148+
require('lua-console').setup {
149+
external_evaluators = {
150+
python = {
151+
cmd = { 'python3', '-c' },
152+
env = { PYTHONPATH = '~/projects' },
153+
timeout = 100000,
154+
formatter = function(result) do_something; return result end,
155+
},
156+
}
157+
}
158+
```
159+
160+
- You can also setup a custom formatter to format the evaluator output before appending results to the console or buffer. Example is in the config.
161+
162+
<details><summary>Default External Evaluators Settings</summary>
163+
164+
<!-- config:start -->
165+
`exev_config.lua`
166+
```lua
167+
---Formats the output of external evaluator
168+
---@param result string[]
169+
---@return string[]
170+
local function generic_formatter(result)
171+
local width = vim.o.columns
172+
local sep_start = ('='):rep(width)
173+
local sep_end = ('='):rep(width)
174+
175+
table.insert(result, 1, sep_start)
176+
table.insert(result, sep_end)
177+
178+
return result
179+
end
180+
181+
local external_evaluators = {
182+
lang_prefix = '===',
183+
default_process_opts = {
184+
cwd = nil,
185+
env = { EMPTY = '' },
186+
clear_env = false,
187+
stdin = false,
188+
stdout = false,
189+
stderr = false,
190+
text = true,
191+
timeout = nil,
192+
detach = false,
193+
on_exit = nil,
194+
},
195+
196+
ruby = {
197+
cmd = { 'ruby', '-e' },
198+
env = { RUBY_VERSION = '3.3.0' },
199+
code_prefix = '$stdout.sync = true;',
200+
formatter = generic_formatter,
201+
},
202+
203+
racket = {
204+
cmd = { 'racket', '-e' },
205+
formatter = generic_formatter,
206+
},
207+
}
208+
209+
return external_evaluators
210+
```
211+
<!-- config:end -->
212+
213+
</details>
214+
215+
#### Usage
216+
217+
- The language evaluator is determined either from (in order of precedence):
218+
219+
- The code prefix `===lang` on the line above your code snippet, in which case it only applies to the snippet directly below and it should be included in the selection
220+
for evaluation. The prefix can be changed in the config.
221+
- The code prefix on the top line of the console/buffer, in which case it applies to the whole buffer.
222+
- The file type of the buffer.
223+
<br/>
224+
225+
```racket
226+
===racket
227+
228+
229+
(define (log str)
230+
(displayln (format "~v" str)))
231+
232+
233+
(define (fact n)
234+
(if (= n 0)
235+
1
236+
(* n (fact (- n 1)))))
237+
238+
(displayln (fact 111))
239+
```
240+
241+
```ruby
242+
===ruby
243+
5.times { puts 'Hey' }
244+
```
245+
123246
## Alternatives and comparison
124247

125248
There are a number of alternatives available, notably:

lua/lazy.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
return {
2-
"yarospace/lua-console.nvim",
2+
'yarospace/lua-console.nvim',
33
lazy = true,
4-
keys = "`",
4+
keys = '`',
55
opts = {},
66
}

0 commit comments

Comments
 (0)