1 | // |
---|
2 | // Lol Engine - VsLol add-in for Visual Studio |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net> |
---|
5 | // This program is free software; you can redistribute it and/or |
---|
6 | // modify it under the terms of the Do What The Fuck You Want To |
---|
7 | // Public License, Version 2, as published by Sam Hocevar. See |
---|
8 | // http://www.wtfpl.net/ for more details. |
---|
9 | // |
---|
10 | |
---|
11 | using System; |
---|
12 | using System.Collections.Generic; |
---|
13 | using System.ComponentModel.Composition; |
---|
14 | using System.Windows.Media; |
---|
15 | using System.Text.RegularExpressions; |
---|
16 | |
---|
17 | using Microsoft.VisualStudio.Text; |
---|
18 | using Microsoft.VisualStudio.Text.Classification; |
---|
19 | using Microsoft.VisualStudio.Text.Formatting; |
---|
20 | using Microsoft.VisualStudio.Language.StandardClassification; |
---|
21 | using Microsoft.VisualStudio.Utilities; |
---|
22 | |
---|
23 | namespace lol |
---|
24 | { |
---|
25 | |
---|
26 | [Export(typeof(IClassifierProvider))] |
---|
27 | [ContentType("c/c++")] |
---|
28 | [ContentType("csharp")] |
---|
29 | [ContentType("lolfx")] |
---|
30 | internal class LolClassifierProvider : IClassifierProvider |
---|
31 | { |
---|
32 | [Import] |
---|
33 | internal IClassificationTypeRegistryService m_type_registry = null; |
---|
34 | [Import] |
---|
35 | internal IClassifierAggregatorService m_aggregator = null; |
---|
36 | [Import] |
---|
37 | internal IClassificationFormatMapService m_format_map = null; |
---|
38 | |
---|
39 | internal static bool m_inprogress = false; |
---|
40 | |
---|
41 | public IClassifier GetClassifier(ITextBuffer buffer) |
---|
42 | { |
---|
43 | /* Avoid infinite recursion */ |
---|
44 | if (m_inprogress) |
---|
45 | return null; |
---|
46 | |
---|
47 | LolGenericFormat.SetRegistry(m_type_registry, m_format_map); |
---|
48 | |
---|
49 | try |
---|
50 | { |
---|
51 | m_inprogress = true; |
---|
52 | return buffer.Properties.GetOrCreateSingletonProperty<CppKeywordClassifier>(delegate { return new CppKeywordClassifier(m_type_registry, m_aggregator.GetClassifier(buffer), buffer.ContentType); }); |
---|
53 | } |
---|
54 | finally { m_inprogress = false; } |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | class CppKeywordClassifier : IClassifier |
---|
59 | { |
---|
60 | private IClassifier m_classifier; |
---|
61 | |
---|
62 | private IClassificationType m_types_type, m_constant_type; |
---|
63 | private Regex m_types_regex, m_constant_regex; |
---|
64 | |
---|
65 | private static const string[] m_all_types = |
---|
66 | { |
---|
67 | "auto|void|bool|int|unsigned|char|short|long|float|double"; |
---|
68 | "class|struct|union|template|namespace|typename|typedef", |
---|
69 | "const|static|extern|volatile|inline|explicit" |
---|
70 | }; |
---|
71 | |
---|
72 | private static const string[] m_cpp_types = |
---|
73 | { |
---|
74 | "u?int(8|16|32|64|ptr)_t", |
---|
75 | "(wchar|size|ssize)_t", |
---|
76 | "va_list", |
---|
77 | }; |
---|
78 | |
---|
79 | /* ldouble real half |
---|
80 | "(f(16|128)||d|[ui](8|16||64)|r)(vec[234]|mat[234]|quat|cmplx)"; |
---|
81 | */ |
---|
82 | |
---|
83 | private static const string[] m_csharp_types = |
---|
84 | { |
---|
85 | "var|string", |
---|
86 | "out|ref|internal|sealed|public|private|protected|override" |
---|
87 | }; |
---|
88 | |
---|
89 | private static const string[] m_lolfx_types = |
---|
90 | { |
---|
91 | "attribute|varying|uniform|in|out", |
---|
92 | "int|uint", |
---|
93 | "(|[dui])(vec|mat)[234]" |
---|
94 | }; |
---|
95 | |
---|
96 | private static const string[] m_all_constants = |
---|
97 | { |
---|
98 | "true|false" |
---|
99 | }; |
---|
100 | |
---|
101 | private static const string[] m_cpp_constants = |
---|
102 | { |
---|
103 | "NULL|nullptr", |
---|
104 | "EXIT_SUCCESS|EXIT_FAILURE", |
---|
105 | "M_(E|LOG(2|10)E|LN2|LN10|PI|PI_2|PI_4|1_PI|2_PI|2_SQRTPI|SQRT(2|1_2))", |
---|
106 | "SIG(HUP|INT|QUIT|ILL|TRAP|ABRT|FPE|KILL|USR1|SEGV|USR2|PIPE|ALRM)", |
---|
107 | "SIG(TERM|CHLD|CONT|STOP|TSTP|TTIN|TTOU)" |
---|
108 | }; |
---|
109 | |
---|
110 | private static const string[] m_csharp_constants = |
---|
111 | { |
---|
112 | "null", |
---|
113 | }; |
---|
114 | |
---|
115 | private static const string[] m_lolfx_constants = |
---|
116 | { |
---|
117 | "gl_Position|gl_FragColor", |
---|
118 | }; |
---|
119 | |
---|
120 | internal CppKeywordClassifier(IClassificationTypeRegistryService registry, |
---|
121 | IClassifier classifier, |
---|
122 | IContentType type) |
---|
123 | { |
---|
124 | m_classifier = classifier; |
---|
125 | |
---|
126 | /* Regex for types and specifiers */ |
---|
127 | m_types_type = registry.GetClassificationType("LolAnyType"); |
---|
128 | |
---|
129 | List<string> types_list = m_all_types.ToList(); |
---|
130 | if (type.IsOfType("c/c++")) |
---|
131 | types_list = types_list.Concat(m_cpp_types).ToList(); |
---|
132 | if (type.IsOfType("csharp")) |
---|
133 | types_list = types_list.Concat(m_csharp_types).ToList(); |
---|
134 | if (type.IsOfType("lolfx")) |
---|
135 | types_list = types_list.Concat(m_lolfx_types).ToList(); |
---|
136 | m_types_regex = |
---|
137 | new Regex(@"\b(" + String.Join("|", types_list.ToArray()) + @")\b"); |
---|
138 | |
---|
139 | /* Regex for constant words */ |
---|
140 | m_constant_type = registry.GetClassificationType("LolAnyConstant"); |
---|
141 | |
---|
142 | List<string> constants_list = m_all_constants.ToList(); |
---|
143 | if (type.IsOfType("c/c++")) |
---|
144 | constants_list = constants_list.Concat(m_cpp_constants).ToList(); |
---|
145 | if (type.IsOfType("csharp")) |
---|
146 | constants_list = constants_list.Concat(m_csharp_constants).ToList(); |
---|
147 | if (type.IsOfType("lolfx")) |
---|
148 | constants_list = constants_list.Concat(m_lolfx_constants).ToList(); |
---|
149 | m_constant_regex = |
---|
150 | new Regex(@"\b(" + String.Join("|", constants_list.ToArray()) + @")\b"); |
---|
151 | } |
---|
152 | |
---|
153 | public IList<ClassificationSpan> GetClassificationSpans(SnapshotSpan span) |
---|
154 | { |
---|
155 | List<ClassificationSpan> ret = new List<ClassificationSpan>(); |
---|
156 | |
---|
157 | foreach (ClassificationSpan cs in m_classifier.GetClassificationSpans(span)) |
---|
158 | { |
---|
159 | string cs_class = cs.ClassificationType.Classification.ToLower(); |
---|
160 | |
---|
161 | /* Only apply our rules if we found a keyword or an identifier */ |
---|
162 | if (cs_class == "keyword" || cs_class == "identifier") |
---|
163 | { |
---|
164 | if (m_types_regex.IsMatch(cs.Span.GetText())) |
---|
165 | { |
---|
166 | ret.Add(new ClassificationSpan(cs.Span, m_types_type)); |
---|
167 | continue; |
---|
168 | } |
---|
169 | |
---|
170 | if (m_constant_regex.IsMatch(cs.Span.GetText())) |
---|
171 | { |
---|
172 | ret.Add(new ClassificationSpan(cs.Span, m_constant_type)); |
---|
173 | continue; |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | ret.Add(cs); |
---|
178 | } |
---|
179 | |
---|
180 | return ret; |
---|
181 | } |
---|
182 | |
---|
183 | public event EventHandler<ClassificationChangedEventArgs> ClassificationChanged; |
---|
184 | } |
---|
185 | |
---|
186 | internal class LolGenericFormat : ClassificationFormatDefinition |
---|
187 | { |
---|
188 | static IClassificationTypeRegistryService m_type_registry; |
---|
189 | static IClassificationFormatMapService m_format_map; |
---|
190 | |
---|
191 | public static void SetRegistry(IClassificationTypeRegistryService type_registry, |
---|
192 | IClassificationFormatMapService format_map) |
---|
193 | { |
---|
194 | m_type_registry = type_registry; |
---|
195 | m_format_map = format_map; |
---|
196 | } |
---|
197 | |
---|
198 | protected void CopyStyleColor(string category) |
---|
199 | { |
---|
200 | if (m_type_registry == null || m_format_map == null) |
---|
201 | return; |
---|
202 | |
---|
203 | var map = m_format_map.GetClassificationFormatMap("Text Editor"); |
---|
204 | if (map == null) |
---|
205 | return; |
---|
206 | |
---|
207 | //string[] foo = { "Comment", "Keyword", "C/C++ User Keywords", "Call Return", "HTML Comment" , "User Types", "User Types (Type parameters)", "User Types (Value types)"}; |
---|
208 | |
---|
209 | var type = m_type_registry.GetClassificationType(category); |
---|
210 | if (type == null) |
---|
211 | return; |
---|
212 | |
---|
213 | var prop = map.GetExplicitTextProperties(type); |
---|
214 | if (prop == null) |
---|
215 | return; |
---|
216 | |
---|
217 | var c1 = prop.ForegroundBrush as SolidColorBrush; |
---|
218 | if (c1 != null && c1.Color != Colors.Transparent) |
---|
219 | { |
---|
220 | this.ForegroundColor = c1.Color; |
---|
221 | this.ForegroundOpacity = 1.0; |
---|
222 | } |
---|
223 | var c2 = prop.BackgroundBrush as SolidColorBrush; |
---|
224 | if (c2 != null && c2.Color != Colors.Transparent) |
---|
225 | { |
---|
226 | this.BackgroundColor = c2.Color; |
---|
227 | this.BackgroundOpacity = 1.0; |
---|
228 | } |
---|
229 | } |
---|
230 | } |
---|
231 | |
---|
232 | internal static class LolClassifierClassificationDefinition |
---|
233 | { |
---|
234 | [Export(typeof(ClassificationTypeDefinition))] |
---|
235 | [Name(LolCppTypeFormat.m_name)] |
---|
236 | internal static ClassificationTypeDefinition LolCustomClassType = null; |
---|
237 | |
---|
238 | [Export(typeof(ClassificationTypeDefinition))] |
---|
239 | [Name(LolCppConstantFormat.m_name)] |
---|
240 | internal static ClassificationTypeDefinition LolCustomConstantType = null; |
---|
241 | } |
---|
242 | |
---|
243 | [Export(typeof(EditorFormatDefinition))] |
---|
244 | [ClassificationType(ClassificationTypeNames = LolCppTypeFormat.m_name)] |
---|
245 | [Name(LolCppTypeFormat.m_name)] |
---|
246 | [UserVisible(true)] |
---|
247 | [Order(After = Priority.Default)] /* Override the Visual Studio classifiers */ |
---|
248 | internal sealed class LolCppTypeFormat : LolGenericFormat |
---|
249 | { |
---|
250 | public const string m_name = "LolAnyType"; |
---|
251 | public LolCppTypeFormat() |
---|
252 | { |
---|
253 | this.DisplayName = "C/C++ Types and Qualifiers"; |
---|
254 | this.ForegroundColor = Colors.Lime; |
---|
255 | this.ForegroundOpacity = 1.0; |
---|
256 | this.IsBold = true; |
---|
257 | //CopyStyleColor("User Types"); |
---|
258 | } |
---|
259 | } |
---|
260 | |
---|
261 | [Export(typeof(EditorFormatDefinition))] |
---|
262 | [ClassificationType(ClassificationTypeNames = LolCppConstantFormat.m_name)] |
---|
263 | [Name(LolCppConstantFormat.m_name)] |
---|
264 | [UserVisible(true)] |
---|
265 | [Order(After = Priority.Default)] /* Override the Visual Studio classifiers */ |
---|
266 | internal sealed class LolCppConstantFormat : LolGenericFormat |
---|
267 | { |
---|
268 | public const string m_name = "LolAnyConstant"; |
---|
269 | public LolCppConstantFormat() |
---|
270 | { |
---|
271 | this.DisplayName = "C/C++ Constants"; |
---|
272 | this.ForegroundColor = Colors.Magenta; |
---|
273 | this.ForegroundOpacity = 1.0; |
---|
274 | this.IsBold = true; |
---|
275 | //CopyStyleColor("User Types"); |
---|
276 | } |
---|
277 | } |
---|
278 | |
---|
279 | } /* namespace lol */ |
---|