Code Coverage Statistics for Source File
c:\Tools\SD3\src\Libraries\ICSharpCode.TextEditor\Project\Src\Document\HighlightingStrategy\HighlightingManager.cs
|
Sequence Point Coverage
N/A
0 of 0
|
Branch Coverage
N/A
0 of 0
|
Lines
163
|
Highlight:
Uncovered Code
Covered Code
| L | V | Source |
|---|---|---|
1 |
// <file> |
|
2 |
// <copyright see="prj:///doc/copyright.txt"/> |
|
3 |
// <license see="prj:///doc/license.txt"/> |
|
4 |
// <owner name="Mike Krüger" email="mike@icsharpcode.net"/> |
|
5 |
// <version>$Revision: 2533 $</version> |
|
6 |
// </file> |
|
7 |
||
8 |
using System; |
|
9 |
using System.Collections; |
|
10 |
using System.Collections.Generic; |
|
11 |
using System.IO; |
|
12 |
using System.Diagnostics; |
|
13 |
||
14 |
namespace ICSharpCode.TextEditor.Document |
|
15 |
{ |
|
16 |
public class HighlightingManager |
|
17 |
{ |
|
18 |
ArrayList syntaxModeFileProviders = new ArrayList(); |
|
19 |
static HighlightingManager highlightingManager; |
|
20 |
||
21 |
// hash table from extension name to highlighting definition, |
|
22 |
// OR from extension name to Pair SyntaxMode,ISyntaxModeFileProvider |
|
23 |
Hashtable highlightingDefs = new Hashtable(); |
|
24 |
||
25 |
Hashtable extensionsToName = new Hashtable(); |
|
26 |
||
27 |
public Hashtable HighlightingDefinitions { |
|
28 |
get { |
|
29 |
return highlightingDefs; |
|
30 |
} |
|
31 |
} |
|
32 |
||
33 |
public static HighlightingManager Manager { |
|
34 |
get { |
|
35 |
return highlightingManager; |
|
36 |
} |
|
37 |
} |
|
38 |
||
39 |
static HighlightingManager() |
|
40 |
{ |
|
41 |
highlightingManager = new HighlightingManager(); |
|
42 |
highlightingManager.AddSyntaxModeFileProvider(new ResourceSyntaxModeProvider()); |
|
43 |
} |
|
44 |
||
45 |
public HighlightingManager() |
|
46 |
{ |
|
47 |
CreateDefaultHighlightingStrategy(); |
|
48 |
} |
|
49 |
||
50 |
public void AddSyntaxModeFileProvider(ISyntaxModeFileProvider syntaxModeFileProvider) |
|
51 |
{ |
|
52 |
foreach (SyntaxMode syntaxMode in syntaxModeFileProvider.SyntaxModes) { |
|
53 |
highlightingDefs[syntaxMode.Name] = new DictionaryEntry(syntaxMode, syntaxModeFileProvider); |
|
54 |
foreach (string extension in syntaxMode.Extensions) { |
|
55 |
extensionsToName[extension.ToUpperInvariant()] = syntaxMode.Name; |
|
56 |
} |
|
57 |
} |
|
58 |
if (!syntaxModeFileProviders.Contains(syntaxModeFileProvider)) { |
|
59 |
syntaxModeFileProviders.Add(syntaxModeFileProvider); |
|
60 |
} |
|
61 |
} |
|
62 |
||
63 |
public void AddHighlightingStrategy(IHighlightingStrategy highlightingStrategy) |
|
64 |
{ |
|
65 |
highlightingDefs[highlightingStrategy.Name] = highlightingStrategy; |
|
66 |
foreach (string extension in highlightingStrategy.Extensions) |
|
67 |
{ |
|
68 |
extensionsToName[extension.ToUpperInvariant()] = highlightingStrategy.Name; |
|
69 |
} |
|
70 |
} |
|
71 |
||
72 |
public void ReloadSyntaxModes() |
|
73 |
{ |
|
74 |
highlightingDefs.Clear(); |
|
75 |
extensionsToName.Clear(); |
|
76 |
CreateDefaultHighlightingStrategy(); |
|
77 |
foreach (ISyntaxModeFileProvider provider in syntaxModeFileProviders) { |
|
78 |
provider.UpdateSyntaxModeList(); |
|
79 |
AddSyntaxModeFileProvider(provider); |
|
80 |
} |
|
81 |
OnReloadSyntaxHighlighting(EventArgs.Empty); |
|
82 |
} |
|
83 |
||
84 |
void CreateDefaultHighlightingStrategy() |
|
85 |
{ |
|
86 |
DefaultHighlightingStrategy defaultHighlightingStrategy = new DefaultHighlightingStrategy(); |
|
87 |
defaultHighlightingStrategy.Extensions = new string[] {}; |
|
88 |
defaultHighlightingStrategy.Rules.Add(new HighlightRuleSet()); |
|
89 |
highlightingDefs["Default"] = defaultHighlightingStrategy; |
|
90 |
} |
|
91 |
||
92 |
IHighlightingStrategy LoadDefinition(DictionaryEntry entry) |
|
93 |
{ |
|
94 |
SyntaxMode syntaxMode = (SyntaxMode)entry.Key; |
|
95 |
ISyntaxModeFileProvider syntaxModeFileProvider = (ISyntaxModeFileProvider)entry.Value; |
|
96 |
||
97 |
DefaultHighlightingStrategy highlightingStrategy = null; |
|
98 |
try { |
|
99 |
highlightingStrategy = HighlightingDefinitionParser.Parse(syntaxMode, syntaxModeFileProvider.GetSyntaxModeFile(syntaxMode)); |
|
100 |
if (highlightingStrategy.Name != syntaxMode.Name) { |
|
101 |
throw new HighlightingDefinitionInvalidException("The name specified in the .xshd '" + highlightingStrategy.Name + "' must be equal the syntax mode name '" + syntaxMode.Name + "'"); |
|
102 |
} |
|
103 |
} finally { |
|
104 |
if (highlightingStrategy == null) { |
|
105 |
highlightingStrategy = DefaultHighlighting; |
|
106 |
} |
|
107 |
highlightingDefs[syntaxMode.Name] = highlightingStrategy; |
|
108 |
highlightingStrategy.ResolveReferences(); |
|
109 |
} |
|
110 |
return highlightingStrategy; |
|
111 |
} |
|
112 |
||
113 |
public DefaultHighlightingStrategy DefaultHighlighting { |
|
114 |
get { |
|
115 |
return (DefaultHighlightingStrategy)highlightingDefs["Default"]; |
|
116 |
} |
|
117 |
} |
|
118 |
||
119 |
internal KeyValuePair<SyntaxMode, ISyntaxModeFileProvider> FindHighlighterEntry(string name) |
|
120 |
{ |
|
121 |
foreach (ISyntaxModeFileProvider provider in syntaxModeFileProviders) { |
|
122 |
foreach (SyntaxMode mode in provider.SyntaxModes) { |
|
123 |
if (mode.Name == name) { |
|
124 |
return new KeyValuePair<SyntaxMode, ISyntaxModeFileProvider>(mode, provider); |
|
125 |
} |
|
126 |
} |
|
127 |
} |
|
128 |
return new KeyValuePair<SyntaxMode, ISyntaxModeFileProvider>(null, null); |
|
129 |
} |
|
130 |
||
131 |
public IHighlightingStrategy FindHighlighter(string name) |
|
132 |
{ |
|
133 |
object def = highlightingDefs[name]; |
|
134 |
if (def is DictionaryEntry) { |
|
135 |
return LoadDefinition((DictionaryEntry)def); |
|
136 |
} |
|
137 |
return def == null ? DefaultHighlighting : (IHighlightingStrategy)def; |
|
138 |
} |
|
139 |
||
140 |
public IHighlightingStrategy FindHighlighterForFile(string fileName) |
|
141 |
{ |
|
142 |
string highlighterName = (string)extensionsToName[Path.GetExtension(fileName).ToUpperInvariant()]; |
|
143 |
if (highlighterName != null) { |
|
144 |
object def = highlightingDefs[highlighterName]; |
|
145 |
if (def is DictionaryEntry) { |
|
146 |
return LoadDefinition((DictionaryEntry)def); |
|
147 |
} |
|
148 |
return def == null ? DefaultHighlighting : (IHighlightingStrategy)def; |
|
149 |
} else { |
|
150 |
return DefaultHighlighting; |
|
151 |
} |
|
152 |
} |
|
153 |
||
154 |
protected virtual void OnReloadSyntaxHighlighting(EventArgs e) |
|
155 |
{ |
|
156 |
if (ReloadSyntaxHighlighting != null) { |
|
157 |
ReloadSyntaxHighlighting(this, e); |
|
158 |
} |
|
159 |
} |
|
160 |
||
161 |
public event EventHandler ReloadSyntaxHighlighting; |
|
162 |
} |
|
163 |
} |