public
Last active

  • Download Gist
Classifier.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Windows.Media;
using Microsoft.VisualStudio.ApplicationModel.Environments;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Classification;
using Microsoft.VisualStudio.Utilities;
 
namespace ClassifyAllBut
{
public static class TypeAndFormatExports
{
[Export(typeof(ClassificationTypeDefinition))]
[Name("strike-out")]
internal static ClassificationTypeDefinition StrikeOutType;
 
[Export(typeof(EditorFormatDefinition))]
[ClassificationType(ClassificationTypeNames = "strike-out")]
[Name("Strike-out")]
[DisplayName("Strike-out Text")]
[UserVisible(true)]
[Order(Before = Priority.High)]
internal sealed class StrikeOutFormat : ClassificationFormatDefinition
{
public StrikeOutFormat()
{
this.TextDecorations = System.Windows.TextDecorations.Strikethrough;
}
}
}
 
[Export(typeof(IClassifierProvider))]
[ContentType("CSharp")]
internal sealed class GrayedOutClassifierProvider : IClassifierProvider
{
[Import]
internal IClassificationTypeRegistryService TypeRegistry;
[Import]
internal IClassifierAggregatorService AggregatorService;
 
internal static bool ignoreRequest = false;
 
public IClassifier GetClassifier(ITextBuffer textBuffer, IEnvironment context)
{
if (ignoreRequest)
return null;
 
try
{
ignoreRequest = true;
return textBuffer.Properties.GetOrCreateSingletonProperty(
() => new GrayedOutClassifier(AggregatorService.GetClassifier(textBuffer, context), TypeRegistry));
}
finally
{
ignoreRequest = false;
}
}
}
 
internal sealed class GrayedOutClassifier : IClassifier
{
private IClassifier aggregator;
private IClassificationTypeRegistryService typeRegistry;
 
public GrayedOutClassifier(IClassifier aggregator, IClassificationTypeRegistryService typeRegistry)
{
this.aggregator = aggregator;
this.typeRegistry = typeRegistry;
}
 
public event EventHandler<ClassificationChangedEventArgs> ClassificationChanged;
 
public IList<ClassificationSpan> GetClassificationSpans(SnapshotSpan span)
{
var classifiedSpans = from cspan in aggregator.GetClassificationSpans(span)
let name = cspan.ClassificationType.Classification.ToLower()
where name.Contains("string") || name.Contains("comment") || name.Contains("xml doc")
select cspan.Span;
 
NormalizedSnapshotSpanCollection ignored = new NormalizedSnapshotSpanCollection(classifiedSpans);
NormalizedSnapshotSpanCollection request = new NormalizedSnapshotSpanCollection(span);
 
var spansToStrikeOut = NormalizedSnapshotSpanCollection.Difference(request, ignored);
 
IClassificationType type = typeRegistry.GetClassificationType("strike-out");
 
return spansToStrikeOut.Select(s => new ClassificationSpan(s, type))
.ToList();
}
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.