From 947d73507622594f2b331f32da73a09d3c7a34c5 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Mon, 3 Apr 2006 13:56:33 +0000 Subject: [PATCH] Default syntax highlighting strategy can be extended - patch by Robert Zaunere. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@1258 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../DefaultHighlightingStrategy.cs | 50 +++++++++++-------- .../HighlightingDefinitionParser.cs | 17 ++++--- .../HighlightingManager.cs | 9 ++++ 3 files changed, 47 insertions(+), 29 deletions(-) diff --git a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/HighlightingStrategy/DefaultHighlightingStrategy.cs b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/HighlightingStrategy/DefaultHighlightingStrategy.cs index 099b1bf2f0..20ceb1c789 100644 --- a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/HighlightingStrategy/DefaultHighlightingStrategy.cs +++ b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/HighlightingStrategy/DefaultHighlightingStrategy.cs @@ -112,7 +112,7 @@ namespace ICSharpCode.TextEditor.Document rules.Add(aRuleSet); } - internal void ResolveReferences() + public void ResolveReferences() { // Resolve references from Span definitions to RuleSets ResolveRuleSetReferences(); @@ -184,7 +184,7 @@ namespace ICSharpCode.TextEditor.Document } } - internal void SetColorFor(string name, HighlightColor color) + public void SetColorFor(string name, HighlightColor color) { if (name == "Default") defaultTextColor = new HighlightColor(color.Color, color.Bold, color.Italic); @@ -204,7 +204,7 @@ namespace ICSharpCode.TextEditor.Document return GetColor(defaultRuleSet, document, currentSegment, currentOffset, currentLength); } - HighlightColor GetColor(HighlightRuleSet ruleSet, IDocument document, LineSegment currentSegment, int currentOffset, int currentLength) + protected virtual HighlightColor GetColor(HighlightRuleSet ruleSet, IDocument document, LineSegment currentSegment, int currentOffset, int currentLength) { if (ruleSet != null) { if (ruleSet.Reference != null) { @@ -235,10 +235,10 @@ namespace ICSharpCode.TextEditor.Document } // Line state variable - LineSegment currentLine; + protected LineSegment currentLine; // Span stack state variable - Stack currentSpanStack; + protected Stack currentSpanStack; public void MarkTokens(IDocument document) { @@ -428,13 +428,13 @@ namespace ICSharpCode.TextEditor.Document } // Span state variables - bool inSpan; - Span activeSpan; - HighlightRuleSet activeRuleSet; + protected bool inSpan; + protected Span activeSpan; + protected HighlightRuleSet activeRuleSet; // Line scanning state variables - int currentOffset; - int currentLength; + protected int currentOffset; + protected int currentLength; void UpdateSpanStateVariables() { @@ -593,18 +593,21 @@ namespace ICSharpCode.TextEditor.Document 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(); + + if (!OverrideSpan(regex, document, words, span, ref i)) { + 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(); } - currentSpanStack.Push(span); - - UpdateSpanStateVariables(); goto skip; } @@ -631,6 +634,11 @@ namespace ICSharpCode.TextEditor.Document return words; } + + protected virtual bool OverrideSpan(string spanBegin, IDocument document, List words, Span span, ref int lineOffset) + { + return false; + } /// /// pushes the curWord string on the word list, with the diff --git a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/HighlightingStrategy/HighlightingDefinitionParser.cs b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/HighlightingStrategy/HighlightingDefinitionParser.cs index d96c0ca91e..218e1b226d 100644 --- a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/HighlightingStrategy/HighlightingDefinitionParser.cs +++ b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/HighlightingStrategy/HighlightingDefinitionParser.cs @@ -16,16 +16,16 @@ using System.Reflection; namespace ICSharpCode.TextEditor.Document { - internal class HighlightingDefinitionParser + public static class HighlightingDefinitionParser { - private HighlightingDefinitionParser() - { - // This is a pure utility class with no instances. - } - static ArrayList errors = null; - + public static DefaultHighlightingStrategy Parse(SyntaxMode syntaxMode, XmlTextReader xmlTextReader) + { + return Parse(null, syntaxMode, xmlTextReader); + } + + public static DefaultHighlightingStrategy Parse(DefaultHighlightingStrategy highlighter, SyntaxMode syntaxMode, XmlTextReader xmlTextReader) { if (syntaxMode == null) throw new ArgumentNullException("syntaxMode"); @@ -48,7 +48,8 @@ namespace ICSharpCode.TextEditor.Document XmlDocument doc = new XmlDocument(); doc.Load(validatingReader); - DefaultHighlightingStrategy highlighter = new DefaultHighlightingStrategy(doc.DocumentElement.Attributes["name"].InnerText); + if (highlighter == null) + highlighter = new DefaultHighlightingStrategy(doc.DocumentElement.Attributes["name"].InnerText); if (doc.DocumentElement.Attributes["extensions"]!= null) { highlighter.Extensions = doc.DocumentElement.Attributes["extensions"].InnerText.Split(new char[] { ';', '|' }); diff --git a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/HighlightingStrategy/HighlightingManager.cs b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/HighlightingStrategy/HighlightingManager.cs index 23440d6033..aa58d3ebd1 100644 --- a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/HighlightingStrategy/HighlightingManager.cs +++ b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/HighlightingStrategy/HighlightingManager.cs @@ -57,6 +57,15 @@ namespace ICSharpCode.TextEditor.Document syntaxModeFileProviders.Add(syntaxModeFileProvider); } } + + public void AddHighlightingStrategy(IHighlightingStrategy highlightingStrategy) + { + highlightingDefs[highlightingStrategy.Name] = highlightingStrategy; + foreach (string extension in highlightingStrategy.Extensions) + { + extensionsToName[extension.ToUpperInvariant()] = highlightingStrategy.Name; + } + } public void ReloadSyntaxModes() {