Skip to content

Commit

Permalink
fixed handling of mupliple arguments by pretty print
Browse files Browse the repository at this point in the history
  • Loading branch information
YaroSpace committed Nov 14, 2024
1 parent 34a3a5b commit 184f413
Show file tree
Hide file tree
Showing 10 changed files with 304 additions and 25 deletions.
4 changes: 3 additions & 1 deletion .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ read_globals = {
"assert"
}

exclude_files = { }
exclude_files = {
"spec/utfTerminal.lua"
}
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ stylua:

test:
eval $(luarocks path --lua-version 5.1 --bin)
busted --run unit
busted -o spec/utfTerminal.lua --run unit

watch:
while sleep 0.1; do ls -d spec/**/*.lua | entr -d -c make test; done
while sleep 0.1; do ls -d spec/**/*.lua | entr -d -c busted -o spec/utfTerminal.lua --run unit; done

watch_current:
while sleep 0.1; do ls -d spec/**/*.lua | entr -d -c busted --run unit -t=current; done
while sleep 0.1; do ls -d spec/**/*.lua | entr -d -c busted -o spec/utfTerminal.lua --run unit -t=current; done
32 changes: 29 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ otherwise, install with your favourite package manager and add

## ⚙️ Configuration

All settings are very straight forward, but please read below about `preserve_context` option below.

<details><summary>Default Settings</summary>

<!-- config:start -->
Expand All @@ -49,7 +51,8 @@ opts = {
buffer = {
prepend_result_with = '=> ',
save_path = vim.fn.stdpath('state') .. '/lua-console.lua',
load_on_start = true -- load saved session on first entry
load_on_start = true, -- load saved session on first entry
preserve_context = true -- preserve context between executions
},
window = {
anchor = 'SW',
Expand Down Expand Up @@ -83,8 +86,31 @@ opts = {
- 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.
- Use `print()` in your code to output the result into the console. Objects and functions are pretty printed.
- Use `print()` in your code to output the results into the console. Objects and functions are pretty printed.
- Press `M` to load Neovim messages into the console.
- Press `gf` to follow the paths in stack traces and to function sources.
- Press `gf` to follow the paths in stack traces and to function sources. Truncated paths work too.
- Use `S` and `L` to save / load the console session to preserve history of your hacking.
- You can resize the console with `<C-Up>` and `<C-Down>`.

## 📓 Notes on globals, locals and preserving context
> [!NOTE]
> Highlights information that users should take into account, even when skimming.
> [!TIP]
> Optional information to help a user be more successful.
> [!IMPORTANT]
> Crucial information necessary for users to succeed.
> [!WARNING]
> Critical content demanding immediate user attention due to potential risks.
> [!CAUTION]
> Negative potential consequences of an action.
-
-

## Alternatives and comparison

-
-
5 changes: 4 additions & 1 deletion doc/lua-console.nvim_api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
------------------------------------------------------------------------------
*load_console()*

`load_console`()
`load_console`({on_start})


saved console
Parameters ~
{on_start} `(boolean)`

------------------------------------------------------------------------------
*append_current_buffer()*
Expand Down
27 changes: 19 additions & 8 deletions lua/lua-console/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ local to_table = function(str)
return vim.split(str or '', '\n', { trimempty = true })
end

local pack = function(...)
local ret = {}
for i = 1, select("#", ...) do
local el = select(i, ...)
table.insert(ret, el)
end

return ret
end

local toggle_help = function()
local buf = Lua_console.buf
local cm = config.mappings
Expand All @@ -28,8 +38,6 @@ local toggle_help = function()

vim.api.nvim_buf_set_extmark(buf, ns, 0, 0, { id=2, virt_text = { { message, 'Comment' } }, virt_text_pos = 'overlay', undo_restore = false, invalidate = true })
end

ids = vim.api.nvim_buf_get_extmarks(buf, ns, 0, -1, {})
end

---Loads saved console
Expand All @@ -48,8 +56,8 @@ end
local infer_truncated_path = function(truncated_path)
local pos, _ = truncated_path:find('/lua/')
local path = truncated_path:sub(pos + 1, #truncated_path)

local found = vim.api.nvim_get_runtime_file(path, true)

return not vim.tbl_isempty(found) and found[1] or false
end

Expand Down Expand Up @@ -97,7 +105,6 @@ end
local pretty_print = function(...)
local result, var_no = '', ''
local nargs = select('#', ...)

for i=1, nargs do
local o = select(i, ...)

Expand Down Expand Up @@ -189,11 +196,15 @@ local eval_lua = function(lines)
print_buffer = {}

---@cast code function
local status, result = xpcall(code, debug.traceback)
if status then
pretty_print(result)
local result = pack(xpcall(code, debug.traceback))
if result[1] then
table.remove(result, 1)
if #result > 0 then pretty_print(unpack(result))
else
pretty_print(nil)
end
else
vim.list_extend(print_buffer, clean_stacktrace(result))
vim.list_extend(print_buffer, clean_stacktrace(result[2]))
end

return print_buffer
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ M.get_virtual_text = function(buf, line)
local ids = vim.api.nvim_buf_get_extmarks(buf, ns, line, -1, {})

if vim.tbl_isempty(ids) then
LOG('No extmarks found')
_G.LOG('No extmarks found')
return
end

Expand Down
3 changes: 1 addition & 2 deletions spec/unit/lua-console_spec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local h = require('spec_helper')

describe('lua-console.nvim', function()
local buf, win
local console, config
local expected, result

Expand Down Expand Up @@ -47,8 +48,6 @@ describe('lua-console.nvim', function()
end)

describe('lua-console - open/close window', function()
local buf, win

before_each(function()
console.toggle_console()
buf = vim.fn.bufnr('lua-console')
Expand Down
3 changes: 2 additions & 1 deletion spec/unit/mappings_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ describe("lua-console.nvim - mappings", function()
h.send_keys("gf")

local new_buf = vim.fn.bufnr()
assert.has_string(vim.fn.bufname(new_buf), "/usr/share/nvim-linux64/share/nvim/runtime/lua/vim/lsp/diagnostic.lua")
local path = vim.fn.expand('$VIMRUNTIME') .. "/lua/vim/lsp/diagnostic.lua"
assert.has_string(vim.fn.bufname(new_buf), path)

local line = vim.fn.line('.')
assert.is_same(line, 189)
Expand Down
23 changes: 18 additions & 5 deletions spec/unit/utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ describe("lua-console.utils", function()
result = eval_lua(code)
assert.has_string(result, "nil")
end)

it('handles code that returns multiple values', function()
code = h.to_table([[
a = 'test_string'
a:find('str')
]])

result = eval_lua(code)
assert.has_string(result, "[1] 6, [2] 8")
end)
end)

describe('preserving context', function()
Expand Down Expand Up @@ -164,7 +174,7 @@ describe("lua-console.utils", function()
local eval_lua = utils.eval_lua
local code, result, expected

it("pretty prints objects", function()
it("pretty prints objects #current", function()
code = h.to_table([[
local ret = {}
for i=1, 5 do
Expand Down Expand Up @@ -381,8 +391,9 @@ describe("lua-console.utils", function()
end)

it("infers truncated paths in the stacktrace", function()
local rtp = vim.fn.expand('$VIMRUNTIME')
local truncated = '...e/nvim-linux64/share/nvim/runtime/lua/vim/diagnostic.lua'
expected = "/usr/share/nvim-linux64/share/nvim/runtime/lua/vim/diagnostic.lua"
expected = rtp .. "/lua/vim/diagnostic.lua"

local path, _ = utils.get_path_lnum(truncated)
assert.is_same(expected, path)
Expand All @@ -395,15 +406,17 @@ describe("lua-console.utils", function()
end)

it("infers truncated paths and line number in the stacktrace", function()
local truncated = '...testing/start/lua-console.nvim/lua/lua-console/utils.lua'
content = h.to_table([[
'...testing/start/lua-console.nvim/lua/lua-console/utils.lua:85'
]])
h.set_buffer(buf, content)
vim.api.nvim_win_set_cursor(win, { 11, 0 })
vim.api.nvim_win_set_cursor(win, { 1, 0 })

expected = vim.fn.expand('$XDG_PLUGIN_PATH') .. '/lua/lua-console/utils.lua'

path, lnum = utils.get_path_lnum(truncated)
local path, lnum = utils.get_path_lnum(truncated)
assert.is_same(expected, path)
assert.is_same(85, lnum)
end)

Expand All @@ -427,7 +440,7 @@ describe("lua-console.utils", function()
vim.api.nvim_win_set_cursor(win, { 11, 0 })

local truncated = ".../.local/share/nvim/lazy/arrow.nvim/lua/arrow/persist.lua"
local path, lnum = utils.get_path_lnum(truncated)
local _, lnum = utils.get_path_lnum(truncated)
assert.equals(125, lnum)
end)

Expand Down
Loading

0 comments on commit 184f413

Please sign in to comment.