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 21, 2024
1 parent 2e29892 commit 09428dd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
4 changes: 3 additions & 1 deletion lua/lua-console.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ local get_or_create_buffer = function()
local buf = _G.Lua_console.buf
if buf and vim.fn.bufloaded(buf) == 1 then return buf end

local buf_name = utils.get_plugin_path() .. '/console'
local buf_name = utils.get_plugin_path() .. '/console.lua'

buf = vim.fn.bufnr(buf_name)
if buf ~= -1 then vim.api.nvim_buf_delete(buf, { force = true }) end
Expand All @@ -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 09428dd

Please sign in to comment.