With neovim, you can show parts of your code as other code languages, e.g. a string that contains javascript code, or a python docstring that contains restructured text:
nvim-treesitter already does this for a lot of use cases. With this plugin, you can toggle pre-set and custom language injections in your session. When you are happy with the settings, save 💽 them in the queries
-folder in your config and 🗑️ delete this plugin.
With the :InjectmeToggle
command, you get a Telescope picker (see Commands for details) to change the injections in your session:
Use a package manager, e.g. lazy.nvim. Requires Neovim >= v0.9.4
{
'Dronakurl/injectme.nvim',
dependencies = {
"nvim-treesitter/nvim-treesitter",
"nvim-lua/plenary.nvim",
"nvim-telescope/telescope.nvim",
},
-- This is for lazy load and more performance on startup only
cmd = { "InjectmeToggle", "InjectmeSave", "InjectmeInfo" , "InjectmeLeave"},
}
InjectmeToggle
: With no arguments, the picker window is started. With tab completion, you can also directly set lanuage and injection, e.g.:InjectmeToggle python rst_for_docstring
InjectmeSave
: Saves the injections settings to your runtime. When you are happy with this setting and do not want to change it dynamically any more, you can safely delete the plugin, the injections will continue to work.InjectmeLeave
: The plugin saves your settings locally in the.local/share/nvim/state_injectme.lua
file. When you want to delete the plugin, you can use this to clean up.InjectmeInfo
: Display current configured injections in the messages
🚧 This is only stuff for my use cases or for testing, please contribute 🚧
rst_for_docstring
: Python docstring is shown as restructured textjavascript_variables
: All string variables ending withjs
are shown as javascriptcss_variables
: All string variables ending withcss
are shown as csshtml_variables
: All string variables ending withhtml
are shown as htmlstyle_attribute_css
: All strings in a.style('somethin')
attribute are shown as cssloads_attribute_json
: All strings in a.loads('somethin')
attribute are shown as json
codeblocks_as_lua
: All code blocks are shown as lua
pythoncode
: All text in apre
tag with classpython
is shown as python
Setup calling setup()
. This is optional and below are standard values for all further settings.
require("injectme").setup({
mode = "standard",
-- "all"/ "none" if all/no injections should be activated on startup
-- When you use, lazy loading, call :InjectemeInfo to activate
-- "standard", if no injections should be changed from standard settings in
-- the runtime directory, i.e. ~/.config/nvim/queries/<language>/injections.scm
reload_all_buffers = true,
-- after toggling an injection, all buffers are reloaded to reset treesitter
-- you can set this to false, and avoid that the plugin asks you to save buffers
-- before changing an injection
reset_treesitter = true,
-- after toggling an injections, the treesitter parser is reset
-- Currently, this does nothing, see this discussion on github
-- https://github.com/nvim-treesitter/nvim-treesitter/discussions/5684
})
I created this plugin so it is easier to get started with custom injections in treesitter. Another way would be just to set up the treesitter config on your own. I found this tedious() and decided to provide this config-kickstart plugin. (: Now, I found that nvim-treesitter is not really hard to configure after 🤦 reading the docs 📖 and I have doubts if this plugin will be useful to anyone. Maybe, the following list is 🙂)
Here are the steps, if you want to do this without this plugin:
- Install nvim-treesitter/nvim-treesitter
- Install nvim-treesitter/playground (not needed when your use neovim>0.10)
- Read the docs of the treesitter query language
- Read the
:treesitter-language-injections
- Get some inspiration for queries from the standard injections in
~/.local/share/nvim/lazy/nvim-treesitter/queries/{language}/injections.scm
- Start treesitter playground with
:TSPlaygroundToggle
, and try out queries (or:InspectTree
in neovim>0.10), by hittinge
(or:EditQuery
in neovim>0.10) - Open the file injections files by
:TSEditQueryUser injections markdown
, for example - Put your queries there and do not forget the
;extends
comment on top, when you want to keep the standard queries provided by nvim-treesitter
💡 Another reason: I was inspired to make a plugin by this YouTube video about plugin development.
All contributions are welcome!
- In particular, please submit useful injections in the file preset_injections.lua.