PavelTorgashov / FastColoredTextBox

Fast Colored TextBox for Syntax Highlighting. The text editor component for .NET.
Other
1.22k stars 465 forks source link

JSON support #130

Closed paolosalvatori closed 5 years ago

paolosalvatori commented 6 years ago

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

Larry57 commented 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.

Hexman768 commented 5 years ago

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!

PavelTorgashov commented 5 years ago

JSON built-in highlighter added.

SjaaLom commented 2 years ago

@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?

SjaaLom commented 2 years ago

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

WrongBit commented 2 years ago

@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 commented 2 years ago

@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.

SjaaLom commented 2 years ago

But I think this code is no longer required as Pavel has integrated native JSON support. (Just the Nugget package needs to be updated).

SjaaLom commented 2 years ago

The above (regex)code does work however, at least for me for previewing valid JSON content. ;-)