My Neovim Plugins #10 — nvim-treesitter
With nvim-treesitter, I have a lot going on in my config. Let's split it up a bit.
The basic treesitter config is mostly the default from their GitHub page but trimmed down. Because I will not have “javascript” to be ignored on install and also don't need to disable highlights. The other thing I use, which was before the playground, to show the treesitter objects under my cursor. Which is helpful for developing colorschemes.
Config: treesitter
return {
"nvim-treesitter/nvim-treesitter",
build = ":TSUpdate",
keys = {
{ "<leader>T", ":Inspect<cr>" },
},
config = function()
require("nvim-treesitter.configs").setup({
ensure_installed = "all",
sync_install = false,
auto_install = true,
ignore_install = {},
highlight = { enable = true },
})
end,
}
Config: treesitter-refactor
To extend treesitter, you can add dependencies to the return object. For example refactor. Which I use mainly to rename things or highlight all definitions of a variable under my cursor.
return {
-- ...
dependencies = {
"nvim-treesitter/nvim-treesitter-refactor",
},
-- ...
config = function()
require("nvim-treesitter.configs").setup({
-- ...
refactor = {
highlight_definitions = {
enable = true,
clear_on_cursor_move = true,
},
smart_rename = {
enable = true,
keymaps = {
smart_rename = "grr",
},
},
},
})
end,
}
Config: treesitter-textobject
The next dependency I use is textobjects. I installed it, just to change/delete text inner or outer of a template literal.
return {
-- ...
dependencies = {
"nvim-treesitter/nvim-treesitter-textobjects",
},
-- ...
config = function()
require("nvim-treesitter.configs").setup({
-- ...
textobjects = {
select = {
enable = true,
lookahead = true,
keymaps = {
["af"] = "@function.outer",
["if"] = "@function.inner",
["ak"] = "@block.outer",
["ik"] = "@block.inner",
-- Add this for template literals
["a`"] = { query = "@template_literal.outer", desc = "Select outer part of a template literal" },
["i`"] = { query = "@template_literal.inner", desc = "Select inner part of a template literal" },
},
},
},
})
end,
}
To archive this, I also need to add query files:
;; ./nvim/queries/javascript/textobjects.scm
;; Capture the entire template literal block (outer)
((template_string) @template_literal.outer)
;; Capture the inner text content only (inner)
((template_substitution) @template_literal.inner)
Config: treesitter-context
A small little dependency I live is context. It will show you the context of an object or function you are in. Which is a nice helper on smaller screens.
return {
-- ...
dependencies = {
"nvim-treesitter/nvim-treesitter-context",
},
-- ...
config = function()
-- ...
require("treesitter-context").setup({
enable = true,
max_lines = 5,
multiline_threshold = 1,
trim_scope = "inner",
})
end,
}
Config: colorizer
Nothing related to treesitter, but it has something to do with the code, so I thought the initialization fits good for colorizer. It simply highlights colors inside neovim.
return {
-- ...
dependencies = {
"norcalli/nvim-colorizer.lua",
},
-- ...
config = function()
-- ...
require("colorizer").setup({
"lua",
"css",
"javascript",
"javascriptreact",
"scss",
"typescript",
"typescriptreact",
"yaml",
}, {
css = true,
css_fn = true,
})
end,
}
Config: all together
return {
"nvim-treesitter/nvim-treesitter",
build = ":TSUpdate",
dependencies = {
"nvim-treesitter/nvim-treesitter-textobjects",
"nvim-treesitter/nvim-treesitter-context",
"nvim-treesitter/nvim-treesitter-refactor",
"norcalli/nvim-colorizer.lua",
},
keys = {
{ "<leader>T", ":Inspect<cr>" },
},
config = function()
require("nvim-treesitter.configs").setup({
ensure_installed = "all",
sync_install = false,
auto_install = true,
ignore_install = {},
highlight = { enable = true },
refactor = {
highlight_definitions = {
enable = true,
clear_on_cursor_move = true,
},
smart_rename = {
enable = true,
keymaps = {
smart_rename = "grr",
},
},
},
textobjects = {
select = {
enable = true,
lookahead = true,
keymaps = {
["af"] = "@function.outer",
["if"] = "@function.inner",
["ak"] = "@block.outer",
["ik"] = "@block.inner",
-- Add this for template literals
["a`"] = { query = "@template_literal.outer", desc = "Select outer part of a template literal" },
["i`"] = { query = "@template_literal.inner", desc = "Select inner part of a template literal" },
},
},
},
})
require("colorizer").setup({
"lua",
"css",
"javascript",
"javascriptreact",
"scss",
"typescript",
"typescriptreact",
"yaml",
}, {
css = true,
css_fn = true,
})
require("treesitter-context").setup({
enable = true,
max_lines = 5,
multiline_threshold = 1,
trim_scope = "inner",
})
end,
}
80 of #100DaysToOffload
#log #neovim
Thoughts? Discuss...