Skip to content

Commit

Permalink
Fix cleanup external files issue codecadwallader#748
Browse files Browse the repository at this point in the history
  • Loading branch information
heku committed Nov 7, 2021
1 parent 220a94d commit fa96ee9
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 21 deletions.
1 change: 1 addition & 0 deletions CodeMaidShared/CodeMaidShared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Helpers\TextDocumentHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\TypeFormatHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\UIHierarchyHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\UIThread.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\UndoTransactionHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Integration\Commands\AboutCommand.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Integration\Commands\BaseCommand.cs" />
Expand Down
25 changes: 8 additions & 17 deletions CodeMaidShared/Helpers/TextDocumentHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal static class TextDocumentHelper
internal static IEnumerable<EditPoint> FindMatches(TextDocument textDocument, string patternString)
{
var matches = new List<EditPoint>();
RunOnUIThread(() =>
UIThread.Run(() =>
{
ThreadHelper.ThrowIfNotOnUIThread();

Expand All @@ -69,7 +69,7 @@ internal static IEnumerable<EditPoint> FindMatches(TextDocument textDocument, st
internal static IEnumerable<EditPoint> FindMatches(TextSelection textSelection, string patternString)
{
var matches = new List<EditPoint>();
RunOnUIThread(() =>
UIThread.Run(() =>
{
ThreadHelper.ThrowIfNotOnUIThread();
if (TryGetTextBufferAt(textSelection.Parent.Parent.FullName, out ITextBuffer textBuffer))
Expand All @@ -95,7 +95,7 @@ internal static IEnumerable<EditPoint> FindMatches(TextSelection textSelection,
internal static EditPoint FirstOrDefaultMatch(TextDocument textDocument, string patternString)
{
EditPoint result = null;
RunOnUIThread(() =>
UIThread.Run(() =>
{
ThreadHelper.ThrowIfNotOnUIThread();

Expand Down Expand Up @@ -125,7 +125,7 @@ internal static bool TryFindNextMatch(EditPoint startPoint, ref EditPoint endPoi
bool result = false;
EditPoint resultEndPoint = null;

RunOnUIThread(() =>
UIThread.Run(() =>
{
ThreadHelper.ThrowIfNotOnUIThread();

Expand Down Expand Up @@ -154,7 +154,7 @@ internal static bool TryFindNextMatch(EditPoint startPoint, ref EditPoint endPoi
internal static string GetTextToFirstMatch(TextPoint startPoint, string matchString)
{
string result = null;
RunOnUIThread(() =>
UIThread.Run(() =>
{
ThreadHelper.ThrowIfNotOnUIThread();

Expand Down Expand Up @@ -299,7 +299,7 @@ internal static void SelectCodeItem(Document document, BaseCodeItem codeItem)
/// <param name="replacementString">The replacement string.</param>
internal static void SubstituteAllStringMatches(TextDocument textDocument, string patternString, string replacementString)
{
RunOnUIThread(() =>
UIThread.Run(() =>
{
ThreadHelper.ThrowIfNotOnUIThread();

Expand All @@ -320,7 +320,7 @@ internal static void SubstituteAllStringMatches(TextDocument textDocument, strin
/// <param name="replacementString">The replacement string.</param>
internal static void SubstituteAllStringMatches(TextSelection textSelection, string patternString, string replacementString)
{
RunOnUIThread(() =>
UIThread.Run(() =>
{
ThreadHelper.ThrowIfNotOnUIThread();

Expand All @@ -342,7 +342,7 @@ internal static void SubstituteAllStringMatches(TextSelection textSelection, str
/// <param name="replacementString">The replacement string.</param>
internal static void SubstituteAllStringMatches(EditPoint startPoint, EditPoint endPoint, string patternString, string replacementString)
{
RunOnUIThread(() =>
UIThread.Run(() =>
{
ThreadHelper.ThrowIfNotOnUIThread();

Expand Down Expand Up @@ -440,15 +440,6 @@ private static void ReplaceAll(ITextBuffer textBuffer, IEnumerable<FinderReplace
}
}

private static void RunOnUIThread(Action action)
{
CodeMaidPackage.Instance.JoinableTaskFactory.Run(async () =>
{
await CodeMaidPackage.Instance.JoinableTaskFactory.SwitchToMainThreadAsync();
action();
});
}

private static bool TryGetTextBufferAt(string filePath, out ITextBuffer textBuffer)
{
IVsWindowFrame windowFrame;
Expand Down
34 changes: 34 additions & 0 deletions CodeMaidShared/Helpers/UIThread.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Microsoft.VisualStudio.Shell;
using System;

namespace SteveCadwallader.CodeMaid.Helpers
{
internal static class UIThread
{
public static void Run(Action action)
{
if (ThreadHelper.CheckAccess())
{
action();
}
ThreadHelper.JoinableTaskFactory.Run(async () =>
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
action();
});
}

public static T Run<T>(Func<T> func)
{
if (ThreadHelper.CheckAccess())
{
return func();
}
return ThreadHelper.JoinableTaskFactory.Run(async () =>
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
return func();
});
}
}
}
7 changes: 5 additions & 2 deletions CodeMaidShared/Logic/Cleaning/CodeCleanupAvailabilityLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,11 @@ private static bool PromptUserAboutCleaningExternalFiles(Document document)
CanRemember = true
};

var window = new YesNoPromptWindow { DataContext = viewModel };
var response = window.ShowModal();
var response = UIThread.Run(() =>
{
var window = new YesNoPromptWindow { DataContext = viewModel };
return window.ShowDialog();
});

if (!response.HasValue)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,11 @@ private static bool PromptUserAboutReorganizingPreprocessorConditionals(Document
CanRemember = true
};

var window = new YesNoPromptWindow { DataContext = viewModel };
var response = window.ShowModal();
var response = UIThread.Run(() =>
{
var window = new YesNoPromptWindow { DataContext = viewModel };
return window.ShowDialog();
});

if (!response.HasValue)
{
Expand Down

0 comments on commit fa96ee9

Please sign in to comment.