Skip to content

Commit

Permalink
fix: treesitter higligting
Browse files Browse the repository at this point in the history
  • Loading branch information
YaroSpace committed Dec 22, 2024
1 parent 2e29892 commit 481e942
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
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
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

0 comments on commit 481e942

Please sign in to comment.