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

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

vslol: rename package to VsLol instead of VsLol.Sample and refactor
minor stuff.

File size: 5.4 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.Text.Formatting;
10using Microsoft.VisualStudio.Language.StandardClassification;
11using Microsoft.VisualStudio.Utilities;
12
13namespace Lol.VisualStudio.Plugin
14{
15    [Export(typeof(IClassifierProvider))]
16    [ContentType("c/c++")]
17    [ContentType("csharp")]
18    [ContentType("lolfx")]
19    internal class LolClassifierProvider : IClassifierProvider
20    {
21        [Import]
22        internal IClassificationTypeRegistryService m_type_registry = null; /* Set via MEF */
23
24        [Import]
25        internal IClassificationFormatMapService m_format_map = null;
26
27        public IClassifier GetClassifier(ITextBuffer buffer)
28        {
29            LolGenericFormat.SetRegistry(m_type_registry, m_format_map);
30
31            return buffer.Properties.GetOrCreateSingletonProperty<CppKeywordClassifier>(delegate { return new CppKeywordClassifier(m_type_registry, buffer.ContentType); });
32        }
33    }
34
35    class CppKeywordClassifier : IClassifier
36    {
37        private IClassificationType m_customclass_type;
38        private Regex m_regex;
39
40        internal CppKeywordClassifier(IClassificationTypeRegistryService registry,
41                                      IContentType type)
42        {
43            m_customclass_type = registry.GetClassificationType("LolCustomClass");
44
45            string tmp = @"\b(";
46            tmp += "void|bool|int|unsigned|char|short|long|float|double|ldouble|";
47            tmp += "class|struct|union|template|const|static|extern|volatile|inline|namespace|";
48            if (type.IsOfType("lolfx"))
49                tmp += "attribute|varying|uniform|in|out|";
50            if (type.IsOfType("csharp"))
51                tmp += "var|string|internal|sealed|public|private|protected|";
52            if (!type.IsOfType("csharp"))
53                tmp += "(f(16|128)||d|[ui](8|16||64)|r)(vec[234]|mat[234]|quat|cmplx)|";
54            if (type.IsOfType("c/c++"))
55            {
56                tmp += "u?int(8|16|32|64|ptr)_t|";
57                tmp += "real|half|explicit|typename|typedef|";
58            }
59            tmp += @")\b";
60            m_regex = new Regex(tmp);
61        }
62
63        public IList<ClassificationSpan> GetClassificationSpans(SnapshotSpan span)
64        {
65            List<ClassificationSpan> ret = new List<ClassificationSpan>();
66
67            string tmp = span.GetText();
68            var matches = m_regex.Matches(tmp);
69            foreach (Match m in matches)
70            {
71                Span newspan = new Span(span.Start.Position + m.Index, m.Length);
72                SnapshotSpan newsnapshot = new SnapshotSpan(span.Snapshot, newspan);
73                ret.Add(new ClassificationSpan(newsnapshot, m_customclass_type));
74            }
75
76            return ret;
77        }
78
79        public event EventHandler<ClassificationChangedEventArgs> ClassificationChanged;
80    }
81
82    internal class LolGenericFormat : ClassificationFormatDefinition
83    {
84        static IClassificationTypeRegistryService m_type_registry;
85        static IClassificationFormatMapService m_format_map;
86
87        public static void SetRegistry(IClassificationTypeRegistryService type_registry,
88                                       IClassificationFormatMapService format_map)
89        {
90            m_type_registry = type_registry;
91            m_format_map = format_map;
92        }
93
94        protected void CopyStyleColor(string category)
95        {
96            if (m_type_registry == null || m_format_map == null)
97                return;
98
99            var map = m_format_map.GetClassificationFormatMap("Text Editor");
100            if (map == null)
101                return;
102
103            //string[] foo = { "Comment", "Keyword", "C/C++ User Keywords", "Call Return", "HTML Comment" , "User Types", "User Types (Type parameters)", "User Types (Value types)"};
104
105            var type = m_type_registry.GetClassificationType(category);
106            if (type == null)
107                return;
108
109            var prop = map.GetExplicitTextProperties(type);
110            if (prop == null)
111                return;
112
113            var c1 = prop.ForegroundBrush as SolidColorBrush;
114            if (c1 != null && c1.Color != Colors.Transparent)
115                this.ForegroundColor = c1.Color;
116            var c2 = prop.BackgroundBrush as SolidColorBrush;
117            if (c2 != null && c2.Color != Colors.Transparent)
118                this.BackgroundColor = c1.Color;
119        }
120    }
121
122    internal static class LolClassifierClassificationDefinition
123    {
124        [Export(typeof(ClassificationTypeDefinition))]
125        [Name(LolCppTypeFormat.m_name)]
126        internal static ClassificationTypeDefinition LolCustomClassType = null;
127    }
128
129    [Export(typeof(EditorFormatDefinition))]
130    [ClassificationType(ClassificationTypeNames = LolCppTypeFormat.m_name)]
131    [Name(LolCppTypeFormat.m_name)]
132    [UserVisible(true)]
133    [Order(After = Priority.Default)] /* Override the Visual Studio classifiers */
134    internal sealed class LolCppTypeFormat : LolGenericFormat
135    {
136        public const string m_name = "LolCustomClass";
137        public LolCppTypeFormat()
138        {
139            this.DisplayName = "C/C++ Types and Qualifiers";
140            this.ForegroundColor = Colors.Lime;
141            this.IsBold = true;
142            //CopyStyleColor("User Types");
143        }
144    }
145}
Note: See TracBrowser for help on using the repository browser.