nvim-treesitter/lua/nvim-treesitter/info.lua

191 lines
5.1 KiB
Lua
Raw Normal View History

local api = vim.api
2021-07-04 16:12:17 -05:00
local configs = require "nvim-treesitter.configs"
local parsers = require "nvim-treesitter.parsers"
local M = {}
local function install_info()
local max_len = 0
for _, ft in pairs(parsers.available_parsers()) do
2021-07-04 16:12:17 -05:00
if #ft > max_len then
max_len = #ft
end
end
2021-04-21 22:52:50 +02:00
local parser_list = parsers.available_parsers()
table.sort(parser_list)
for _, lang in pairs(parser_list) do
local is_installed = #api.nvim_get_runtime_file("parser/" .. lang .. ".so", false) > 0
api.nvim_out_write(lang .. string.rep(" ", max_len - #lang + 1))
if is_installed then
2021-07-04 16:12:17 -05:00
api.nvim_out_write "[✓] installed\n"
elseif pcall(vim.treesitter.inspect_lang, lang) then
api.nvim_out_write "[✗] not installed (but still loaded. Restart Neovim!)\n"
else
2021-07-04 16:12:17 -05:00
api.nvim_out_write "[✗] not installed\n"
end
end
end
2021-04-05 22:47:27 +02:00
-- Sort a list of modules into namespaces.
-- {'mod1', 'mod2.sub1', 'mod2.sub2', 'mod3'}
-- ->
-- { default = {'mod1', 'mod3'}, mod2 = {'sub1', 'sub2'}}
---@param modulelist string[]
---@return table
2021-04-05 22:47:27 +02:00
local function namespace_modules(modulelist)
local modules = {}
2021-04-05 22:47:27 +02:00
for _, module in ipairs(modulelist) do
2021-07-04 16:12:17 -05:00
if module:find "%." then
local namespace, submodule = module:match "^(.*)%.(.*)$"
2021-04-05 22:47:27 +02:00
if not modules[namespace] then
modules[namespace] = {}
end
table.insert(modules[namespace], submodule)
else
if not modules.default then
modules.default = {}
end
2021-04-05 22:47:27 +02:00
table.insert(modules.default, module)
end
end
2021-04-05 22:47:27 +02:00
return modules
end
---@param list string[]
---@return integer length
2021-04-05 22:47:27 +02:00
local function longest_string_length(list)
local length = 0
for _, value in ipairs(list) do
if #value > length then
length = #value
end
end
return length
end
---@param curbuf integer
---@param origbuf integer
---@param parserlist string[]
---@param namespace string
---@param modulelist string[]
local function append_module_table(curbuf, origbuf, parserlist, namespace, modulelist)
2021-04-05 22:47:27 +02:00
local maxlen_parser = longest_string_length(parserlist)
table.sort(modulelist)
-- header
2021-07-04 16:12:17 -05:00
local header = ">> " .. namespace .. string.rep(" ", maxlen_parser - #namespace - 1)
2021-04-05 22:47:27 +02:00
for _, module in pairs(modulelist) do
2021-07-04 16:12:17 -05:00
header = header .. module .. " "
2021-04-05 22:47:27 +02:00
end
2021-07-04 16:12:17 -05:00
api.nvim_buf_set_lines(curbuf, -1, -1, true, { header })
2021-04-05 22:47:27 +02:00
-- actual table
for _, parser in ipairs(parserlist) do
2021-07-04 16:12:17 -05:00
local padding = string.rep(" ", maxlen_parser - #parser + 2)
local line = parser .. padding
local namespace_prefix = (namespace == "default") and "" or namespace .. "."
2021-04-05 22:47:27 +02:00
for _, module in pairs(modulelist) do
local modlen = #module
module = namespace_prefix .. module
if configs.is_enabled(module, parser, origbuf) then
2021-07-04 16:12:17 -05:00
line = line .. ""
else
2021-07-04 16:12:17 -05:00
line = line .. ""
end
2021-07-04 16:12:17 -05:00
line = line .. string.rep(" ", modlen + 1)
end
2021-07-04 16:12:17 -05:00
api.nvim_buf_set_lines(curbuf, -1, -1, true, { line })
end
2021-04-05 22:47:27 +02:00
2021-07-04 16:12:17 -05:00
api.nvim_buf_set_lines(curbuf, -1, -1, true, { "" })
2021-04-05 22:47:27 +02:00
end
local function print_info_modules(parserlist, module)
local origbuf = api.nvim_get_current_buf()
2021-07-04 16:12:17 -05:00
api.nvim_command "enew"
2021-04-05 22:47:27 +02:00
local curbuf = api.nvim_get_current_buf()
local modules
if module then
2021-07-04 16:12:17 -05:00
modules = namespace_modules { module }
else
modules = namespace_modules(configs.available_modules())
end
2021-04-05 22:47:27 +02:00
---@type string[]
2021-04-05 22:47:27 +02:00
local namespaces = {}
2021-07-04 16:12:17 -05:00
for k, _ in pairs(modules) do
table.insert(namespaces, k)
end
2021-04-05 22:47:27 +02:00
table.sort(namespaces)
table.sort(parserlist)
2021-04-05 22:47:27 +02:00
for _, namespace in ipairs(namespaces) do
append_module_table(curbuf, origbuf, parserlist, namespace, modules[namespace])
2021-04-05 22:47:27 +02:00
end
2021-07-04 16:12:17 -05:00
api.nvim_buf_set_option(curbuf, "modified", false)
api.nvim_buf_set_option(curbuf, "buftype", "nofile")
vim.cmd [[
syntax match TSModuleInfoGood //
syntax match TSModuleInfoBad //
syntax match TSModuleInfoHeader /^>>.*$/ contains=TSModuleInfoNamespace
syntax match TSModuleInfoNamespace /^>> \w*/ contained
syntax match TSModuleInfoParser /^[^> ]*\ze /
]]
local highlights = {
TSModuleInfoGood = { fg = "LightGreen", bold = true, default = true },
TSModuleInfoBad = { fg = "Crimson", default = true },
TSModuleInfoHeader = { link = "Type", default = true },
TSModuleInfoNamespace = { link = "Statement", default = true },
TSModuleInfoParser = { link = "Identifier", default = true },
}
for k, v in pairs(highlights) do
api.nvim_set_hl(0, k, v)
end
end
local function module_info(module)
2021-07-04 16:12:17 -05:00
if module and not configs.get_module(module) then
return
end
2020-06-22 11:56:55 +02:00
local parserlist = parsers.available_parsers()
if module then
print_info_modules(parserlist, module)
else
2020-06-22 11:56:55 +02:00
print_info_modules(parserlist)
end
end
---@return string[]
2020-07-31 20:27:06 +02:00
function M.installed_parsers()
local installed = {}
for _, p in pairs(parsers.available_parsers()) do
if parsers.has_parser(p) then
table.insert(installed, p)
end
end
return installed
end
M.commands = {
TSInstallInfo = {
run = install_info,
args = {
"-nargs=0",
},
},
TSModuleInfo = {
run = module_info,
args = {
"-nargs=?",
"-complete=custom,nvim_treesitter#available_modules",
},
},
}
return M