Giter Club home page Giter Club logo

Comments (7)

jmbuhr avatar jmbuhr commented on July 30, 2024 1

Indeed, my dream-API would be to get the ranges of injections directly from the treesitter module in neovim. Upon closer searching, I think this is possible.
https://github.com/neovim/neovim/blob/58618d208acd3827c4e86668529edb619bb9b8dd/runtime/lua/vim/treesitter/query.lua#L210 can get the query for all injections and I believe this includes user-defined ones.
(Or rather the cached version: https://github.com/nvim-treesitter/nvim-treesitter/blob/23dfae4db84f71e2ddb56c024845b322035182ee/lua/nvim-treesitter/query.lua#LL106C1-L106C1 , which should also make it faster!)

And from this we should be able to get those captures:
https://github.com/neovim/neovim/blob/58618d208acd3827c4e86668529edb619bb9b8dd/runtime/doc/treesitter.txt#L484-L505

from otter.nvim.

wouthoekstra avatar wouthoekstra commented on July 30, 2024

Treesitter supports syntax highlighting for nested languages by adding a query to your nvim configuration, by putting it in a queries/LANGUAGE/injections.scm file. Would it be possible to use these same queries?

For example; I made a query to get syntax highlighting for SQL in my Javascript files by creating the following query in ~/.config/nvim/queries/javascript/injections.scm. Because the output variable is '@Sql', treesitter recognizes it as sequel and highlights it as such:

(call_expression
  (member_expression
    object: (identifier) @_object (#eq? @_object "knex")
    property: (property_identifier) @_property (#eq? @_property "raw")
  )

  (arguments
    (template_string) @sql
    (#offset! @sql 0 1 0 -1)
  )
)

from otter.nvim.

wouthoekstra avatar wouthoekstra commented on July 30, 2024

Wow this is great! For me it still does not properly work however, and that might be out of the scope of your package. But the used query injection does not seem to acknowledge the offset I've defined for querying my sql. Which means the backticks in my code are incorrectly parsed as part of the SQL:

For example, the query in my previous comment finds the SQL inside the knex.raw(), but it trips over the backticks:

    const plan = await knex.raw(`
      SELECT plan.name
        FROM subscription
        JOIN plan ON subscription.plan_id = plan.id
       WHERE account_id = :accountId
   `, { accountId: user.accountId }).then(firstOrNull);

This might be more an issue with treesitter, but I am not sure.

from otter.nvim.

jmbuhr avatar jmbuhr commented on July 30, 2024

Looks like the offset directive only affects the range reported by the nodes metadata, not the text returned by get_node_text as currently used (

text = ts.get_node_text(node, main_nr, metadata)
).
However, I can't get treesitter to give me the correct range with directives applied, which supposedly was supposed to happen with the way I do it in the commit mentioned above. Any idea?

from otter.nvim.

wouthoekstra avatar wouthoekstra commented on July 30, 2024

If I read this correctly, someone proposed a change to treesitter to update the injection with the offset: https://github.com/nvim-treesitter/nvim-treesitter/pull/1240/files#diff-81f58ecd0f4978b575136c86619adc8dbc9a0c9e87f87702ddf22615d0ac6c4bR93-R102. But it is already two years old.

from otter.nvim.

jmbuhr avatar jmbuhr commented on July 30, 2024

I might be able to revisit this with the upstream changes to injections

from otter.nvim.

jmbuhr avatar jmbuhr commented on July 30, 2024

Workaround is implemented via #73. The underlying issue remains, but the most common use cases should be fixed.

from otter.nvim.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.