You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- 📓 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|
13
14
- Alternatives and comparison|lua-console.nvim-💻-lua-console-main-develop-luarocks-alternatives-and-comparison|
14
15
- 🔥 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!|
15
16
@@ -21,6 +22,9 @@ development and Neovim exploration and configuration. Acts as a user friendly
21
22
replacement of command mode - messages loop and as a handy scratch pad to store
22
23
and test your code gists.
23
24
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
+
24
28
25
29
26
30
@@ -36,15 +40,15 @@ needed something better, so there it is.
- Install, press the mapped key ``` and start exploring.
106
116
- Enter code as normal, in insert mode.
107
117
- 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.
109
119
- 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.
113
124
- Press `gf` to follow the paths in stack traces and to function sources. Truncated paths work too.
114
125
115
126
@@ -118,34 +129,145 @@ Default Settings ~
118
129
`vim.lsp.handlers['textDocument/hover']` for example, you can jump to its
119
130
current definition, while Lsp/tags would take you to the original one.
120
131
- 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.
122
134
- You can resize the console with `<C-Up>` and `<C-Down>`.
123
135
124
136
125
137
📓 NOTES ON GLOBALS, LOCALS AND PRESERVING EXECUTION CONTEXT*lua-console.nvim-💻-lua-console-main-develop-luarocks-📓-notes-on-globals,-locals-and-preserving-execution-context*
126
138
127
139
128
140
[!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.
130
142
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 = ..`.
134
147
135
-
The option `preserve_context` means that if you assign variables without
148
+
The option `preserve_context` means that although you declare variables without
136
149
`local`, they will be stored in console’s local context and preserved between
137
150
separate executions. So, if you first execute `a = 1`, then `a = a + 1` and
138
151
then `a` - you will get `2`. Variables with `local` are not preserved.
139
152
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`.
142
155
143
156
There are two functions available within the console:
144
157
145
158
- `_ctx()` - will print the contents of the context
146
159
- `_ctx_clear()` - clears the context
147
160
148
161
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
+
149
271
ALTERNATIVES AND COMPARISON*lua-console.nvim-💻-lua-console-main-develop-luarocks-alternatives-and-comparison*
150
272
151
273
There are a number of alternatives available, notably:
0 commit comments