From 6352a6015edf1d9833742048223c08c292556ba6 Mon Sep 17 00:00:00 2001 From: Mads Kristensen Date: Sun, 9 Jan 2022 08:04:45 -0800 Subject: [PATCH] Refactorings [release] --- src/Language/DropdownBars.cs | 6 ++-- src/Language/MarkdownEditor.cs | 4 +-- src/Language/TokenTagger.cs | 59 ++++++++++++-------------------- src/MarkdownEditor2022.csproj | 4 ++- src/MarkdownEditor2022Package.cs | 12 +++---- src/VSCommandTable.vsct | 1 + 6 files changed, 35 insertions(+), 51 deletions(-) diff --git a/src/Language/DropdownBars.cs b/src/Language/DropdownBars.cs index 53113aa..49f8f99 100644 --- a/src/Language/DropdownBars.cs +++ b/src/Language/DropdownBars.cs @@ -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) @@ -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)); } diff --git a/src/Language/MarkdownEditor.cs b/src/Language/MarkdownEditor.cs index a73c463..e7a806a 100644 --- a/src/Language/MarkdownEditor.cs +++ b/src/Language/MarkdownEditor.cs @@ -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; diff --git a/src/Language/TokenTagger.cs b/src/Language/TokenTagger.cs index cb82761..04f3140 100644 --- a/src/Language/TokenTagger.cs +++ b/src/Language/TokenTagger.cs @@ -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 @@ -24,71 +23,57 @@ public ITagger CreateTagger(ITextBuffer buffer) where T : ITag => buffer.Properties.GetOrCreateSingletonProperty(() => new TokenTagger(buffer)) as ITagger; } - internal class TokenTagger : ITagger, IDisposable + internal class TokenTagger : TokenTaggerBase, IDisposable { private readonly Document _document; - private readonly ITextBuffer _buffer; - private Dictionary> _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>(); - - ThreadHelper.JoinableTaskFactory.RunAsync(async () => - { - await TaskScheduler.Default; - ReParse(_document); - }).FireAndForget(); } - public IEnumerable> 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> list = new(); + List> 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> list, MarkdownObject item) + private void AddTagToList(List> 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 errors = item.GetErrors(_document.FileName); - list.Add(item, new TagSpan(span, tag)); + SnapshotSpan span = new(Buffer.CurrentSnapshot, GetApplicapleSpan(item)); + TokenTag tag = CreateToken(GetItemType(item), true, supportsOutlining, errors); + + list.Add(new TagSpan(span, tag)); } - private static string GetOutliningText(string text) + public override string GetOutliningText(string text) { string firstLine = text.Split('\n').FirstOrDefault()?.Trim(); string language = ""; @@ -101,7 +86,7 @@ private static string GetOutliningText(string text) return $"{language} Code Block "; } - private Task GetTooltipAsync(SnapshotPoint triggerPoint) + public override Task GetTooltipAsync(SnapshotPoint triggerPoint) { LinkInline item = _document.Markdown.Descendants() .OfType() @@ -166,7 +151,5 @@ public void Dispose() _isDisposed = true; } - - public event EventHandler TagsChanged; } } diff --git a/src/MarkdownEditor2022.csproj b/src/MarkdownEditor2022.csproj index 31c45ec..80112be 100644 --- a/src/MarkdownEditor2022.csproj +++ b/src/MarkdownEditor2022.csproj @@ -142,7 +142,9 @@ - + + compile; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/MarkdownEditor2022Package.cs b/src/MarkdownEditor2022Package.cs index 6d9f390..a6ad8ba 100644 --- a/src/MarkdownEditor2022Package.cs +++ b/src/MarkdownEditor2022Package.cs @@ -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 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(); diff --git a/src/VSCommandTable.vsct b/src/VSCommandTable.vsct index 6867298..e998105 100644 --- a/src/VSCommandTable.vsct +++ b/src/VSCommandTable.vsct @@ -108,6 +108,7 @@ +