From 0eedbfd9f91de2fe172ea26888c4ff937a0be96b Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Fri, 30 Nov 2012 00:51:53 +0000 Subject: [PATCH] vslol: only apply our syntax highlighting to keywords and identifiers and update the version of the extension for publishing. --- tools/vslol/CppKeywordClassifier.cs | 46 ++++++++++++++++++----- tools/vslol/Properties/AssemblyInfo.cs | 4 +- tools/vslol/source.extension.vsixmanifest | 2 +- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/tools/vslol/CppKeywordClassifier.cs b/tools/vslol/CppKeywordClassifier.cs index ef86b9d0..87b5c2c4 100644 --- a/tools/vslol/CppKeywordClassifier.cs +++ b/tools/vslol/CppKeywordClassifier.cs @@ -20,26 +20,43 @@ namespace Lol.VisualStudio.Plugin { [Import] internal IClassificationTypeRegistryService m_type_registry = null; /* Set via MEF */ - + [Import] + internal IClassifierAggregatorService m_aggregator = null; [Import] internal IClassificationFormatMapService m_format_map = null; + internal static bool m_inprogress = false; + public IClassifier GetClassifier(ITextBuffer buffer) { + /* Avoid infinite recursion */ + if (m_inprogress) + return null; + LolGenericFormat.SetRegistry(m_type_registry, m_format_map); - return buffer.Properties.GetOrCreateSingletonProperty(delegate { return new CppKeywordClassifier(m_type_registry, buffer.ContentType); }); + try + { + m_inprogress = true; + return buffer.Properties.GetOrCreateSingletonProperty(delegate { return new CppKeywordClassifier(m_type_registry, m_aggregator.GetClassifier(buffer), buffer.ContentType); }); + } + finally { m_inprogress = false; } } } class CppKeywordClassifier : IClassifier { + private IClassifier m_classifier; + private IClassificationType m_customclass_type; - private Regex m_regex; + private Regex m_customclass_regex; internal CppKeywordClassifier(IClassificationTypeRegistryService registry, + IClassifier classifier, IContentType type) { + m_classifier = classifier; + m_customclass_type = registry.GetClassificationType("LolCustomClass"); string tmp = @"\b("; @@ -56,21 +73,30 @@ namespace Lol.VisualStudio.Plugin tmp += "u?int(8|16|32|64|ptr)_t|"; tmp += "real|half|explicit|typename|typedef|"; } + tmp = tmp.Remove(tmp.Length - 1); tmp += @")\b"; - m_regex = new Regex(tmp); + m_customclass_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) + foreach (ClassificationSpan cs in m_classifier.GetClassificationSpans(span)) { - 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)); + string cs_class = cs.ClassificationType.Classification.ToLower(); + + /* Only apply our rules if we found a keyword or an identifier */ + if (cs_class == "keyword" || cs_class == "identifier") + { + if (m_customclass_regex.IsMatch(cs.Span.GetText())) + { + ret.Add(new ClassificationSpan(cs.Span, m_customclass_type)); + continue; + } + } + + ret.Add(cs); } return ret; diff --git a/tools/vslol/Properties/AssemblyInfo.cs b/tools/vslol/Properties/AssemblyInfo.cs index b299e488..b361d2d4 100644 --- a/tools/vslol/Properties/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.4")] -[assembly: AssemblyFileVersion("1.0.0.4")] +[assembly: AssemblyVersion("1.0.0.5")] +[assembly: AssemblyFileVersion("1.0.0.5")] diff --git a/tools/vslol/source.extension.vsixmanifest b/tools/vslol/source.extension.vsixmanifest index 27f8b5cf..9f78f037 100644 --- a/tools/vslol/source.extension.vsixmanifest +++ b/tools/vslol/source.extension.vsixmanifest @@ -3,7 +3,7 @@ VsLol Lol - 1.0.0.4 + 1.0.0.5 Lol Engine Productivity Tools. 1033 http://lol.zoy.org/