From dc6c98c579e02b0f2faaf9038a554620875f1b64 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Thu, 29 Nov 2012 00:37:32 +0000 Subject: [PATCH] vslol: add a classifier for C, C++, C# and LolFx files. Only classifies using a naive regex for now. --- tools/vslol/CppKeywordClassifier.cs | 115 +++++++++++++++++++ tools/vslol/{ => Properties}/AssemblyInfo.cs | 4 +- tools/vslol/VsLol.cs | 23 +++- tools/vslol/VsLol.csproj | 13 ++- tools/vslol/source.extension.vsixmanifest | 3 +- 5 files changed, 149 insertions(+), 9 deletions(-) create mode 100644 tools/vslol/CppKeywordClassifier.cs rename tools/vslol/{ => Properties}/AssemblyInfo.cs (91%) diff --git a/tools/vslol/CppKeywordClassifier.cs b/tools/vslol/CppKeywordClassifier.cs new file mode 100644 index 00000000..ca98ea8b --- /dev/null +++ b/tools/vslol/CppKeywordClassifier.cs @@ -0,0 +1,115 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.Composition; +using System.Windows.Media; +using System.Text.RegularExpressions; + +using Microsoft.VisualStudio.Text; +using Microsoft.VisualStudio.Text.Classification; +using Microsoft.VisualStudio.Language.StandardClassification; +using Microsoft.VisualStudio.Utilities; + +namespace Lol.VisualStudio.Plugin +{ + [Export(typeof(IClassifierProvider))] + [ContentType("c/c++")] + [ContentType("csharp")] + [ContentType("lolfx")] + internal class LolClassifierProvider : IClassifierProvider + { + [Import] + internal IClassificationTypeRegistryService m_class_registry = null; /* Set via MEF */ + + //[Import] + //internal IClassificationFormatMapService m_lol = null; + + public IClassifier GetClassifier(ITextBuffer buffer) + { + //var test = m_lol.GetClassificationFormatMap("Text Editor"); + //string[] foo = { "Comment", "Keyword", "C/C++ User Keywords", "Call Return", "HTML Comment" }; + //foreach (var s in foo) + //{ + // var type = m_class_registry.GetClassificationType(s); + // if (type == null) + // continue; + // var prop = test.GetExplicitTextProperties(type); + // if (prop == null) + // continue; + // var c1 = prop.ForegroundBrush as SolidColorBrush; + // var c2 = prop.BackgroundBrush as SolidColorBrush; + // Logger.Info("[" + s + "]: " + c1.ToString() + " " + c2.ToString() + "\n"); + //} + + return buffer.Properties.GetOrCreateSingletonProperty(delegate { return new CppKeywordClassifier(m_class_registry, buffer.ContentType); }); + } + } + + class CppKeywordClassifier : IClassifier + { + private IClassificationType m_customclass_type; + private Regex m_regex; + + internal CppKeywordClassifier(IClassificationTypeRegistryService registry, + IContentType type) + { + m_customclass_type = registry.GetClassificationType("LolCustomClass"); + + string tmp = @"\b("; + tmp += "void|int|unsigned|char|short|long|float|double|"; + tmp += "class|struct|template|const|static|volatile|inline|namespace|"; + if (type.IsOfType("lolfx")) + tmp += "attribute|varying|uniform|in|out|"; + if (type.IsOfType("csharp")) + tmp += "var|string|internal|sealed|public|private|"; + if (!type.IsOfType("csharp")) + tmp += "vec2|vec3|vec4|quat|mat2|mat3|mat4|"; + if (type.IsOfType("c/c++")) + tmp += "real|half|"; + tmp += @")\b"; + m_regex = new Regex(tmp); + } + + public IList GetClassificationSpans(SnapshotSpan span) + { + List ret = new List(); + + string tmp = span.GetText(); + var matches = m_regex.Matches(tmp); + foreach (Match m in matches) + { + Span newspan = new Span(span.Start.Position + m.Index, m.Length); + SnapshotSpan newsnapshot = new SnapshotSpan(span.Snapshot, newspan); + ret.Add(new ClassificationSpan(newsnapshot, m_customclass_type)); + } + + return ret; + } + + public event EventHandler ClassificationChanged; + } + + internal static class LolClassifierClassificationDefinition + { + [Export(typeof(ClassificationTypeDefinition))] + [Name(LolCppTypeFormat.m_name)] + internal static ClassificationTypeDefinition LolCustomClassType = null; + } + + [Export(typeof(EditorFormatDefinition))] + [ClassificationType(ClassificationTypeNames = LolCppTypeFormat.m_name)] + [Name(LolCppTypeFormat.m_name)] + [UserVisible(true)] + [Order(After = Priority.Default)] /* Override the Visual Studio classifiers */ + internal sealed class LolCppTypeFormat : ClassificationFormatDefinition + { + public const string m_name = "LolCustomClass"; + public LolCppTypeFormat() + { + this.DisplayName = "C/C++ Types and Qualifiers"; + //this.BackgroundColor = Colors.BlueViolet; + this.ForegroundColor = Colors.Lime; + this.IsBold = true; + //this.TextDecorations = System.Windows.TextDecorations.Underline; + } + } +} diff --git a/tools/vslol/AssemblyInfo.cs b/tools/vslol/Properties/AssemblyInfo.cs similarity index 91% rename from tools/vslol/AssemblyInfo.cs rename to tools/vslol/Properties/AssemblyInfo.cs index fdf5a7aa..b299e488 100644 --- a/tools/vslol/AssemblyInfo.cs +++ b/tools/vslol/Properties/AssemblyInfo.cs @@ -20,5 +20,5 @@ using System.Runtime.InteropServices; // The following GUID is for the ID of the typelib if this project is exposed to COM [assembly: Guid("58968f91-edb8-4a4c-9f4f-ba39fdb4a21a")] -[assembly: AssemblyVersion("1.0.0.3")] -[assembly: AssemblyFileVersion("1.0.0.3")] +[assembly: AssemblyVersion("1.0.0.4")] +[assembly: AssemblyFileVersion("1.0.0.4")] diff --git a/tools/vslol/VsLol.cs b/tools/vslol/VsLol.cs index c279323c..363f3a7e 100644 --- a/tools/vslol/VsLol.cs +++ b/tools/vslol/VsLol.cs @@ -92,29 +92,42 @@ namespace Lol.VisualStudio.Plugin if (Microsoft.VisualStudio.ErrorHandler.Failed(m_window.GetPane(ref guidBuild, out m_pane))) m_pane = null; } + + if (m_pane != null) + m_pane.Activate(); } public static void Clear() { OpenBuildPane(); - if (m_pane != null) + if (m_pane == null) { - m_pane.Activate(); - m_pane.Clear(); + m_backlog = ""; + return; } + + m_pane.Clear(); } public static void Info(string s) { OpenBuildPane(); - if (m_pane != null) - m_pane.OutputString(s); + if (m_pane == null) + { + m_backlog += s; + return; + } + + m_pane.OutputString(m_backlog); + m_backlog = ""; + m_pane.OutputString(s); } private static IVsOutputWindow m_window = null; private static IVsOutputWindowPane m_pane = null; + private static string m_backlog = ""; } internal static class GuidsList diff --git a/tools/vslol/VsLol.csproj b/tools/vslol/VsLol.csproj index 4bf810f6..9847c915 100644 --- a/tools/vslol/VsLol.csproj +++ b/tools/vslol/VsLol.csproj @@ -54,27 +54,38 @@ ..\..\..\..\Program Files (x86)\Common Files\Microsoft Shared\MSEnv\PublicAssemblies\envdte.dll + + + + + + + false + + + + @@ -84,7 +95,7 @@ VsLol.resx - + diff --git a/tools/vslol/source.extension.vsixmanifest b/tools/vslol/source.extension.vsixmanifest index 8a9676c5..9e23d47b 100644 --- a/tools/vslol/source.extension.vsixmanifest +++ b/tools/vslol/source.extension.vsixmanifest @@ -3,7 +3,7 @@ VsLol Lol - 1.0.0.3 + 1.0.0.4 Lol Engine Productivity Tools. 1033 http://lol.zoy.org/ @@ -21,5 +21,6 @@ |%CurrentProject%;PkgdefProjectOutputGroup| + |%CurrentProject%|