RunSearchAsync
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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