Skip to content

Commit

Permalink
Merge pull request #6 from YaroSpace/develop
Browse files Browse the repository at this point in the history
v1.2.2
  • Loading branch information
YaroSpace authored Dec 22, 2024
2 parents 073e1dd + b28cefc commit 57f514e
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
- name: Push Changes
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: "docs(vimdoc): Auto-generate user / API documentation + vimtags"
commit_message: "docs: Auto-generate user / API documentation + vimtags"
commit_user_name: "github-actions[bot]"
commit_user_email: "github-actions[bot]@users.noreply.github.com"
commit_author: "github-actions[bot] <github-actions[bot]@users.noreply.github.com>"
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
Acts as a user friendly replacement of command mode - messages loop and as a handy scratch pad to store and test your code gists.


***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).***
**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).

<br/><img src="doc/demo.gif">
<br/><img src="https://github.com/YaroSpace/assets/blob/main/lua-console.nvim/demo.gif">

## 💡 Motivation

Expand Down
Binary file removed doc/demo.gif
Binary file not shown.
Binary file removed doc/demo.jpg
Binary file not shown.
2 changes: 2 additions & 0 deletions lua/lua-console.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ local get_or_create_buffer = function()

injections.set_highlighting()
vim.api.nvim_set_option_value('filetype', 'lua', { buf = buf })
vim.api.nvim_set_option_value('syntax', 'lua', { buf = buf })

vim.diagnostic.enable(false, { bufnr = buf })

mappings.set_console_mappings(buf)
Expand Down
18 changes: 13 additions & 5 deletions lua/lua-console/injections.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,31 @@ M.set_highlighting = function()
local lang_prefix = config.external_evaluators.lang_prefix
local lang_pattern = ('^%s([^\\n]-)\\n.+$'):format(lang_prefix)

vim.treesitter.query.add_directive('indent!', function(_, _, _, predicate, metadata)
vim.treesitter.query.add_directive('deindent!', function(_, _, _, predicate, metadata) -- remove indentaion in the region
local capture_id = predicate[2]
if not metadata[capture_id].range then return end

metadata[capture_id].range[2] = tonumber(predicate[3]) -- set indent col to 0
end, { all = true, force = true })

local function extend_query(query)
local extended = ''
vim.tbl_map(function(path)
extended = extended .. io.open(path):read("*a") .. '\n'
end, vim.treesitter.query.get_files('lua', 'injections'))

return extended .. query
end

local query = ([[ ;query
; extends
(string
content: (string_content) @injection.language @injection.content
((string_content) @injection.language @injection.content
(#lua-match? @injection.language "^@1")
(#gsub! @injection.language "@2" "%1")
(#offset! @injection.content 1 0 0 0)
(#indent! @injection.content 0))
(#deindent! @injection.content 0))
]]):gsub('@1', lang_prefix):gsub('@2', lang_pattern)

query = extend_query(query)
vim.treesitter.query.set('lua', 'injections', query)
end

Expand Down
23 changes: 17 additions & 6 deletions lua/lua-console/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ local to_string = function(tbl, sep, trim)
return line
end

local to_table = function(str)
return vim.split(str or '', '\n', { trimempty = true })
---@param obj string|string[]
---@return string[]
local to_table = function(obj)
obj = type(obj) == 'string' and { obj } or obj

return vim.iter(obj):map(function(line)
return vim.split(line or '', '\n', { trimempty = true })
end):flatten():totable()
end

local function remove_indentation(tbl)
Expand Down Expand Up @@ -141,11 +147,14 @@ local append_current_buffer = function(buf, lines)

local lnum = vim.fn.line('.')
local prefix = config.buffer.result_prefix
local empty_results = { 'nil', '', '""', "''" }

local virtual_text
if lines[#lines] == 'nil' then
local line = lines[#lines]

if vim.tbl_contains(empty_results, line) then
table.remove(lines)
virtual_text = 'nil'
virtual_text = line
end

local assignment_value = get_assignment(vim.fn.getbufline(buf, lnum, lnum))
Expand Down Expand Up @@ -181,7 +190,7 @@ local pretty_print = function(...)
result = to_table(result)
vim.list_extend(print_buffer, result)

return result
-- return result
end

local function remove_empty_lines(tbl)
Expand Down Expand Up @@ -424,14 +433,16 @@ local load_messages = function(buf)
---This way we catch the output of messages command, in case it was overriden by some other plugin, like Noice
vim.ui_attach(ns, { ext_messages = true }, function(event, entries) ---@diagnostic disable-line
if event ~= 'msg_history_show' then return end

local messages = vim.tbl_map(function(e)
return e[2][1][2]
end, entries)

if #messages == 0 then return end

vim.schedule(function()
append_current_buffer(buf, messages)
vim.api.nvim_input('<Down>') -- forcing to redraw buffer
append_current_buffer(buf, to_table(messages))
end)
end)

Expand Down

0 comments on commit 57f514e

Please sign in to comment.