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

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

vslol: add a classifier for C, C++, C# and LolFx files. Only classifies
using a naive regex for now.

File size: 4.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.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|int|unsigned|char|short|long|float|double|";
59            tmp += "class|struct|template|const|static|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 += "vec2|vec3|vec4|quat|mat2|mat3|mat4|";
66            if (type.IsOfType("c/c++"))
67                tmp += "real|half|";
68            tmp += @")\b";
69            m_regex = new Regex(tmp);
70        }
71
72        public IList<ClassificationSpan> GetClassificationSpans(SnapshotSpan span)
73        {
74            List<ClassificationSpan> ret = new List<ClassificationSpan>();
75
76            string tmp = span.GetText();
77            var matches = m_regex.Matches(tmp);
78            foreach (Match m in matches)
79            {
80                Span newspan = new Span(span.Start.Position + m.Index, m.Length);
81                SnapshotSpan newsnapshot = new SnapshotSpan(span.Snapshot, newspan);
82                ret.Add(new ClassificationSpan(newsnapshot, m_customclass_type));
83            }
84
85            return ret;
86        }
87
88        public event EventHandler<ClassificationChangedEventArgs> ClassificationChanged;
89    }
90
91    internal static class LolClassifierClassificationDefinition
92    {
93        [Export(typeof(ClassificationTypeDefinition))]
94        [Name(LolCppTypeFormat.m_name)]
95        internal static ClassificationTypeDefinition LolCustomClassType = null;
96    }
97
98    [Export(typeof(EditorFormatDefinition))]
99    [ClassificationType(ClassificationTypeNames = LolCppTypeFormat.m_name)]
100    [Name(LolCppTypeFormat.m_name)]
101    [UserVisible(true)]
102    [Order(After = Priority.Default)] /* Override the Visual Studio classifiers */
103    internal sealed class LolCppTypeFormat : ClassificationFormatDefinition
104    {
105        public const string m_name = "LolCustomClass";
106        public LolCppTypeFormat()
107        {
108            this.DisplayName = "C/C++ Types and Qualifiers";
109            //this.BackgroundColor = Colors.BlueViolet;
110            this.ForegroundColor = Colors.Lime;
111            this.IsBold = true;
112            //this.TextDecorations = System.Windows.TextDecorations.Underline;
113        }
114    }
115}
Note: See TracBrowser for help on using the repository browser.