From 62786ec7c60ea29cbbd48ae658cde7042dba4bb3 Mon Sep 17 00:00:00 2001 From: kiyan42 Date: Wed, 22 Apr 2020 11:13:05 +0200 Subject: feat/refacto: improve configurations - You should now get the configs through functions - Configs for languages are now inside a local object called parsers - You can get the parser installation configurations with `get_parser_configs` - A new object has been initialized inside configs to specify module config (called config). - Provide functions to enable/disable a module on one buffer - Provide functions to enable/disable a module on all buffers, and if filetype is specified, for specific filetype - Provide function to determine if module is activated for a specified filetype --- lua/nvim-treesitter.lua | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) (limited to 'lua/nvim-treesitter.lua') diff --git a/lua/nvim-treesitter.lua b/lua/nvim-treesitter.lua index b053f39b0..731f55e5d 100644 --- a/lua/nvim-treesitter.lua +++ b/lua/nvim-treesitter.lua @@ -1,30 +1,28 @@ 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 highlight = require'nvim-treesitter.highlight' +local utils = require'nvim-treesitter.utils' +local info = require'nvim-treesitter.info' +local configs = require'nvim-treesitter.configs' 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) - if parsers.has_parser(lang) then - local autocmd = "autocmd NvimTreesitter FileType %s lua require'nvim-treesitter.highlight'.setup()" - api.nvim_command(string.format(autocmd, lang)) - end -end + utils.setup_commands('install', install.commands) + utils.setup_commands('info', info.commands) + utils.setup_commands('configs', configs.commands) --- This function initialize the plugin --- it is run at startup -M._root = {} -function M._root.setup() - install.setup() + for _, ft in pairs(configs.available_parsers()) do + for _, mod in pairs(configs.available_modules()) do + if parsers.has_parser(ft) and configs.is_enabled(mod, ft) then + local cmd = string.format("lua require'nvim-treesitter.%s'.attach()", mod) + api.nvim_command(string.format("autocmd FileType %s %s", ft, cmd)) + end + end + end end return M -- cgit v1.2.3-70-g09d2 From f489b4b0a337379973d6801805aa9b7800d2e6e8 Mon Sep 17 00:00:00 2001 From: kiyan42 Date: Sat, 25 Apr 2020 16:11:53 +0200 Subject: fix: config is enabled also checks parser existence --- lua/nvim-treesitter.lua | 3 +-- lua/nvim-treesitter/configs.lua | 12 +++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) (limited to 'lua/nvim-treesitter.lua') diff --git a/lua/nvim-treesitter.lua b/lua/nvim-treesitter.lua index 731f55e5d..e1c95ed31 100644 --- a/lua/nvim-treesitter.lua +++ b/lua/nvim-treesitter.lua @@ -1,5 +1,4 @@ local api = vim.api -local parsers = require'nvim-treesitter.parsers' local install = require'nvim-treesitter.install' local locals = require'nvim-treesitter.locals' local utils = require'nvim-treesitter.utils' @@ -17,7 +16,7 @@ function M.setup(lang) for _, ft in pairs(configs.available_parsers()) do for _, mod in pairs(configs.available_modules()) do - if parsers.has_parser(ft) and configs.is_enabled(mod, ft) then + if configs.is_enabled(mod, ft) then local cmd = string.format("lua require'nvim-treesitter.%s'.attach()", mod) api.nvim_command(string.format("autocmd FileType %s %s", ft, cmd)) end diff --git a/lua/nvim-treesitter/configs.lua b/lua/nvim-treesitter/configs.lua index c9450ed61..64d4170e2 100644 --- a/lua/nvim-treesitter/configs.lua +++ b/lua/nvim-treesitter/configs.lua @@ -206,7 +206,9 @@ local function enable_all(mod, ft) end end if ft then - enable_mod_conf_autocmd(mod, ft) + if parser_utils.has_parser(ft) then + enable_mod_conf_autocmd(mod, ft) + end else for _, ft in pairs(M.available_parsers()) do if parser_utils.has_parser(ft) then @@ -245,9 +247,7 @@ local function disable_all(mod, ft) disable_mod_conf_autocmd(mod, ft) else for _, ft in pairs(M.available_parsers()) do - if parser_utils.has_parser(ft) then - disable_mod_conf_autocmd(mod, ft) - end + disable_mod_conf_autocmd(mod, ft) end config[mod].enable = false end @@ -291,7 +291,9 @@ M.commands = { -- @param mod: module (string) -- @param ft: filetype (string) function M.is_enabled(mod, ft) - if not M.get_parser_configs()[ft] then return false end + if not M.get_parser_configs()[ft] or not parser_utils.has_parser(ft) then + return false + end local module_config = M.get_config()[mod] if not module_config then return false end -- cgit v1.2.3-70-g09d2 From 7682a1a49f7c3be58df18dfc18ad3ba65dd3be1f Mon Sep 17 00:00:00 2001 From: Thomas Vigouroux Date: Tue, 5 May 2020 15:04:01 +0200 Subject: feat: provide a statusline indicator It will show the current branch at the cursor going the tree as such. root->node->subnode->leaf If an argument is provided to `statusline`, then the tree will be truncated as follows : ..->subnode->subnode --- autoload/nvim_treesitter.vim | 3 +++ lua/nvim-treesitter.lua | 31 +++++++++++++++++++++++++++++++ lua/nvim-treesitter/parsers.lua | 3 ++- 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 autoload/nvim_treesitter.vim (limited to 'lua/nvim-treesitter.lua') diff --git a/autoload/nvim_treesitter.vim b/autoload/nvim_treesitter.vim new file mode 100644 index 000000000..715befe0f --- /dev/null +++ b/autoload/nvim_treesitter.vim @@ -0,0 +1,3 @@ +function! nvim_treesitter#statusline(len) + return luaeval("require'nvim-treesitter'.statusline(_A)", a:len) +endfunction diff --git a/lua/nvim-treesitter.lua b/lua/nvim-treesitter.lua index e1c95ed31..90b5468a3 100644 --- a/lua/nvim-treesitter.lua +++ b/lua/nvim-treesitter.lua @@ -24,4 +24,35 @@ function M.setup(lang) end end +function M.statusline(indicator_size) + local indicator_size = indicator_size or 1000 + local expr = require"nvim-treesitter.utils".expression_at_point() + local current_node = + require'nvim-treesitter.node_movement'.current_node[api.nvim_get_current_buf()] + + local indicator = "" + while expr and (#indicator + #(expr:type()) + 5) < indicator_size do + + local prefix = "" + if expr:parent() then + prefix = "->" + end + + + if expr == current_node then + indicator = string.format("%s[%s]%s", prefix, expr:type(), indicator) + else + indicator = prefix .. expr:type() .. indicator + end + + expr = expr:parent() + end + + if expr then + return "..." .. indicator + else + return indicator + end +end + return M diff --git a/lua/nvim-treesitter/parsers.lua b/lua/nvim-treesitter/parsers.lua index e046ca45c..07f3e9d34 100644 --- a/lua/nvim-treesitter/parsers.lua +++ b/lua/nvim-treesitter/parsers.lua @@ -5,11 +5,12 @@ local M = {} function M.has_parser(lang) local lang = lang or api.nvim_buf_get_option(0, 'filetype') + if not lang or #lang == 0 then return false end return #api.nvim_get_runtime_file('parser/' .. lang .. '.*', false) > 0 end function M.get_parser(bufnr, lang) - if M.has_parser() then + if M.has_parser(lang) then local buf = bufnr or api.nvim_get_current_buf() local lang = lang or api.nvim_buf_get_option(buf, 'ft') if not M[buf] then -- cgit v1.2.3-70-g09d2