aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarco Hinz <mh.codebro@gmail.com>2021-04-05 22:47:27 +0200
committerKiyan <yazdani.kiyan@protonmail.com>2021-04-06 21:32:46 +0200
commitc64c8a401755b5a2004ef10008f1381dd521cd9d (patch)
treeef3cf6bb9c40c9079a8100b4060224a1430d3e1c
parentinjections(js): add regex highlighting (diff)
downloadnvim-treesitter-c64c8a401755b5a2004ef10008f1381dd521cd9d.tar
nvim-treesitter-c64c8a401755b5a2004ef10008f1381dd521cd9d.tar.gz
nvim-treesitter-c64c8a401755b5a2004ef10008f1381dd521cd9d.tar.bz2
nvim-treesitter-c64c8a401755b5a2004ef10008f1381dd521cd9d.tar.lz
nvim-treesitter-c64c8a401755b5a2004ef10008f1381dd521cd9d.tar.xz
nvim-treesitter-c64c8a401755b5a2004ef10008f1381dd521cd9d.tar.zst
nvim-treesitter-c64c8a401755b5a2004ef10008f1381dd521cd9d.zip
TSModuleInfo: use custom buffer
-rw-r--r--lua/nvim-treesitter/info.lua102
1 files changed, 83 insertions, 19 deletions
diff --git a/lua/nvim-treesitter/info.lua b/lua/nvim-treesitter/info.lua
index bbb32dad5..632f2bba6 100644
--- a/lua/nvim-treesitter/info.lua
+++ b/lua/nvim-treesitter/info.lua
@@ -37,33 +37,97 @@ local function print_info_module(sorted_languages, mod)
end
end
-local function print_info_modules(parserlist)
- table.sort(parserlist, function(a, b) return #a > #b end)
- local max_str_len = #parserlist[1]
+-- Sort a list of modules into namespaces.
+-- {'mod1', 'mod2.sub1', 'mod2.sub2', 'mod3'}
+-- ->
+-- { default = {'mod1', 'mod3'}, mod2 = {'sub1', 'sub2'}}
+local function namespace_modules(modulelist)
+ local modules = { default = {} }
+ for _, module in ipairs(modulelist) do
+ if module:find('%.') then
+ local namespace, submodule = module:match('^(.*)%.(.*)$')
+ if not modules[namespace] then
+ modules[namespace] = {}
+ end
+ table.insert(modules[namespace], submodule)
+ else
+ table.insert(modules.default, module)
+ end
+ end
+ return modules
+end
- local header = string.rep(' ', max_str_len + 1)
- local mods = configs.available_modules()
- table.sort(mods)
- for _, mod in pairs(mods) do
- header = string.format('%s %s ', header, mod)
+local function longest_string_length(list)
+ local length = 0
+ for _, value in ipairs(list) do
+ if #value > length then
+ length = #value
+ end
end
- api.nvim_out_write(header .. '\n')
+ return length
+end
- table.sort(parserlist)
- for _, lang in pairs(parserlist) do
- local padding = string.rep(' ', max_str_len - #lang + 1)
- api.nvim_out_write(lang .. ":" .. padding)
+local function append_module_table(curbuf, parserlist, namespace, modulelist)
+ local maxlen_parser = longest_string_length(parserlist)
+ table.sort(modulelist)
+
+ -- header
+ local header = '>> ' .. namespace .. string.rep(' ', maxlen_parser - #namespace - 1)
+ for _, module in pairs(modulelist) do
+ header = header .. module .. ' '
+ end
+ api.nvim_buf_set_lines(curbuf, -1, -1, true, {header})
- for _, mod in pairs(mods) do
- if configs.is_enabled(mod, lang) then
- api.nvim_out_write('✓')
+ -- actual table
+ for _, parser in ipairs(parserlist) do
+ local padding = string.rep(' ', maxlen_parser - #parser + 2)
+ local line = parser .. padding
+ local namespace_prefix = (namespace == 'default') and '' or namespace .. '.'
+ for _, module in pairs(modulelist) do
+ local modlen = #module
+ module = namespace_prefix .. module
+ if configs.is_enabled(module, parser) then
+ line = line .. '✓'
else
- api.nvim_out_write('✗')
+ line = line .. '✗'
end
- api.nvim_out_write(string.rep(' ', #mod + 1))
+ line = line .. string.rep(' ', modlen + 1)
end
- api.nvim_out_write('\n')
+ api.nvim_buf_set_lines(curbuf, -1, -1, true, {line})
end
+
+ api.nvim_buf_set_lines(curbuf, -1, -1, true, {''})
+end
+
+local function print_info_modules(parserlist)
+ api.nvim_command('enew')
+ local curbuf = api.nvim_get_current_buf()
+
+ local modules = namespace_modules(configs.available_modules())
+ table.sort(parserlist)
+
+ local namespaces = {}
+ for k, _ in pairs(modules) do table.insert(namespaces, k) end
+ table.sort(namespaces)
+
+ for _, namespace in ipairs(namespaces) do
+ append_module_table(curbuf, parserlist, namespace, modules[namespace])
+ end
+
+ api.nvim_buf_set_option(curbuf, 'modified', false)
+ api.nvim_buf_set_option(curbuf, 'buftype', 'nofile')
+ api.nvim_exec([[
+ syntax match TSModuleInfoGood /✓/
+ syntax match TSModuleInfoBad /✗/
+ syntax match TSModuleInfoHeader /^>>.*$/ contains=TSModuleInfoNamespace
+ syntax match TSModuleInfoNamespace /^>> \w*/ contained
+ syntax match TSModuleInfoParser /^[^> ]*\ze /
+ highlight default TSModuleInfoGood guifg=LightGreen gui=bold
+ highlight default TSModuleInfoBad guifg=Crimson
+ highlight default link TSModuleInfoHeader Type
+ highlight default link TSModuleInfoNamespace Statement
+ highlight default link TSModuleInfoParser Identifier
+ ]], false)
end
local function module_info(mod)