diff options
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/nvim-treesitter/info.lua | 102 |
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) |
