Skip to content

Commit b7ec0d1

Browse files
docs(vimdoc): Auto-generate user / API documentation + vimtags
1 parent 5e12038 commit b7ec0d1

3 files changed

Lines changed: 259 additions & 44 deletions

File tree

doc/lua-console.nvim.txt

Lines changed: 151 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ Table of Contents *lua-console.nvim-table-of-contents*
88
- ✨ Features|lua-console.nvim-💻-lua-console-main-develop-luarocks-✨-features|
99
- 📦 Installation|lua-console.nvim-💻-lua-console-main-develop-luarocks-📦-installation|
1010
- ⚙️ Configuration|lua-console.nvim-💻-lua-console-main-develop-luarocks-⚙️-configuration|
11-
- 🚀 Usage (with default mappings)|lua-console.nvim-💻-lua-console-main-develop-luarocks-🚀-usage-(with-default-mappings)|
11+
- 🚀 Basic usage (with default mappings)|lua-console.nvim-💻-lua-console-main-develop-luarocks-🚀-basic-usage-(with-default-mappings)|
1212
- 📓 Notes on globals, locals and preserving execution context|lua-console.nvim-💻-lua-console-main-develop-luarocks-📓-notes-on-globals,-locals-and-preserving-execution-context|
13+
- ⭐ Extra features|lua-console.nvim-💻-lua-console-main-develop-luarocks-⭐-extra-features|
1314
- Alternatives and comparison|lua-console.nvim-💻-lua-console-main-develop-luarocks-alternatives-and-comparison|
1415
- 🔥 All feedback and feature requests are very welcome! Enjoy!|lua-console.nvim-💻-lua-console-main-develop-luarocks-🔥-all-feedback-and-feature-requests-are-very-welcome!-enjoy!|
1516

@@ -21,6 +22,9 @@ development and Neovim exploration and configuration. Acts as a user friendly
2122
replacement of command mode - messages loop and as a handy scratch pad to store
2223
and test your code gists.
2324

25+
**Update: Although it originated as a tool for Lua development, it has now
26+
evolved into supporting other languages too. See evaluating other languages.**
27+
2428

2529

2630

@@ -36,15 +40,15 @@ needed something better, so there it is.
3640

3741
✨ FEATURES*lua-console.nvim-💻-lua-console-main-develop-luarocks-✨-features*
3842

39-
- Evaluate single line expressions
40-
- Evaluate visually selected lines of code
43+
- Evaluate single line expressions and statements, visually selected lines of code or the whole buffer
4144
- Pretty print Lua objects, including function details and their source paths
42-
- Show normal and error output in the console, including output of `print()`, errors and stacktraces.
45+
- Show normal and error output in the console/buffer, including output of `print()`, errors and stacktraces.
4346
- Syntax highlighting and autocompletion
4447
- Load Neovim’s messages into console for inspection and copy/paste
4548
- Open links from stacktraces and function sources
46-
- Save / Load console session
47-
- Use as scratch pad for code gists
49+
- Save / Load / Autosave console session
50+
- Use as a scratch pad for code gists
51+
- Attach code evaluators to any buffer
4852

4953

5054
📦 INSTALLATION*lua-console.nvim-💻-lua-console-main-develop-luarocks-📦-installation*
@@ -59,57 +63,64 @@ With lazy.nvim <https://github.com/folke/lazy.nvim>:
5963
<
6064

6165
otherwise, install with your favorite package manager and add
62-
`require('lua-console').setup({opts})` somewhere in your config.
66+
`require('lua-console').setup { custom_options }` somewhere in your config.
6367

6468

6569
⚙️ CONFIGURATION*lua-console.nvim-💻-lua-console-main-develop-luarocks-⚙️-configuration*
6670

6771

68-
[!NOTE] All settings are very straight forward, but please read below about
72+
[!NOTE] All settings are self explanatory, but please read below about
6973
|lua-console.nvim-`preserve_context`| option.
70-
Mappings are local to the console, except the one for toggling, which is - ```
71-
by default.
74+
Mappings are local to the console, except the ones for toggling the console -
75+
``` and attaching to a buffer - `<Leader>``. All mappings can be overridden in
76+
your custom config. If you want to delete a mapping - set its value to `false`.
7277

7378
Default Settings ~
7479

80+
`config.lua`
81+
7582
>lua
7683
opts = {
7784
buffer = {
78-
prepend_result_with = '=> ',
85+
result_prefix = '=> ',
7986
save_path = vim.fn.stdpath('state') .. '/lua-console.lua',
80-
load_on_start = true, -- load saved session on first entry
81-
preserve_context = true -- preserve context between executions
87+
autosave = true, -- autosave on console hide / close
88+
load_on_start = true, -- load saved session on start
89+
preserve_context = true, -- preserve results between evaluations
8290
},
8391
window = {
84-
border = 'double', -- single|double|rounded
92+
border = 'double', -- single|double|rounded
8593
height = 0.6, -- percentage of main window
8694
},
8795
mappings = {
8896
toggle = '`',
97+
attach = '<Leader>`',
8998
quit = 'q',
9099
eval = '<CR>',
91-
clear = 'C',
100+
eval_buffer = '<S-CR>',
101+
open = 'gf',
92102
messages = 'M',
93103
save = 'S',
94104
load = 'L',
95105
resize_up = '<C-Up>',
96106
resize_down = '<C-Down>',
97107
help = '?'
98-
}
108+
},
99109
}
100110
<
101111

102112

103-
🚀 USAGE (WITH DEFAULT MAPPINGS)*lua-console.nvim-💻-lua-console-main-develop-luarocks-🚀-usage-(with-default-mappings)*
113+
🚀 BASIC USAGE (WITH DEFAULT MAPPINGS)*lua-console.nvim-💻-lua-console-main-develop-luarocks-🚀-basic-usage-(with-default-mappings)*
104114

105115
- Install, press the mapped key ``` and start exploring.
106116
- Enter code as normal, in insert mode.
107117
- Hit `Enter` in normal mode to evaluate a variable, statement or an expression in the current line.
108-
- Visually select a range of lines and press `Enter` to evaluate the code in the range.
118+
- 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.
109119
- The evaluation of the last line is returned and printed, so no `return` is needed in most cases.
110-
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.
111-
- Use `print()` in your code to output the results into the console. Accepts variable number of arguments, e.g. `print(var_1, var_2, ...)`.
112-
- Objects and functions are pretty printed, with function source paths.
120+
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.
121+
The result of assignments on the last line will be also shown as virtual text.
122+
- 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, ...)`.
123+
- Objects and functions are pretty printed, with function details and their source paths.
113124
- Press `gf` to follow the paths in stack traces and to function sources. Truncated paths work too.
114125

115126

@@ -118,34 +129,145 @@ Default Settings ~
118129
`vim.lsp.handlers['textDocument/hover']` for example, you can jump to its
119130
current definition, while Lsp/tags would take you to the original one.
120131
- Press `M` to load Neovim messages into the console.
121-
- Use `S` and `L` to save / load the console session to preserve history of your hacking.
132+
- 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
133+
saved whenever it is toggled or closed.
122134
- You can resize the console with `<C-Up>` and `<C-Down>`.
123135

124136

125137
📓 NOTES ON GLOBALS, LOCALS AND PRESERVING EXECUTION CONTEXT*lua-console.nvim-💻-lua-console-main-develop-luarocks-📓-notes-on-globals,-locals-and-preserving-execution-context*
126138

127139

128140
[!IMPORTANT] By default, the option `preserve_context` is on, which means that
129-
the context is preserved between executions.
141+
the execution context is preserved between evaluations.
130142
All the code executed in the console is evaluated in isolated environment. This
131-
means that any variables you declare will not be persisted in Neovim’s global
132-
environment, although all global variables are accessible. If you want
133-
purposefully to alter the global state, use `_G.My_variable = ..`.
143+
means that any variables you declare without the `local` keyword will not be
144+
persisted in Neovim’s global environment, although all global variables are
145+
accessible. If you want purposefully to alter the global state, use
146+
`_G.My_variable = ..`.
134147

135-
The option `preserve_context` means that if you assign variables without
148+
The option `preserve_context` means that although you declare variables without
136149
`local`, they will be stored in console’s local context and preserved between
137150
separate executions. So, if you first execute `a = 1`, then `a = a + 1` and
138151
then `a` - you will get `2`. Variables with `local` are not preserved.
139152

140-
If you want a classic REPL experience, when the context is cleared on every
141-
execution, set `preserve_context = false`.
153+
If you want the context to be cleared before every execution, set
154+
`preserve_context = false`.
142155

143156
There are two functions available within the console:
144157

145158
- `_ctx()` - will print the contents of the context
146159
- `_ctx_clear()` - clears the context
147160

148161

162+
⭐ EXTRA FEATURES*lua-console.nvim-💻-lua-console-main-develop-luarocks-⭐-extra-features*
163+
164+
165+
ATTACHING CODE EVALUATOR TO OTHER BUFFERS ~
166+
167+
- The evaluator behind the console can be attached/detached to any buffer by pressing `<Leader>`` or executing command `LuaConsole AttachToggle`.
168+
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.
169+
170+
171+
EVALUATING OTHER LANGUAGES ~
172+
173+
174+
SETTING UP
175+
176+
- It is possible to setup external code executors for other languages. Evaluators
177+
for `ruby` and `racket` are working out of the box, support for other languages
178+
is coming. Meanwhile, you can easily setup your own language.
179+
- Below is the default configuration which can be overridden or extended by your
180+
custom config (`default_process_opts` will be replaced by language specific
181+
opts), e.g. a possible config for `python` could be:
182+
>lua
183+
require('lua-console').setup {
184+
external_evaluators = {
185+
python = {
186+
cmd = { 'python3', '-c' },
187+
env = { PYTHONPATH = '~/projects' },
188+
timeout = 100000,
189+
formatter = function(result) do_something; return result end,
190+
},
191+
}
192+
}
193+
<
194+
- You can also setup a custom formatter to format the evaluator output before
195+
appending results to the console or buffer. Example is in the config.
196+
Default External Evaluators Settings
197+
~
198+
`exev_config.lua`
199+
>lua
200+
---Formats the output of external evaluator
201+
---@param result string[]
202+
---@return string[]
203+
local function generic_formatter(result)
204+
local width = vim.o.columns
205+
local sep_start = ('='):rep(width)
206+
local sep_end = ('='):rep(width)
207+
208+
table.insert(result, 1, sep_start)
209+
table.insert(result, sep_end)
210+
211+
return result
212+
end
213+
214+
local external_evaluators = {
215+
lang_prefix = '===',
216+
default_process_opts = {
217+
cwd = nil,
218+
env = { EMPTY = '' },
219+
clear_env = false,
220+
stdin = false,
221+
stdout = false,
222+
stderr = false,
223+
text = true,
224+
timeout = nil,
225+
detach = false,
226+
on_exit = nil,
227+
},
228+
229+
ruby = {
230+
cmd = { 'ruby', '-e' },
231+
env = { RUBY_VERSION = '3.3.0' },
232+
code_prefix = '$stdout.sync = true;',
233+
formatter = generic_formatter,
234+
},
235+
236+
racket = {
237+
cmd = { 'racket', '-e' },
238+
formatter = generic_formatter,
239+
},
240+
}
241+
242+
return external_evaluators
243+
<
244+
USAGE
245+
- The language evaluator is determined either from (in order of precedence):
246+
- 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
247+
for evaluation. The prefix can be changed in the config.
248+
- The code prefix on the top line of the console/buffer, in which case it applies to the whole buffer.
249+
- The file type of the buffer.
250+
>racket
251+
===racket
252+
253+
254+
(define (log str)
255+
(displayln (format "~v" str)))
256+
257+
258+
(define (fact n)
259+
(if (= n 0)
260+
1
261+
(* n (fact (- n 1)))))
262+
263+
(displayln (fact 111))
264+
<
265+
>ruby
266+
===ruby
267+
5.times { puts 'Hey' }
268+
<
269+
270+
149271
ALTERNATIVES AND COMPARISON*lua-console.nvim-💻-lua-console-main-develop-luarocks-alternatives-and-comparison*
150272

151273
There are a number of alternatives available, notably:

0 commit comments

Comments
 (0)