Skip to content

Commit dff6b5e

Browse files
authored
feat: switch to main branch of nvim-treesitter (#461)
Note: - tree-sitter-cli needs to be installed (in macOS, run `brew install tree-sitter-cli`) - if nvim-treesitter and nvim-treesitter-textobjects are using the old master branch, we need to delete the old plugin and then re-install them to have the main branch
1 parent 94bb7b7 commit dff6b5e

File tree

3 files changed

+112
-50
lines changed

3 files changed

+112
-50
lines changed
Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,50 @@
1-
require("nvim-treesitter.configs").setup {
2-
textobjects = {
3-
select = {
4-
enable = true,
5-
6-
-- Automatically jump forward to textobj, similar to targets.vim
7-
lookahead = true,
8-
9-
keymaps = {
10-
-- You can use the capture groups defined in textobjects.scm
11-
["af"] = "@function.outer",
12-
["if"] = "@function.inner",
13-
["ac"] = "@class.outer",
14-
-- You can optionally set descriptions to the mappings (used in the desc parameter of
15-
-- nvim_buf_set_keymap) which plugins like which-key display
16-
["ic"] = { query = "@class.inner", desc = "Select inner part of a class region" },
17-
},
18-
-- You can choose the select mode (default is charwise 'v')
19-
--
20-
-- Can also be a function which gets passed a table with the keys
21-
-- * query_string: eg '@function.inner'
22-
-- * method: eg 'v' or 'o'
23-
-- and should return the mode ('v', 'V', or '<c-v>') or a table
24-
-- mapping query_strings to modes.
25-
selection_modes = {
26-
["@function.inner"] = "V", -- linewise
27-
["@function.outer"] = "V", -- linewise
28-
["@class.outer"] = "V", -- linewise
29-
["@class.inner"] = "V", -- linewise
30-
["@parameter.outer"] = "v", -- charwise
31-
},
32-
-- If you set this to `true` (default is `false`) then any textobject is
33-
-- extended to include preceding or succeeding whitespace. Succeeding
34-
-- whitespace has priority in order to act similarly to eg the built-in
35-
-- `ap`.
36-
--
37-
-- Can also be a function which gets passed a table with the keys
38-
-- * query_string: eg '@function.inner'
39-
-- * selection_mode: eg 'v'
40-
-- and should return true or false
41-
include_surrounding_whitespace = false,
1+
-- configuration
2+
require("nvim-treesitter-textobjects").setup {
3+
select = {
4+
-- Automatically jump forward to textobj, similar to targets.vim
5+
lookahead = true,
6+
-- You can choose the select mode (default is charwise 'v')
7+
--
8+
-- Can also be a function which gets passed a table with the keys
9+
-- * query_string: eg '@function.inner'
10+
-- * method: eg 'v' or 'o'
11+
-- and should return the mode ('v', 'V', or '<c-v>') or a table
12+
-- mapping query_strings to modes.
13+
selection_modes = {
14+
["@function.inner"] = "V", -- linewise
15+
["@function.outer"] = "V", -- linewise
16+
["@class.outer"] = "V", -- linewise
17+
["@class.inner"] = "V", -- linewise
18+
["@parameter.outer"] = "v", -- charwise
4219
},
20+
-- If you set this to `true` (default is `false`) then any textobject is
21+
-- extended to include preceding or succeeding whitespace. Succeeding
22+
-- whitespace has priority in order to act similarly to eg the built-in
23+
-- `ap`.
24+
--
25+
-- Can also be a function which gets passed a table with the keys
26+
-- * query_string: eg '@function.inner'
27+
-- * selection_mode: eg 'v'
28+
-- and should return true of false
29+
include_surrounding_whitespace = false,
4330
},
4431
}
32+
33+
-- keymaps
34+
-- You can use the capture groups defined in `textobjects.scm`
35+
vim.keymap.set({ "x", "o" }, "af", function()
36+
require("nvim-treesitter-textobjects.select").select_textobject("@function.outer", "textobjects")
37+
end)
38+
vim.keymap.set({ "x", "o" }, "if", function()
39+
require("nvim-treesitter-textobjects.select").select_textobject("@function.inner", "textobjects")
40+
end)
41+
vim.keymap.set({ "x", "o" }, "ac", function()
42+
require("nvim-treesitter-textobjects.select").select_textobject("@class.outer", "textobjects")
43+
end)
44+
vim.keymap.set({ "x", "o" }, "ic", function()
45+
require("nvim-treesitter-textobjects.select").select_textobject("@class.inner", "textobjects")
46+
end)
47+
-- You can also use captures from other query groups like `locals.scm`
48+
vim.keymap.set({ "x", "o" }, "as", function()
49+
require("nvim-treesitter-textobjects.select").select_textobject("@local.scope", "locals")
50+
end)

lua/config/treesitter.lua

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,52 @@
1-
require("nvim-treesitter.configs").setup {
2-
ensure_installed = { "python", "cpp", "lua", "vim", "json", "toml" },
3-
ignore_install = {}, -- List of parsers to ignore installing
4-
highlight = {
5-
enable = true, -- false will disable the whole extension
6-
disable = { "help" }, -- list of language that will be disabled
7-
},
1+
-- a list of filetypes to install treesitter parsers and queries
2+
local ensure_installed = {
3+
"python",
4+
"cpp",
5+
"lua",
6+
"vim",
7+
"json",
8+
"toml",
9+
"yaml",
10+
"javascript",
11+
"go",
12+
"typescript",
13+
"markdown",
14+
"sh",
15+
"zsh",
816
}
17+
18+
vim.api.nvim_create_autocmd("FileType", {
19+
pattern = ensure_installed,
20+
21+
callback = function(args)
22+
local ft = vim.bo[args.buf].filetype
23+
local lang = vim.treesitter.language.get_lang(ft)
24+
25+
-- check if parser is available
26+
if not vim.treesitter.language.add(lang) then
27+
local available = vim.g.ts_available or require("nvim-treesitter").get_available()
28+
if not vim.g.ts_available then
29+
vim.g.ts_available = available
30+
end
31+
32+
if vim.tbl_contains(available, lang) then
33+
-- install treesitter parsers and queries
34+
local install_msg = string.format("Installing parsers and queries for %s", lang)
35+
vim.print(install_msg)
36+
require("nvim-treesitter").install(lang)
37+
end
38+
end
39+
40+
if vim.treesitter.language.add(lang) then
41+
-- start treesitter highlighting
42+
vim.treesitter.start(args.buf, lang)
43+
44+
-- the following two statements will enable treesitter folding
45+
-- vim.wo[0][0].foldexpr = "v:lua.vim.treesitter.foldexpr()"
46+
-- vim.wo[0][0].foldmethod = "expr"
47+
48+
-- enable treesitter-based indentation
49+
-- vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()"
50+
end
51+
end,
52+
})

lua/plugin_specs.lua

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,28 @@ local plugin_specs = {
6262
},
6363
{
6464
"nvim-treesitter/nvim-treesitter",
65-
lazy = true,
65+
lazy = false,
6666
build = ":TSUpdate",
67+
branch = "main",
6768
config = function()
6869
require("config.treesitter")
6970
end,
7071
},
7172
{
7273
"nvim-treesitter/nvim-treesitter-textobjects",
7374
event = "VeryLazy",
74-
branch = "master",
75+
branch = "main",
76+
init = function()
77+
-- Disable entire built-in ftplugin mappings to avoid conflicts.
78+
-- See https://github.com/neovim/neovim/tree/master/runtime/ftplugin for built-in ftplugins.
79+
vim.g.no_plugin_maps = true
80+
81+
-- Or, disable per filetype (add as you like)
82+
-- vim.g.no_python_maps = true
83+
-- vim.g.no_ruby_maps = true
84+
-- vim.g.no_rust_maps = true
85+
-- vim.g.no_go_maps = true
86+
end,
7587
config = function()
7688
require("config.treesitter-textobjects")
7789
end,

0 commit comments

Comments
 (0)