DariusCorvus / tree-sitter-language-injection.nvim

A NVIM Plugin which applies inline language injections, when a string above contains the name of the language or the string contains a comment with the language name.
16 stars 3 forks source link
nvim nvim-configs nvim-lua nvim-plugin python sql surrealdb treesitter typescript

tree-sitter-language-injection.nvim

A NVIM Plugin which applies inline language injections, when a string above contains the name of the language or the string contains a comment with the language name. You can configure it with own queries for languages that aren't built in yet.

Features

Comment Inline

When a string is found, and the first line is language specifc comment, for the desired language, followed by the language name, syntax highlighting gets applied.

as example we use the language typescript and want that the string gets highlighted as sql

const select = `
--sql
SELECT * FROM user
WHERE active = 1
`;

which results in

typescript_inline_sql

Comment Above

When a comment is found above a variable, and starts with the name of the desired language, syntax highlighting gets applied.

as example we use the language typescript and want that the string gets highlighted as sql

// sql
const select = `
SELECT * FROM user
WHERE active = 1
`;

which results in

typescript_above_sql

Configuration Of A New Language

To add as example javascript sql string inline comment language injection you need to provide the query for string and the langs you want to match, thename is the name of the treesitter parser and the match is the pattern to match the comment inside a string.

To add as example javascript sql comment above language injection you need to provide the query for comment and the langs you want to match, the name is the name of the treesitter parser and the match is the pattern to match the comment.

it's already inbuilt by the way, so tinker with a language you need or create a issue and i see if i can help.

return {
  "dariuscorvus/tree-sitter-language-injection.nvim",
  opts = {
    javascript = {
      string = {
        langs = {
          { name = "sql", match = "^(\r\n|\r|\n)*-{2,}( )*{lang}"}
        },
        query = [[
; query
;; string {name} injection
((string_fragment) @injection.content
                   (#match? @injection.content "{match}")
                   (#set! injection.language "{name}"))
        ]]
      },
      comment = {
        langs = {
          { name = "sql", match = "^//+( )*{lang}( )*"}
        },
        query = [[
; query
;; comment {name} injection
((comment)
 @comment .
 (lexical_declaration
   (variable_declarator
     value: [
             (string(string_fragment)@injection.content)
             (template_string(string_fragment)@injection.content)
             ]@injection.content)
   )
  (#match? @comment "{match}")
  (#set! injection.language "{name}")
 )
        ]]
      }
    }
  }
}

Configurable

Now its possible to add languages and their treesitter scm quiries via the setup.

before the configuration:

Screenshot 2024-10-10 111556

configuration:

Screenshot 2024-10-10 111439

after the configuration:

Unbenannt

Installation

Packer

use({"dariuscorvus/tree-sitter-language-injection.nvim", after="nvim-treesitter"})

Setup

require("tree-sitter-language-injection").setup()

Lazy

return {
  "dariuscorvus/tree-sitter-language-injection.nvim",
  opts = {} 'calls the setup
}

Built In Languages