2020-04-22 11:13:05 +02:00
local api = vim.api
local queries = require ' nvim-treesitter.query '
local parser_utils = require ' nvim-treesitter.parsers '
local parsers = { }
2020-04-21 23:40:23 +02:00
2020-04-22 11:13:05 +02:00
parsers.javascript = {
install_info = {
2020-04-21 23:40:23 +02:00
url = " https://github.com/tree-sitter/tree-sitter-javascript " ,
files = { " src/parser.c " , " src/scanner.c " } ,
2020-04-22 11:13:05 +02:00
}
}
parsers.c = {
install_info = {
2020-04-21 23:40:23 +02:00
url = " https://github.com/tree-sitter/tree-sitter-c " ,
files = { " src/parser.c " }
2020-04-22 11:13:05 +02:00
}
}
parsers.cpp = {
install_info = {
2020-04-21 23:40:23 +02:00
url = " https://github.com/tree-sitter/tree-sitter-cpp " ,
files = { " src/parser.c " , " src/scanner.cc " }
2020-04-22 11:13:05 +02:00
}
}
parsers.rust = {
install_info = {
2020-04-21 23:40:23 +02:00
url = " https://github.com/tree-sitter/tree-sitter-rust " ,
files = { " src/parser.c " , " src/scanner.c " } ,
2020-04-22 11:13:05 +02:00
}
}
parsers.lua = {
install_info = {
2020-04-21 23:40:23 +02:00
url = " https://github.com/nvim-treesitter/tree-sitter-lua " ,
files = { " src/parser.c " , " src/scanner.cc " }
2020-04-22 11:13:05 +02:00
}
}
parsers.python = {
install_info = {
2020-04-21 23:40:23 +02:00
url = " https://github.com/tree-sitter/tree-sitter-python " ,
files = { " src/parser.c " , " src/scanner.cc " } ,
2020-04-22 11:13:05 +02:00
}
}
parsers.go = {
install_info = {
2020-04-21 23:40:23 +02:00
url = " https://github.com/tree-sitter/tree-sitter-go " ,
files = { " src/parser.c " } ,
2020-04-22 11:13:05 +02:00
}
}
parsers.ruby = {
install_info = {
2020-04-21 23:40:23 +02:00
url = " https://github.com/tree-sitter/tree-sitter-ruby " ,
files = { " src/parser.c " , " src/scanner.cc " } ,
2020-04-22 11:13:05 +02:00
}
}
parsers.bash = {
install_info = {
2020-04-21 23:40:23 +02:00
url = " https://github.com/tree-sitter/tree-sitter-bash " ,
files = { " src/parser.c " , " src/scanner.cc " } ,
2020-04-22 11:13:05 +02:00
}
}
parsers.php = {
install_info = {
2020-04-21 23:40:23 +02:00
url = " https://github.com/tree-sitter/tree-sitter-php " ,
files = { " src/parser.c " , " src/scanner.cc " } ,
2020-04-22 11:13:05 +02:00
}
}
parsers.java = {
install_info = {
2020-04-21 23:40:23 +02:00
url = " https://github.com/tree-sitter/tree-sitter-java " ,
files = { " src/parser.c " } ,
2020-04-22 11:13:05 +02:00
}
}
parsers.html = {
install_info = {
2020-04-21 23:40:23 +02:00
url = " https://github.com/tree-sitter/tree-sitter-html " ,
files = { " src/parser.c " , " src/scanner.cc " } ,
2020-04-22 11:13:05 +02:00
}
}
parsers.julia = {
install_info = {
2020-04-21 23:40:23 +02:00
url = " https://github.com/tree-sitter/tree-sitter-julia " ,
files = { " src/parser.c " , " src/scanner.c " } ,
2020-04-22 11:13:05 +02:00
}
}
parsers.json = {
install_info = {
2020-04-21 23:40:23 +02:00
url = " https://github.com/tree-sitter/tree-sitter-json " ,
files = { " src/parser.c " } ,
2020-04-22 11:13:05 +02:00
}
}
parsers.css = {
install_info = {
2020-04-21 23:40:23 +02:00
url = " https://github.com/tree-sitter/tree-sitter-css " ,
files = { " src/parser.c " , " src/scanner.c " } ,
2020-04-22 11:13:05 +02:00
}
}
parsers.ocaml = {
install_info = {
2020-04-21 23:40:23 +02:00
url = " https://github.com/tree-sitter/tree-sitter-ocaml " ,
files = { " src/parser.c " , " src/scanner.cc " } ,
2020-04-22 11:13:05 +02:00
}
}
parsers.swift = {
install_info = {
2020-04-21 23:40:23 +02:00
url = " https://github.com/tree-sitter/tree-sitter-swift " ,
files = { " src/parser.c " } ,
2020-04-22 11:13:05 +02:00
}
}
parsers.csharp = {
install_info = {
2020-04-21 23:40:23 +02:00
url = " https://github.com/tree-sitter/tree-sitter-c-sharp " ,
files = { " src/parser.c " , " src/scanner.c " } ,
2020-04-22 11:13:05 +02:00
}
}
parsers.typescript = {
install_info = {
2020-04-21 23:40:23 +02:00
url = " https://github.com/tree-sitter/tree-sitter-typescript " ,
files = { " src/parser.c " , " src/scanner.c " } ,
location = " tree-sitter-typescript/typescript "
2020-04-22 11:13:05 +02:00
}
}
parsers.tsx = {
install_info = {
2020-04-21 23:40:23 +02:00
url = " https://github.com/tree-sitter/tree-sitter-typescript " ,
files = { " src/parser.c " , " src/scanner.c " } ,
location = " tree-sitter-tsx/tsx "
}
}
2020-05-01 13:31:05 +02:00
parsers.scala = {
install_info = {
url = " https://github.com/tree-sitter/tree-sitter-scala " ,
files = { " src/parser.c " , " src/scanner.c " } ,
}
}
parsers.haskell = {
install_info = {
url = " https://github.com/tree-sitter/tree-sitter-haskell " ,
files = { " src/parser.c " , " src/scanner.cc " } ,
}
}
parsers.markdown = {
install_info = {
url = " https://github.com/ikatyang/tree-sitter-markdown " ,
files = { " src/parser.c " , " src/scanner.cc " } ,
}
}
parsers.toml = {
install_info = {
url = " https://github.com/ikatyang/tree-sitter-toml " ,
files = { " src/parser.c " , " src/scanner.c " } ,
}
}
parsers.vue = {
install_info = {
url = " https://github.com/ikatyang/tree-sitter-vue " ,
files = { " src/parser.c " , " src/scanner.cc " } ,
}
}
parsers.elm = {
install_info = {
url = " https://github.com//razzeee/tree-sitter-elm " ,
files = { " src/parser.c " , " src/scanner.cc " } ,
}
}
parsers.yaml = {
install_info = {
url = " https://github.com/ikatyang/tree-sitter-yaml " ,
files = { " src/parser.c " , " src/scanner.cc " } ,
}
}
parsers.nix = {
install_info = {
url = " https://github.com/cstrahan/tree-sitter-nix " ,
files = { " src/parser.c " , " src/scanner.cc " } ,
}
}
2020-04-22 11:13:05 +02:00
-- @enable can be true or false
-- @disable is a list of languages, only relevant if enable is true
-- @keymaps list of user mappings for a given module if relevant
-- @is_supported function which, given a ft, will return true if the ft works on the module
local config = {
2020-05-01 12:26:57 +02:00
modules = {
highlight = {
enable = false ,
disable = { } ,
is_supported = function ( ft )
return queries.get_query ( ft , ' highlights ' ) ~= nil
end
} ,
2020-05-02 11:33:32 +02:00
incremental_selection = {
2020-05-01 12:26:57 +02:00
enable = false ,
disable = { } ,
keymaps = {
node_incremental = " grn " ,
scope_incremental = " grc "
} ,
is_supported = function ( ) return true end
2020-04-25 16:35:10 +02:00
} ,
2020-05-01 16:25:06 +02:00
node_movement = {
enable = false ,
disable = { } ,
is_supported = function ( ) return true end ,
keymaps = {
move_up = " <a-k> " ,
move_down = " <a-j> " ,
move_left = " <a-h> " ,
move_right = " <a-l> " ,
} ,
} ,
2020-05-01 12:26:57 +02:00
-- folding = {
-- enable = false,
-- disable = {},
-- keymaps = {},
-- is_supported = function() return false end
-- }
2020-04-25 16:35:10 +02:00
} ,
2020-05-01 12:26:57 +02:00
ensure_installed = nil
2020-04-22 11:13:05 +02:00
}
local M = { }
local function enable_module ( mod , bufnr , ft )
local bufnr = bufnr or api.nvim_get_current_buf ( )
local ft = ft or api.nvim_buf_get_option ( bufnr , ' ft ' )
2020-05-01 12:26:57 +02:00
if not parsers [ ft ] or not config.modules [ mod ] then
2020-04-22 11:13:05 +02:00
return
end
local loaded_mod = require ( string.format ( " nvim-treesitter.%s " , mod ) )
loaded_mod.attach ( bufnr , ft )
end
local function enable_mod_conf_autocmd ( mod , ft )
2020-05-01 12:26:57 +02:00
if not config.modules [ mod ] or M.is_enabled ( mod , ft ) then return end
2020-04-22 11:13:05 +02:00
local cmd = string.format ( " lua require'nvim-treesitter.%s'.attach() " , mod )
api.nvim_command ( string.format ( " autocmd FileType %s %s " , ft , cmd ) )
2020-05-01 12:26:57 +02:00
for i , parser in pairs ( config.modules [ mod ] . disable ) do
2020-04-22 11:13:05 +02:00
if parser == ft then
2020-05-01 12:26:57 +02:00
table.remove ( config.modules [ mod ] . disable , i )
2020-04-22 11:13:05 +02:00
break
end
end
end
local function enable_all ( mod , ft )
2020-05-01 12:26:57 +02:00
if not config.modules [ mod ] then return end
2020-04-22 11:13:05 +02:00
for _ , bufnr in pairs ( api.nvim_list_bufs ( ) ) do
if not ft or api.nvim_buf_get_option ( bufnr , ' ft ' ) == ft then
enable_module ( mod , bufnr , ft )
end
end
if ft then
2020-04-25 16:11:53 +02:00
if parser_utils.has_parser ( ft ) then
enable_mod_conf_autocmd ( mod , ft )
end
2020-04-22 11:13:05 +02:00
else
for _ , ft in pairs ( M.available_parsers ( ) ) do
if parser_utils.has_parser ( ft ) then
enable_mod_conf_autocmd ( mod , ft )
end
end
end
2020-05-01 12:26:57 +02:00
config.modules [ mod ] . enable = true
2020-04-22 11:13:05 +02:00
end
local function disable_module ( mod , bufnr , ft )
local bufnr = bufnr or api.nvim_get_current_buf ( )
local ft = ft or api.nvim_buf_get_option ( bufnr , ' ft ' )
2020-05-01 12:26:57 +02:00
if not parsers [ ft ] or not config.modules [ mod ] then
2020-04-22 11:13:05 +02:00
return
end
local loaded_mod = require ( string.format ( " nvim-treesitter.%s " , mod ) )
loaded_mod.detach ( bufnr , ft )
end
local function disable_mod_conf_autocmd ( mod , ft )
2020-05-01 12:26:57 +02:00
if not config.modules [ mod ] or not M.is_enabled ( mod , ft ) then return end
2020-04-22 11:13:05 +02:00
api.nvim_command ( string.format ( " autocmd! FileType %s " , ft ) )
2020-05-01 12:26:57 +02:00
table.insert ( config.modules [ mod ] . disable , ft )
2020-04-22 11:13:05 +02:00
end
local function disable_all ( mod , ft )
for _ , bufnr in pairs ( api.nvim_list_bufs ( ) ) do
if not ft or api.nvim_buf_get_option ( bufnr , ' ft ' ) == ft then
disable_module ( mod , bufnr , ft )
end
end
if ft then
disable_mod_conf_autocmd ( mod , ft )
else
for _ , ft in pairs ( M.available_parsers ( ) ) do
2020-04-25 16:11:53 +02:00
disable_mod_conf_autocmd ( mod , ft )
2020-04-22 11:13:05 +02:00
end
2020-05-01 12:26:57 +02:00
config.modules [ mod ] . enable = false
2020-04-22 11:13:05 +02:00
end
end
M.commands = {
TSBufEnable = {
run = enable_module ,
args = {
" -nargs=1 " ,
" -complete=custom,v:lua.ts_available_modules "
} ,
description = ' `:TSBufEnable module_name` enable a specified module on the current buffer '
} ,
TSBufDisable = {
run = disable_module ,
args = {
" -nargs=1 " ,
" -complete=custom,v:lua.ts_available_modules "
} ,
description = ' `:TSBufDisable module_name` disable a specified module on the current buffer '
} ,
TSEnableAll = {
run = enable_all ,
args = {
" -nargs=+ " ,
" -complete=custom,v:lua.ts_available_modules "
} ,
description = ' `:TSEnableAll module_name (filetype)` enables a specified module on all buffers. If filetype is specified, enable only for specified filetype '
} ,
TSDisableAll = {
run = disable_all ,
args = {
" -nargs=+ " ,
" -complete=custom,v:lua.ts_available_modules "
} ,
description = ' `:TSDisableAll module_name (filetype)` disables a specified module on all buffers. If filetype is specified, disable only for specified filetype '
} ,
}
-- @param mod: module (string)
-- @param ft: filetype (string)
function M . is_enabled ( mod , ft )
2020-04-25 16:11:53 +02:00
if not M.get_parser_configs ( ) [ ft ] or not parser_utils.has_parser ( ft ) then
return false
end
2020-04-22 11:13:05 +02:00
2020-05-01 12:26:57 +02:00
local module_config = config.modules [ mod ]
2020-04-22 11:13:05 +02:00
if not module_config then return false end
if not module_config.enable or not module_config.is_supported ( ft ) then
return false
end
for _ , parser in pairs ( module_config.disable ) do
if ft == parser then return false end
end
return true
end
function M . setup ( user_data )
if not user_data then return end
for mod , data in pairs ( user_data ) do
2020-05-01 12:26:57 +02:00
if config.modules [ mod ] then
2020-04-22 11:13:05 +02:00
if type ( data.enable ) == ' boolean ' then
2020-05-01 12:26:57 +02:00
config.modules [ mod ] . enable = data.enable
2020-04-22 11:13:05 +02:00
end
if type ( data.disable ) == ' table ' then
2020-05-01 12:26:57 +02:00
config.modules [ mod ] . disable = data.disable
2020-04-22 11:13:05 +02:00
end
2020-05-01 12:26:57 +02:00
if config.modules [ mod ] . keymaps and type ( data.keymaps ) == ' table ' then
config.modules [ mod ] . keymaps = data.keymaps
2020-05-01 10:20:19 +02:00
end
2020-05-01 12:26:57 +02:00
elseif mod == ' ensure_installed ' then
config.ensure_installed = data
require ' nvim-treesitter/install ' . ensure_installed ( data )
2020-04-22 11:13:05 +02:00
end
end
end
function M . get_parser_configs ( )
return parsers
end
function M . available_parsers ( )
return vim.tbl_keys ( parsers )
end
function M . available_modules ( )
2020-05-01 12:26:57 +02:00
return vim.tbl_keys ( config.modules )
2020-04-22 11:13:05 +02:00
end
2020-05-02 11:33:32 +02:00
function M . get_module ( mod )
return config.modules [ mod ]
end
2020-04-21 23:40:23 +02:00
return M