aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkiyan42 <yazdani.kiyan@protonmail.com>2020-04-19 14:43:55 +0200
committerkiyan42 <yazdani.kiyan@protonmail.com>2020-04-19 15:13:38 +0200
commit2967ca5203cd3a3d0315b2101c66e4f0ee5f421a (patch)
treee9c4e72352f6dc110a8cb58423f0a82accb6035f
parentMerge pull request #1 from vigoux/master (diff)
downloadnvim-treesitter-2967ca5203cd3a3d0315b2101c66e4f0ee5f421a.tar
nvim-treesitter-2967ca5203cd3a3d0315b2101c66e4f0ee5f421a.tar.gz
nvim-treesitter-2967ca5203cd3a3d0315b2101c66e4f0ee5f421a.tar.bz2
nvim-treesitter-2967ca5203cd3a3d0315b2101c66e4f0ee5f421a.tar.lz
nvim-treesitter-2967ca5203cd3a3d0315b2101c66e4f0ee5f421a.tar.xz
nvim-treesitter-2967ca5203cd3a3d0315b2101c66e4f0ee5f421a.tar.zst
nvim-treesitter-2967ca5203cd3a3d0315b2101c66e4f0ee5f421a.zip
add parser installer
-rw-r--r--lua/nvim-treesitter.lua5
-rw-r--r--lua/nvim-treesitter/install.lua74
-rw-r--r--parser/.gitignore2
3 files changed, 81 insertions, 0 deletions
diff --git a/lua/nvim-treesitter.lua b/lua/nvim-treesitter.lua
index ab3edf071..abb3b6d91 100644
--- a/lua/nvim-treesitter.lua
+++ b/lua/nvim-treesitter.lua
@@ -1,5 +1,6 @@
local api = vim.api
local parsers = require'nvim-treesitter.parsers'
+local install = require'nvim-treesitter.install'
local M = {}
@@ -10,4 +11,8 @@ 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
+
return M
diff --git a/lua/nvim-treesitter/install.lua b/lua/nvim-treesitter/install.lua
new file mode 100644
index 000000000..2f40db11c
--- /dev/null
+++ b/lua/nvim-treesitter/install.lua
@@ -0,0 +1,74 @@
+local api = vim.api
+local fn = vim.fn
+
+local M = {}
+local package_path = nil
+local 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"
+ }
+}
+
+-- TODO(kyazdani): there might be a better way to get the plugin path
+for _, path in pairs(api.nvim_list_runtime_paths()) do
+ if string.match(path, '.*/nvim%-treesitter') then
+ package_path = path
+ break
+ end
+end
+
+local function create_compile_command(ft, path, files)
+ -- TODO(kyazdani): should download the parser in $XDG_CACHE_HOME instead of /tmp
+ return "cd /tmp/tree-sitter-"..ft..[[ && \
+ gcc -o parser.so -shared ]]..files..[[ -Os -I./src && \
+ mv parser.so ]]..path.."/parsers/"..ft..[[.so && \
+ rm -rf /tmp/tree-sitter-]]..ft
+end
+
+local function create_download_command(url)
+ return "cd /tmp && git clone "..url
+end
+
+function M.install_parser(lang)
+ if fn.has('win32') == 1 then
+ -- TODO(kyazdani): this should work on windows too
+ api.nvim_err_writeln('This command is not available on windows at the moment.')
+ return
+ end
+
+ if not lang then
+ api.nvim_err_writeln("usage: install_parser('language')")
+ return
+ end
+
+ local repository = repositories[lang]
+ if not repository then
+ api.nvim_err_writeln('Parser not available for language '..lang)
+ return
+ end
+
+ if not package_path then
+ api.nvim_err_writeln('Plugin runtime path not found.')
+ return
+ end
+
+ if not fn.executable('git') == 1 then
+ api.nvim_err_writeln('Please install `git` to download parsers.')
+ return
+ end
+
+ print("Downloading parser...")
+ fn.system(create_download_command(repository.url))
+
+ print("Compiling parser...")
+ fn.system(create_compile_command(lang, package_path, repository.files))
+
+ print("Install for the " ..lang.." parser was successfull")
+end
+
+return M
diff --git a/parser/.gitignore b/parser/.gitignore
new file mode 100644
index 000000000..d6b7ef32c
--- /dev/null
+++ b/parser/.gitignore
@@ -0,0 +1,2 @@
+*
+!.gitignore