mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-13 08:50:11 -04:00
feat/refacto: add configs.lua, setup install
- configs.lua holds the `repositories` data - install health moved to health.lua - plugins loads _root.setup() on startup - install and list command are available through vim > use them with `:TSInstall lang` and `:TSInstallInfo`
This commit is contained in:
parent
37932fc3d3
commit
62ce7744db
5 changed files with 170 additions and 121 deletions
|
|
@ -1,10 +1,15 @@
|
||||||
local api = vim.api
|
local api = vim.api
|
||||||
local parsers = require'nvim-treesitter.parsers'
|
local parsers = require'nvim-treesitter.parsers'
|
||||||
|
local configs = require 'nvim-treesitter.configs'
|
||||||
local install = require'nvim-treesitter.install'
|
local install = require'nvim-treesitter.install'
|
||||||
local locals = require'nvim-treesitter.locals'
|
local locals = require'nvim-treesitter.locals'
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
|
function M.available_parsers()
|
||||||
|
return vim.tbl_keys(configs.repositories)
|
||||||
|
end
|
||||||
|
|
||||||
-- This function sets up everythin needed for a given language
|
-- This function sets up everythin needed for a given language
|
||||||
-- this is the main interface through the plugin
|
-- this is the main interface through the plugin
|
||||||
function M.setup(lang)
|
function M.setup(lang)
|
||||||
|
|
@ -12,9 +17,11 @@ function M.setup(lang)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- To install, run `:lua require'nvim-treesitter'.install_parser('language')`
|
-- This function initialize the plugin
|
||||||
-- we should add a vim layer over the lua function
|
-- it is run at startup
|
||||||
M.install_parser = install.install_parser
|
M._root = {}
|
||||||
M.list_parsers = install.list_parsers
|
function M._root.setup()
|
||||||
|
install.setup()
|
||||||
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
||||||
88
lua/nvim-treesitter/configs.lua
Normal file
88
lua/nvim-treesitter/configs.lua
Normal file
|
|
@ -0,0 +1,88 @@
|
||||||
|
local M = {}
|
||||||
|
|
||||||
|
M.repositories = {
|
||||||
|
javascript = {
|
||||||
|
url = "https://github.com/tree-sitter/tree-sitter-javascript",
|
||||||
|
files = { "src/parser.c", "src/scanner.c" },
|
||||||
|
},
|
||||||
|
c = {
|
||||||
|
url = "https://github.com/tree-sitter/tree-sitter-c",
|
||||||
|
files = { "src/parser.c" }
|
||||||
|
},
|
||||||
|
cpp = {
|
||||||
|
url = "https://github.com/tree-sitter/tree-sitter-cpp",
|
||||||
|
files = { "src/parser.c", "src/scanner.cc" }
|
||||||
|
},
|
||||||
|
rust = {
|
||||||
|
url = "https://github.com/tree-sitter/tree-sitter-rust",
|
||||||
|
files = { "src/parser.c", "src/scanner.c" },
|
||||||
|
},
|
||||||
|
lua = {
|
||||||
|
url = "https://github.com/nvim-treesitter/tree-sitter-lua",
|
||||||
|
files = { "src/parser.c", "src/scanner.cc" }
|
||||||
|
},
|
||||||
|
python = {
|
||||||
|
url = "https://github.com/tree-sitter/tree-sitter-python",
|
||||||
|
files = { "src/parser.c", "src/scanner.cc" },
|
||||||
|
},
|
||||||
|
go = {
|
||||||
|
url = "https://github.com/tree-sitter/tree-sitter-go",
|
||||||
|
files = { "src/parser.c" },
|
||||||
|
},
|
||||||
|
ruby = {
|
||||||
|
url = "https://github.com/tree-sitter/tree-sitter-ruby",
|
||||||
|
files = { "src/parser.c", "src/scanner.cc" },
|
||||||
|
},
|
||||||
|
bash = {
|
||||||
|
url = "https://github.com/tree-sitter/tree-sitter-bash",
|
||||||
|
files = { "src/parser.c", "src/scanner.cc" },
|
||||||
|
},
|
||||||
|
php = {
|
||||||
|
url = "https://github.com/tree-sitter/tree-sitter-php",
|
||||||
|
files = { "src/parser.c", "src/scanner.cc" },
|
||||||
|
},
|
||||||
|
java = {
|
||||||
|
url = "https://github.com/tree-sitter/tree-sitter-java",
|
||||||
|
files = { "src/parser.c" },
|
||||||
|
},
|
||||||
|
html = {
|
||||||
|
url = "https://github.com/tree-sitter/tree-sitter-html",
|
||||||
|
files = { "src/parser.c", "src/scanner.cc" },
|
||||||
|
},
|
||||||
|
julia = {
|
||||||
|
url = "https://github.com/tree-sitter/tree-sitter-julia",
|
||||||
|
files = { "src/parser.c", "src/scanner.c" },
|
||||||
|
},
|
||||||
|
json = {
|
||||||
|
url = "https://github.com/tree-sitter/tree-sitter-json",
|
||||||
|
files = { "src/parser.c" },
|
||||||
|
},
|
||||||
|
css = {
|
||||||
|
url = "https://github.com/tree-sitter/tree-sitter-css",
|
||||||
|
files = { "src/parser.c", "src/scanner.c" },
|
||||||
|
},
|
||||||
|
ocaml = {
|
||||||
|
url = "https://github.com/tree-sitter/tree-sitter-ocaml",
|
||||||
|
files = { "src/parser.c", "src/scanner.cc" },
|
||||||
|
},
|
||||||
|
swift = {
|
||||||
|
url = "https://github.com/tree-sitter/tree-sitter-swift",
|
||||||
|
files = { "src/parser.c" },
|
||||||
|
},
|
||||||
|
csharp = {
|
||||||
|
url = "https://github.com/tree-sitter/tree-sitter-c-sharp",
|
||||||
|
files = { "src/parser.c", "src/scanner.c" },
|
||||||
|
},
|
||||||
|
typescript = {
|
||||||
|
url = "https://github.com/tree-sitter/tree-sitter-typescript",
|
||||||
|
files = { "src/parser.c", "src/scanner.c" },
|
||||||
|
location = "tree-sitter-typescript/typescript"
|
||||||
|
},
|
||||||
|
tsx = {
|
||||||
|
url = "https://github.com/tree-sitter/tree-sitter-typescript",
|
||||||
|
files = { "src/parser.c", "src/scanner.c" },
|
||||||
|
location = "tree-sitter-tsx/tsx"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return M
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
local api = vim.api
|
local api = vim.api
|
||||||
|
local fn = vim.fn
|
||||||
|
|
||||||
local install = require'nvim-treesitter.install'
|
|
||||||
local queries = require'nvim-treesitter.query'
|
local queries = require'nvim-treesitter.query'
|
||||||
local locals = require'nvim-treesitter.locals'
|
local locals = require'nvim-treesitter.locals'
|
||||||
|
local configs = require'nvim-treesitter.configs'
|
||||||
|
|
||||||
local health_start = vim.fn["health#report_start"]
|
local health_start = vim.fn["health#report_start"]
|
||||||
local health_ok = vim.fn['health#report_ok']
|
local health_ok = vim.fn['health#report_ok']
|
||||||
|
|
@ -12,15 +13,36 @@ local health_error = vim.fn['health#report_error']
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
|
local function configs_health()
|
||||||
|
if fn.executable('git') == 0 then
|
||||||
|
health_error('`git` executable not found.', {
|
||||||
|
'Install it with your package manager.',
|
||||||
|
'Check that your `$PATH` is set correctly.'
|
||||||
|
})
|
||||||
|
else
|
||||||
|
health_ok('`git` executable found.')
|
||||||
|
end
|
||||||
|
|
||||||
|
if fn.executable('cc') == 0 then
|
||||||
|
health_error('`cc` executable not found.', {
|
||||||
|
'Install `gcc` with your package manager.',
|
||||||
|
'Install `clang` with your package manager.',
|
||||||
|
'Check that your `$PATH` is set correctly.'
|
||||||
|
})
|
||||||
|
else
|
||||||
|
health_ok('`cc` executable found.')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-- TODO(vigoux): Maybe we should move each check to be perform in its own module
|
-- TODO(vigoux): Maybe we should move each check to be perform in its own module
|
||||||
function M.checkhealth()
|
function M.checkhealth()
|
||||||
-- Installation dependency checks
|
-- Installation dependency checks
|
||||||
health_start('Installation')
|
health_start('Installation')
|
||||||
install.checkhealth()
|
configs_health()
|
||||||
|
|
||||||
local missing_parsers = {}
|
local missing_parsers = {}
|
||||||
-- Parser installation checks
|
-- Parser installation checks
|
||||||
for parser_name, repo in pairs(install.repositories) do
|
for parser_name in pairs(configs.repositories) do
|
||||||
local installed = #api.nvim_get_runtime_file('parser/'..parser_name..'.so', false)
|
local installed = #api.nvim_get_runtime_file('parser/'..parser_name..'.so', false)
|
||||||
|
|
||||||
-- Only print informations about installed parsers
|
-- Only print informations about installed parsers
|
||||||
|
|
@ -42,7 +64,7 @@ function M.checkhealth()
|
||||||
|
|
||||||
-- TODO(vigoux): The installation command should be changed so that its easier to find
|
-- TODO(vigoux): The installation command should be changed so that its easier to find
|
||||||
health_warn('Some parsers are not installed:\n' .. table.concat(missing_parsers, '\n'), {
|
health_warn('Some parsers are not installed:\n' .. table.concat(missing_parsers, '\n'), {
|
||||||
"Install them using `:lua require'nvim-treesitter'.install_parser('language')`"})
|
"Install them using `:TSInstall language"})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,92 +1,9 @@
|
||||||
local api = vim.api
|
local api = vim.api
|
||||||
local fn = vim.fn
|
local fn = vim.fn
|
||||||
local luv = vim.loop
|
local luv = vim.loop
|
||||||
|
local repositories = require'nvim-treesitter/configs'.repositories
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
M.repositories = {
|
|
||||||
javascript = {
|
|
||||||
url = "https://github.com/tree-sitter/tree-sitter-javascript",
|
|
||||||
files = { "src/parser.c", "src/scanner.c" },
|
|
||||||
},
|
|
||||||
c = {
|
|
||||||
url = "https://github.com/tree-sitter/tree-sitter-c",
|
|
||||||
files = { "src/parser.c" }
|
|
||||||
},
|
|
||||||
cpp = {
|
|
||||||
url = "https://github.com/tree-sitter/tree-sitter-cpp",
|
|
||||||
files = { "src/parser.c", "src/scanner.cc" }
|
|
||||||
},
|
|
||||||
rust = {
|
|
||||||
url = "https://github.com/tree-sitter/tree-sitter-rust",
|
|
||||||
files = { "src/parser.c", "src/scanner.c" },
|
|
||||||
},
|
|
||||||
lua = {
|
|
||||||
url = "https://github.com/nvim-treesitter/tree-sitter-lua",
|
|
||||||
files = { "src/parser.c", "src/scanner.cc" }
|
|
||||||
},
|
|
||||||
python = {
|
|
||||||
url = "https://github.com/tree-sitter/tree-sitter-python",
|
|
||||||
files = { "src/parser.c", "src/scanner.cc" },
|
|
||||||
},
|
|
||||||
go = {
|
|
||||||
url = "https://github.com/tree-sitter/tree-sitter-go",
|
|
||||||
files = { "src/parser.c" },
|
|
||||||
},
|
|
||||||
ruby = {
|
|
||||||
url = "https://github.com/tree-sitter/tree-sitter-ruby",
|
|
||||||
files = { "src/parser.c", "src/scanner.cc" },
|
|
||||||
},
|
|
||||||
bash = {
|
|
||||||
url = "https://github.com/tree-sitter/tree-sitter-bash",
|
|
||||||
files = { "src/parser.c", "src/scanner.cc" },
|
|
||||||
},
|
|
||||||
php = {
|
|
||||||
url = "https://github.com/tree-sitter/tree-sitter-php",
|
|
||||||
files = { "src/parser.c", "src/scanner.cc" },
|
|
||||||
},
|
|
||||||
java = {
|
|
||||||
url = "https://github.com/tree-sitter/tree-sitter-java",
|
|
||||||
files = { "src/parser.c" },
|
|
||||||
},
|
|
||||||
html = {
|
|
||||||
url = "https://github.com/tree-sitter/tree-sitter-html",
|
|
||||||
files = { "src/parser.c", "src/scanner.cc" },
|
|
||||||
},
|
|
||||||
julia = {
|
|
||||||
url = "https://github.com/tree-sitter/tree-sitter-julia",
|
|
||||||
files = { "src/parser.c", "src/scanner.c" },
|
|
||||||
},
|
|
||||||
json = {
|
|
||||||
url = "https://github.com/tree-sitter/tree-sitter-json",
|
|
||||||
files = { "src/parser.c" },
|
|
||||||
},
|
|
||||||
css = {
|
|
||||||
url = "https://github.com/tree-sitter/tree-sitter-css",
|
|
||||||
files = { "src/parser.c", "src/scanner.c" },
|
|
||||||
},
|
|
||||||
ocaml = {
|
|
||||||
url = "https://github.com/tree-sitter/tree-sitter-ocaml",
|
|
||||||
files = { "src/parser.c", "src/scanner.cc" },
|
|
||||||
},
|
|
||||||
swift = {
|
|
||||||
url = "https://github.com/tree-sitter/tree-sitter-swift",
|
|
||||||
files = { "src/parser.c" },
|
|
||||||
},
|
|
||||||
csharp = {
|
|
||||||
url = "https://github.com/tree-sitter/tree-sitter-c-sharp",
|
|
||||||
files = { "src/parser.c", "src/scanner.c" },
|
|
||||||
},
|
|
||||||
typescript = {
|
|
||||||
url = "https://github.com/tree-sitter/tree-sitter-typescript",
|
|
||||||
files = { "src/parser.c", "src/scanner.c" },
|
|
||||||
location = "tree-sitter-typescript/typescript"
|
|
||||||
},
|
|
||||||
tsx = {
|
|
||||||
url = "https://github.com/tree-sitter/tree-sitter-typescript",
|
|
||||||
files = { "src/parser.c", "src/scanner.c" },
|
|
||||||
location = "tree-sitter-tsx/tsx"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
local function get_package_path()
|
local function get_package_path()
|
||||||
for _, path in pairs(api.nvim_list_runtime_paths()) do
|
for _, path in pairs(api.nvim_list_runtime_paths()) do
|
||||||
|
|
@ -185,7 +102,7 @@ local function run_install(cache_folder, package_path, ft, repo)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- TODO(kyazdani): this should work on windows too
|
-- TODO(kyazdani): this should work on windows too
|
||||||
function M.install_parser(ft)
|
local function install(ft)
|
||||||
if fn.has('win32') == 1 then
|
if fn.has('win32') == 1 then
|
||||||
return api.nvim_err_writeln('This command is not available on windows at the moment.')
|
return api.nvim_err_writeln('This command is not available on windows at the moment.')
|
||||||
end
|
end
|
||||||
|
|
@ -200,11 +117,16 @@ function M.install_parser(ft)
|
||||||
if not string.match(yesno, '^y.*') then return end
|
if not string.match(yesno, '^y.*') then return end
|
||||||
end
|
end
|
||||||
|
|
||||||
local repository = M.repositories[ft]
|
local repository = repositories[ft]
|
||||||
if not repository then
|
if not repository then
|
||||||
return api.nvim_err_writeln('Parser not available for language '..ft)
|
return api.nvim_err_writeln('Parser not available for language '..ft)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
vim.validate {
|
||||||
|
url={ repository.url, 'string' },
|
||||||
|
files={ repository.files, 'table' }
|
||||||
|
}
|
||||||
|
|
||||||
if fn.executable('git') == 0 then
|
if fn.executable('git') == 0 then
|
||||||
return api.nvim_err_writeln('Git is required on your system to run this command')
|
return api.nvim_err_writeln('Git is required on your system to run this command')
|
||||||
end
|
end
|
||||||
|
|
@ -218,33 +140,7 @@ function M.install_parser(ft)
|
||||||
run_install(cache_folder, package_path, ft, repository)
|
run_install(cache_folder, package_path, ft, repository)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.checkhealth()
|
local function install_info()
|
||||||
local health_ok = vim.fn['health#report_ok']
|
|
||||||
local health_info = vim.fn['health#report_info']
|
|
||||||
local health_warn = vim.fn['health#report_warn']
|
|
||||||
local health_error = vim.fn['health#report_error']
|
|
||||||
|
|
||||||
if fn.executable('git') == 0 then
|
|
||||||
health_error('`git` executable not found.', {
|
|
||||||
'Install it with your package manager.',
|
|
||||||
'Check that your `$PATH` is set correctly.'
|
|
||||||
})
|
|
||||||
else
|
|
||||||
health_ok('`git` executable found.')
|
|
||||||
end
|
|
||||||
|
|
||||||
if fn.executable('cc') == 0 then
|
|
||||||
health_error('`cc` executable not found.', {
|
|
||||||
'Install `gcc` with your package manager.',
|
|
||||||
'Install `clang` with your package manager.',
|
|
||||||
'Check that your `$PATH` is set correctly.'
|
|
||||||
})
|
|
||||||
else
|
|
||||||
health_ok('`cc` executable found.')
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function M.list_parsers()
|
|
||||||
local max_len = 0
|
local max_len = 0
|
||||||
for parser_name, _ in pairs(repositories) do
|
for parser_name, _ in pairs(repositories) do
|
||||||
if #parser_name > max_len then max_len = #parser_name end
|
if #parser_name > max_len then max_len = #parser_name end
|
||||||
|
|
@ -261,4 +157,33 @@ function M.list_parsers()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
M.commands = {
|
||||||
|
TSInstall = {
|
||||||
|
run = install,
|
||||||
|
args = {
|
||||||
|
"-nargs=1",
|
||||||
|
"-complete=custom,v:lua.ts_installable_parsers"
|
||||||
|
},
|
||||||
|
description = '`:TSInstall {ft}` installs a parser under nvim-treesitter/parser/{name}.so'
|
||||||
|
},
|
||||||
|
TSInstallInfo = {
|
||||||
|
run = install_info,
|
||||||
|
args = { "-nargs=0" },
|
||||||
|
description = '`:TSInstallInfo` print installation state for every filetype'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function M.setup()
|
||||||
|
for command_name, def in pairs(M.commands) do
|
||||||
|
local call_fn = string.format("lua require'nvim-treesitter.install'.commands.%s.run(<f-args>)", command_name)
|
||||||
|
local parts = vim.tbl_flatten({
|
||||||
|
"command!",
|
||||||
|
def.args,
|
||||||
|
command_name,
|
||||||
|
call_fn,
|
||||||
|
})
|
||||||
|
api.nvim_command(table.concat(parts, " "))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,13 @@ if exists('g:loaded_nvim_treesitter')
|
||||||
finish
|
finish
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
lua << EOF
|
||||||
|
ts_installable_parsers = function()
|
||||||
|
return table.concat(require'nvim-treesitter'.available_parsers(), '\n')
|
||||||
|
end
|
||||||
|
require'nvim-treesitter'._root.setup()
|
||||||
|
EOF
|
||||||
|
|
||||||
let g:loaded_nvim_treesitter = 1
|
let g:loaded_nvim_treesitter = 1
|
||||||
|
|
||||||
augroup NvimTreesitter
|
augroup NvimTreesitter
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue