A simple bracket matcher for JavaScript

jQuery is a great Framework, but it can be a bit overwhelming in terms of nesting, especially when working with sub-par JavaScript editors like Visual Studio. When working with jQuery, you may end up with many open brackets { that need closing brackets }, but then there are also open brackets like ({ that need }). So when you are at the end of a file that has a bunch of closing brackets, it can be painful to try to figure out if all of them are properly closed.

I hacked together a simple Bracket Matcher tool that takes a JavaScript text and highlights the blocks:
Bracket Matcher

It is not finished - }) end tags are not properly highlighted (even though ({ are) and the "expectedEndTag" part that should automatically tell me if my blocks are properly closed doesn't work. Reason is simply that the current version was good enough to solve a problem at hand, so I might improve it later.

Create a simple WinForms application with a TextBox (MultiLine) and a RichTextEdit (WordWrap = false) and wire it up like this:

private void button1_Click(object sender, EventArgs e)
{
    richTextBox1.Clear();
    string input = textBox1.Text.Replace("\r\n", "\n");

    var expectedEndTokens = new Stack<string>();
    richTextBox1.SelectionColor = Color.Black;
    for (int i = 0; i < input.Length; i++)
    {
        char cprev = (i > 0) ? input[i - 1] : char.MinValue;
        char c = input[i];
        char cnext = (i < input.Length - 1) ? input[i + 1] : char.MinValue;
        if (c == '{')
        {
            ColorChooser.SelectNextColor();
            richTextBox1.Select(richTextBox1.Text.Length - 1, 1);
            richTextBox1.SelectionColor = ColorChooser.Color;
            if (cprev == '(')
            {
                expectedEndTokens.Push("})");
            }
            else
            {
                expectedEndTokens.Push("}");
            }
        }

        if (cprev == '\n') richTextBox1.SelectionColor = ColorChooser.Color;

        richTextBox1.AppendText(c.ToString());

        if (c == '}')
        {
            ColorChooser.SelectPreviousColor();
            richTextBox1.SelectionColor = ColorChooser.Color;
        }
    }
}

private static class ColorChooser
{
   // Change these to whatever you prefer, add or remove if you want
    private static readonly Color[] HighlightColors = new Color[]{
        Color.Blue,
        Color.DarkGreen,
        Color.Violet,
        Color.Red,
        Color.DarkCyan,
        Color.Gray,
        Color.Magenta
    };

    private static int currentColorIndex = 0;

    public static Color Color
    {
        get
        {
            return HighlightColors[currentColorIndex];
        }
    }

    public static void SelectNextColor()
    {
        lock (HighlightColors)
        {
            currentColorIndex++;
            if (currentColorIndex >= HighlightColors.Length) currentColorIndex = 0;
        }
    }

    public static void SelectPreviousColor()
    {
        lock (HighlightColors)
        {
            currentColorIndex--;
            if (currentColorIndex < 0) currentColorIndex = HighlightColors.Length-1;
        }
    }
}

Comments (1)

MidhatMay 14th, 2010 at 07:29

Hi

Is it finished yet?.

I believe this would make a great visual studio plugin.