source: trunk/tools/vslol/CppKeywordClassifier.cs @ 2127

Last change on this file since 2127 was 2127, checked in by sam, 8 years ago

vslol: more compilation fixes; this is the final 1.0.0.5 upload.

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