kkawakam / rustyline

Readline Implementation in Rust
https://crates.io/crates/rustyline/
MIT License
1.54k stars 176 forks source link

Highlighter Example #327

Open JesterOrNot opened 4 years ago

JesterOrNot commented 4 years ago

After a few hours of reading the documentation and looking around online I was unable to understand how to implement a highlighter with rustyline can you give me a minimal example?

JesterOrNot commented 4 years ago

Is there an update on this?

gwenn commented 4 years ago

There is a basic example here to highlight matching brackets. You can also search all dependent crates for more advanced examples: https://github.com/search?q=language%3Arust+rustyline+impl+Highlighter+syntax&type=Code Like https://github.com/nushell/nushell/blob/f8be1becf28e29b728bd7cf481d3c717b0831751/src/shell/helper.rs#L67

And you should be aware that, currently, highlighting is not always reevaluated each time you press a key at the end of line. Furthermore, we would like to introduce a Tokenizer to avoid reparsing whole input each time we do completion / highlighting / hinting / validation. So the API may change...

JesterOrNot commented 4 years ago

I was thinking more of an example of how to make your own like if I want to make it so that the exit is yellow or something like that. Instead of just showing the built-in ones

JesterOrNot commented 4 years ago

See https://github.com/JesterOrNot/rustsh

gwenn commented 4 years ago

What's wrong with: https://github.com/nushell/nushell/blob/f8be1becf28e29b728bd7cf481d3c717b0831751/src/shell/helper.rs#L67 ?

JesterOrNot commented 4 years ago

Nothing, from time to time I help with nushell, I think it's awesome, but it's hard to replicate they use external libraries for parsing, etc I am proposing that you show a minimal example of how to create your own syntax highlighter with rustyline for people to build off of.

JesterOrNot commented 4 years ago

See https://github.com/nushell/nushell/tree/master/crates/nu-parser

gwenn commented 4 years ago

I would prefer to add such minimal example when we have a Tokenizer.

JesterOrNot commented 4 years ago

Can I contribute to the Tokenizer I really like this library

gwenn commented 4 years ago

Sure ! There is no draft but only comments here:

JesterOrNot commented 4 years ago

What still needs to be done? Can we create a draft PR?

gwenn commented 4 years ago

A draft PR would be perfect.

JesterOrNot commented 4 years ago

Is there a branch where it is being worked on?

gwenn commented 4 years ago

Nope.

JesterOrNot commented 4 years ago

So what is the idea behind the Tokenizer so that I can get started

gwenn commented 4 years ago

Provide a TokenTree / Document instead of a simple String for the Completer / Highlighter / Hinter / Validator.

JesterOrNot commented 4 years ago

So basically a lexer?

gwenn commented 4 years ago

An API compatible with an incremental lexer / parser. We should pass the whole line in case the lexer / parser is not incremental and a change. We should then have access to an AST / Document which can be used by Completer / Highlighter / Hinter / Validator.