aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuis Carlos Cruz Carballo <lcruzc@linkux-it.com>2020-11-30 21:30:07 -0400
committerKiyan <yazdani.kiyan@protonmail.com>2020-12-09 19:11:41 +0100
commit76486942e79973095795e8699f95bb509e063d0d (patch)
tree28cf4ff15c1c67e7f125be5a211b46674e3bba2b
parentUpdate Elm parser (diff)
downloadnvim-treesitter-76486942e79973095795e8699f95bb509e063d0d.tar
nvim-treesitter-76486942e79973095795e8699f95bb509e063d0d.tar.gz
nvim-treesitter-76486942e79973095795e8699f95bb509e063d0d.tar.bz2
nvim-treesitter-76486942e79973095795e8699f95bb509e063d0d.tar.lz
nvim-treesitter-76486942e79973095795e8699f95bb509e063d0d.tar.xz
nvim-treesitter-76486942e79973095795e8699f95bb509e063d0d.tar.zst
nvim-treesitter-76486942e79973095795e8699f95bb509e063d0d.zip
Allow user configure parsers using metatables
-rw-r--r--README.md13
-rw-r--r--lua/nvim-treesitter/parsers.lua39
2 files changed, 40 insertions, 12 deletions
diff --git a/README.md b/README.md
index 03466ba02..ba3b45a71 100644
--- a/README.md
+++ b/README.md
@@ -275,6 +275,19 @@ EOF
You can also skip step 2 and use `:TSInstallFromGrammar zimbu` to install directly from a `grammar.js` in the top-level directory specified by `url`.
Once the parser is installed, you can update it (from the latest revision of the `main` branch if `url` is a Github repository) with `:TSUpdate zimbu`.
+## Update parsers used_by
+
+Sometimes needs to use some parser for different filetype.
+
+Add the following snippet to your `init.vim`:
+
+```vim
+lua <<EOF
+local parser_config = require "nvim-treesitter.parsers".get_parser_configs()
+parser_config.typescript.used_by = "javascriptflow"
+EOF
+```
+
## Adding queries
Queries are what `nvim-treesitter` uses to extract informations from the syntax tree; they are
diff --git a/lua/nvim-treesitter/parsers.lua b/lua/nvim-treesitter/parsers.lua
index e43d2ff04..7b10f1ca6 100644
--- a/lua/nvim-treesitter/parsers.lua
+++ b/lua/nvim-treesitter/parsers.lua
@@ -1,7 +1,33 @@
local api = vim.api
local ts = vim.treesitter
-local list = {}
+local ft_to_parsername = {}
+
+local function update_ft_to_parsername(name, parser)
+ if type(parser.used_by) == 'table' then
+ for _, ft in pairs(parser.used_by) do
+ ft_to_parsername[ft] = name
+ end
+ end
+ ft_to_parsername[parser.filetype or name] = name
+end
+
+local list = setmetatable({}, {
+ __newindex = function(table, parsername, parserconfig)
+
+ rawset(table, parsername, setmetatable(parserconfig, {
+ __newindex = function(parserconfigtable, key, value)
+ if key == "used_by" then
+ ft_to_parsername[value] = parsername
+ else
+ rawset(parserconfigtable, key, value)
+ end
+ end
+ }))
+
+ update_ft_to_parsername(parsername, parserconfig)
+ end
+})
list.javascript = {
install_info = {
@@ -349,17 +375,6 @@ local M = {
list = list
}
-local ft_to_parsername = {}
-
-for name, obj in pairs(M.list) do
- if type(obj.used_by) == 'table' then
- for _, ft in pairs(obj.used_by) do
- ft_to_parsername[ft] = name
- end
- end
- ft_to_parsername[obj.filetype or name] = name
-end
-
function M.ft_to_lang(ft)
return ft_to_parsername[ft] or ft
end