Code Monkey home page Code Monkey logo

Comments (17)

tek avatar tek commented on June 12, 2024

in general, you can use injections to defer to a different grammar, by putting this into $VIMCONFIG/queries/haskell/injections.scm:

(string) @grammar-with-escape-highlights

And I can look into parsing strings directly in the grammar.

from tree-sitter-haskell.

tek avatar tek commented on June 12, 2024

so, the other grammar would need to be specifically parsing only the string

from tree-sitter-haskell.

jpe90 avatar jpe90 commented on June 12, 2024

Got it working after I realized you didn't mean to literally add @grammar-with-escape-highlights, LOL. Thank you!!

from tree-sitter-haskell.

tek avatar tek commented on June 12, 2024

oh really, which grammar did you use?

from tree-sitter-haskell.

jpe90 avatar jpe90 commented on June 12, 2024

I just tried a couple at random and went with Rust because it picked up \" and \n without giving me problems in the project I'm working on

from tree-sitter-haskell.

tek avatar tek commented on June 12, 2024

oh heh. you could create a minimal grammar that only parses strings tho

from tree-sitter-haskell.

jpe90 avatar jpe90 commented on June 12, 2024

I'm not really familiar with treesitter internals (or parsing in general really) to know how to go about that.. would reading https://tree-sitter.github.io/tree-sitter/creating-parsers get me started, or is there other documentation you would also recommend?

from tree-sitter-haskell.

tek avatar tek commented on June 12, 2024

I did a similar thing for quasiquotes here: https://github.com/tek/vim-bundle-conf/tree/master/tree-sitter/exon

it's bare bones, you'll just have to modify grammar.js, it should be fairly intuitive, using that page you linked.

then you'll have to add it in you vim config in a plugin/foo.vim:

lua <<EOF
local parser_config = require "nvim-treesitter.parsers".get_parser_configs()
parser_config.string_grammar_name = {
  install_info = {
    url = "/path/to/string/grammar/",
    files = {"src/parser.c"}
  }
}
EOF

and you can refer to it in an injection with @string_grammar_name after :TSInstall string_grammar_name

from tree-sitter-haskell.

jpe90 avatar jpe90 commented on June 12, 2024

Thank you, that example was very helpful. I ended up giving it a shot. I set up a minimal grammar named "porcupine" which only parses strings into two nodes, "string_fragment" or "escape_sequence". I inject it into haskell with (string) @porcupine in injections.scm, it installs successfully with :TSInstall porcupine and is applied in the right places when I look at a source file with TSPlayground:

escape_sequence

string_fragment

However I can't seem to get the capture groups set up for them. In the minimal grammar, I have a queries/highlights.scm containing:

(string_fragment) @string
(escape_sequence) @escape

and a corresponding "escape" custom capture group set up in my vim config.

But those don't seem to get picked up:

no_captures_found

I'm sure I'm missing something on how capture groups work, so I'll try to figure it out, but let me know if anything obvious jumps out that I'm missing.

from tree-sitter-haskell.

tek avatar tek commented on June 12, 2024

by "in the minimal grammar", do you mean that the highlights file is in the porcupine/queries directory? because that won't get picked up by nvim I think.

from tree-sitter-haskell.

jpe90 avatar jpe90 commented on June 12, 2024

Yeah, I had tried adding those highlights in the haskell queries directory in the fork of nvim-treesitter that my config is hooked up to and treesitter exploded with an error referencing an "invalid node". If I included a porcupine highlight group it got picked up, though. Anyways I should probably find time to figure out how tree-sitter actually works before trying to hack something together :^) my attempt is here

from tree-sitter-haskell.

tek avatar tek commented on June 12, 2024

well, for nvim, you have to put all the queries and injections into the nvim config. so either your nvim-treesitter fork or your local ~/.config/nvim.
and the injection has to be in queries/haskell/injections.scm, while the highlighting for porcupine has to be in queries/porcupine/highlights.scm.

from tree-sitter-haskell.

tek avatar tek commented on June 12, 2024

@jpe90 can this be closed?

from tree-sitter-haskell.

jpe90 avatar jpe90 commented on June 12, 2024

Sure, thanks for trying to help.

from tree-sitter-haskell.

tek avatar tek commented on June 12, 2024

@jpe90 oh, were you no able to implement your goal?

from tree-sitter-haskell.

jpe90 avatar jpe90 commented on June 12, 2024

No, I got stuck getting highlights/injections recognized and felt a little out of my depth. I wanted to make more of an effort to understand how they work before I kept bugging for help. I'll give it another shot when I'm feeling brave :^)

from tree-sitter-haskell.

tek avatar tek commented on June 12, 2024

gotcha, feel free to continue here when you need more input!

from tree-sitter-haskell.

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.