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

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

vslol: add many more C/C++ types to the default list.

File size: 4.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel.Composition;
4using System.Windows.Media;
5using System.Text.RegularExpressions;
6
7using Microsoft.VisualStudio.Text;
8using Microsoft.VisualStudio.Text.Classification;
9using Microsoft.VisualStudio.Language.StandardClassification;
10using Microsoft.VisualStudio.Utilities;
11
12namespace Lol.VisualStudio.Plugin
13{
14    [Export(typeof(IClassifierProvider))]
15    [ContentType("c/c++")]
16    [ContentType("csharp")]
17    [ContentType("lolfx")]
18    internal class LolClassifierProvider : IClassifierProvider
19    {
20        [Import]
21        internal IClassificationTypeRegistryService m_class_registry = null; /* Set via MEF */
22
23        //[Import]
24        //internal IClassificationFormatMapService m_lol = null;
25
26        public IClassifier GetClassifier(ITextBuffer buffer)
27        {
28            //var test = m_lol.GetClassificationFormatMap("Text Editor");
29            //string[] foo = { "Comment", "Keyword", "C/C++ User Keywords", "Call Return", "HTML Comment" };
30            //foreach (var s in foo)
31            //{
32            //    var type = m_class_registry.GetClassificationType(s);
33            //    if (type == null)
34            //        continue;
35            //    var prop = test.GetExplicitTextProperties(type);
36            //    if (prop == null)
37            //        continue;
38            //    var c1 = prop.ForegroundBrush as SolidColorBrush;
39            //    var c2 = prop.BackgroundBrush as SolidColorBrush;
40            //    Logger.Info("[" + s + "]: " + c1.ToString() + " " + c2.ToString() + "\n");
41            //}
42
43            return buffer.Properties.GetOrCreateSingletonProperty<CppKeywordClassifier>(delegate { return new CppKeywordClassifier(m_class_registry, buffer.ContentType); });
44        }
45    }
46
47    class CppKeywordClassifier : IClassifier
48    {
49        private IClassificationType m_customclass_type;
50        private Regex m_regex;
51
52        internal CppKeywordClassifier(IClassificationTypeRegistryService registry,
53                                      IContentType type)
54        {
55            m_customclass_type = registry.GetClassificationType("LolCustomClass");
56
57            string tmp = @"\b(";
58            tmp += "void|bool|int|unsigned|char|short|long|float|double|ldouble|";
59            tmp += "class|struct|union|template|const|static|extern|volatile|inline|namespace|";
60            if (type.IsOfType("lolfx"))
61                tmp += "attribute|varying|uniform|in|out|";
62            if (type.IsOfType("csharp"))
63                tmp += "var|string|internal|sealed|public|private|";
64            if (!type.IsOfType("csharp"))
65                tmp += "(f(16|128)||d|[ui](8|16||64)|r)(vec[234]|mat[234]|quat|cmplx)|";
66            if (type.IsOfType("c/c++"))
67            {
68                tmp += "u?int(8|16|32|64|ptr)_t|";
69                tmp += "real|half|explicit|typename|typedef|";
70            }
71            tmp += @")\b";
72            m_regex = new Regex(tmp);
73        }
74
75        public IList<ClassificationSpan> GetClassificationSpans(SnapshotSpan span)
76        {
77            List<ClassificationSpan> ret = new List<ClassificationSpan>();
78
79            string tmp = span.GetText();
80            var matches = m_regex.Matches(tmp);
81            foreach (Match m in matches)
82            {
83                Span newspan = new Span(span.Start.Position + m.Index, m.Length);
84                SnapshotSpan newsnapshot = new SnapshotSpan(span.Snapshot, newspan);
85                ret.Add(new ClassificationSpan(newsnapshot, m_customclass_type));
86            }
87
88            return ret;
89        }
90
91        public event EventHandler<ClassificationChangedEventArgs> ClassificationChanged;
92    }
93
94    internal static class LolClassifierClassificationDefinition
95    {
96        [Export(typeof(ClassificationTypeDefinition))]
97        [Name(LolCppTypeFormat.m_name)]
98        internal static ClassificationTypeDefinition LolCustomClassType = null;
99    }
100
101    [Export(typeof(EditorFormatDefinition))]
102    [ClassificationType(ClassificationTypeNames = LolCppTypeFormat.m_name)]
103    [Name(LolCppTypeFormat.m_name)]
104    [UserVisible(true)]
105    [Order(After = Priority.Default)] /* Override the Visual Studio classifiers */
106    internal sealed class LolCppTypeFormat : ClassificationFormatDefinition
107    {
108        public const string m_name = "LolCustomClass";
109        public LolCppTypeFormat()
110        {
111            this.DisplayName = "C/C++ Types and Qualifiers";
112            //this.BackgroundColor = Colors.BlueViolet;
113            this.ForegroundColor = Colors.Lime;
114            this.IsBold = true;
115            //this.TextDecorations = System.Windows.TextDecorations.Underline;
116        }
117    }
118}
Note: See TracBrowser for help on using the repository browser.