Closed paolosalvatori closed 5 years ago
Hi,
Here is a naive attempt, based on the work here.
TextStyle key = new TextStyle(Brushes.Green, null, FontStyle.Regular);
TextStyle str = new TextStyle(Brushes.Orange, null, FontStyle.Regular);
TextStyle boo = new TextStyle(Brushes.DarkBlue, null, FontStyle.Regular);
TextStyle nul = new TextStyle(Brushes.DarkGray, null, FontStyle.Regular);
void fastColoredTextBox1_TextChanged(object sender, FastColoredTextBoxNS.TextChangedEventArgs e) {
e.ChangedRange.ClearFoldingMarkers();
e.ChangedRange.SetFoldingMarkers("{", "}");
e.ChangedRange.SetFoldingMarkers("\\[", "\\]");
fastColoredTextBox1.Range.ClearStyle(key, str, boo, nul);
foreach(Range found in fastColoredTextBox1.GetRanges(@"(¤(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\¤])*¤(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)".Replace('¤', '"'))) {
if(Regex.IsMatch(found.Text, @"^¤".Replace('¤', '"'))) {
if(Regex.IsMatch(found.Text, ":$")) {
found.SetStyle(key);
}
else {
found.SetStyle(str);
}
}
else if(Regex.IsMatch(found.Text, "true|false")) {
found.SetStyle(boo);
}
else if(Regex.IsMatch(found.Text, "null")) {
found.SetStyle(nul);
}
}
}
I'm not a big fan nor a pro in regex, so if someone feels like to optimize it a bit, please go ahead.
Has anyone tried this regex? If so, could you please report your findings, whether or not it needs to be improved or if there are any visible or apparent issues with the highlighting. I would love to implement this into my text editor!
JSON built-in highlighter added.
@PavelTorgashov Dear sir, first of all, amazing work on this control. Thank you so much! Second, it would be nice if you could release a Nuget package that includes this native JSON highlighter. Is there any chance you can get around to do this?
Btw, I know this issue is closed, so I will add my request to an other open issue about this. https://github.com/PavelTorgashov/FastColoredTextBox/issues/183
@Larry57 : I got what '¤' means (just placeholder for quote). But ONE regex for all?? It's ridiculous! At least it requires DFA or PEG parser - then we can be sure no rubbish is highlighted. Esp. with scientific numbers or unmatched/unbalanced braces.
@Larry57 : I got what '¤' means (just placeholder for quote). But ONE regex for all?? It's ridiculous! At least it requires DFA or PEG parser - then we can be sure no rubbish is highlighted. Esp. with scientific numbers or unmatched/unbalanced braces.
Totally agreed, this need some more serious work. As it is, this only fits my own simple and not-so-important use case.
But I think this code is no longer required as Pavel has integrated native JSON support. (Just the Nugget package needs to be updated).
The above (regex)code does work however, at least for me for previewing valid JSON content. ;-)
Hi Pavel I'm not sure you are still maintaining this library, but just in case, do you have any plans to provide out-of-the-box support for JSON formatting? I know that the library is extensible, so in case I can do it by myself. Thanks! Paolo