From 481e9422eef652738496db6adc17c980621f9405 Mon Sep 17 00:00:00 2001 From: Yaro Date: Sat, 21 Dec 2024 22:18:08 +0000 Subject: [PATCH] fix: treesitter higligting --- README.md | 4 ++-- lua/lua-console.lua | 2 ++ lua/lua-console/injections.lua | 18 +++++++++++++----- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 3d5fb36..151928a 100644 --- a/README.md +++ b/README.md @@ -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). -
+
## 💡 Motivation diff --git a/lua/lua-console.lua b/lua/lua-console.lua index 9cda634..2d4c177 100644 --- a/lua/lua-console.lua +++ b/lua/lua-console.lua @@ -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) diff --git a/lua/lua-console/injections.lua b/lua/lua-console/injections.lua index 9c9b478..a5b7822 100644 --- a/lua/lua-console/injections.lua +++ b/lua/lua-console/injections.lua @@ -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