Dronakurl / injectme.nvim

Neovim plugin to help setup language injections with treesitter
MIT License
40 stars 2 forks source link
neovim neovim-plugin nvim nvim-lua nvim-plugin syntax-highlighting tree-sitter treesitter

injectme.nvim - Language highlights in your code

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:

Installation

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"},
}

Commands

Available custom injection queries

🚧 This is only stuff for my use cases or for testing, please contribute 🚧

python

markdown

html

(Optional) Configuration

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
})

Why this plugin?

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:

  1. Install nvim-treesitter/nvim-treesitter
  2. Install nvim-treesitter/playground (not needed when your use neovim>0.10)
  3. Read the docs of the treesitter query language
  4. Read the :treesitter-language-injections
  5. Get some inspiration for queries from the standard injections in ~/.local/share/nvim/lazy/nvim-treesitter/queries/{language}/injections.scm
  6. Start treesitter playground with :TSPlaygroundToggle, and try out queries (or :InspectTree in neovim>0.10), by hitting e (or :EditQuery in neovim>0.10)
  7. Open the file injections files by :TSEditQueryUser injections markdown, for example
  8. 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.

Contribution

All contributions are welcome!