Skip to content

Commit

Permalink
Forced parsing and tagging off the main thread
Browse files Browse the repository at this point in the history
[release]
  • Loading branch information
madskristensen committed Jan 2, 2022
1 parent dc511b0 commit d1f1b39
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 25 deletions.
41 changes: 26 additions & 15 deletions src/Language/TokenTagger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Adornments;
using Microsoft.VisualStudio.Text.Tagging;
using Microsoft.VisualStudio.Threading;
using Microsoft.VisualStudio.Utilities;

namespace PkgdefLanguage
Expand Down Expand Up @@ -37,7 +38,12 @@ internal TokenTagger(ITextBuffer buffer)
_document = buffer.GetDocument();
_document.Processed += ReParse;
_tagsCache = new Dictionary<ParseItem, ITagSpan<TokenTag>>();
ReParse();

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

public IEnumerable<ITagSpan<TokenTag>> GetTags(NormalizedSnapshotSpanCollection spans)
Expand All @@ -47,26 +53,31 @@ public IEnumerable<ITagSpan<TokenTag>> GetTags(NormalizedSnapshotSpanCollection

private void ReParse(object sender = null, EventArgs e = null)
{
ThreadHelper.JoinableTaskFactory.StartOnIdle(() =>
{
Dictionary<ParseItem, ITagSpan<TokenTag>> list = new();
// Make sure this is running on a background thread.
ThreadHelper.ThrowIfOnUIThread();

foreach (ParseItem item in _document.Items)
{
AddTagToList(list, item);
Dictionary<ParseItem, ITagSpan<TokenTag>> list = new();

foreach (ParseItem variable in item.References)
{
AddTagToList(list, variable);
}
foreach (ParseItem item in _document.Items)
{
if (_document.IsProcessing)
{
// Abort and wait for the next parse event to finish
return;
}

_tagsCache = list;
AddTagToList(list, item);

foreach (ParseItem variable in item.References)
{
AddTagToList(list, variable);
}
}

SnapshotSpan span = new(_buffer.CurrentSnapshot, 0, _buffer.CurrentSnapshot.Length);
TagsChanged?.Invoke(this, new SnapshotSpanEventArgs(span));
_tagsCache = list;

}, VsTaskRunContext.UIThreadIdlePriority);
SnapshotSpan span = new(_buffer.CurrentSnapshot, 0, _buffer.CurrentSnapshot.Length);
TagsChanged?.Invoke(this, new SnapshotSpanEventArgs(span));
}

private void AddTagToList(Dictionary<ParseItem, ITagSpan<TokenTag>> list, ParseItem item)
Expand Down
31 changes: 21 additions & 10 deletions src/Parser/Document.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Threading;

namespace PkgdefLanguage
{
Expand Down Expand Up @@ -59,23 +61,32 @@ public static Document FromLines(params string[] lines)
return doc;
}

public Task ProcessAsync()
public async Task ProcessAsync()
{
IsProcessing = true;
var success = false;

return Task.Run(() =>
await TaskScheduler.Default;

try
{
try
{
Parse();
ValidateDocument();
}
finally
Parse();
ValidateDocument();
success = true;
}
catch (Exception ex)
{
await ex.LogAsync();
}
finally
{
IsProcessing = false;

if (success)
{
IsProcessing = false;
Processed?.Invoke(this, EventArgs.Empty);
}
});
}
}

public ParseItem FindItemFromPosition(int position)
Expand Down

0 comments on commit d1f1b39

Please sign in to comment.