自定义算法代码高亮

2015-12-13 15:49:32  访问(4277) 赞(0) 踩(0)


相关下载:SlowX.HighLightCodeApp[code]  SlowX.HighLightCodeApp[release]  百度网盘     



  • 
    
            // 高亮SQL //
            CodeHighLight.SQLHighLight(richTextBox_SQL代码);
    
            // 高亮CS //
            CodeHighLight.CSharpHighLight(richTextBox_CS代码);
    
    
  • 
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace SlowX.HighLightCodeApp.Enums
    {
        /// <summary>
        /// 
        /// </summary>
        public partial class HighLightModel
        {
            /// <summary>
            /// 高亮类型
            /// </summary>
            public enum EmHighLightModel
            {
                /// <summary>
                /// 无
                /// </summary>
                无 = 1,
                /// <summary>
                /// 纯文本
                /// </summary>
                纯文本,
                /// <summary>
                /// 注释
                /// </summary>
                注释3,
                /// <summary>
                /// 注释2
                /// </summary>
                注释2,
                /// <summary>
                /// 注释范围
                /// </summary>
                注释范围,
                /// <summary>
                /// 字符串
                /// </summary>
                字符串,
                /// <summary>
                /// 字符串at
                /// </summary>
                字符串at,
                /// <summary>
                /// 字符
                /// </summary>
                字符,
                /// <summary>
                /// 关键字
                /// </summary>
                关键字,
                /// <summary>
                /// 类
                /// </summary>
                类,
                /// <summary>
                /// SQL字符
                /// </summary>
                SQL字符,
                /// <summary>
                /// SQL注释
                /// </summary>
                SQL注释
            }
    
        }
    }
    
    
  • 
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace SlowX.HighLightCodeApp.Classes.HL
    {
        /// <summary>
        /// 查找闭合关系的代码
        /// </summary>
        [Serializable]
        public class FindChar
        {
            /// <summary>
            /// 查找闭合关系的代码
            /// </summary>
            public FindChar()
            {
            }
    
            /// <summary>
            /// 查找闭合关系的代码
            /// </summary>
            /// <param name="_em"></param>
            /// <param name="_strChar"></param>
            /// <param name="_endChar"></param>
            public FindChar
                (
                    SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel _em,
                    string _strChar,
                    string _endChar
                )
            {
                strChar = _strChar;
                endChar = _endChar;
                em = _em;
            }
    
            /// <summary>
            /// 类型
            /// </summary>
            public SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel em
                =
                SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.无;
    
            /// <summary>
            /// 开始符
            /// </summary>
            public string strChar = null;
    
            /// <summary>
            /// 结束符
            /// </summary>
            public string endChar = null;
    
            /// <summary>
            /// CSharp模式的
            /// </summary>
            /// <returns></returns>
            public static List<FindChar> FindCharListGet()
            {
                List<FindChar> theResult = new List<FindChar>();
                FindChar item = null;
    
                item = new FindChar();
                item.em = SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.注释3;
                item.strChar = "///";
                item.endChar = "" + '\n';
                theResult.Add(item);
    
                item = new FindChar();
                item.em = SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.注释2;
                item.strChar = "//";
                item.endChar = "" + '\n';
                theResult.Add(item);
    
                item = new FindChar();
                item.em = SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.注释范围;
                item.strChar = "/*";
                item.endChar = "*/";
                theResult.Add(item);
    
                item = new FindChar();
                item.em = SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.字符串at;
                item.strChar = "@\"";
                item.endChar = "\"";
                theResult.Add(item);
    
                item = new FindChar();
                item.em = SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.字符串;
                item.strChar = "\"";
                item.endChar = "\"";
                theResult.Add(item);
    
                item = new FindChar();
                item.em = SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.字符;
                item.strChar = "\'";
                item.endChar = "\'";
                theResult.Add(item);
    
                return theResult;
            }
    
    
    
            /// <summary>
            /// CSharp模式的
            /// </summary>
            /// <returns></returns>
            public static List<FindChar> FindCharListSQLGet()
            {
                List<FindChar> theResult = new List<FindChar>();
                FindChar item = null;
    
                item = new FindChar();
                item.em = SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.SQL注释;
                item.strChar = "--";
                item.endChar = "" + '\n';
                theResult.Add(item);
    
                item = new FindChar();
                item.em = SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.注释范围;
                item.strChar = "/*";
                item.endChar = "*/";
                theResult.Add(item);
    
                item = new FindChar();
                item.em = SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.SQL字符;
                item.strChar = "\'";
                item.endChar = "\'";
                theResult.Add(item);
    
                return theResult;
            }
        }
    }
    
    
    
  • 
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace SlowX.HighLightCodeApp.Classes.HL
    {
        /// <summary>
        /// 
        /// </summary>
        public class HighLightTextItem
        {
            
            /// <summary>
            /// 
            /// </summary>
            public HighLightTextItem()
            {
    
            }
     
    
            /// <summary>
            /// 
            /// </summary>
            public int StartIndex = -1;
    
            /// <summary>
            /// 
            /// </summary>
            public int TextSize = -1;
    
            /// <summary>
            /// 
            /// </summary>
            public SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel em 
                = 
                SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.无;
    
    
            /// <summary>
            /// 颜色
            /// </summary>
            public System.Drawing.Color theColor = System.Drawing.Color.Empty;
    
        }
    }
    
    
  • 
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace SlowX.HighLightCodeApp.Classes.HL
    {
        /// <summary>
        /// 文本高亮的项目
        /// </summary>
        [Serializable]
        public class TextHighLightItem
        {
            /// <summary>
            /// 文本高亮的项目
            /// </summary>
            public TextHighLightItem()
            { 
    
            
            }
    
    
            /// <summary>
            /// 文本高亮的项目
            /// </summary>
            public TextHighLightItem(string _keyword, System.Drawing.Color _theColor )
            {
                m_keyword = _keyword;
    
                m_theColor = _theColor;
            }
    
            /// <summary>
            /// 文本高亮的项目
            /// </summary>
            /// <param name="_keyword"></param>
            /// <param name="_theColor"></param>
            /// <param name="_ignoreCase"></param>
            public TextHighLightItem(string _keyword, System.Drawing.Color _theColor, bool _ignoreCase)
            {
                m_keyword = _keyword;
    
                m_theColor = _theColor;
    
                m_ignoreCase = _ignoreCase;
            }
    
    
            /// <summary>
            /// 文本高亮的项目
            /// </summary>
            /// <param name="_keyword"></param>
            /// <param name="_theColor"></param>
            /// <param name="_ignoreCase"></param>
            /// <param name="_linkChar"></param>
            public TextHighLightItem
                (
                    string _keyword,
                    System.Drawing.Color _theColor, 
                    bool _ignoreCase,
                    char _linkChar
                    )
            {
                m_keyword = _keyword;
    
                m_theColor = _theColor;
    
                m_ignoreCase = _ignoreCase;
    
                m_linkChar = new char[] { _linkChar };
    
            }
    
            /// <summary>
            /// 文本高亮的项目
            /// </summary>
            /// <param name="_keyword"></param>
            /// <param name="_theColor"></param>
            /// <param name="_ignoreCase"></param>
            /// <param name="_linkChar"></param>
            public static TextHighLightItem Create
                (
                    string _keyword, 
                    System.Drawing.Color _theColor, 
                    bool _ignoreCase,
                    params char[] _linkChar
                )
            {
                TextHighLightItem theResult = new TextHighLightItem();
    
                theResult.keyword = _keyword;
    
                theResult.theColor = _theColor;
    
                theResult.ignoreCase = _ignoreCase;
    
                theResult.linkChar = _linkChar;
    
                return theResult;
            }
    
            /// <summary>
            /// 文本高亮的项目
            /// </summary>
            public TextHighLightItem(string _keyword, System.Drawing.Color _theColor, char _linkChar)
            {
                m_keyword = _keyword;
    
                m_theColor = _theColor;
    
                m_linkChar = new char[] { _linkChar };
            }
    
            /// <summary>
            /// 关键字
            /// </summary>
            protected string m_keyword = "";
    
            /// <summary>
            /// 关键字
            /// </summary>
            public string keyword
            {
                get
                {
                    return m_keyword;
                }
                set
                {
                    m_keyword = value;
                }
            }
    
            /// <summary>
            /// 颜色
            /// </summary>
            protected System.Drawing.Color m_theColor = System.Drawing.Color.Empty;
    
            /// <summary>
            /// 颜色
            /// </summary>
            public System.Drawing.Color  theColor
            {
                get
                {
                    return m_theColor;
                }
                set
                {
                    m_theColor = value;
                }
            }
    
            /// <summary>
            /// 链接的Char
            /// </summary>
            protected char[] m_linkChar = null;
    
            /// <summary>
            /// 链接的Char
            /// </summary>
            public char[] linkChar
            {
                get 
                {
                    return m_linkChar ;
                }
                set
                {
                    m_linkChar = value;
                }
            }
    
    
            /// <summary>
            /// 忽略大小写
            /// </summary>
            protected bool m_ignoreCase = false;
    
            /// <summary>
            /// 忽略大小写
            /// </summary>
            public bool ignoreCase
            {
                get
                {
                    return m_ignoreCase;
                }
                set
                {
                    m_ignoreCase = value;
                }
    
            }
    
     
    
            /// <summary>
            /// 高亮关键字
            /// </summary>
            public readonly static List<TextHighLightItem> listCSharpHighLightItem 
                =
                GetListCSharpHighLightItem();
    
            /// <summary>
            /// 
            /// </summary>
            public readonly static List<char> listCSharpLinkChar = GetListCSharpLinkChar();
    
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public static List<char> GetListCSharpLinkChar()
            {
                List<char> theResult = new List<char>();
    
                theResult.Add('{');
                theResult.Add('(');
                theResult.Add('[');
                theResult.Add('.');
                theResult.Add(';');
                theResult.Add('<');
    
                return theResult;
    
            }
    
            /// <summary>
            /// 高亮关键字
            /// </summary>
            /// <returns></returns>
            public static List<TextHighLightItem> GetListCSharpHighLightItem()
            {
    
    
                System.Drawing.Color varColor = System.Drawing.Color.FromArgb(255, 0, 0);
                System.Drawing.Color keywordColor = System.Drawing.Color.FromArgb(0, 0, 255);
                System.Drawing.Color classColor = System.Drawing.Color.FromArgb(71, 145, 180);
    
                List<TextHighLightItem> theResult = new List<TextHighLightItem>();
    
                theResult.Add(new TextHighLightItem("string", varColor, '['));
                theResult.Add(new TextHighLightItem("int", varColor, '['));
                theResult.Add(new TextHighLightItem("uint", varColor, '['));
                theResult.Add(new TextHighLightItem("long", varColor, '['));
                theResult.Add(new TextHighLightItem("ulong", varColor, '['));
                theResult.Add(new TextHighLightItem("DateTime", varColor, '['));
                theResult.Add(new TextHighLightItem("Boolean", keywordColor, '['));
                theResult.Add(new TextHighLightItem("private", keywordColor));
                theResult.Add(new TextHighLightItem("void", keywordColor));
                theResult.Add(TextHighLightItem.Create("this", keywordColor, false, '.', ';'));
                theResult.Add(new TextHighLightItem("public", keywordColor));
                theResult.Add(new TextHighLightItem("char", varColor, '['));
                theResult.Add(new TextHighLightItem("bool", varColor, '['));
                theResult.Add(new TextHighLightItem("return", keywordColor));
                theResult.Add(new TextHighLightItem("if", keywordColor,'('));
                theResult.Add(new TextHighLightItem("else", keywordColor, '{'));
                theResult.Add(new TextHighLightItem("for", keywordColor, '('));
                theResult.Add(new TextHighLightItem("ref", keywordColor));
                theResult.Add(new TextHighLightItem("new", keywordColor));
                theResult.Add(new TextHighLightItem("try", keywordColor, '{'));
                theResult.Add(new TextHighLightItem("catch", keywordColor, '('));
                theResult.Add(new TextHighLightItem("true", keywordColor));
                theResult.Add(new TextHighLightItem("finally", keywordColor, '{'));
                theResult.Add(new TextHighLightItem("false", keywordColor));
                theResult.Add(new TextHighLightItem("not", keywordColor));
                theResult.Add(new TextHighLightItem("null", keywordColor));
                theResult.Add(new TextHighLightItem("using", keywordColor));
                theResult.Add(new TextHighLightItem("namespace", keywordColor));
                theResult.Add(new TextHighLightItem("partial", keywordColor));
                theResult.Add(new TextHighLightItem("class", keywordColor));
                theResult.Add(new TextHighLightItem("protected", keywordColor));
                theResult.Add(new TextHighLightItem("float", varColor, '['));
                theResult.Add(new TextHighLightItem("foreach", keywordColor, '('));
                theResult.Add(new TextHighLightItem("do", keywordColor, '('));
                theResult.Add(new TextHighLightItem("while", keywordColor, '('));
                theResult.Add(new TextHighLightItem("switch", keywordColor, '('));
                theResult.Add(new TextHighLightItem("case", keywordColor, '('));
                theResult.Add(new TextHighLightItem("in", keywordColor));
                theResult.Add(new TextHighLightItem("object", varColor, '['));
                theResult.Add(new TextHighLightItem("throw", keywordColor));
                theResult.Add(new TextHighLightItem("Exception", classColor));
                theResult.Add(new TextHighLightItem("break", keywordColor, ';'));
                theResult.Add(new TextHighLightItem("continue", keywordColor, ';'));
                theResult.Add(new TextHighLightItem("List", classColor, '<'));
                theResult.Add(new TextHighLightItem("#endregion", keywordColor));
                theResult.Add(new TextHighLightItem("#region", keywordColor));
                theResult.Add(new TextHighLightItem("static", keywordColor));
                theResult.Add(new TextHighLightItem("const", keywordColor));
                theResult.Add(new TextHighLightItem("byte", varColor, '['));
                theResult.Add(new TextHighLightItem("EventArgs", classColor, '.'));
                theResult.Add(new TextHighLightItem("RichTextBox", classColor, '.'));
                theResult.Add(new TextHighLightItem("TreeView", classColor, '.'));
                theResult.Add(new TextHighLightItem("TextBox", classColor, '.'));
                theResult.Add(new TextHighLightItem("Label", classColor, '.'));
                theResult.Add(new TextHighLightItem("System.Drawing.Color", classColor, '.'));
                theResult.Add(new TextHighLightItem("System.Drawing.Font", classColor, '.'));
    
                return theResult;
            }
    
    
            /// <summary>
            /// 高亮关键字
            /// </summary>
            public readonly static List<TextHighLightItem> listSqlHighLightItem 
                = 
                GetListSqlHighLightItem();
    
            /// <summary>
            /// 高亮关键字
            /// </summary>
            /// <returns></returns>
            public static List<TextHighLightItem> GetListSqlHighLightItem()
            {
    
                System.Drawing.Color classColor = System.Drawing.Color.FromArgb(71, 145, 180);
                System.Drawing.Color keywordColor = System.Drawing.Color.FromArgb(0, 0, 255);
    
                List<TextHighLightItem> theResult = new List<TextHighLightItem>();
    
                theResult.Add(new TextHighLightItem("select", keywordColor, true));
                theResult.Add(new TextHighLightItem("create", keywordColor, true));
                theResult.Add(new TextHighLightItem("values", keywordColor, true));
                theResult.Add(new TextHighLightItem("from", keywordColor, true));
                theResult.Add(new TextHighLightItem("where", keywordColor, true));
                theResult.Add(new TextHighLightItem("insert", keywordColor, true));
                theResult.Add(new TextHighLightItem("update", keywordColor, true));
                theResult.Add(new TextHighLightItem("delete", keywordColor, true));
                theResult.Add(new TextHighLightItem("table", keywordColor, true));
                theResult.Add(new TextHighLightItem("into", keywordColor, true));
                theResult.Add(new TextHighLightItem("drop", keywordColor, true));
                theResult.Add(new TextHighLightItem("set", keywordColor, true));
                theResult.Add(new TextHighLightItem("count", keywordColor, true, '('));
                theResult.Add(new TextHighLightItem("min", keywordColor, true, '('));
                theResult.Add(new TextHighLightItem("max", keywordColor, true,'('));
                theResult.Add(new TextHighLightItem("avg", keywordColor, true, '('));
                theResult.Add(new TextHighLightItem("sum", keywordColor, true, '('));
                theResult.Add(new TextHighLightItem("top", keywordColor, true));
                theResult.Add(new TextHighLightItem("order by", keywordColor, true));
                theResult.Add(new TextHighLightItem("bigint", keywordColor, true,','));
                theResult.Add(new TextHighLightItem("datetime", keywordColor, true));
                theResult.Add(new TextHighLightItem("not", keywordColor, true));
                theResult.Add(new TextHighLightItem("null", keywordColor, true, ','));
                theResult.Add(new TextHighLightItem("default", keywordColor, true));
                theResult.Add(new TextHighLightItem("ntext", keywordColor, true, ','));
                theResult.Add(new TextHighLightItem("primary", keywordColor, true));
                theResult.Add(new TextHighLightItem("foreign", keywordColor, true));
                theResult.Add(new TextHighLightItem("references", keywordColor, true));             
                theResult.Add(new TextHighLightItem("key", keywordColor, true,','));
                theResult.Add(new TextHighLightItem("decimal", keywordColor, true, '('));
                theResult.Add(new TextHighLightItem("char", keywordColor, true, '('));
                theResult.Add(new TextHighLightItem("nchar", keywordColor, true, '('));
                theResult.Add(new TextHighLightItem("text", keywordColor, true, ','));
                theResult.Add(new TextHighLightItem("image", keywordColor, true, ','));
                theResult.Add(new TextHighLightItem("int", keywordColor, true, ','));
                theResult.Add(new TextHighLightItem("nvarchar", keywordColor, true, '('));
                theResult.Add(new TextHighLightItem("varchar", keywordColor, true, '('));
                theResult.Add(new TextHighLightItem("varchar2", keywordColor, true, '('));
                theResult.Add(new TextHighLightItem("execute", keywordColor, true));
                theResult.Add(new TextHighLightItem("sp_addextendedproperty", keywordColor, true));              
                theResult.Add(new TextHighLightItem("getdate()", classColor, true));
    
                return theResult;
            }
        }
    }
    
    
  •  
                using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing;
    using System.Reflection;
    
    namespace SlowX.HighLightCodeApp.Classes.HL
    {
        /// <summary>
        /// 代码高亮
        /// </summary>
        public class CodeHighLight
        {
     
    
            /// <summary>
            /// 字符串的颜色
            /// </summary>
            protected static System.Drawing.Color ColorStringChar
            {
                get
                {
                    return System.Drawing.Color.FromArgb(155, 0, 0);
                }
            }
    
            /// <summary>
            /// 关键词
            /// </summary>
            protected static System.Drawing.Color ColorKeyWord
            {
                get
                {
                    return System.Drawing.Color.Blue;
                }
            }
    
            /// <summary>
            /// 类
            /// </summary>
            protected static System.Drawing.Color ColorClass
            {
                get
                {
                    return System.Drawing.Color.FromArgb(71, 145, 180);
                }
            }
    
    
            /// <summary>
            /// 注释
            /// </summary>
            protected static System.Drawing.Color ColorComment
            {
                get
                {
                    return System.Drawing.Color.FromArgb(0, 155, 0);
                }
            }
    
            /// <summary>
            /// 
            /// </summary>
            public CodeHighLight()
            {
    
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="rtb"></param>
            /// <param name="defaultFont"></param>
            public static void ResetFont(RichTextBox rtb,Font defaultFont)
            {
                int index = rtb.SelectionStart;  // 记录修改的位置 
                rtb.SelectAll();
    
                rtb.Select(0, rtb.Text.Length);
                
                rtb.SelectionFont = defaultFont;
    
                if (index != -1)
                {
                    rtb.Select(index, 0);   //返回修改的位置
                }
            }
    
            /// <summary>
            /// 重设RichTextBox的文字颜色
            /// </summary>
            /// <param name="rtb"></param> 
            public static void ResetColor(RichTextBox rtb)
            {
                ResetColor(rtb, System.Drawing.Color.Black);
            }
    
            /// <summary>
            /// 重设RichTextBox的文字颜色
            /// </summary>
            /// <param name="rtb"></param>
            /// <param name="defaultColor"></param>
            public static void ResetColor(RichTextBox rtb, Color defaultColor)
            {
                // 记录修改的位置 
                int index = rtb.SelectionStart;  
                rtb.SelectAll(); 
    
                rtb.SelectionColor
                    = defaultColor;
    
                if (index != -1)
                {
                    // 返回修改的位置
                    rtb.Select(index, 0);   
                }
            }
    
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="rtb"></param>
            public static void SQLHighLight
                (
                    RichTextBox rtb
                )
            {
                SQLHighLight
                    (
                        rtb,
                        TextHighLightItem.listSqlHighLightItem,
                        Color.Black
                    );
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="rtb"></param>
            /// <param name="theList"></param> 
            /// <param name="defaultColor"></param>
            public static void SQLHighLight
                (
                    RichTextBox rtb,
                    List<TextHighLightItem> theList,
                    Color defaultColor
                )
            {
    
                int index = rtb.SelectionStart;  // 记录修改的位置 
                rtb.SelectAll();
                rtb.SelectionColor
                   = defaultColor;
    
                ToHighLightKey(rtb, theList);
    
                List<HighLightTextItem> resultList = new List<HighLightTextItem>();
                List<FindChar> FindCharList = FindChar.FindCharListSQLGet();
    
                string strText = rtb.Text;
    
                HighLightTextItem item = null;
                FindChar curFC = null;
                int saveIdx = int.MaxValue;
                int idx = -1;
                int AddIndex = 0;
                int startIndex = -1;
                int endIndex = -1;
    
                string strOne = null;
                string strTwo = null;
                string strThree = null;
    
                while (true)
                {
                    curFC = null;
    
                    saveIdx = int.MaxValue;
    
                    foreach (FindChar fc in FindCharList)
                    {
                        idx = strText.IndexOf(fc.strChar);
    
                        if (idx != -1)
                        {
                            if (idx < saveIdx)
                            {
                                curFC = fc;
                                saveIdx = idx;
                            }
                        }
                    }
    
                    if (saveIdx == int.MaxValue || curFC == null)
                    {
                        item = new HighLightTextItem();
                        item.em = SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.无;
                        item.StartIndex = AddIndex;
                        item.TextSize = strText.Length;
                        resultList.Add(item);
                        break;
                    }
    
                    endIndex = -1;
                    startIndex = saveIdx + curFC.strChar.Length;
    
                    switch (curFC.em)
                    {
                        case SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.SQL注释:
                            endIndex = strText.IndexOf(curFC.endChar, startIndex);
                            break;
                        case SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.注释范围:
                            endIndex = strText.IndexOf(curFC.endChar, startIndex);
                            break;
                        case SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.SQL字符:
                            endIndex = FindAtSqlCharEndIndex(strText, startIndex);
                            break;
                        default:
                            break;
                            //throw new Exception
                            //    (
                            //        "方法:"
                            //        + MethodBase.GetCurrentMethod().ReflectedType.FullName
                            //        + " "
                            //        + MethodBase.GetCurrentMethod().ToString()
                            //        + " 发生异常:"
                            //        + "枚举("
                            //        + curFC.em.GetType().FullName
                            //        + "."
                            //        + curFC.em.ToString()
                            //        + ")未知,对应的代码尚未实现。"
                            //    );
    
                    }
    
                    // 开始分段 //
                    if (saveIdx != 0)
                    {
                        strOne = strText.Substring(0, saveIdx);
                    }
                    else
                        strOne = "";
    
                    if (endIndex == -1)
                    {
                        strThree = "";
                    }
                    else
                        strThree = strText.Substring(endIndex + curFC.endChar.Length);
    
                    if (endIndex == -1)
                    {
                        strTwo = strText.Substring(saveIdx);
                    }
                    else
                    {
                        strTwo = strText.Substring(saveIdx, endIndex - saveIdx + curFC.endChar.Length);
                    }
    
                    if (strOne.Length > 0)
                    {
                        item = new HighLightTextItem();
                        item.em = SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.无;
                        item.StartIndex = AddIndex;
                        item.TextSize = strOne.Length;
                        resultList.Add(item);
    
                        AddIndex = AddIndex + item.TextSize;
                    }
    
                    if (strTwo.Length > 0)
                    {
                        item = new HighLightTextItem();
                        item.em = curFC.em;
                        item.StartIndex = AddIndex;
                        item.TextSize = strTwo.Length;
                        resultList.Add(item);
    
                        AddIndex = AddIndex + item.TextSize;
                    }
    
                    strText = strThree;
                }
    
    
    
                foreach (HighLightTextItem hlitem in resultList)
                {
                    switch (hlitem.em)
                    {
                        case SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.无:
                        case SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.纯文本:
                            break;
                        case SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.字符串at:
                        case SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.字符串:
                        case SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.字符:
                        case SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.SQL字符:
                            SetRichTextBoxColorByLen(rtb, hlitem.StartIndex, hlitem.TextSize, ColorStringChar);
                            break;
                        case SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.注释2:
                        case SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.注释3:
                        case SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.注释范围:
                        case SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.SQL注释:
                            SetRichTextBoxColorByLen(rtb, hlitem.StartIndex, hlitem.TextSize, ColorComment);
                            break;
                        default:
                            break;
                            //throw new Exception
                            //    (
                            //        "方法:"
                            //        + MethodBase.GetCurrentMethod().ReflectedType.FullName
                            //        + " "
                            //        + MethodBase.GetCurrentMethod().ToString()
                            //        + " 发生异常:"
                            //        + "枚举("
                            //        + hlitem.em.GetType().FullName
                            //        + "."
                            //        + hlitem.em.ToString()
                            //        + ")未知,对应的代码尚未实现。"
                            //    );
                    }
                }
    
    
                if (index != -1)
                {
                    // 返回修改的位置
                    rtb.Select(index, 0);
                }
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="rtb"></param>
            public static void CSharpHighLight
                (
                    RichTextBox rtb
                )
            {
                CSharpHighLight
                    (
                        rtb, 
                        TextHighLightItem.listCSharpHighLightItem,  
                        Color.Black
                    );
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="rtb"></param>
            /// <param name="theList"></param> 
            /// <param name="defaultColor"></param>
            public static void CSharpHighLight
                (
                    RichTextBox rtb, 
                    List<TextHighLightItem> theList, 
                    Color defaultColor
                )
            {
    
                int index = rtb.SelectionStart;  // 记录修改的位置 
                rtb.SelectAll();
                rtb.SelectionColor
                   = defaultColor;
    
                ToHighLightKey(rtb, theList);
    
                List<HighLightTextItem> resultList = new List<HighLightTextItem>();
                List<FindChar> FindCharList = FindChar.FindCharListGet();
    
                string strText = rtb.Text;
    
                HighLightTextItem item = null;
                FindChar curFC = null;
                int saveIdx = int.MaxValue;
                int idx = -1;
                int AddIndex = 0;
                int startIndex = -1;
                int endIndex = -1;
                
                string strOne = null;
                string strTwo = null;
                string strThree = null;
    
                while (true)
                {
                    curFC=null;
                    saveIdx = int.MaxValue;
    
                    foreach (FindChar fc in FindCharList)
                    {
                        idx = strText.IndexOf(fc.strChar);
    
                        if (idx != -1)
                        {
                            if (idx < saveIdx)
                            {
                                curFC=fc;
                                saveIdx = idx;
                            }
                        }
                    }
    
                    if (saveIdx == int.MaxValue || curFC==null)
                    {
                        item = new HighLightTextItem();
                        item.em = SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.无;
                        item.StartIndex = AddIndex;
                        item.TextSize = strText.Length; 
                        resultList.Add(item);
                        break;
                    }
    
                    endIndex = -1;
                    startIndex = saveIdx + curFC.strChar.Length;
                    switch (curFC.em)
                    {
                        case SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.注释2:
                        case SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.注释3:
                            endIndex = strText.IndexOf(curFC.endChar, startIndex);
                            break;
                        case SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.注释范围:
                            endIndex = strText.IndexOf(curFC.endChar, startIndex);
                            break;
                        case SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.字符:
                            endIndex = FindCharEndIndex(strText, startIndex);
                            break;
                        case SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.字符串:
                            endIndex = FindQCharEndIndex(strText, startIndex);
                            break;
                        case SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.字符串at:
                            endIndex = FindAtQCharEndIndex(strText, startIndex);
                            break;
                        default:
                            break;
                    }
    
                    // 开始分段 //
                    if (saveIdx != 0)
                    {
                        strOne = strText.Substring(0, saveIdx);
                    }
                    else
                        strOne = "";
    
                    if (endIndex == -1)
                    {
                        strThree = "";
                    }
                    else
                        strThree = strText.Substring(endIndex + curFC.endChar.Length);
    
                    if (endIndex == -1)
                    {
                        strTwo = strText.Substring(saveIdx);
                    }
                    else
                    {
                        strTwo = strText.Substring(saveIdx, endIndex - saveIdx + curFC.endChar.Length);
                    }
    
                    if (strOne.Length > 0)
                    {
                        item = new HighLightTextItem();
                        item.em = SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.无;
                        item.StartIndex = AddIndex;
                        item.TextSize = strOne.Length;
                        resultList.Add(item);
    
                        AddIndex = AddIndex + item.TextSize;
                    }
    
                    if (strTwo.Length > 0)
                    {
                        item = new HighLightTextItem();
                        item.em = curFC.em;
                        item.StartIndex = AddIndex;
                        item.TextSize = strTwo.Length;
                        resultList.Add(item);
    
                        AddIndex = AddIndex + item.TextSize;
                    }
    
                    strText = strThree;
                }
    
    
    
                foreach (HighLightTextItem hlitem in resultList)
                {
                    switch(hlitem.em)
                    {
                        case SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.无:
                        case SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.纯文本:
                            break;
                        case SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.字符串at:
                        case SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.字符串:
                        case SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.字符:
                            SetRichTextBoxColorByLen(rtb, hlitem.StartIndex, hlitem.TextSize, ColorStringChar);
                            break;
                        case SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.注释2:
                        case SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.注释3:
                        case SlowX.HighLightCodeApp.Enums.HighLightModel.EmHighLightModel.注释范围:
                            SetRichTextBoxColorByLen(rtb, hlitem.StartIndex, hlitem.TextSize, ColorComment);
                            break;
                        default:
                            break;
                            //throw new Exception
                            //    (
                            //        "方法:"
                            //        + MethodBase.GetCurrentMethod().ReflectedType.FullName
                            //        + " "
                            //        + MethodBase.GetCurrentMethod().ToString()
                            //        + " 发生异常:"
                            //        + "枚举("
                            //        + hlitem.em.GetType().FullName
                            //        + "."
                            //        + hlitem.em.ToString()
                            //        + ")未知,对应的代码尚未实现。"
                            //    );
                    }
                }
    
    
                if (index != -1)
                {
                    // 返回修改的位置
                    rtb.Select(index, 0);   
                }
            }
             
            /// <summary>
            /// 
            /// </summary>
            /// <param name="rtb"></param>
            /// <param name="theList"></param>
            protected static void ToHighLightKey
                (
                    RichTextBox rtb, 
                    List<TextHighLightItem> theList
                )
            {
                string strText = rtb.Text;
    
                string strLowerText = strText.ToLower();
                string lowerKeyWord= null;
                int idx = -1;
                int endIdx = -1;
                int keywordLen = -1;
                int startIndex = 0;
                char headChar = (char)0;
                char endChar = (char)0;
                int textLen = strText.Length - 1;
    
                foreach (TextHighLightItem item in theList)
                {
                    startIndex = 0;
                    lowerKeyWord = item.keyword.ToLower();
                    keywordLen = item.keyword.Length;
    
                    while (true)
                    {
                        if (item.ignoreCase)
                        {
                            idx = strLowerText.IndexOf(lowerKeyWord, startIndex);
                        }
                        else
                        {
                            idx = strText.IndexOf(item.keyword, startIndex);
                        }
    
                        if (idx == -1)
                            break;
                        startIndex = idx + 1;
    
                        endIdx = idx + keywordLen;
    
                        if (idx != 0)
                            headChar = strText[idx - 1];
                        else
                            headChar = (char)0;
    
                        if (!IsOKChar(headChar, null))
                        {
    
                            continue;
                        }
    
    
                        if (endIdx != textLen && endIdx < textLen)
                            endChar = strText[endIdx];
                        else
                            endChar = (char)0;
    
    
                        if (!IsOKChar(endChar, item.linkChar))
                        {
                            continue;
                        }
    
                        SetRichTextBoxColorByLen(rtb, idx, keywordLen, item.theColor);
                    }
                }
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="curChar"></param>
            /// <param name="allowChar"></param>
            /// <returns></returns>
            protected static bool IsOKChar(char curChar, char[] allowChar)
            {
                if (curChar == 0)
                    return true;
    
                switch (curChar)
                {
                    case ' ':
                    case '\n':
                    case '\t':
                    case '\r':
                        return true;
                    case '(':
                    case ')':
                    case '[':
                    case ']':
                    case '{':
                    case '}':
                    case ';':
                    case '.':
                    case '<':
                    case '>':
                    case '/':
                    case ',':
                        return true;
                    default:
                        break;
                }
    
                if (allowChar != null)
                {
                    foreach (char c in allowChar)
                    {
                        if (c == curChar)
                            return true;
                    }
                }
    
                return false;
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="strText"></param>
            /// <param name="startIndex"></param>
            /// <returns></returns>
            protected static int FindCharEndIndex(string strText, int startIndex)
            {
                int idx = -1;
    
                while (true)
                {
                    idx = strText.IndexOf('\'', startIndex);
    
                    if (idx == -1)
                        return -1;
    
                    int iCount = 0;
    
                    for (int j = idx - 1; j >= startIndex; --j)
                    {
                        if (strText[j] != '\\')
                            break;
                     
                        ++iCount;
                    }
    
                    if (iCount % 2 == 0)
                        return idx;
    
                    startIndex = idx + 1;
                }
            }
    
    
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="strText"></param>
            /// <param name="startIndex"></param>
            /// <returns></returns>
            protected static int FindQCharEndIndex(string strText, int startIndex)
            {
                // 测试
                /*
                 * canoe
                 */
    
                //char ca = 'c';
                //string str = "scut";
                //string strOne = "scur\"ca";
                //string strs = @"csd""vcx";
    
                int idx = -1;
    
                while (true)
                {
                    idx = strText.IndexOf('\"', startIndex);
    
                    if (idx == -1)
                        return -1;
    
                    int iCount = 0;
    
                    for (int j = idx - 1; j >= startIndex; --j)
                    {
                        if (strText[j] != '\\')
                            break;
    
                        ++iCount;
                    }
    
                    if (iCount % 2 == 0)
                        return idx;
    
                    startIndex = idx + 1;
                }
            }
    
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="strText"></param>
            /// <param name="startIndex"></param>
            /// <returns></returns>
            protected static int FindAtQCharEndIndex(string strText, int startIndex)
            {
                int idxOne = -1;
                int idxTwo = -1;
    
                while (true)
                {
                    idxOne = strText.IndexOf('\"', startIndex);
    
                    if (idxOne == -1)
                        return -1;
    
                    idxTwo = strText.IndexOf("\"\"", startIndex);
    
                    if (idxTwo == -1)
                        return idxOne;
    
                    if (idxOne == idxTwo)
                        startIndex = idxTwo + 2;
                }
            }
    
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="strText"></param>
            /// <param name="startIndex"></param>
            /// <returns></returns>
            protected static int FindAtSqlCharEndIndex(string strText, int startIndex)
            {
                int idxOne = -1;
                int idxTwo = -1;
    
                while (true)
                {
                    idxOne = strText.IndexOf('\'', startIndex);
    
                    if (idxOne == -1)
                        return -1;
    
                    idxTwo = strText.IndexOf("\'\'", startIndex);
    
                    if (idxTwo == -1)
                        return idxOne;
    
                    if (idxOne == idxTwo)
                        startIndex = idxTwo + 2;
                }
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="theList"></param>
            /// <param name="strText"></param>
            /// <param name="isLinkCharCheck"></param>
            /// <param name="endChar"></param>
            /// <returns></returns>
            protected static TextHighLightItem FindTextHighLightItem
                (
                    List<TextHighLightItem> theList,
                    string strText,
                    bool isLinkCharCheck,
                    char endChar
                )
            {
                string lowerText = strText.ToLower();
    
                foreach (TextHighLightItem item in theList)
                {
                    if (item.ignoreCase)
                    {
                        if (item.keyword != lowerText)
                        {
                            continue;
                        }
                    }
                    else
                    {
                        if (item.keyword != strText)
                            continue;
                    }
    
                    if (isLinkCharCheck)
                    {
                        if (item.linkChar != null)
                        {
                            foreach (char c in item.linkChar)
                            {
                                if (c == endChar)
                                    return item;
                            }
                        }
                    }
                    else
                    {
                        return item;
                    }
                }
    
                return null;
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="rtb"></param>
            /// <param name="startIndex"></param>
            /// <param name="endIndex"></param>
            /// <param name="fontColor"></param>
            protected static void SetRichTextBoxColor
                (
                    RichTextBox rtb,
                    int startIndex,
                    int endIndex,
                    System.Drawing.Color fontColor
                )
            {
                if (startIndex == -1 || endIndex == -1)
                    return;
    
                if (endIndex < startIndex)
                    return;
    
                rtb.Select(startIndex, endIndex - startIndex + 1);        //选中要添加的文本 
                rtb.SelectionColor = fontColor;  //设置要添加的文本的字体色 
            }
    
    
            /// <summary>
            /// RichTextBox设置指定位置的文本颜色
            /// </summary>
            /// <param name="rtb"></param>
            /// <param name="startIndex"></param>
            /// <param name="endIndex"></param>
            /// <param name="fontColor"></param>
            protected static void SetRichTextBoxColorByLen
                (
                    RichTextBox rtb,
                    int startIndex,
                    int textLen,
                    System.Drawing.Color fontColor
                )
            {
                if (startIndex == -1 || textLen == 0)
                    return;
    
                // 选中要添加的文本 
                rtb.Select(startIndex, textLen);
    
                // 设置要添加的文本的字体色 
                rtb.SelectionColor = fontColor;  
            }
        }
    }
    
    

标签:自定义算法代码高亮 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)
 
  ┈全部┈  
 
(显示默认分类)