Skip to content

Commit

Permalink
- Fixed a crash caused by NaviBar when syntax highlighting and scroll…
Browse files Browse the repository at this point in the history
…bar marker were both disabled (#145)
  • Loading branch information
wmjordan committed Feb 4, 2021
1 parent 47acf3f commit effd2df
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 26 deletions.
1 change: 1 addition & 0 deletions Codist/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public static class EditorFormatKeys

public const string CodeKeyword = "Keyword";
public const string CodeComment = "Comment";
public const string CodeText = "text";

public const string CSharpAbstractionKeyword = "C#: Abstraction keyword";
public const string CSharpBranchingKeyword = "C#: Branching keyword";
Expand Down
34 changes: 20 additions & 14 deletions Codist/NaviBar/MarkdownBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@ namespace Codist.NaviBar
public sealed class MarkdownBar : NaviBar
{
const string DefaultActiveTitle = "Headings";
static readonly IClassificationType
_H1 = ServicesHelper.Instance.ClassificationTypeRegistry.GetClassificationType(Constants.MarkdownHeading1),
_H2 = ServicesHelper.Instance.ClassificationTypeRegistry.GetClassificationType(Constants.MarkdownHeading2),
_H3 = ServicesHelper.Instance.ClassificationTypeRegistry.GetClassificationType(Constants.MarkdownHeading3),
_H4 = ServicesHelper.Instance.ClassificationTypeRegistry.GetClassificationType(Constants.MarkdownHeading4),
_H5 = ServicesHelper.Instance.ClassificationTypeRegistry.GetClassificationType(Constants.MarkdownHeading5),
_H6 = ServicesHelper.Instance.ClassificationTypeRegistry.GetClassificationType(Constants.MarkdownHeading6);
static readonly Microsoft.VisualStudio.Text.Tagging.ClassificationTag
_H1 = MarkdownTaggerProvider.HeaderClassificationTypes[1],
_H2 = MarkdownTaggerProvider.HeaderClassificationTypes[2],
_H3 = MarkdownTaggerProvider.HeaderClassificationTypes[3],
_H4 = MarkdownTaggerProvider.HeaderClassificationTypes[4],
_H5 = MarkdownTaggerProvider.HeaderClassificationTypes[5],
_H6 = MarkdownTaggerProvider.HeaderClassificationTypes[6],
_DummyTag1 = MarkdownTaggerProvider.DummyHeaderTags[1],
_DummyTag2 = MarkdownTaggerProvider.DummyHeaderTags[2],
_DummyTag3 = MarkdownTaggerProvider.DummyHeaderTags[3],
_DummyTag4 = MarkdownTaggerProvider.DummyHeaderTags[4],
_DummyTag5 = MarkdownTaggerProvider.DummyHeaderTags[5],
_DummyTag6 = MarkdownTaggerProvider.DummyHeaderTags[6];
readonly ITextSearchService2 _TextSearch;
readonly TaggerResult _Tags;
readonly ThemedToolBarText _ActiveTitleLabel;
Expand Down Expand Up @@ -273,27 +279,27 @@ static Thickness
public LocationItem(TaggedContentSpan span) {
_Span = span;
Content = new ThemedMenuText(span.ContentText);
var t = span.Tag.ClassificationType;
if (t == _H1) {
var t = span.Tag;
if (t == _H1 || t == _DummyTag1) {
Content.FontWeight = FontWeights.Bold;
_ImageId = IconIds.Heading1;
}
else if (t == _H2) {
else if (t == _H2 || t == _DummyTag2) {
_ImageId = IconIds.Heading2;
}
else if (t == _H3) {
else if (t == _H3 || t == _DummyTag3) {
_ImageId = IconIds.Heading3;
Content.Padding = _H3Padding;
}
else if (t == _H4) {
else if (t == _H4 || t == _DummyTag4) {
_ImageId = IconIds.Heading4;
Content.Padding = _H4Padding;
}
else if (t == _H5) {
else if (t == _H5 || t == _DummyTag5) {
_ImageId = IconIds.Heading5;
Content.Padding = _H5Padding;
}
else if (t == _H6) {
else if (t == _H6 || t == _DummyTag6) {
_ImageId = IconIds.None;
Content.Padding = _H6Padding;
}
Expand Down
35 changes: 23 additions & 12 deletions Codist/Taggers/MarkdownTagger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,45 @@ namespace Codist.Taggers
[TagType(typeof(IClassificationTag))]
sealed class MarkdownTaggerProvider : IViewTaggerProvider
{
static readonly ClassificationTag[] _HeaderClassificationTypes = new ClassificationTag[7];
internal static readonly ClassificationTag[] HeaderClassificationTypes = new ClassificationTag[7];
internal static readonly ClassificationTag[] DummyHeaderTags = new ClassificationTag[7]; // used when syntax highlight is disabled

public ITagger<T> CreateTagger<T>(ITextView textView, ITextBuffer buffer) where T : ITag {
if (Config.Instance.Features.MatchFlags(Features.SyntaxHighlight) == false) {
// the results produced by the tagger are also reused by the NaviBar
if (Config.Instance.Features.HasAnyFlag(Features.SyntaxHighlight | Features.NaviBar) == false) {
return null;
}
if (textView.TextBuffer.LikeContentType(Constants.CodeTypes.Markdown) == false) {
return null;
}
if (_HeaderClassificationTypes[1] == null) {
if (HeaderClassificationTypes[1] == null) {
InitHeaderClassificationTypes();
}
return textView.Properties.GetOrCreateSingletonProperty(() => new MarkdownTagger(textView)) as ITagger<T>;
return textView.Properties.GetOrCreateSingletonProperty(() => new MarkdownTagger(textView, Config.Instance.Features.MatchFlags(Features.SyntaxHighlight))) as ITagger<T>;
}

static void InitHeaderClassificationTypes() {
var r = ServicesHelper.Instance.ClassificationTypeRegistry;
_HeaderClassificationTypes[1] = new ClassificationTag(r.GetClassificationType(Constants.MarkdownHeading1));
_HeaderClassificationTypes[2] = new ClassificationTag(r.GetClassificationType(Constants.MarkdownHeading2));
_HeaderClassificationTypes[3] = new ClassificationTag(r.GetClassificationType(Constants.MarkdownHeading3));
_HeaderClassificationTypes[4] = new ClassificationTag(r.GetClassificationType(Constants.MarkdownHeading4));
_HeaderClassificationTypes[5] = new ClassificationTag(r.GetClassificationType(Constants.MarkdownHeading5));
_HeaderClassificationTypes[6] = new ClassificationTag(r.GetClassificationType(Constants.MarkdownHeading6));
HeaderClassificationTypes[1] = new ClassificationTag(r.GetClassificationType(Constants.MarkdownHeading1));
HeaderClassificationTypes[2] = new ClassificationTag(r.GetClassificationType(Constants.MarkdownHeading2));
HeaderClassificationTypes[3] = new ClassificationTag(r.GetClassificationType(Constants.MarkdownHeading3));
HeaderClassificationTypes[4] = new ClassificationTag(r.GetClassificationType(Constants.MarkdownHeading4));
HeaderClassificationTypes[5] = new ClassificationTag(r.GetClassificationType(Constants.MarkdownHeading5));
HeaderClassificationTypes[6] = new ClassificationTag(r.GetClassificationType(Constants.MarkdownHeading6));
var dummyTag = r.GetClassificationType(Constants.CodeText);
DummyHeaderTags[1] = new ClassificationTag(dummyTag);
DummyHeaderTags[2] = new ClassificationTag(dummyTag);
DummyHeaderTags[3] = new ClassificationTag(dummyTag);
DummyHeaderTags[4] = new ClassificationTag(dummyTag);
DummyHeaderTags[5] = new ClassificationTag(dummyTag);
DummyHeaderTags[6] = new ClassificationTag(dummyTag);
}

sealed class MarkdownTagger : CachedTaggerBase
{
public MarkdownTagger(ITextView textView) : base(textView) {
readonly ClassificationTag[] _Tags;
public MarkdownTagger(ITextView textView, bool syntaxHighlightEnabled) : base(textView) {
_Tags = syntaxHighlightEnabled ? HeaderClassificationTypes : DummyHeaderTags;
}
protected override bool DoFullParseAtFirstLoad => true;
protected override void Parse(SnapshotSpan span, ICollection<TaggedContentSpan> results) {
Expand All @@ -62,7 +73,7 @@ protected override void Parse(SnapshotSpan span, ICollection<TaggedContentSpan>
break;
}
w += c;
results.Add(new TaggedContentSpan(_HeaderClassificationTypes[c], span, w, t.Length - w));
results.Add(new TaggedContentSpan(_Tags[c], span, w, t.Length - w));
}
}
}
Expand Down

0 comments on commit effd2df

Please sign in to comment.