Exemplo n.º 1
0
		/// <summary>
		/// Creates a new instance of <see cref="PrevMarker"/>
		/// </summary>
		public PrevMarker(XmlElement mark)
		{
			color = new HighlightColor(mark);
			what  = mark.InnerText;//可能是半个括号:    如(
			if (mark.Attributes["markmarker"] != null) {
				markMarker = Boolean.Parse(mark.Attributes["markmarker"].InnerText);
			}
		}
		public DefaultHighlightingStrategy(string name) 
		{
			this.name = name;
			this.digitColor      = new HighlightColor("WindowText", "Window", false, false);
			
			// set default color environment'
			environmentColors["Default"]          = new HighlightColor(Color.Black, Color.White, false, false);
			environmentColors["Selection"]        = new HighlightColor(Color.Black, Color.FromArgb(((System.Byte)(102)), ((System.Byte)(210)), ((System.Byte)(214))), false, false);
			environmentColors["VRuler"]           = new HighlightColor("ControlLight", "Window", false, false);
			environmentColors["InvalidLines"]     = new HighlightColor(Color.Red, false, false);
			environmentColors["CaretMarker"]      = new HighlightColor(Color.Yellow, false, false);
			environmentColors["LineNumbers"]      = new HighlightColor("ControlDark", "Window", false, false);
			
			environmentColors["FoldLine"]         = new HighlightColor(Color.FromArgb(0x80, 0x80, 0x80), Color.Black, false, false);
			environmentColors["FoldMarker"]       = new HighlightColor(Color.FromArgb(0x80, 0x80, 0x80), Color.White, false, false);
			environmentColors["SelectedFoldLine"] = new HighlightColor(Color.Black, false, false);
			environmentColors["EOLMarkers"]       = new HighlightColor("ControlLight", "Window", false, false);
			environmentColors["SpaceMarkers"]     = new HighlightColor("ControlLight", "Window", false, false);
			environmentColors["TabMarkers"]       = new HighlightColor("ControlLight", "Window", false, false);
			
		}
Exemplo n.º 3
0
		//每个RuleSet节点的样子:<RuleSet ignorecase = "false">......</RuleSet>
		public HighlightRuleSet(XmlElement el)
		{
			
			if (el.Attributes["name"] != null) {
				Name = el.Attributes["name"].InnerText;
			}
			
			if (el.Attributes["noescapesequences"] != null) {
				noEscapeSequences = Boolean.Parse(el.Attributes["noescapesequences"].InnerText);
			}
			
			if (el.Attributes["reference"] != null) {
				reference = el.Attributes["reference"].InnerText;
			}
			
			if (el.Attributes["ignorecase"] != null) {
				ignoreCase  = Boolean.Parse(el.Attributes["ignorecase"].InnerText);
			}
			
			for (int i  = 0; i < Delimiters.Length; ++i) {
				Delimiters[i] = false;
			}
			
			if (el["Delimiters"] != null) {
				string delimiterString = el["Delimiters"].InnerText;
				foreach (char ch in delimiterString) {
					Delimiters[(int)ch] = true;//将当前字符置为分隔符.
				}
			}

			XmlNodeList nodes = null;

			//以下是初始化Span.
			nodes = el.GetElementsByTagName("Span");
			foreach (XmlElement el2 in nodes) 
			{
				Spans.Add(new Span(el2));
			}

			keyWords    = new LookupTable(!IgnoreCase);
			prevMarkers = new LookupTable(!IgnoreCase);
			nextMarkers = new LookupTable(!IgnoreCase);
			
			//以下是初始化KeyWords.
			nodes = el.GetElementsByTagName("KeyWords");
			foreach (XmlElement el2 in nodes) 
			{
				HighlightColor color = new HighlightColor(el2);
				
				XmlNodeList keys = el2.GetElementsByTagName("Key");
				foreach (XmlElement node in keys) 
				{
					keyWords[node.Attributes["word"].InnerText] = color;//为每个关键字都赋予一个HighLightColor对象.
				}
			}
			
			//每个MarkPrevious节点的样子:
			//<MarkPrevious bold = "true" italic = "false" color = "MidnightBlue">(</MarkPrevious>
			nodes = el.GetElementsByTagName("MarkPrevious");
			foreach (XmlElement el2 in nodes) {
				PrevMarker prev = new PrevMarker(el2);
				prevMarkers[prev.What] = prev;
			}
			//每个MarkFollowing节点的样子和MarkFollowing的样子完全一样.
			nodes = el.GetElementsByTagName("MarkFollowing");
			foreach (XmlElement el2 in nodes) {
				NextMarker next = new NextMarker(el2);
				nextMarkers[next.What] = next;
			}
		}
Exemplo n.º 4
0
		//功能同上面这个构造函数,只不过在初始化实例时提供了一个默认的实例对象.
		public HighlightColor(XmlElement el, HighlightColor defaultColor)
		{
			Debug.Assert(el != null, "NetFocus.DataStructure.TextEditor.Document.SyntaxColor(XmlElement el) : el == null");
			if (el.Attributes["bold"] != null) {
				bold = Boolean.Parse(el.Attributes["bold"].InnerText);
			} else {
				bold = defaultColor.Bold;
			}
			
			if (el.Attributes["italic"] != null) {
				italic = Boolean.Parse(el.Attributes["italic"].InnerText);
			} else {
				italic = defaultColor.Italic;
			}
			
			if (el.Attributes["color"] != null) {
				string c = el.Attributes["color"].InnerText;
				if (c[0] == '#') {
					color = ParseColor(c);
				} else if (c.StartsWith("SystemColors.")) {
					systemColor     = true;
					systemColorName = c.Substring("SystemColors.".Length);
				} else {
					color = (Color)(Color.GetType()).InvokeMember(c, BindingFlags.GetProperty, null, Color, new object[0]);
				}
				hasForgeground = true;
			} else {
				color = defaultColor.color;
			}
			
			if (el.Attributes["bgcolor"] != null) {
				string c = el.Attributes["bgcolor"].InnerText;
				if (c[0] == '#') {
					backgroundcolor = ParseColor(c);
				} else if (c.StartsWith("SystemColors.")) {
					systemBgColor     = true;
					systemBgColorName = c.Substring("SystemColors.".Length);
				} else {
					backgroundcolor = (Color)(Color.GetType()).InvokeMember(c, BindingFlags.GetProperty, null, Color, new object[0]);
				}
				hasBackground = true;
			} else {
				backgroundcolor = defaultColor.BackgroundColor;
			}
		}
		void PushCurWord(IDocument document, ref HighlightColor markNext, ArrayList words)
		{
			//Need to look through the next prev logic.
			if (currentLength > 0) {
				if (words.Count > 0 && activeRuleSet != null) {
					TextWord prevWord = null;
					int pInd = words.Count - 1;
					while (pInd >= 0) {
						if (!((TextWord)words[pInd]).IsWhiteSpace) {
							prevWord = (TextWord)words[pInd];
							if (prevWord.HasDefaultColor) {
								PrevMarker marker = (PrevMarker)activeRuleSet.PrevMarkers[document, currentLine, currentOffset, currentLength];
								if (marker != null) {
									prevWord.HighlightColor = marker.Color;
								}
							}
							break;
						}
						pInd--;
					}
				}
				
				if (inSpan) {
					HighlightColor c = null;
					bool hasDefaultColor = true;
					if (activeSpan.Rule == null) {
						c = activeSpan.Color;
					} else {
						c = GetColor(activeRuleSet, document, currentLine, currentOffset, currentLength);
						hasDefaultColor = false;
					}
					
					if (c == null) {
						c = activeSpan.Color;
						if (c.Color == Color.Transparent) {
							c = GetEnvironmentColorForName("Default");
						}
						hasDefaultColor = true;
					}
					words.Add(new TextWord(document, currentLine, currentOffset, currentLength, markNext != null ? markNext : c, hasDefaultColor));
				} else {
					HighlightColor c = markNext != null ? markNext : GetColor(activeRuleSet, document, currentLine, currentOffset, currentLength);
					if (c == null) {
						words.Add(new TextWord(document, currentLine, currentOffset, currentLength, GetEnvironmentColorForName("Default"), true));
					} else {
						words.Add(new TextWord(document, currentLine, currentOffset, currentLength, c, false));
					}
				}
				
				if (activeRuleSet != null) {
					NextMarker nextMarker = (NextMarker)activeRuleSet.NextMarkers[document, currentLine, currentOffset, currentLength];
					if (nextMarker != null) {
						if (nextMarker.MarkMarker && words.Count > 0) {
							TextWord prevword = ((TextWord)words[words.Count - 1]);
							prevword.HighlightColor = nextMarker.Color;
						}
						markNext = nextMarker.Color;
					} else {
						markNext = null;
					}
				}
				currentOffset += currentLength;
				currentLength = 0;					
			}
		}		
Exemplo n.º 6
0
		public Span(XmlElement span)
		{
			color   = new HighlightColor(span);
			
			if (span.Attributes["rule"] != null) {
				rule = span.Attributes["rule"].InnerText;
			}
			
			if (span.Attributes["noescapesequences"] != null) {
				noEscapeSequences = Boolean.Parse(span.Attributes["noescapesequences"].InnerText);
			}
			
			name    = span.Attributes["name"].InnerText;
			stopEOL = Boolean.Parse(span.Attributes["stopateol"].InnerText);
			begin   = span["Begin"].InnerText.ToCharArray();
			beginColor = new HighlightColor(span["Begin"], color);
			
			if (span["End"] != null) {
				end  = span["End"].InnerText.ToCharArray();
				endColor = new HighlightColor(span["End"], color);
			}
		}
		internal void SetColorForEnvironment(string name, HighlightColor color)
		{
			environmentColors[name] = color;
		}
        //根据doucment,currentLine为参数,来分析得到该行中的所有单词即words
        ArrayList ParseLine(IDocument document)
        {
            ArrayList      words    = new ArrayList();
            HighlightColor markNext = null;

            currentOffset = 0;
            currentLength = 0;
            UpdateSpanStateVariables();
            //从当前行的第一个字符开始循环,逐个分析.
            for (int i = 0; i < currentLine.Length; ++i)
            {
                char ch = document.GetCharAt(currentLine.Offset + i);
                switch (ch)
                {
                case '\n':
                case '\r':
                    PushCurWord(document, ref markNext, words);
                    ++currentOffset;
                    break;

                case ' ':
                    PushCurWord(document, ref markNext, words);
                    if (activeSpan != null && activeSpan.Color.HasBackground)
                    {
                        words.Add(new SpaceTextWord(activeSpan.Color));
                    }
                    else
                    {
                        words.Add(TextWord.Space);
                    }
                    ++currentOffset;
                    break;

                case '\t':
                    PushCurWord(document, ref markNext, words);
                    if (activeSpan != null && activeSpan.Color.HasBackground)
                    {
                        words.Add(new TabTextWord(activeSpan.Color));
                    }
                    else
                    {
                        words.Add(TextWord.Tab);
                    }
                    ++currentOffset;
                    break;

                case '\\':                         // handle escape chars
                    if ((activeRuleSet != null && activeRuleSet.NoEscapeSequences) ||
                        (activeSpan != null && activeSpan.NoEscapeSequences))
                    {
                        goto default;
                    }
                    ++currentLength;
                    if (i + 1 < currentLine.Length)
                    {
                        ++currentLength;
                    }
                    PushCurWord(document, ref markNext, words);
                    ++i;
                    continue;

                default: {
                    // highlight digits
                    if (!inSpan && (Char.IsDigit(ch) || (ch == '.' && i + 1 < currentLine.Length && Char.IsDigit(document.GetCharAt(currentLine.Offset + i + 1)))) && currentLength == 0)
                    {
                        bool ishex           = false;
                        bool isfloatingpoint = false;

                        if (ch == '0' && i + 1 < currentLine.Length && Char.ToUpper(document.GetCharAt(currentLine.Offset + i + 1)) == 'X')                                   // hex digits
                        {
                            const string hex = "0123456789ABCDEF";
                            ++currentLength;
                            ++i;                                     // skip 'x'
                            ++currentLength;
                            ishex = true;
                            while (i + 1 < currentLine.Length && hex.IndexOf(Char.ToUpper(document.GetCharAt(currentLine.Offset + i + 1))) != -1)
                            {
                                ++i;
                                ++currentLength;
                            }
                        }
                        else
                        {
                            ++currentLength;
                            while (i + 1 < currentLine.Length && Char.IsDigit(document.GetCharAt(currentLine.Offset + i + 1)))
                            {
                                ++i;
                                ++currentLength;
                            }
                        }
                        if (!ishex && i + 1 < currentLine.Length && document.GetCharAt(currentLine.Offset + i + 1) == '.')
                        {
                            isfloatingpoint = true;
                            ++i;
                            ++currentLength;
                            while (i + 1 < currentLine.Length && Char.IsDigit(document.GetCharAt(currentLine.Offset + i + 1)))
                            {
                                ++i;
                                ++currentLength;
                            }
                        }

                        if (i + 1 < currentLine.Length && Char.ToUpper(document.GetCharAt(currentLine.Offset + i + 1)) == 'E')
                        {
                            isfloatingpoint = true;
                            ++i;
                            ++currentLength;
                            if (i + 1 < currentLine.Length && (document.GetCharAt(currentLine.Offset + i + 1) == '+' || document.GetCharAt(currentLine.Offset + i + 1) == '-'))
                            {
                                ++i;
                                ++currentLength;
                            }
                            while (i + 1 < currentLine.Length && Char.IsDigit(document.GetCharAt(currentLine.Offset + i + 1)))
                            {
                                ++i;
                                ++currentLength;
                            }
                        }

                        if (i + 1 < currentLine.Length)
                        {
                            char nextch = Char.ToUpper(document.GetCharAt(currentLine.Offset + i + 1));
                            if (nextch == 'F' || nextch == 'M' || nextch == 'D')
                            {
                                isfloatingpoint = true;
                                ++i;
                                ++currentLength;
                            }
                        }

                        if (!isfloatingpoint)
                        {
                            bool isunsigned = false;
                            if (i + 1 < currentLine.Length && Char.ToUpper(document.GetCharAt(currentLine.Offset + i + 1)) == 'U')
                            {
                                ++i;
                                ++currentLength;
                                isunsigned = true;
                            }
                            if (i + 1 < currentLine.Length && Char.ToUpper(document.GetCharAt(currentLine.Offset + i + 1)) == 'L')
                            {
                                ++i;
                                ++currentLength;
                                if (!isunsigned && i + 1 < currentLine.Length && Char.ToUpper(document.GetCharAt(currentLine.Offset + i + 1)) == 'U')
                                {
                                    ++i;
                                    ++currentLength;
                                }
                            }
                        }

                        words.Add(new TextWord(document, currentLine, currentOffset, currentLength, DigitColor, false));
                        currentOffset += currentLength;
                        currentLength  = 0;
                        continue;
                    }

                    // Check for SPAN ENDs
                    if (inSpan)
                    {
                        if (activeSpan.End != null && !activeSpan.End.Equals(""))
                        {
                            if (currentLine.MatchExpr(activeSpan.End, i, document))
                            {
                                PushCurWord(document, ref markNext, words);
                                string regex = currentLine.GetRegString(activeSpan.End, i, document);
                                currentLength += regex.Length;
                                words.Add(new TextWord(document, currentLine, currentOffset, currentLength, activeSpan.EndColor, false));
                                currentOffset += currentLength;
                                currentLength  = 0;
                                i             += regex.Length - 1;
                                currentSpanStack.Pop();
                                UpdateSpanStateVariables();
                                continue;
                            }
                        }
                    }

                    // check for SPAN BEGIN
                    if (activeRuleSet != null)
                    {
                        foreach (Span span in activeRuleSet.Spans)
                        {
                            if (currentLine.MatchExpr(span.Begin, i, document))
                            {
                                PushCurWord(document, ref markNext, words);
                                string regex = currentLine.GetRegString(span.Begin, i, document);
                                currentLength += regex.Length;
                                words.Add(new TextWord(document, currentLine, currentOffset, currentLength, span.BeginColor, false));
                                currentOffset += currentLength;
                                currentLength  = 0;

                                i += regex.Length - 1;
                                if (currentSpanStack == null)
                                {
                                    currentSpanStack = new Stack();
                                }
                                currentSpanStack.Push(span);

                                UpdateSpanStateVariables();

                                goto skip;
                            }
                        }
                    }

                    // check if the char is a delimiter
                    if (activeRuleSet != null && (int)ch < 256 && activeRuleSet.Delimiters[(int)ch])
                    {
                        PushCurWord(document, ref markNext, words);
                        if (currentOffset + currentLength + 1 < currentLine.Length)
                        {
                            ++currentLength;
                            PushCurWord(document, ref markNext, words);
                            goto skip;
                        }
                    }

                    ++currentLength;
                    skip : continue;
                }
                }
            }

            PushCurWord(document, ref markNext, words);

            return(words);
        }
        void PushCurWord(IDocument document, ref HighlightColor markNext, ArrayList words)
        {
            //Need to look through the next prev logic.
            if (currentLength > 0)
            {
                if (words.Count > 0 && activeRuleSet != null)
                {
                    TextWord prevWord = null;
                    int      pInd     = words.Count - 1;
                    while (pInd >= 0)
                    {
                        if (!((TextWord)words[pInd]).IsWhiteSpace)
                        {
                            prevWord = (TextWord)words[pInd];
                            if (prevWord.HasDefaultColor)
                            {
                                PrevMarker marker = (PrevMarker)activeRuleSet.PrevMarkers[document, currentLine, currentOffset, currentLength];
                                if (marker != null)
                                {
                                    prevWord.HighlightColor = marker.Color;
                                }
                            }
                            break;
                        }
                        pInd--;
                    }
                }

                if (inSpan)
                {
                    HighlightColor c = null;
                    bool           hasDefaultColor = true;
                    if (activeSpan.Rule == null)
                    {
                        c = activeSpan.Color;
                    }
                    else
                    {
                        c = GetColor(activeRuleSet, document, currentLine, currentOffset, currentLength);
                        hasDefaultColor = false;
                    }

                    if (c == null)
                    {
                        c = activeSpan.Color;
                        if (c.Color == Color.Transparent)
                        {
                            c = GetEnvironmentColorForName("Default");
                        }
                        hasDefaultColor = true;
                    }
                    words.Add(new TextWord(document, currentLine, currentOffset, currentLength, markNext != null ? markNext : c, hasDefaultColor));
                }
                else
                {
                    HighlightColor c = markNext != null ? markNext : GetColor(activeRuleSet, document, currentLine, currentOffset, currentLength);
                    if (c == null)
                    {
                        words.Add(new TextWord(document, currentLine, currentOffset, currentLength, GetEnvironmentColorForName("Default"), true));
                    }
                    else
                    {
                        words.Add(new TextWord(document, currentLine, currentOffset, currentLength, c, false));
                    }
                }

                if (activeRuleSet != null)
                {
                    NextMarker nextMarker = (NextMarker)activeRuleSet.NextMarkers[document, currentLine, currentOffset, currentLength];
                    if (nextMarker != null)
                    {
                        if (nextMarker.MarkMarker && words.Count > 0)
                        {
                            TextWord prevword = ((TextWord)words[words.Count - 1]);
                            prevword.HighlightColor = nextMarker.Color;
                        }
                        markNext = nextMarker.Color;
                    }
                    else
                    {
                        markNext = null;
                    }
                }
                currentOffset += currentLength;
                currentLength  = 0;
            }
        }
 internal void SetColorForEnvironment(string name, HighlightColor color)
 {
     environmentColors[name] = color;
 }
Exemplo n.º 11
0
		public TabTextWord(HighlightColor  highlightColor)
		{

			base.HighlightColor  = highlightColor;
		}
Exemplo n.º 12
0
		public SpaceTextWord(HighlightColor  highlightColor)
		{
			
			base.HighlightColor  = highlightColor;
		}
Exemplo n.º 13
0
		public TextWord(IDocument document, LineSegment line, int offset, int length, HighlightColor highlightColor, bool hasDefaultColor)
		{
			Debug.Assert(document != null);
			Debug.Assert(line != null);
			Debug.Assert(highlightColor != null);
			
			this.document = document;
			this.line  = line;
			this.offset = offset;
			this.length = length;
			this.highlightColor = highlightColor;
			this.hasDefaultColor = hasDefaultColor;
		}
Exemplo n.º 14
0
        //每个RuleSet节点的样子:<RuleSet ignorecase = "false">......</RuleSet>
        public HighlightRuleSet(XmlElement el)
        {
            if (el.Attributes["name"] != null)
            {
                Name = el.Attributes["name"].InnerText;
            }

            if (el.Attributes["noescapesequences"] != null)
            {
                noEscapeSequences = Boolean.Parse(el.Attributes["noescapesequences"].InnerText);
            }

            if (el.Attributes["reference"] != null)
            {
                reference = el.Attributes["reference"].InnerText;
            }

            if (el.Attributes["ignorecase"] != null)
            {
                ignoreCase = Boolean.Parse(el.Attributes["ignorecase"].InnerText);
            }

            for (int i = 0; i < Delimiters.Length; ++i)
            {
                Delimiters[i] = false;
            }

            if (el["Delimiters"] != null)
            {
                string delimiterString = el["Delimiters"].InnerText;
                foreach (char ch in delimiterString)
                {
                    Delimiters[(int)ch] = true;                    //将当前字符置为分隔符.
                }
            }

            XmlNodeList nodes = null;

            //以下是初始化Span.
            nodes = el.GetElementsByTagName("Span");
            foreach (XmlElement el2 in nodes)
            {
                Spans.Add(new Span(el2));
            }

            keyWords    = new LookupTable(!IgnoreCase);
            prevMarkers = new LookupTable(!IgnoreCase);
            nextMarkers = new LookupTable(!IgnoreCase);

            //以下是初始化KeyWords.
            nodes = el.GetElementsByTagName("KeyWords");
            foreach (XmlElement el2 in nodes)
            {
                HighlightColor color = new HighlightColor(el2);

                XmlNodeList keys = el2.GetElementsByTagName("Key");
                foreach (XmlElement node in keys)
                {
                    keyWords[node.Attributes["word"].InnerText] = color;                    //为每个关键字都赋予一个HighLightColor对象.
                }
            }

            //每个MarkPrevious节点的样子:
            //<MarkPrevious bold = "true" italic = "false" color = "MidnightBlue">(</MarkPrevious>
            nodes = el.GetElementsByTagName("MarkPrevious");
            foreach (XmlElement el2 in nodes)
            {
                PrevMarker prev = new PrevMarker(el2);
                prevMarkers[prev.What] = prev;
            }
            //每个MarkFollowing节点的样子和MarkFollowing的样子完全一样.
            nodes = el.GetElementsByTagName("MarkFollowing");
            foreach (XmlElement el2 in nodes)
            {
                NextMarker next = new NextMarker(el2);
                nextMarkers[next.What] = next;
            }
        }