diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 000000000..46c7a9419 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,99 @@ +# Contributing to `nvim-treesitter` + +First of all, thank you very much for contributing to `nvim-treesitter`. + +If you haven't already, you should really come and reach out to us on our [gitter](https://gitter.im/nvim-treesitter/community?utm_source=share-link&utm_medium=link&utm_campaign=share-link) +room, so we can help you with any question you might have! + +As you know, `nvim-treesitter` is roughly splitted in two parts : + - Parser configurations : for various things like `locals`, `highlights` + - What we like to call *modules* : tiny lua modules that provide a given feature, based on parser configurations + +Depending on which part of the plugin you want to contribute to, please read the appropriate section. + +## Parser configurations + +Contributing to parser configurations is basically modifying one of the `queries/*/*.scm`. +Each of these `scheme` files contains a *tree-sitter query* for a given purpose. +Before going any further, we highly suggest that you [read more about tree-sitter queries](https://tree-sitter.github.io/tree-sitter/using-parsers#pattern-matching-with-queries). + +Each query has an appropriate name, which is then used by modules to extract data from the syntax tree. +For now two types of queries are used by `nvim-treesitter`: + - `highlights.scm` : used for syntax highlighting, using the `highlight` module. + - `locals.scm` : used to extract keyword definitions, scopes, references,... using the `locals` module. + +For both of these types there is a *norm* you will have to follow so that features work fine. +Here are some global advices : + - If your language is listed [here](https://tree-sitter.github.io/tree-sitter/using-parsers#pattern-matching-with-queries), + you can debug and experiment with your queries there. + - If not, you should consider installing the [tree-sitter cli](https://github.com/tree-sitter/tree-sitter/tree/master/cli), + you should then be able to open a local playground using `tree-sitter build-wasm && tree-sitter web-ui` within the + parsers repo. + +### Highlights + +As languages differ quite a lot, here is a set of captures available to you when building a `highlights.scm` query. +One important thing to note is that many of these capture groups are not supported by `neovim` for now, and will not have any +effect on highlighting. We will work on improving highlighting in the near future though. + +#### Misc +`@comment` +`@error` for error `(ERROR)` nodes. +`@punctuation.delimiter` for `;` `.` `,` +`@punctuation.bracket` for `()` or `{}` + +Some captures are related to language injection (like markdown code blocks). As this is not supported by neovim yet, these +are optional and will not have any effect for now. +`@embedded` +`@injection` + `language` + `content` + +#### Constants +`@constant` + `builtin` + `macro` +`@string` + `regex` + `escape` +`@character` +`@number` +`@boolean` +`@float` + +#### Functions +`@function` + `builtin` + `macro` +`@parameter` + +`@method` +`@field` or `@property` + +`@constructor` + +#### Keywords +`@conditional` +`@repeat` +`@label` for C/Lua-like labels +`@operator` +`@keyword` +`@exception` + +`@type` + `builtin` +`@structure` + +### Locals + +`@definition` for various definitions + `function` + `method` + `var` + `macro` + `type` + +`@scope` + +`@reference` + diff --git a/README.md b/README.md index 7a677f2bc..afedbc400 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Treesitter configurations and abstraction layer for Neovim. ## Installation -You can install `nvim-treesitter` with your favorite package manager, or using the default `pack` feature of neovim ! +You can install `nvim-treesitter` with your favorite package manager, or using the default `pack` feature of Neovim! ### Using a package manager @@ -79,19 +79,101 @@ bash [✗] not installed tsx [✗] not installed ``` -And now you should be able to use every functionnality `nvim-treesitter` provides ! +And now you should be able to use every functionality `nvim-treesitter` provides! + +## Setup + +in your `init.vim`: + +```lua +lua <, Vigouroux Thomas + +============================================================================== +INTRODUCTION *nvim-treesitter-intro* + +nvim-treesitter wraps the neovim treesitter api to provide functionnalities such +as highlighting and incremental selection, and a command to easily install parsers. + +============================================================================== +QUICK START *nvim-treesitter-quickstart* + +Install the parser for your language + +> + :TSInstall {language} +< + +To get a list of supported languages + +> + :TSInstallInfo +< + +By default, everything is disabled. To enable support for features, in your `init.vim`: + +> + lua < 1 then health_warn(string.format("Multiple parsers found for %s, only %s will be used.", parser_name, installed[1])) else diff --git a/lua/nvim-treesitter/highlight.lua b/lua/nvim-treesitter/highlight.lua index 110954a6a..9cf259639 100644 --- a/lua/nvim-treesitter/highlight.lua +++ b/lua/nvim-treesitter/highlight.lua @@ -1,12 +1,53 @@ local api = vim.api -local queries = require'nvim-treesitter.query' local ts = vim.treesitter +local queries = require'nvim-treesitter.query' local M = { - highlighters={} + highlighters = {} } -function M.setup(bufnr, ft) +local hlmap = vim.treesitter.TSHighlighter.hl_map + +-- Misc +hlmap.error = "Error" +hlmap["punctuation.delimiter"] = "Delimiter" +hlmap["punctuation.bracket"] = "Delimiter" + +-- Constants +hlmap["constant"] = "Constant" +hlmap["constant.builtin"] = "Special" +hlmap["constant.macro"] = "Define" +hlmap["string"] = "String" +hlmap["string.regex"] = "String" +hlmap["string.escape"] = "SpecialChar" +hlmap["character"] = "Character" +hlmap["number"] = "Number" +hlmap["boolean"] = "Boolean" +hlmap["float"] = "Float" + +-- Functions +hlmap["function"] = "Function" +hlmap["function.builtin"] = "Special" +hlmap["function.macro"] = "Macro" +hlmap["parameter"] = "Identifier" +hlmap["method"] = "Function" +hlmap["field"] = "Identifier" +hlmap["property"] = "Identifier" +hlmap["constructor"] = "Special" + +-- Keywords +hlmap["conditional"] = "Conditional" +hlmap["repeat"] = "Repeat" +hlmap["label"] = "Label" +hlmap["operator"] = "Operator" +hlmap["keyword"] = "Keyword" +hlmap["exception"] = "Exception" + +hlmap["type"] = "Type" +hlmap["type.builtin"] = "Type" +hlmap["structure"] = "Structure" + +function M.attach(bufnr, ft) local buf = bufnr or api.nvim_get_current_buf() local ft = ft or api.nvim_buf_get_option(buf, 'ft') @@ -16,4 +57,13 @@ function M.setup(bufnr, ft) M.highlighters[buf] = ts.TSHighlighter.new(query, buf, ft) end +function M.detach(bufnr) + local buf = bufnr or api.nvim_get_current_buf() + if M.highlighters[buf] then + M.highlighters[buf]:set_query("") + M.highlighters[buf] = nil + end + api.nvim_buf_set_option(buf, 'syntax', 'on') +end + return M diff --git a/lua/nvim-treesitter/incremental_selection.lua b/lua/nvim-treesitter/incremental_selection.lua new file mode 100644 index 000000000..70eefe37c --- /dev/null +++ b/lua/nvim-treesitter/incremental_selection.lua @@ -0,0 +1,78 @@ +local api = vim.api +local utils = require'nvim-treesitter.utils' +local parsers = require'nvim-treesitter.parsers' +local M = {} + +local function node_range_to_vim(node) + if not node then return end + + local start_row, start_col, end_row, end_col = node:range() + + local select_range = [[ + call cursor(%d, %d) + normal v + call cursor(%d, %d) + ]] + local exec_command = string.format(select_range, + start_row+1, start_col+1, + end_row+1, end_col+1) + + api.nvim_exec(exec_command, false) +end + +local function select_incremental(increment_func) + return function() + local buf, sel_start_line, sel_start_col, _ = unpack(vim.fn.getpos("'<")) + local buf, sel_end_line, sel_end_col, _ = unpack(vim.fn.getpos("'>")) + + local node = nil + if parsers.has_parser() then + local root = parsers.get_parser():parse():root() + node = root:named_descendant_for_range(sel_start_line-1, sel_start_col-1, sel_end_line-1, sel_end_col) + local node_start_row, node_start_col, node_end_row, node_end_col = node:range() + + if (sel_start_line-1) == node_start_row and (sel_start_col-1) == node_start_col + and (sel_end_line-1) == node_end_row and sel_end_col == node_end_col then + node = increment_func(node) + end + end + + return node_range_to_vim(node) + end +end + +M.node_incremental = select_incremental(function(node) + if node then + return node:parent() or node + end +end) + +M.scope_incremental = select_incremental(function(node) + if node then + return utils.smallest_containing_scope(node:parent() or node) + end +end) + +function M.attach(bufnr) + local buf = bufnr or api.nvim_get_current_buf() + + local config = require'nvim-treesitter.configs'.get_module('incremental_selection') + for funcname, mapping in pairs(config.keymaps) do + api.nvim_buf_set_keymap(buf, 'v', mapping, + string.format(":lua require'nvim-treesitter.incremental_selection'.%s()", funcname), { silent = true }) + api.nvim_buf_set_keymap(buf, 'o', mapping, + string.format(":normal v%s", mapping), { silent = true }) + end +end + +function M.detach(bufnr) + local buf = bufnr or api.nvim_get_current_buf() + + local config = require'nvim-treesitter.configs'.get_module('incremental_selection') + for _, mapping in pairs(config.keymaps) do + api.nvim_buf_del_keymap(buf, 'v', mapping) + api.nvim_buf_del_keymap(buf, 'o', mapping) + end +end + +return M diff --git a/lua/nvim-treesitter/info.lua b/lua/nvim-treesitter/info.lua new file mode 100644 index 000000000..18b1b611f --- /dev/null +++ b/lua/nvim-treesitter/info.lua @@ -0,0 +1,96 @@ +local api = vim.api +local configs = require'nvim-treesitter.configs' + +local M = {} + +local function install_info() + local max_len = 0 + for _, ft in pairs(configs.available_parsers()) do + if #ft > max_len then max_len = #ft end + end + + for _, ft in pairs(configs.available_parsers()) do + local is_installed = #api.nvim_get_runtime_file('parser/'..ft..'.so', false) > 0 + api.nvim_out_write(ft..string.rep(' ', max_len - #ft + 1)) + if is_installed then + api.nvim_out_write("[✓] installed\n") + else + api.nvim_out_write("[✗] not installed\n") + end + end +end + +local function print_info_module(sorted_filetypes, mod) + local max_str_len = #sorted_filetypes[1] + local header = string.format('%s%s', string.rep(' ', max_str_len + 2), mod) + api.nvim_out_write(header..'\n') + for _, ft in pairs(sorted_filetypes) do + local padding = string.rep(' ', max_str_len - #ft + #mod / 2 + 1) + api.nvim_out_write(ft..":"..padding) + if configs.is_enabled(mod, ft) then + api.nvim_out_write('✓') + else + api.nvim_out_write('✗') + end + api.nvim_out_write('\n') + end +end + +local function print_info_modules(sorted_filetypes) + local max_str_len = #sorted_filetypes[1] + local header = string.rep(' ', max_str_len + 2) + for _, mod in pairs(configs.available_modules()) do + header = string.format('%s%s ', header, mod) + end + api.nvim_out_write(header..'\n') + + for _, ft in pairs(sorted_filetypes) do + local padding = string.rep(' ', max_str_len - #ft) + api.nvim_out_write(ft..":"..padding) + + for _, mod in pairs(configs.available_modules()) do + local pad_len = #mod / 2 + 1 + api.nvim_out_write(string.rep(' ', pad_len)) + + if configs.is_enabled(mod, ft) then + api.nvim_out_write('✓') + else + api.nvim_out_write('✗') + end + api.nvim_out_write(string.rep(' ', pad_len - 1)) + end + api.nvim_out_write('\n') + end +end + +local function module_info(mod) + if mod and not configs.get_config()[mod] then return end + + local ft_by_len = configs.available_parsers() + table.sort(ft_by_len, function(a, b) return #a > #b end) + if mod then + print_info_module(ft_by_len, mod) + else + print_info_modules(ft_by_len) + end +end + +M.commands = { + TSInstallInfo = { + run = install_info, + args = { + "-nargs=0", + }, + description = '`:TSInstallInfo` print installation state for every filetype' + }, + TSModuleInfo = { + run = module_info, + args = { + "-nargs=?", + "-complete=custom,v:lua.ts_available_modules" + }, + description = '`:TSModuleInfo` print module state for every filetype, if module is specified, only for current module' + } +} + +return M diff --git a/lua/nvim-treesitter/install.lua b/lua/nvim-treesitter/install.lua index b00d3d773..77e0fccc4 100644 --- a/lua/nvim-treesitter/install.lua +++ b/lua/nvim-treesitter/install.lua @@ -1,7 +1,9 @@ local api = vim.api local fn = vim.fn local luv = vim.loop -local repositories = require'nvim-treesitter/configs'.repositories +local configs = require'nvim-treesitter/configs' +local parsers = configs.get_parser_configs() +local has_parser = require'nvim-treesitter/parsers'.has_parser local M = {} @@ -33,14 +35,16 @@ local function get_cache_dir() end local function iter_cmd(cmd_list, i, ft) - if i == #cmd_list then return print('Treesitter parser for '..ft..' has been installed') end + if i == #cmd_list + 1 then return print('Treesitter parser for '..ft..' has been installed') end - local attr = cmd_list[i + 1] + local attr = cmd_list[i] if attr.info then print(attr.info) end + local handle + handle = luv.spawn(attr.cmd, attr.opts, vim.schedule_wrap(function(code) handle:close() - if code ~= 0 then return api.nvim_err_writeln(attr.err) end + if code ~= 0 then return api.nvim_err_writeln(attr.err) end iter_cmd(cmd_list, i + 1, ft) end)) end @@ -75,12 +79,12 @@ local function run_install(cache_folder, package_path, ft, repo) args = vim.tbl_flatten({ '-o', 'parser.so', + '-I./src', + repo.files, '-shared', + '-Os', '-lstdc++', '-fPIC', - '-Os', - '-I./src', - repo.files }), cwd = compile_location } @@ -99,7 +103,7 @@ local function run_install(cache_folder, package_path, ft, repo) } } - iter_cmd(command_list, 0, ft) + iter_cmd(command_list, 1, ft) end -- TODO(kyazdani): this should work on windows too @@ -118,14 +122,15 @@ local function install(ft) if not string.match(yesno, '^y.*') then return end end - local repository = repositories[ft] - if not repository then + local parser_config = parsers[ft] + if not parser_config then return api.nvim_err_writeln('Parser not available for language '..ft) end + local install_info = parser_config.install_info vim.validate { - url={ repository.url, 'string' }, - files={ repository.files, 'table' } + url={ install_info.url, 'string' }, + files={ install_info.files, 'table' } } if fn.executable('git') == 0 then @@ -138,26 +143,27 @@ local function install(ft) local cache_folder, err = get_cache_dir() if err then return api.nvim_err_writeln(err) end - run_install(cache_folder, package_path, ft, repository) + run_install(cache_folder, package_path, ft, install_info) end -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 + +M.ensure_installed = function(languages) + if type(languages) == 'string' then + if languages == 'all' then + languages = configs.available_parsers() + else + languages = {languages} + end end - for parser_name, _ in pairs(repositories) do - local is_installed = #api.nvim_get_runtime_file('parser/'..parser_name..'.so', false) > 0 - api.nvim_out_write(parser_name..string.rep(' ', max_len - #parser_name + 1)) - if is_installed then - api.nvim_out_write("[✓] installed\n") - else - api.nvim_out_write("[✗] not installed\n") + for _, ft in ipairs(languages) do + if not has_parser(ft) then + install(ft) end end end + M.commands = { TSInstall = { run = install, @@ -166,25 +172,7 @@ M.commands = { "-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()", command_name) - local parts = vim.tbl_flatten({ - "command!", - def.args, - command_name, - call_fn, - }) - api.nvim_command(table.concat(parts, " ")) - end -end - return M diff --git a/lua/nvim-treesitter/locals.lua b/lua/nvim-treesitter/locals.lua index 388c7e489..b16408ac5 100644 --- a/lua/nvim-treesitter/locals.lua +++ b/lua/nvim-treesitter/locals.lua @@ -10,22 +10,6 @@ local M = { locals={} } -function M.checkhealth(lang) - local health_start = vim.fn["health#report_start"] - 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 not queries.get_query(lang, "locals") then - health_warn("No `locals.scm` query found for " .. lang, { - "Open an issue at https://github.com/nvim-treesitter/nvim-treesitter" - }) - else - health_ok("`locals.scm` found.") - end -end - function M.collect_locals(bufnr) local ft = api.nvim_buf_get_option(bufnr, "ft") if not ft then return end @@ -69,7 +53,7 @@ function M.get_definitions(bufnr) for _, loc in ipairs(locals) do if loc.definition then - table.insert(defs, {definition=loc.definition, kind=loc.kind}) + table.insert(defs, loc.definition) end end @@ -82,8 +66,8 @@ function M.get_scopes(bufnr) local scopes = {} for _, loc in ipairs(locals) do - if loc.scope then - table.insert(scopes, loc.scope) + if loc.scope and loc.scope.node then + table.insert(scopes, loc.scope.node) end end @@ -96,8 +80,8 @@ function M.get_references(bufnr) local refs = {} for _, loc in ipairs(locals) do - if loc.reference then - table.insert(refs, loc.reference) + if loc.reference and loc.reference.node then + table.insert(refs, loc.reference.node) end end diff --git a/lua/nvim-treesitter/node_movement.lua b/lua/nvim-treesitter/node_movement.lua new file mode 100644 index 000000000..5d4813bc4 --- /dev/null +++ b/lua/nvim-treesitter/node_movement.lua @@ -0,0 +1,91 @@ +local api = vim.api +local parsers = require'nvim-treesitter.parsers' +local utils = require'nvim-treesitter.utils' +local M = {} + + +M.NodeMovementKind = { + up = 'up', + down = 'down', + left = 'left', + right = 'right', +} + +M.current_node = {} + +local function node_start_to_vim(node) + if not node then return end + + local row, col = node:start() + local exec_command = string.format('call cursor(%d, %d)', row+1, col+1) + api.nvim_exec(exec_command, false) +end + +M.do_node_movement = function(kind) + local buf, line, col = unpack(vim.fn.getpos(".")) + + local current_node = M.current_node[buf] + + if current_node then + local node_line, node_col = current_node:start() + if line-1 ~= node_line or col-1 ~= node_col then + current_node = nil + end + end + local destination_node + + if parsers.has_parser() then + local root = parsers.get_parser():parse():root() + if not current_node then + current_node = root:named_descendant_for_range(line-1, col-1, line-1, col) + end + + if kind == M.NodeMovementKind.up then + destination_node = current_node:parent() + elseif kind == M.NodeMovementKind.down then + if current_node:named_child_count() > 0 then + destination_node = current_node:named_child(0) + else + local next_node = utils.get_next_node(current_node) + if next_node and next_node:named_child_count() > 0 then + destination_node = next_node:named_child(0) + end + end + elseif kind == M.NodeMovementKind.left then + destination_node = utils.get_previous_node(current_node, true, true) + elseif kind == M.NodeMovementKind.right then + destination_node = utils.get_next_node(current_node, true, true) + end + M.current_node[buf] = destination_node or current_node + end + + if destination_node then + node_start_to_vim(destination_node) + end +end + +M.move_up = function() M.do_node_movement(M.NodeMovementKind.up) end +M.move_down = function() M.do_node_movement(M.NodeMovementKind.down) end +M.move_left = function() M.do_node_movement(M.NodeMovementKind.left) end +M.move_right = function() M.do_node_movement(M.NodeMovementKind.right) end + +function M.attach(bufnr) + local buf = bufnr or api.nvim_get_current_buf() + + local config = require'nvim-treesitter.configs'.get_module('node_movement') + for funcname, mapping in pairs(config.keymaps) do + api.nvim_buf_set_keymap(buf, 'n', mapping, + string.format(":lua require'nvim-treesitter.node_movement'.%s()", funcname), { silent = true }) + end +end + +function M.detach(bufnr) + local buf = bufnr or api.nvim_get_current_buf() + + local config = require'nvim-treesitter.configs'.get_module('node_movement') + for _, mapping in pairs(config.keymaps) do + api.nvim_buf_del_keymap(buf, 'n', mapping) + 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 diff --git a/lua/nvim-treesitter/utils.lua b/lua/nvim-treesitter/utils.lua index 8313a46a7..9d591eab6 100644 --- a/lua/nvim-treesitter/utils.lua +++ b/lua/nvim-treesitter/utils.lua @@ -1,6 +1,7 @@ -- Utils collection for nvim-treesitter local api = vim.api local parsers = require'nvim-treesitter.parsers' +local locals = require'nvim-treesitter.locals' local M = {} @@ -63,4 +64,92 @@ function M.is_parent(dest, source) return false end +function M.setup_commands(mod, commands) + for command_name, def in pairs(commands) do + local call_fn = string.format("lua require'nvim-treesitter.%s'.commands.%s.run()", mod, command_name) + local parts = vim.tbl_flatten({ + "command!", + def.args, + command_name, + call_fn, + }) + api.nvim_command(table.concat(parts, " ")) + end +end + +--- Gets the smallest scope which contains @param node +function M.smallest_containing_scope(node, bufnr) + local bufnr = bufnr or api.nvim_get_current_buf() + + local root = parsers.get_parser(bufnr):parse():root() + if not node then return root end + + local scopes = locals.get_scopes(bufnr) + local current = node + while current ~= nil and not vim.tbl_contains(scopes, current) do + current = current:parent() + end + + return current or root +end + +--- Get next node with same parent +-- @param node node +-- @param allow_switch_parents allow switching parents if last node +-- @param allow_next_parent allow next parent if last node and next parent without children +function M.get_next_node(node, allow_switch_parents, allow_next_parent) + local destination_node + local parent = node:parent() + + if parent then + local found_pos = 0 + for i = 0,parent:named_child_count()-1,1 do + if parent:named_child(i) == node then + found_pos = i + break + end + end + if parent:named_child_count() > found_pos + 1 then + destination_node = parent:named_child(found_pos + 1) + elseif allow_switch_parents then + local next_node = M.get_next_node(node:parent()) + if next_node and next_node:named_child_count() > 0 then + destination_node = next_node:named_child(0) + elseif next_node and allow_next_parent then + destination_node = next_node + end + end + end + return destination_node +end + +--- Get previous node with same parent +-- @param node node +-- @param allow_switch_parents allow switching parents if first node +-- @param allow_previous_parent allow previous parent if first node and previous parent without children +function M.get_previous_node(node, allow_switch_parents, allow_previous_parent) + local destination_node + local parent = node:parent() + if parent then + local found_pos = 0 + for i = 0,parent:named_child_count()-1,1 do + if parent:named_child(i) == node then + found_pos = i + break + end + end + if 0 < found_pos then + destination_node = parent:named_child(found_pos - 1) + elseif allow_switch_parents then + local previous_node = M.get_previous_node(node:parent()) + if previous_node and previous_node:named_child_count() > 0 then + destination_node = previous_node:named_child(previous_node:named_child_count() - 1) + elseif previous_node and allow_previous_parent then + destination_node = previous_node + end + end + end + return destination_node +end + return M diff --git a/plugin/nvim-treesitter.vim b/plugin/nvim-treesitter.vim index 06c760a18..30ee3241d 100644 --- a/plugin/nvim-treesitter.vim +++ b/plugin/nvim-treesitter.vim @@ -1,17 +1,20 @@ -" Last Change: 2020 avril 19 +" Last Change: 2020 avril 25 if exists('g:loaded_nvim_treesitter') finish endif -lua << EOF -ts_installable_parsers = function() - return table.concat(require'nvim-treesitter'.available_parsers(), '\n') -end -require'nvim-treesitter'._root.setup() -EOF +augroup NvimTreesitter +augroup END let g:loaded_nvim_treesitter = 1 -augroup NvimTreesitter -augroup END +lua << EOF +ts_installable_parsers = function() + return table.concat(require'nvim-treesitter.configs'.available_parsers(), '\n') +end +ts_available_modules = function() + return table.concat(require'nvim-treesitter.configs'.available_modules(), '\n') +end +require'nvim-treesitter'.setup() +EOF diff --git a/queries/lua/highlights.scm b/queries/lua/highlights.scm index 3077be108..a674638e8 100644 --- a/queries/lua/highlights.scm +++ b/queries/lua/highlights.scm @@ -3,16 +3,16 @@ ;;; Builtins ;; Keywords "local" @keyword -"if" @keyword -"then" @keyword -"else" @keyword -"elseif" @keyword +"if" @conditional +"then" @conditional +"else" @conditional +"elseif" @conditional "end" @keyword "return" @keyword -"do" @keyword -"while" @keyword -"repeat" @keyword -"for" @keyword +"do" @repeat +"while" @repeat +"repeat" @repeat +"for" @repeat (break_statement) @keyword "goto" @keyword @@ -43,15 +43,16 @@ "#" @operator ;; Constants -(false) @constant -(true) @constant -(nil) @constant +(false) @boolean +(true) @boolean +(nil) @constant.builtin (spread) @constant ;; "..." ;; Nodes (function "function" @function "end" @function) +(function_definition "function" @function "end" @function) (local_function "function" @function "end" @function) -(table "{" @operator "}" @operator) +(table "{" @constructor "}" @constructor) (comment) @comment (string) @string (number) @number diff --git a/queries/lua/locals.scm b/queries/lua/locals.scm index 5f21e0aaf..ee2927328 100644 --- a/queries/lua/locals.scm +++ b/queries/lua/locals.scm @@ -2,25 +2,20 @@ ;; Variable and field declarations ((variable_declarator - (identifier) @definition) - (set! definition.kind "v")) + (identifier) @definition.var)) ((variable_declarator - (field_expression object:(*) @definition.associated (property_identifier) @definition)) - (set! difinition.kind "v")) + (field_expression object:(*) @definition.associated (property_identifier) @definition.var))) ;; Parameters ((local_function - (parameters (identifier) @definition)) - (set! definition.kind "v")) + (parameters (identifier) @definition.var))) ((function - (parameters (identifier) @definition)) - (set! definition.kind "v")) + (parameters (identifier) @definition.var))) ;; Loops ((loop_expression - (identifier) @definition) - (set! definition.kind "v")) + (identifier) @definition.var)) ;; Function definitions ;; Functions definitions creates both a definition and a new scope @@ -28,16 +23,13 @@ (function_name (function_name_field (identifier) @definition.associated - (property_identifier) @definition))) @scope - (set! definition.kind "m")) + (property_identifier) @definition.method))) @scope) ((function - (function_name (identifier) @definition)) @scope - (set! definition.kind "f")) + (function_name (identifier) @definition.function)) @scope) ((local_function - (identifier) @definition) @scope - (set! definition.kind "f")) + (identifier) @definition.function) @scope) ((if_statement) @scope) ((for_in_statement) @scope) diff --git a/queries/ruby/highlights.scm b/queries/ruby/highlights.scm new file mode 100644 index 000000000..22e2267ed --- /dev/null +++ b/queries/ruby/highlights.scm @@ -0,0 +1,140 @@ +; Keywords + +"alias" @keyword +"and" @keyword +"begin" @keyword +"break" @keyword +"case" @keyword +"class" @keyword +"def" @keyword +"do" @keyword +"else" @keyword +"elsif" @keyword +"end" @keyword +"ensure" @keyword +"for" @keyword +"if" @keyword +"in" @keyword +"module" @keyword +"next" @keyword +"or" @keyword +"rescue" @keyword +"retry" @keyword +"return" @keyword +"then" @keyword +"unless" @keyword +"until" @keyword +"when" @keyword +"while" @keyword +"yield" @keyword + +((identifier) @keyword + (match? @keyword "^(private|protected|public)$")) + +; Function calls + +((identifier) @function + (eq? @function "require")) + +"defined?" @function + +(call + receiver: (constant) @constant) +(method_call + receiver: (constant) @constant) +(call + method: (identifier) @function) +(method_call + method: (identifier) @function) +(call + method: (constant) @function) +(method_call + method: (constant) @function) + +; Function definitions + +(alias (identifier) @function) +(setter (identifier) @function) +(method name: (identifier) @function) +(method name: (constant) @constant) +(class name: (constant) @constant) +(singleton_method name: (identifier) @function) +(singleton_method name: (constant) @constant) + +; Identifiers + +(class_variable) @Identifier +(instance_variable) @Identifier + +((identifier) @constant + (match? @constant "^__(FILE|LINE|ENCODING)__$")) + +((constant) @constant + (match? @constant "^[A-Z\\d_]+$")) + +(constant) @constant + +(self) @constant +(super) @Identifier + +(method_parameters (identifier) @Type) +(lambda_parameters (identifier) @Type) +(block_parameters (identifier) @Type) +(splat_parameter (identifier) @Type) +(hash_splat_parameter (identifier) @Type) +(optional_parameter (identifier) @Type) +(destructured_parameter (identifier) @Type) +(block_parameter (identifier) @Type) +(keyword_parameter (identifier) @Type) + +((identifier) @function + (is-not? local)) + +; Literals + +(string) @string +(bare_string) @string +(bare_symbol) @constant +(subshell) @string +(heredoc_beginning) @Delimiter +(heredoc_body) @string +(heredoc_end) @Delimiter +(symbol) @constant +(regex) @string +(escape_sequence) @Special +(integer) @number +(float) @number + +(nil) @Identifier +(true) @Identifier +(false) @Identifier + +(interpolation + "#{" @Delimiter + (identifier) @Identifier + "}" @Delimiter) @embedded + +(comment) @comment + +; Operators + +"=" @operator +"=>" @operator +"->" @operator +"+" @operator +"-" @operator +"*" @operator +"/" @operator + +"," @Normal +";" @Normal +"." @Normal + +"(" @Normal +")" @Normal +"[" @Normal +"]" @Normal +"{" @Normal +"}" @Normal +"%w(" @Normal +"%i(" @Normal diff --git a/queries/ruby/locals.scm b/queries/ruby/locals.scm new file mode 100644 index 000000000..6c1b25238 --- /dev/null +++ b/queries/ruby/locals.scm @@ -0,0 +1,45 @@ +; The MIT License (MIT) +; +; Copyright (c) 2016 Rob Rix +; +; Permission is hereby granted, free of charge, to any person obtaining a copy +; of this software and associated documentation files (the "Software"), to deal +; in the Software without restriction, including without limitation the rights +; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +; copies of the Software, and to permit persons to whom the Software is +; furnished to do so, subject to the following conditions: +; +; The above copyright notice and this permission notice shall be included in all +; copies or substantial portions of the Software. +; +; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +; SOFTWARE. + +;;; DESCLARATIONS AND SCOPES +((method) @scope + (set! scope-inherits false)) + +(block) @scope +(do_block) @scope + +(method_parameters (identifier) @definition) +(lambda_parameters (identifier) @definition) +(block_parameters (identifier) @definition) +(splat_parameter name: (identifier) @definition) +(hash_splat_parameter name: (identifier) @definition) +(optional_parameter name: (identifier) @definition) +(destructured_parameter name: (identifier) @definition) +(block_parameter name: (identifier) @definition) +(keyword_parameter name: (identifier) @definition) + +(identifier) @reference + +(assignment left:(identifier) @definition) +(left_assignment_list (identifier) @definition) +(rest_assignment (identifier) @definition) +(destructured_left_assignment (identifier) @definition)