26 using System.Collections.Generic;
27 using System.ComponentModel.Composition;
28 using System.Diagnostics;
29 using Microsoft.VisualStudio.Language.StandardClassification;
30 using Microsoft.VisualStudio.Text;
31 using Microsoft.VisualStudio.Text.Adornments;
32 using Microsoft.VisualStudio.Text.Classification;
35 using System.Text.RegularExpressions;
37 using SharpYaml.Schemas;
40 namespace SiliconStudio.
Paradox.VisualStudio.Assets
43 #region Provider definition
57 internal IClassificationTypeRegistryService ClassificationRegistry = null;
59 public IClassifier GetClassifier(ITextBuffer buffer)
61 return buffer.Properties.GetOrCreateSingletonProperty<AssetObjectEditor>(delegate {
return new AssetObjectEditor(ClassificationRegistry); });
64 #endregion //provider def
72 private readonly CoreSchema schema;
73 private readonly IClassificationType _comment;
74 private readonly IClassificationType _anchor;
75 private readonly IClassificationType _alias;
76 private readonly IClassificationType _key;
77 private readonly IClassificationType _value;
78 private readonly IClassificationType _bool;
79 private readonly IClassificationType _number;
80 private readonly IClassificationType _string;
81 private readonly IClassificationType _tag;
82 private readonly IClassificationType _symbol;
83 private readonly IClassificationType _directive;
84 private readonly IClassificationType _tab;
88 schema =
new CoreSchema();
89 _comment = registry.GetClassificationType(PredefinedClassificationTypeNames.Comment);
90 _anchor = registry.GetClassificationType(AssetObjectDefinitions.AnchorClassificationName);
91 _alias = registry.GetClassificationType(AssetObjectDefinitions.AliasClassificationName);
92 _key = registry.GetClassificationType(AssetObjectDefinitions.KeyClassificationName);
93 _value = registry.GetClassificationType(PredefinedClassificationTypeNames.Identifier);
94 _number = registry.GetClassificationType(AssetObjectDefinitions.NumberClassificationName);
95 _string = registry.GetClassificationType(PredefinedClassificationTypeNames.String);
96 _bool = registry.GetClassificationType(PredefinedClassificationTypeNames.Keyword);
97 _tag = registry.GetClassificationType(PredefinedClassificationTypeNames.SymbolDefinition);
98 _symbol = registry.GetClassificationType(PredefinedClassificationTypeNames.Operator);
99 _directive = registry.GetClassificationType(PredefinedClassificationTypeNames.PreprocessorKeyword);
100 _tab = registry.GetClassificationType(AssetObjectDefinitions.ErrorClassificationName);
111 var classifications =
new List<ClassificationSpan>();
113 var text = span.GetText();
115 var commentIndex = text.IndexOf(
'#');
116 if (commentIndex >= 0)
119 new ClassificationSpan(
122 new Span(span.Start + commentIndex, span.Length - commentIndex)
128 text = text.Substring(0, commentIndex);
131 var match = Regex.Match(text,
@"^( *(\t+))+");
134 foreach (Capture capture
in match.Groups[2].Captures)
137 new ClassificationSpan(
140 new Span(span.Start + capture.Index, capture.Length)
150 var scanner =
new Scanner(
new StringReader(text));
152 Type previousTokenType = null;
153 while (scanner.MoveNext())
155 IClassificationType classificationType = null;
157 var currentTokenType = scanner.Current.GetType();
158 var tokenLength = scanner.Current.End.Index - scanner.Current.Start.Index;
160 if (currentTokenType == typeof(Anchor))
162 classificationType = _anchor;
164 else if (currentTokenType == typeof(AnchorAlias))
166 classificationType = _alias;
168 else if (currentTokenType == typeof(Scalar))
170 if (previousTokenType == typeof (Key))
172 classificationType = _key;
177 var scalarToken = (Scalar) scanner.Current;
178 var scalar =
new SharpYaml.Events.Scalar(scalarToken.Value);
179 switch (schema.GetDefaultTag(scalar))
181 case JsonSchema.BoolShortTag:
182 classificationType = _bool;
184 case JsonSchema.FloatShortTag:
185 case JsonSchema.IntShortTag:
186 classificationType = _number;
188 case SchemaBase.StrShortTag:
189 classificationType = scalarToken.Style == ScalarStyle.DoubleQuoted || scalarToken.Style == ScalarStyle.SingleQuoted ? _string : _value;
192 classificationType = _value;
198 else if (currentTokenType == typeof(Tag))
200 classificationType = _tag;
202 else if (currentTokenType == typeof(TagDirective))
204 classificationType = _directive;
206 else if (currentTokenType == typeof(VersionDirective))
208 classificationType = _directive;
210 else if (tokenLength > 0)
212 classificationType = _symbol;
215 previousTokenType = currentTokenType;
217 if (classificationType != null && tokenLength > 0)
220 new ClassificationSpan(
223 new Span(span.Start + scanner.Current.Start.Index, tokenLength)
233 Trace.WriteLine(
"Exception in AssetObjectEditor " + ex);
236 return classifications;
239 #pragma warning disable 67
244 #pragma warning restore 67
246 #endregion //Classifier
IList< ClassificationSpan > GetClassificationSpans(SnapshotSpan span)
This method scans the given SnapshotSpan for potential matches for this classification. In this instance, it classifies everything and returns each span as a new ClassificationSpan.
EventHandler< ClassificationChangedEventArgs > ClassificationChanged
global::MonoTouch.Constants Constants
SiliconStudio.Core.Utilities Utilities
Classifier that classifies all text as an instance of the OrinaryClassifierType
Tokens
Summary Canonical example of MPLEX automaton