Skip to content

Commit

Permalink
Refactorings
Browse files Browse the repository at this point in the history
[release]
  • Loading branch information
madskristensen committed Jan 9, 2022
1 parent d756186 commit 6352a60
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 51 deletions.
6 changes: 3 additions & 3 deletions src/Language/DropdownBars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ private void SynchronizeDropdowns()
return;
}

ThreadHelper.JoinableTaskFactory.StartOnIdle(() =>
_ = ThreadHelper.JoinableTaskFactory.StartOnIdle(() =>
{
if (!_document.IsParsing)
{
_languageService.SynchronizeDropdowns();
}
}, VsTaskRunContext.UIThreadBackgroundPriority).FireAndForget();
}, VsTaskRunContext.UIThreadIdlePriority);
}

public override bool OnSynchronizeDropdowns(LanguageService languageService, IVsTextView textView, int line, int col, ArrayList dropDownTypes, ArrayList dropDownMembers, ref int selectedType, ref int selectedMember)
Expand All @@ -71,7 +71,7 @@ public override bool OnSynchronizeDropdowns(LanguageService languageService, IVs
if (dropDownTypes.Count == 0)
{
string thisExt = $"{Vsix.Name} ({Vsix.Version})";
string markdig = Path.GetFileName($"Powered by Markdig ({Markdig.Markdown.Version})");
string markdig = Path.GetFileName($" Powered by Markdig ({Markdig.Markdown.Version})");
dropDownTypes.Add(new DropDownMember(thisExt, new TextSpan(), 126, DROPDOWNFONTATTR.FONTATTR_GRAY));
dropDownTypes.Add(new DropDownMember(markdig, new TextSpan(), 126, DROPDOWNFONTATTR.FONTATTR_GRAY));
}
Expand Down
4 changes: 1 addition & 3 deletions src/Language/MarkdownEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ internal sealed class MarkdownEditor : LanguageBase
private DropdownBars _dropdownBars;

public MarkdownEditor(object site) : base(site)
{
ThreadHelper.ThrowIfNotOnUIThread();
}
{ }

public override string Name => Constants.LanguageName;

Expand Down
59 changes: 21 additions & 38 deletions src/Language/TokenTagger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Adornments;
using Microsoft.VisualStudio.Text.Tagging;
using Microsoft.VisualStudio.Threading;
using Microsoft.VisualStudio.Utilities;

namespace MarkdownEditor2022
Expand All @@ -24,71 +23,57 @@ public ITagger<T> CreateTagger<T>(ITextBuffer buffer) where T : ITag =>
buffer.Properties.GetOrCreateSingletonProperty(() => new TokenTagger(buffer)) as ITagger<T>;
}

internal class TokenTagger : ITagger<TokenTag>, IDisposable
internal class TokenTagger : TokenTaggerBase, IDisposable
{
private readonly Document _document;
private readonly ITextBuffer _buffer;
private Dictionary<MarkdownObject, ITagSpan<TokenTag>> _tagsCache;
private bool _isDisposed;
private static readonly ImageId _errorIcon = KnownMonikers.StatusWarning.ToImageId();
private bool _isDisposed;

internal TokenTagger(ITextBuffer buffer)
internal TokenTagger(ITextBuffer buffer) : base(buffer)
{
_buffer = buffer;
_document = buffer.GetDocument();
_document.Parsed += ReParse;
_tagsCache = new Dictionary<MarkdownObject, ITagSpan<TokenTag>>();

ThreadHelper.JoinableTaskFactory.RunAsync(async () =>
{
await TaskScheduler.Default;
ReParse(_document);
}).FireAndForget();
}

public IEnumerable<ITagSpan<TokenTag>> GetTags(NormalizedSnapshotSpanCollection spans)
private void ReParse(Document document)
{
return _tagsCache.Values;
_ = TokenizeAsync();
}

private void ReParse(Document document)
public override Task TokenizeAsync()
{
// Make sure this is running on a background thread.
ThreadHelper.ThrowIfOnUIThread();

Dictionary<MarkdownObject, ITagSpan<TokenTag>> list = new();
List<ITagSpan<TokenTag>> list = new();

foreach (MarkdownObject item in document.Markdown.Descendants())
foreach (MarkdownObject item in _document.Markdown.Descendants())
{
if (document.IsParsing)
if (_document.IsParsing)
{
// Abort and wait for the next parse event to finish
return;
return Task.CompletedTask;
}

AddTagToList(list, item);
}

_tagsCache = list;

SnapshotSpan span = new(_buffer.CurrentSnapshot, 0, _buffer.CurrentSnapshot.Length);
TagsChanged?.Invoke(this, new SnapshotSpanEventArgs(span));
OnTagsUpdated(list);
return Task.CompletedTask;
}

private void AddTagToList(Dictionary<MarkdownObject, ITagSpan<TokenTag>> list, MarkdownObject item)
private void AddTagToList(List<ITagSpan<TokenTag>> list, MarkdownObject item)
{
SnapshotSpan span = new(_buffer.CurrentSnapshot, GetApplicapleSpan(item));
TokenTag tag = new(GetItemType(item), item is FencedCodeBlock)
{
Errors = item.GetErrors(_document.FileName).ToList(),
GetTooltipAsync = GetTooltipAsync,
GetOutliningText = GetOutliningText
};
bool supportsOutlining = item is FencedCodeBlock;
IEnumerable<ErrorListItem> errors = item.GetErrors(_document.FileName);

list.Add(item, new TagSpan<TokenTag>(span, tag));
SnapshotSpan span = new(Buffer.CurrentSnapshot, GetApplicapleSpan(item));
TokenTag tag = CreateToken(GetItemType(item), true, supportsOutlining, errors);

list.Add(new TagSpan<TokenTag>(span, tag));
}

private static string GetOutliningText(string text)
public override string GetOutliningText(string text)
{
string firstLine = text.Split('\n').FirstOrDefault()?.Trim();
string language = "";
Expand All @@ -101,7 +86,7 @@ private static string GetOutliningText(string text)
return $"{language} Code Block ";
}

private Task<object> GetTooltipAsync(SnapshotPoint triggerPoint)
public override Task<object> GetTooltipAsync(SnapshotPoint triggerPoint)
{
LinkInline item = _document.Markdown.Descendants()
.OfType<LinkInline>()
Expand Down Expand Up @@ -166,7 +151,5 @@ public void Dispose()

_isDisposed = true;
}

public event EventHandler<SnapshotSpanEventArgs> TagsChanged;
}
}
4 changes: 3 additions & 1 deletion src/MarkdownEditor2022.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Community.VisualStudio.VSCT" Version="16.0.29.6" PrivateAssets="all" />
<PackageReference Include="Community.VisualStudio.Toolkit.17" Version="17.0.369" ExcludeAssets="Runtime" />
<PackageReference Include="Community.VisualStudio.Toolkit.17" Version="17.0.378" ExcludeAssets="Runtime">
<IncludeAssets>compile; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Markdig" Version="0.26.0" />
<PackageReference Include="Microsoft.VSSDK.BuildTools" Version="17.0.5232" PrivateAssets="all" />
</ItemGroup>
Expand Down
12 changes: 6 additions & 6 deletions src/MarkdownEditor2022Package.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ namespace MarkdownEditor2022
[ProvideLanguageService(typeof(MarkdownEditor), Constants.LanguageName, 0, ShowHotURLs = false, DefaultToNonHotURLs = true, EnableLineNumbers = true, EnableAsyncCompletion = true, EnableCommenting = true, ShowCompletion = true, ShowDropDownOptions = true)]
[ProvideLanguageEditorOptionPage(typeof(OptionsProvider.AdvancedOptions), Constants.LanguageName, "", "Advanced", null, 0)]
[ProvideLanguageExtension(typeof(MarkdownEditor), Constants.FileExtension)]

[ProvideEditorExtension(typeof(MarkdownEditor), Constants.FileExtension, 50)]
[ProvideFileIcon(Constants.FileExtension, "KnownMonikers.RegistrationScript")]
[ProvideEditorFactory(typeof(MarkdownEditor), 0, false, CommonPhysicalViewAttributes = (int)__VSPHYSICALVIEWATTRIBUTES.PVA_SupportsPreview, TrustLevel = __VSEDITORTRUSTLEVEL.ETL_AlwaysTrusted)]
[ProvideEditorLogicalView(typeof(MarkdownEditor), VSConstants.LOGVIEWID.TextView_string, IsTrusted = true)]

[ProvideFileIcon(Constants.FileExtension, "KnownMonikers.RegistrationScript")]
public sealed class MarkdownEditor2022Package : ToolkitPackage
{
internal static MarkdownEditor MarkdownEditor { get; private set; }

protected override Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
MarkdownEditor = new(this);
RegisterEditorFactory(MarkdownEditor);
MarkdownEditor language = new(this);
RegisterEditorFactory(language);

((IServiceContainer)this).AddService(typeof(MarkdownEditor), MarkdownEditor, true);
((IServiceContainer)this).AddService(typeof(MarkdownEditor), language, true);

SetInternetExplorerRegistryKey();

Expand Down
1 change: 1 addition & 0 deletions src/VSCommandTable.vsct
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
<VisibilityItem guid="MarkdownEditor2022" id="ShowKeybindings" context="EditorFactory" />
<VisibilityItem guid="MarkdownEditor2022" id="ShowMarkdownReference" context="EditorFactory" />
<VisibilityItem guid="MarkdownEditor2022" id="OpenSettings" context="EditorFactory" />
<VisibilityItem guid="MarkdownEditor2022" id="InsertLink" context="EditorFactory" />
</VisibilityConstraints>

<KeyBindings>
Expand Down

0 comments on commit 6352a60

Please sign in to comment.