diff options
| author | Thomas Vigouroux <39092278+vigoux@users.noreply.github.com> | 2020-04-22 09:14:18 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-04-22 09:14:18 +0200 |
| commit | e2a25a313520df8bc259b3b547a7482438645ffe (patch) | |
| tree | b569c95e6d32e24c94012303fd79f284880eab52 /lua | |
| parent | Merge pull request #16 from kyazdani42/fix-compiling-parser (diff) | |
| parent | feat/refacto: add configs.lua, setup install (diff) | |
| download | nvim-treesitter-e2a25a313520df8bc259b3b547a7482438645ffe.tar nvim-treesitter-e2a25a313520df8bc259b3b547a7482438645ffe.tar.gz nvim-treesitter-e2a25a313520df8bc259b3b547a7482438645ffe.tar.bz2 nvim-treesitter-e2a25a313520df8bc259b3b547a7482438645ffe.tar.lz nvim-treesitter-e2a25a313520df8bc259b3b547a7482438645ffe.tar.xz nvim-treesitter-e2a25a313520df8bc259b3b547a7482438645ffe.tar.zst nvim-treesitter-e2a25a313520df8bc259b3b547a7482438645ffe.zip | |
Merge pull request #17 from kyazdani42/add-configs-ft
feat/refacto: add configs.lua, setup install cmd
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/nvim-treesitter.lua | 15 | ||||
| -rw-r--r-- | lua/nvim-treesitter/configs.lua | 88 | ||||
| -rw-r--r-- | lua/nvim-treesitter/health.lua | 30 | ||||
| -rw-r--r-- | lua/nvim-treesitter/install.lua | 151 |
4 files changed, 163 insertions, 121 deletions
diff --git a/lua/nvim-treesitter.lua b/lua/nvim-treesitter.lua index 8628e499b..8f2fa05ed 100644 --- a/lua/nvim-treesitter.lua +++ b/lua/nvim-treesitter.lua @@ -1,10 +1,15 @@ local api = vim.api local parsers = require'nvim-treesitter.parsers' +local configs = require 'nvim-treesitter.configs' local install = require'nvim-treesitter.install' local locals = require'nvim-treesitter.locals' local M = {} +function M.available_parsers() + return vim.tbl_keys(configs.repositories) +end + -- This function sets up everythin needed for a given language -- this is the main interface through the plugin function M.setup(lang) @@ -12,9 +17,11 @@ function M.setup(lang) end end --- To install, run `:lua require'nvim-treesitter'.install_parser('language')` --- we should add a vim layer over the lua function -M.install_parser = install.install_parser -M.list_parsers = install.list_parsers +-- This function initialize the plugin +-- it is run at startup +M._root = {} +function M._root.setup() + install.setup() +end return M diff --git a/lua/nvim-treesitter/configs.lua b/lua/nvim-treesitter/configs.lua new file mode 100644 index 000000000..fd3b41d29 --- /dev/null +++ b/lua/nvim-treesitter/configs.lua @@ -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 diff --git a/lua/nvim-treesitter/health.lua b/lua/nvim-treesitter/health.lua index b5e869804..25da7fe35 100644 --- a/lua/nvim-treesitter/health.lua +++ b/lua/nvim-treesitter/health.lua @@ -1,8 +1,9 @@ local api = vim.api +local fn = vim.fn -local install = require'nvim-treesitter.install' local queries = require'nvim-treesitter.query' local locals = require'nvim-treesitter.locals' +local configs = require'nvim-treesitter.configs' local health_start = vim.fn["health#report_start"] local health_ok = vim.fn['health#report_ok'] @@ -12,15 +13,36 @@ local health_error = vim.fn['health#report_error'] 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 function M.checkhealth() -- Installation dependency checks health_start('Installation') - install.checkhealth() + configs_health() local missing_parsers = {} -- 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) -- 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 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 diff --git a/lua/nvim-treesitter/install.lua b/lua/nvim-treesitter/install.lua index 70e9aee2b..fcad58912 100644 --- a/lua/nvim-treesitter/install.lua +++ b/lua/nvim-treesitter/install.lua @@ -1,92 +1,9 @@ local api = vim.api local fn = vim.fn local luv = vim.loop +local repositories = require'nvim-treesitter/configs'.repositories 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() 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 -- TODO(kyazdani): this should work on windows too -function M.install_parser(ft) +local function install(ft) if fn.has('win32') == 1 then return api.nvim_err_writeln('This command is not available on windows at the moment.') end @@ -200,11 +117,16 @@ function M.install_parser(ft) if not string.match(yesno, '^y.*') then return end end - local repository = M.repositories[ft] + local repository = repositories[ft] if not repository then return api.nvim_err_writeln('Parser not available for language '..ft) end + vim.validate { + url={ repository.url, 'string' }, + files={ repository.files, 'table' } + } + if fn.executable('git') == 0 then return api.nvim_err_writeln('Git is required on your system to run this command') end @@ -218,33 +140,7 @@ function M.install_parser(ft) run_install(cache_folder, package_path, ft, repository) end -function M.checkhealth() - 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 function install_info() local max_len = 0 for parser_name, _ in pairs(repositories) do if #parser_name > max_len then max_len = #parser_name end @@ -261,4 +157,33 @@ function M.list_parsers() 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 |
