Use stylua for autoformat code (#1480)

This commit is contained in:
Santos Gallegos 2021-07-04 16:12:17 -05:00 committed by GitHub
parent 90f15d9bf7
commit be8f656087
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
32 changed files with 1181 additions and 979 deletions

View file

@ -1,10 +1,10 @@
local api = vim.api
local queries = require'nvim-treesitter.query'
local ts_query = require'vim.treesitter.query'
local parsers = require'nvim-treesitter.parsers'
local utils = require'nvim-treesitter.utils'
local caching = require'nvim-treesitter.caching'
local queries = require "nvim-treesitter.query"
local ts_query = require "vim.treesitter.query"
local parsers = require "nvim-treesitter.parsers"
local utils = require "nvim-treesitter.utils"
local caching = require "nvim-treesitter.caching"
local M = {}
@ -12,7 +12,7 @@ local config = {
modules = {},
ensure_installed = {},
ignore_install = {},
update_strategy = 'lockfile',
update_strategy = "lockfile",
}
-- List of modules that need to be setup on initialization.
local queued_modules_defs = {}
@ -20,31 +20,33 @@ local queued_modules_defs = {}
local is_initialized = false
local builtin_modules = {
highlight = {
module_path = 'nvim-treesitter.highlight',
module_path = "nvim-treesitter.highlight",
enable = false,
disable = {'markdown'}, -- FIXME(vigoux): markdown highlighting breaks everything for now
disable = { "markdown" }, -- FIXME(vigoux): markdown highlighting breaks everything for now
custom_captures = {},
is_supported = queries.has_highlights,
additional_vim_regex_highlighting = false,
},
incremental_selection = {
module_path = 'nvim-treesitter.incremental_selection',
module_path = "nvim-treesitter.incremental_selection",
enable = false,
disable = {},
keymaps = {
init_selection="gnn",
node_incremental="grn",
scope_incremental="grc",
node_decremental="grm"
init_selection = "gnn",
node_incremental = "grn",
scope_incremental = "grc",
node_decremental = "grm",
},
is_supported = function() return true end
is_supported = function()
return true
end,
},
indent = {
module_path = 'nvim-treesitter.indent',
module_path = "nvim-treesitter.indent",
enable = false,
disable = {},
is_supported = queries.has_indents
}
is_supported = queries.has_indents,
},
}
local attached_buffers_by_module = caching.create_buffer_cache()
@ -53,11 +55,13 @@ local attached_buffers_by_module = caching.create_buffer_cache()
local function resolve_module(mod_name)
local config_mod = M.get_module(mod_name)
if not config_mod then return end
if not config_mod then
return
end
if type(config_mod.attach) == 'function' and type(config_mod.detach) == 'function' then
if type(config_mod.attach) == "function" and type(config_mod.detach) == "function" then
return config_mod
elseif type(config_mod.module_path) == 'string' then
elseif type(config_mod.module_path) == "string" then
return require(config_mod.module_path)
end
end
@ -92,7 +96,9 @@ end
-- @param mod path to module
local function enable_all(mod)
local config_mod = M.get_module(mod)
if not config_mod then return end
if not config_mod then
return
end
for _, bufnr in pairs(api.nvim_list_bufs()) do
enable_module(mod, bufnr)
@ -120,7 +126,7 @@ local function disable_mod_conf_autocmd(mod)
end
-- TODO(kyazdani): detach the correct autocmd... doesn't work when using %s, cmd.
-- This will remove all autocomands!
api.nvim_command("autocmd! NvimTreesitter FileType *")
api.nvim_command "autocmd! NvimTreesitter FileType *"
config_mod.loaded = false
end
@ -129,7 +135,9 @@ end
-- @param mod path to module
local function disable_all(mod)
local config_mod = M.get_module(mod)
if not config_mod or not config_mod.enable then return end
if not config_mod or not config_mod.enable then
return
end
for _, bufnr in pairs(api.nvim_list_bufs()) do
disable_module(mod, bufnr)
@ -158,7 +166,9 @@ end
-- @param mod path to module
local function toggle_all(mod)
local config_mod = M.get_module(mod)
if not config_mod then return end
if not config_mod then
return
end
if config_mod.enable then
disable_all(mod)
@ -175,11 +185,11 @@ local function recurse_modules(accumulator, root, path)
local root = root or config.modules
for name, module in pairs(root) do
local new_path = path and (path..'.'..name) or name
local new_path = path and (path .. "." .. name) or name
if M.is_module(module) then
accumulator(name, module, new_path, root)
elseif type(module) == 'table' then
elseif type(module) == "table" then
recurse_modules(accumulator, module, new_path)
end
end
@ -189,53 +199,56 @@ end
-- @param process_function function used as the `process` parameter
-- for vim.inspect (https://github.com/kikito/inspect.lua#optionsprocess)
local function config_info(process_function)
process_function = process_function or function(item, path)
if path[#path] == vim.inspect.METATABLE then return end
if path[#path] == "is_supported" then return end
return item
end
print(vim.inspect(config, {process = process_function}))
process_function = process_function
or function(item, path)
if path[#path] == vim.inspect.METATABLE then
return
end
if path[#path] == "is_supported" then
return
end
return item
end
print(vim.inspect(config, { process = process_function }))
end
function M.edit_query_file(query_group, lang)
lang = lang or parsers.get_buf_lang()
local files = ts_query.get_query_files(lang, query_group, true)
if #files == 0 then
vim.notify('No query file found! Creating a new one!')
vim.notify "No query file found! Creating a new one!"
M.edit_query_file_user_after(query_group, lang)
elseif #files == 1 then
vim.cmd(':edit '..files[1])
vim.cmd(":edit " .. files[1])
else
local counter = 0
local choices = {
'Select a file:',
"Select a file:",
unpack(vim.tbl_map(function(f)
counter = counter + 1
return counter..'. '..f
end,
files
))
counter = counter + 1
return counter .. ". " .. f
end, files)),
}
local choice = vim.fn.inputlist(choices)
if choice > 0 and choice <= #files then
vim.cmd(':edit '..files[choice])
vim.cmd(":edit " .. files[choice])
end
end
end
function M.edit_query_file_user_after(query_group, lang)
lang = lang or parsers.get_buf_lang()
local folder = utils.join_path(vim.fn.stdpath('config'), 'after', 'queries', lang)
local file = utils.join_path(folder, query_group..'.scm')
local folder = utils.join_path(vim.fn.stdpath "config", "after", "queries", lang)
local file = utils.join_path(folder, query_group .. ".scm")
if vim.fn.isdirectory(folder) ~= 1 then
local choice = vim.fn.inputlist({'"'..folder.." does not exist. Create it?", "1. Yes", "2. No"})
local choice = vim.fn.inputlist { '"' .. folder .. " does not exist. Create it?", "1. Yes", "2. No" }
if choice == 1 then
vim.fn.mkdir(folder, "p", "0755")
else
return
end
end
vim.cmd(':edit '..file)
vim.cmd(":edit " .. file)
end
M.commands = {
@ -311,14 +324,18 @@ function M.is_enabled(mod, lang)
end
local module_config = M.get_module(mod)
if not module_config then return false end
if not module_config then
return false
end
if not module_config.enable or not module_config.is_supported(lang) then
return false
end
for _, parser in pairs(module_config.disable) do
if lang == parser then return false end
if lang == parser then
return false
end
end
return true
@ -327,12 +344,12 @@ end
-- Setup call for users to override module configurations.
-- @param user_data module overrides
function M.setup(user_data)
config.modules = vim.tbl_deep_extend('force', config.modules, user_data)
config.modules = vim.tbl_deep_extend("force", config.modules, user_data)
config.ignore_install = user_data.ignore_install or {}
local ensure_installed = user_data.ensure_installed or {}
if #ensure_installed > 0 then
require'nvim-treesitter.install'.ensure_installed(ensure_installed)
require("nvim-treesitter.install").ensure_installed(ensure_installed)
end
config.modules.ensure_installed = nil
@ -381,7 +398,9 @@ function M.define_modules(mod_defs)
group[key] = vim.tbl_extend("keep", mod, {
enable = false,
disable = {},
is_supported = function() return true end
is_supported = function()
return true
end,
})
end, mod_defs)
@ -404,9 +423,7 @@ function M.attach_module(mod_name, bufnr, lang)
local lang = lang or parsers.get_buf_lang(bufnr)
local resolved_mod = resolve_module(mod_name)
if resolved_mod
and not attached_buffers_by_module.has(mod_name, bufnr)
and M.is_enabled(mod_name, lang) then
if resolved_mod and not attached_buffers_by_module.has(mod_name, bufnr) and M.is_enabled(mod_name, lang) then
attached_buffers_by_module.set(mod_name, bufnr, true)
resolved_mod.attach(bufnr, lang)
end
@ -450,9 +467,9 @@ end
-- A module should contain an attach and detach function.
-- @param mod the module table
function M.is_module(mod)
return type(mod) == 'table'
and ((type(mod.attach) == 'function' and type(mod.detach) == 'function')
or type(mod.module_path) == 'string')
return type(mod) == "table" and ((type(mod.attach) == "function" and type(mod.detach) == "function") or type(
mod.module_path
) == "string")
end
-- Initializes built-in modules and any queued modules