KristofferC / OhMyREPL.jl

Syntax highlighting and other enhancements for the Julia REPL
https://kristofferc.github.io/OhMyREPL.jl/latest/
Other
768 stars 60 forks source link

Use post-parse `JuliaSyntax.tokenize()` API #306

Closed c42f closed 1 year ago

c42f commented 1 year ago

Going through the parser allows us to be more precise and complete.

For example it automatically:

Also uses is_error to detect error tokens and highlight them with the error color.

Requires https://github.com/JuliaLang/JuliaSyntax.jl/pull/221

As I was writing this, I noticed that the syntax highlighter has some heuristics in it to act a bit like a parser (just a very little bit!). For example, highlighting quoted symbols a different color.

So I'm not sure this is the best approach long term - maybe we should just be using the parsed GreenTree instead for highlighting. It would give us several options for richer highlighting. Also, if using the parser itself, we'd be able to highlight any syntax error ranges (not just token errors).

Regardless of that, though, this is a straightforward modification we can do right away...

Screenshot showing outer and abstract contextual keywords highlighted correctly and some token errors asdf

c42f commented 1 year ago

Note that the tests here won't work until https://github.com/JuliaLang/JuliaSyntax.jl/pull/221 is merged and a new JuliaSyntax release is deployed.

c42f commented 1 year ago

I released JuliaSyntax-0.3.3 to make this work if you can kick off CI again. @KristofferC 0.3.3 contains your precompile changes and the new tokenize() API required by this PR, but not the breaking changes to the tree data structures I've been doing in the last week.

codecov-commenter commented 1 year ago

Codecov Report

Patch coverage: 83.33% and project coverage change: +0.07 :tada:

Comparison is base (a7f4ca7) 56.61% compared to head (1cd38d4) 56.69%.

:mega: This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #306 +/- ## ========================================== + Coverage 56.61% 56.69% +0.07% ========================================== Files 15 15 Lines 1111 1113 +2 ========================================== + Hits 629 631 +2 Misses 482 482 ``` | [Impacted Files](https://codecov.io/gh/KristofferC/OhMyREPL.jl/pull/306?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kristoffer+Carlsson) | Coverage Δ | | |---|---|---| | [src/MarkdownHighlighter.jl](https://codecov.io/gh/KristofferC/OhMyREPL.jl/pull/306?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kristoffer+Carlsson#diff-c3JjL01hcmtkb3duSGlnaGxpZ2h0ZXIuamw=) | `0.00% <ø> (ø)` | | | [src/OhMyREPL.jl](https://codecov.io/gh/KristofferC/OhMyREPL.jl/pull/306?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kristoffer+Carlsson#diff-c3JjL09oTXlSRVBMLmps) | `20.75% <ø> (ø)` | | | [src/passes/BracketHighlighter.jl](https://codecov.io/gh/KristofferC/OhMyREPL.jl/pull/306?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kristoffer+Carlsson#diff-c3JjL3Bhc3Nlcy9CcmFja2V0SGlnaGxpZ2h0ZXIuamw=) | `88.63% <ø> (ø)` | | | [src/passes/RainbowBrackets.jl](https://codecov.io/gh/KristofferC/OhMyREPL.jl/pull/306?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kristoffer+Carlsson#diff-c3JjL3Bhc3Nlcy9SYWluYm93QnJhY2tldHMuamw=) | `100.00% <ø> (ø)` | | | [src/repl.jl](https://codecov.io/gh/KristofferC/OhMyREPL.jl/pull/306?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kristoffer+Carlsson#diff-c3JjL3JlcGwuamw=) | `13.06% <0.00%> (ø)` | | | [src/passes/SyntaxHighlighter.jl](https://codecov.io/gh/KristofferC/OhMyREPL.jl/pull/306?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kristoffer+Carlsson#diff-c3JjL3Bhc3Nlcy9TeW50YXhIaWdobGlnaHRlci5qbA==) | `68.75% <100.00%> (+0.80%)` | :arrow_up: | | [src/repl\_pass.jl](https://codecov.io/gh/KristofferC/OhMyREPL.jl/pull/306?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kristoffer+Carlsson#diff-c3JjL3JlcGxfcGFzcy5qbA==) | `61.05% <100.00%> (ø)` | | Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kristoffer+Carlsson). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kristoffer+Carlsson)

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.

KristofferC commented 1 year ago

I need to update the colorschemes to show a nice red background on errors like in your screenshot.

c42f commented 1 year ago

Oops, I'd broken the docs build with this. Hopefully that's fixed now.

c42f commented 1 year ago

Cool yes updating the color schemes makes sense. I just have this in my startup.jl at the moment.

            cs = OhMyREPL.Passes.SyntaxHighlighter._create_distinguished()
            OhMyREPL.Passes.SyntaxHighlighter.error!(cs,
                OhMyREPL.Crayon(background = (120,50,50)))
            OhMyREPL.Passes.SyntaxHighlighter.add!("myscheme", cs)

We can maybe do better with the errors if we use the full parser but there's a bit of a balance there in terms of avoiding distracting the user when they haven't finished typing. (Might need some heuristic to not report errors due to incomplete syntax etc.) Something for another time anyway.