Skip to content

Commit

Permalink
Relying on TagAggregator's TagChanged event to update YellowNotepadAd…
Browse files Browse the repository at this point in the history
…ornment
  • Loading branch information
Michael Shpilt committed Jan 17, 2018
1 parent d9ae126 commit bd06c04
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 23 deletions.
2 changes: 1 addition & 1 deletion CodyDocs.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26730.15
VisualStudioVersion = 15.0.27130.2010
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodyDocs", "CodyDocs\CodyDocs.csproj", "{31008CE0-2AFF-4980-988C-4478DB4270C9}"
EndProject
Expand Down
1 change: 1 addition & 0 deletions CodyDocs/CodyDocs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
</Compile>
<Compile Include="Models\FileDocumentation.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utils\SpanExtensions.cs" />
<Compile Include="Utils\Consts.cs" />
<Compile Include="Services\DocumentationFileHandler.cs" />
<Compile Include="Services\DocumentationFileSerializer.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,34 @@ internal static ITagger<IntraTextAdornmentTag> GetTagger(IWpfTextView view, IEve
Lazy<ITagAggregator<DocumentedCodeHighlighterTag>> highlightTagger)
{
return view.Properties.GetOrCreateSingletonProperty<EditDocumentationAdornmentTagger>(
() => new EditDocumentationAdornmentTagger(view, eventAggregator, highlightTagger.Value));
() => new EditDocumentationAdornmentTagger(view, highlightTagger.Value));
}


private ITagAggregator<DocumentedCodeHighlighterTag> _highlightTagger;
private DelegateListener<DocumentationAddedEvent> _documentationAddedListener;
private ITagAggregator<DocumentedCodeHighlighterTag> _tagAggregator;
private ITextBuffer _buffer;
private string _codyDocsFilename;

private EditDocumentationAdornmentTagger(IWpfTextView view, IEventAggregator eventAggregator, ITagAggregator<DocumentedCodeHighlighterTag> highlightTagger)
private EditDocumentationAdornmentTagger(IWpfTextView view, ITagAggregator<DocumentedCodeHighlighterTag> tagAggregator)
: base(view)
{
this._highlightTagger = highlightTagger;
_documentationAddedListener = new DelegateListener<DocumentationAddedEvent>(OnDocumentationAdded);
this._tagAggregator = tagAggregator;
_tagAggregator.TagsChanged += OnTagsChanged;
_buffer = view.TextBuffer;
_codyDocsFilename = view.TextBuffer.GetCodyDocsFileName();
eventAggregator.AddListener<DocumentationAddedEvent>(_documentationAddedListener);

}

private void OnDocumentationAdded(DocumentationAddedEvent e)
private void OnTagsChanged(object sender, TagsChangedEventArgs e)
{
string filepath = e.Filepath;
if (filepath == _codyDocsFilename)
{
var span = e.DocumentationFragment.GetSpan();
InvokeTagsChanged(this, new SnapshotSpanEventArgs(
new SnapshotSpan(_buffer.CurrentSnapshot, span)));
}
}
var snapshotSpan = e.Span.GetSnapshotSpan();//Extension method
InvokeTagsChanged(sender, new SnapshotSpanEventArgs(snapshotSpan));

}

public void Dispose()
{
_highlightTagger.Dispose();
_tagAggregator.Dispose();

view.Properties.RemoveProperty(typeof(EditDocumentationAdornmentTagger));
}
Expand All @@ -64,20 +59,22 @@ public void Dispose()

ITextSnapshot snapshot = spans[0].Snapshot;

var colorTags = _highlightTagger.GetTags(spans);
var commentTags = _tagAggregator.GetTags(spans);

foreach (IMappingTagSpan<DocumentedCodeHighlighterTag> dataTagSpan in colorTags)
foreach (IMappingTagSpan<DocumentedCodeHighlighterTag> commentTag in commentTags)
{
NormalizedSnapshotSpanCollection colorTagSpans = dataTagSpan.Span.GetSpans(snapshot);
NormalizedSnapshotSpanCollection colorTagSpans = commentTag.Span.GetSpans(snapshot);

// Ignore data tags that are split by projection.
// This is theoretically possible but unlikely in current scenarios.
if (colorTagSpans.Count != 1)
continue;
if (commentTag.Span.GetSpan().Length == 0)
continue;

SnapshotSpan adornmentSpan = new SnapshotSpan(colorTagSpans[0].End, 0);

yield return Tuple.Create(adornmentSpan, (PositionAffinity?)PositionAffinity.Successor, dataTagSpan.Tag);
yield return Tuple.Create(adornmentSpan, (PositionAffinity?)PositionAffinity.Successor, commentTag.Tag);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
xmlns:local="clr-namespace:CodyDocs.EditorUI.DocumentedCodeEditIntraTextAdornment"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Image Source="../../Resources/yellow_notepad.png" Margin="0 3"/>
<Image Source="../../Resources/yellow_notepad.png" Margin="3 0 1 0"/>


</UserControl>
36 changes: 36 additions & 0 deletions CodyDocs/Utils/SpanExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using CodyDocs.Services;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Tagging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodyDocs.Utils
{
internal static class SpanExtensions
{
private const PositionAffinity positionAffinity = PositionAffinity.Successor;

public static Span GetSpan(this IMappingSpan mappingTagSpan)
{
var buffer = mappingTagSpan.AnchorBuffer;
var startSnapshotPoint = mappingTagSpan.Start.GetPoint(buffer, positionAffinity).Value;
var endSnapshotPoint = mappingTagSpan.End.GetPoint(buffer, positionAffinity).Value;
var length = endSnapshotPoint.Position - startSnapshotPoint.Position;
return new Span(startSnapshotPoint.Position, length);
}

public static SnapshotSpan GetSnapshotSpan(this IMappingSpan mappingTagSpan)
{
var buffer = mappingTagSpan.AnchorBuffer;
var span = GetSpan(mappingTagSpan);
var snapshot = mappingTagSpan.Start.GetPoint(buffer, positionAffinity).Value.Snapshot;
return new SnapshotSpan(snapshot, span);
}



}
}

0 comments on commit bd06c04

Please sign in to comment.