Skip to content

Instantly share code, notes, and snippets.

@kcbanner
Created July 20, 2021 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kcbanner/d747152a35caeba7a68d117170606c1c to your computer and use it in GitHub Desktop.
Save kcbanner/d747152a35caeba7a68d117170606c1c to your computer and use it in GitHub Desktop.
RunSearchAsync
public async Task<bool> RunSearchAsync()
{
if (State.IsRunning)
{
return false;
}
if (State.SelectedSearchRoot == null || string.IsNullOrEmpty(State.SelectedSearchRoot.Path))
{
VsShellUtilities.ShowMessageBox(
this,
"Please select a search directory and try again.",
"No search root specified.",
Microsoft.VisualStudio.Shell.Interop.OLEMSGICON.OLEMSGICON_WARNING,
Microsoft.VisualStudio.Shell.Interop.OLEMSGBUTTON.OLEMSGBUTTON_OK,
Microsoft.VisualStudio.Shell.Interop.OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
return false;
}
if (!Directory.Exists(State.SelectedSearchRoot.Path))
{
VsShellUtilities.ShowMessageBox(
this,
$"Search root \"{ State.SelectedSearchRoot.Path }\" not found. Please select a valid search directory and try again.",
"Search root not found.",
Microsoft.VisualStudio.Shell.Interop.OLEMSGICON.OLEMSGICON_WARNING,
Microsoft.VisualStudio.Shell.Interop.OLEMSGBUTTON.OLEMSGBUTTON_OK,
Microsoft.VisualStudio.Shell.Interop.OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
return false;
}
try
{
State.CancellationTokenSource = new CancellationTokenSource();
AckmateParser parser = new AckmateParser();
parser.MatchFound += (object _, FileMatch match) =>
{
int newMatches = 0;
foreach (LineMatch lineMatch in match.LineMatches)
{
newMatches += lineMatch.Matches.Count;
}
int totalMatches = Interlocked.Add(ref State.NumMatches, newMatches);
if (totalMatches > SEARCH_LIMIT)
{
if (Interlocked.Increment(ref State.TrippedLimit) == 1)
{
var priorityJTF = ThreadHelper.JoinableTaskFactory.WithPriority(Dispatcher.CurrentDispatcher, DispatcherPriority.Background);
priorityJTF.RunAsync(async delegate
{
await JoinableTaskFactory.SwitchToMainThreadAsync();
VsShellUtilities.ShowMessageBox(
this,
$"The search returned over {SEARCH_LIMIT} matches, please refine your query and search again.",
"Too many results!",
Microsoft.VisualStudio.Shell.Interop.OLEMSGICON.OLEMSGICON_WARNING,
Microsoft.VisualStudio.Shell.Interop.OLEMSGBUTTON.OLEMSGBUTTON_OK,
Microsoft.VisualStudio.Shell.Interop.OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
TryCancelSearch();
}).FileAndForget("turbosearch/search/limit");
}
return;
}
match.IsExpanded = TurboSearchSettings.CurrentSettings.ExpandByDefault;
lock (State.FileMatchesLock)
{
State.FileMatches.Add(match);
}
};
State.NumMatches = 0;
State.TrippedLimit = 0;
State.FileMatches.Clear();
State.LastSearchRootPath = State.SelectedSearchRoot.Path;
State.SelectedItem = null;
State.SelectedFileMatchIndex = 0;
State.SelectedLineMatchIndex = 0;
State.IsRunning = true;
System.Diagnostics.Stopwatch Stopwatch = new System.Diagnostics.Stopwatch();
Stopwatch.Start();
int exitCode = await Ag.RunAgAsync(
State.SearchText,
State.SelectedSearchRoot.Path,
State.IgnoreCase,
State.UseRegex,
State.SkipVCSIgnoredFiles,
parser,
State.CancellationTokenSource.Token);
Stopwatch.Stop();
State.DurationMilliseconds = Stopwatch.ElapsedMilliseconds;
return exitCode == 0;
}
catch (OperationCanceledException)
{
return false;
}
finally
{
State.IsRunning = false;
State.CancellationTokenSource.Dispose();
State.CancellationTokenSource = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment