Code Monkey home page Code Monkey logo

Comments (8)

maxbrunsfeld avatar maxbrunsfeld commented on May 28, 2024 1

For the general strategy, the configuration interface for Atom's Tree-sitter-based syntax highlighting is documented in the Atom book. The implementation is here. In addition, there is a completely separate Rust implementation that's a bit newer, but is not yet documented. This new implementation is what uses the highlights.css files in some of the Tree-sitter parser repositories.

Unfortunately, the best Tree-sitter API to use, the TreeCursor, is not currently exposed to Python in py-tree-sitter. The purpose of the TreeCursor is to let you walk the syntax tree efficiently, in a depth-first fashion, without recursion. Both of the highlighting systems that I mentioned above rely on the TreeCursor.

from py-tree-sitter.

maxbrunsfeld avatar maxbrunsfeld commented on May 28, 2024 1

Version 0.0.7 has the TreeCursor. I would suggest using an approach similar to what Atom does, where it uses the TreeCursor to walk the tree, and render highlights according to some mapping of Tree-sitter nodes to highlighting classes. CSS is a good language for defining this mapping: that's what I've been using so far. And there are two separate implementations of the CSS mapping: one in Atom, and one in Tree-sitter itself.

from py-tree-sitter.

brupelo avatar brupelo commented on May 28, 2024 1

Max, this is great news and I must say this new release is pure awesomeness, really exciting ;)

Tomorrow I'll be playing with 0.0.7 for sure... at first I was considering if it'd be worth factor out my highlighters+styles (right now i'm supporting syntect + pygments) so the editor would support pygments+syntect+tree-sitter but I can't think of any good reason for still supporting those regex-based engines, it adds over-complexity for no real reason.

At first I was scared tree-sitter wasn't supporting as many languages as pygments but then after checking how the grammar.js files look like I'm not scared anymore, so I think I'll throw-off to the bin the other highlighters and become a tree-sitter user :D

Let me tell you I think py-tree-sitter has the potential to become the "standard" python reference library for parsing/highlighting (right now i'd say pygments is the winner by far, isn't?)... IMHO one of the keys that will make that to happen is if the number of available tree-sitter grammars will grow consistently. I think one of the keys for this to happen is the file-format, at the end of the day new contributors will add stuff if that can be done easily ;) . Think of antlr, why did it success? Because creating grammars there only requires being familiar with EBNF and you'll get up and running...

Anyway, thank you very much, great job with 0.0.7! :D

from py-tree-sitter.

issue-label-bot avatar issue-label-bot commented on May 28, 2024

Issue-Label Bot is automatically applying the label question to this issue, with a confidence of 0.91. Please mark this comment with 👍 or 👎 to give our bot feedback!

Links: app homepage, dashboard and code for this bot.

from py-tree-sitter.

brupelo avatar brupelo commented on May 28, 2024

Thanks, tomorrow I'll review all this info carefully, specially that Rust implementation may help... in the past I'd port syntect (rust library) to python so I've got a bit of experience wrapping rust code into python...

In any case, before using any of the complex efficient solution you've posted I'd like to implement the most naive way that at least allowed me to work with small files <100kb of python code in the editor, what'd you suggest?

DFS tree traversal in pure python would be that bad? Also, how can I print the missing information in my above snippet?

from py-tree-sitter.

maxbrunsfeld avatar maxbrunsfeld commented on May 28, 2024

I've got a bit of experience wrapping rust code into python...

Creating the Python bindings for TreeCursor might be familiar to you then! 😁

Otherwise, I'm sure it's possible to perform syntax highlighting with the existing APIs, but it's not something I've explored.

from py-tree-sitter.

maxbrunsfeld avatar maxbrunsfeld commented on May 28, 2024

Ok, I added bindings to the new query API. See the pattern-matching section of the README. There are queries for syntax highlighting checked into some of the language repos (Example).

There is still a little bit of work to do to get the python binding supporting all of the features of the query API. Specifically, the match? predicates (for doing regex matches on nodes' text) will not do anything right now. I'm not sure when I'll get around to this, but even without this, syntax-highlighting python should work alright.

from py-tree-sitter.

lunixbochs avatar lunixbochs commented on May 28, 2024

We now have match? predicates merged in HEAD, so I'm closing this. Let me know if you need anything more for this use case.

from py-tree-sitter.

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.