Neovim-from-scratch icon indicating copy to clipboard operation
Neovim-from-scratch copied to clipboard

syntax highlighting and snippet completion for injected languages

Open tvdab opened this issue 2 years ago • 2 comments

I found it difficult to make syntax highlighting and snippet completion work for pieces of code that are written in a different language than the language of the file they are embedded in. I found a hacky solution to cover my situation, but I would like to see a better and more easy way to achieve this.

By default a snippet of html-code inside a javascript file looks like this: image

So by default there is no syntax highlighting nor html-snippets available in this case.

These are the steps needed to make syntax highlighting and html-snippets work for this case:

  • Add a treesitter query that recognizes the above example is a piece of html-code: image

I added this query inside ~/.local/share/nvim/site/pack/packer/start/nvim-treesitter/queries/ecma/injections.scm which probably isn't the best idea. This will probably be deleted when doing a packer clean update?

Now syntax highlighting is working:

image

But still no html-snippet expansion...

  • To make completion work I did the following changes inside lua/user/cmp.lua

image

Now syntax highlighting and html-snippets are working: image

All sugestions to improve on this are welcome!

tvdab avatar Jan 22 '22 17:01 tvdab

open an issue or pr for nvim-treesitter with this injection query

max397574 avatar Jan 22 '22 19:01 max397574

open an issue or pr for nvim-treesitter with this injection query

I would prefer to see a more general solution where injected languages get detected automatically. Not sure how that should/could work though.

tvdab avatar Jan 25 '22 20:01 tvdab