Skip to content

Commit

Permalink
chore: updated docs
Browse files Browse the repository at this point in the history
  • Loading branch information
YaroSpace committed Nov 26, 2024
1 parent a66cdc2 commit 9bef21c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
37 changes: 31 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ return {
}
```
otherwise, install with your favorite package manager and add
`require('lua-console').setup({opts})` somewhere in your config.
`require('lua-console').setup({ your_custom_options })` somewhere in your config.


## ⚙️ Configuration
Expand Down Expand Up @@ -65,7 +65,6 @@ opts = {
toggle = '`',
quit = 'q',
eval = '<CR>',
clear = 'C',
messages = 'M',
save = 'S',
load = 'L',
Expand All @@ -80,16 +79,17 @@ opts = {
</details>


## 🚀 Usage (with default mappings)
## 🚀 Basic usage (with default mappings)

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

> [!NOTE]
Expand Down Expand Up @@ -120,6 +120,31 @@ There are two functions available within the console:
- `_ctx_clear()` - clears the context


## Extra features

### Attaching code evaluator to other buffers

- The evaluator behind the console can be attached to any buffer by calling or mapping `require('lua-console.utils).attach(buf_number)` where `buf_number` is current buffer by default.
You will be able to evaluate the code as in the console and follow links. The evaluators and their contexts are isolated for each attached buffer.
- You can also setup external code runners for languages other than lua.


### Evaluating other languages

- It is possible to setup external code executors for other languages. There are examples for `ruby, python, go, rust and scheme` in the config.
- To tell the evaluator which executor to use prepend your code with ` ===lang ` on the line above or do `vim.bo.filetype = 'lang'`. The prefix can be changed in the config.
- You can also setup a custom formatter to preformat the executor output before appending results to the console or buffer.
E.g.

```
===ruby
5.times { puts 'Hey' }
```

- Unlike lua, the context is not preserved.


## Alternatives and comparison

There are a number of alternatives available, notably:
Expand Down
1 change: 1 addition & 0 deletions lua/lua-console/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ local default_config = {
help = '?'
},
external_evaluators = {
lang_prefix = '===',
ruby = {
cmd = { 'ruby', '-e' },
env = { RUBY_VERSION = '3.3.0' },
Expand Down
6 changes: 3 additions & 3 deletions lua/lua-console/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ end

local get_evaluator = function(buf, lines)
local evaluator
local lang = lines[1]:match("```(.+)")
local lang = lines[1]:match("^" .. config.external_evaluators.lang_prefix .. "(.+)")

if lang then table.remove(lines, 1) end
lang = lang and lang or vim.bo[buf].filetype
Expand Down Expand Up @@ -388,7 +388,7 @@ end

---Attaches evaluator (mappings and context) to a buffer
---@param buf? number buffer number, default is current buffer
local attach_evaluator = function(buf)
local attach = function(buf)
buf = buf or vim.fn.bufnr()
require('lua-console.mappings').set_evaluator_mappings(buf)
vim.notify(string.format('Evaluator attached to buffer [%s]', buf), vim.log.levels.INFO)
Expand All @@ -403,5 +403,5 @@ return {
get_plugin_path = get_plugin_path,
load_messages = load_messages,
get_path_lnum = get_path_lnum,
attach_evaluator = attach_evaluator
attach = attach
}
2 changes: 1 addition & 1 deletion spec/unit/utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ describe('lua-console.utils', function()

expected = config.buffer.prepend_result_with .. 'nil'

result = h.get_virtual_text(buf, 2, 2)
result = h.get_virtual_text(buf, 2, 2)
assert.has_string(result, expected)
end)

Expand Down

0 comments on commit 9bef21c

Please sign in to comment.