Click here to Skip to main content
12,067,928 members (26,552 online)
Click here to Skip to main content

Stats

21.8K views
420 downloads
36 bookmarked
Posted

Visual Studio Add-in for testing regular expressions

, 1 Dec 2010 CPOL
An add-in for debugging and creating regular expression directly in Visual Studio
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace RegexTester.UserControls
{
    /// <summary>
    /// Extension of RichTextBox for highlighting syntax.
    /// </summary>
    public partial class SyntaxHighlightRichTextBox : RichTextBox
    {
        private SyntaxHighlightRichTextBoxModel model = new SyntaxHighlightRichTextBoxModel();
        private int selectedLeftIndex = -1;
        private int selectedRightIndex = -1;

        /// <summary>
        /// C.tor.
        /// </summary>
        public SyntaxHighlightRichTextBox()
        {
            this.KeyUp += setSyntaxHighlighting;
            this.MouseUp += setSyntaxHighlighting;
            this.GotFocus += setSyntaxHighlighting;
            this.LostFocus += lostFocus;
            this.KeyDown += resetSyntaxHighlighting;
            this.MouseDown += resetSyntaxHighlighting;
        }

        private void SetIndexColor(int index, Color color)
        {
            this.Select(index, index + 1);
            this.SelectionBackColor = color;
        }

        private string currentText = null;
        private int currentIndex = -1;

        private void lostFocus(object sender, EventArgs e)
        {
            resetSyntaxHighlighting(sender, e);
            currentIndex = -1;
        }
        
        [DllImport("user32.dll")]
        public static extern bool LockWindowUpdate(IntPtr hWndLock);

        private void resetSyntaxHighlighting(object sender, EventArgs e)
        {
            // Clear any matching parentheses highlighting
            if (this.SelectionLength == 0)
            {
                LockWindowUpdate(this.Handle);
                int index = this.SelectionStart;
                if (selectedLeftIndex != -1)
                {
                    SetIndexColor(selectedLeftIndex, Color.White);
                    selectedLeftIndex = -1;
                }
                if (selectedRightIndex != -1)
                {
                    SetIndexColor(selectedRightIndex, Color.White);
                    selectedRightIndex = -1;
                }
                this.Select(index, 0);
                LockWindowUpdate(IntPtr.Zero);
            }
        }

        private void setSyntaxHighlighting(object sender, EventArgs e)
        {
            // Check if highlighting parentheses is required.
            if (this.SelectionLength == 0 &&
                (currentIndex != this.SelectionStart || this.Text != currentText))
            {
                LockWindowUpdate(this.Handle);
                currentText = this.Text;
                currentIndex = this.SelectionStart;
                int match = model.GetMatchingParentheses(this.Text, currentIndex);
                if (match != -1)
                {
                    if (currentIndex > match)
                    {
                        selectedLeftIndex = match;
                        selectedRightIndex = currentIndex - 1;
                    }
                    else
                    {
                        selectedLeftIndex = currentIndex;
                        selectedRightIndex = match;
                    }
                    this.Select(selectedLeftIndex, 1);
                    this.SelectionBackColor = Color.Cyan;
                    this.Select(selectedRightIndex, 1);
                    this.SelectionBackColor = Color.Cyan;
                }
                this.Select(currentIndex, 0);
                LockWindowUpdate(IntPtr.Zero);
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Andreas Andersen
Software Developer (Senior)
Denmark Denmark
.NET developer. I wanted to be first an astronaut, then a jet pilot, but when I got a Commodore 64 for Christmas I never looked back. Also I would never have qualified for the first two things and everybody knows computer programmers get all the girls.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160208.1 | Last Updated 1 Dec 2010
Article Copyright 2010 by Andreas Andersen
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid