Guard selection logic to avoid ArgumentOutOfRangeException when base name isn’t found

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
This commit is contained in:
Jack Ye 2025-09-21 21:54:40 +08:00 committed by GitHub
parent 0b7c0408f8
commit 785d14945c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -55,13 +55,20 @@ namespace Flow.Launcher.Plugin.Explorer.Views
if (sender is not TextBox textBox) return;
if (_info is DirectoryInfo)
{
await Application.Current.Dispatcher.InvokeAsync(textBox.SelectAll, DispatcherPriority.Background);
await textBox.Dispatcher.InvokeAsync(textBox.SelectAll, DispatcherPriority.Background);
return;
}
// select everything but the extension
else if (_info is FileInfo info)
if (_info is FileInfo info)
{
string properName = Path.GetFileNameWithoutExtension(info.Name);
Application.Current.Dispatcher.Invoke(textBox.Select, DispatcherPriority.Background, textBox.Text.IndexOf(properName), properName.Length);
int start = textBox.Text.LastIndexOf(properName, StringComparison.OrdinalIgnoreCase);
if (start < 0)
{
await textBox.Dispatcher.InvokeAsync(textBox.SelectAll, DispatcherPriority.Background);
return;
}
await textBox.Dispatcher.InvokeAsync(() => textBox.Select(start, properName.Length), DispatcherPriority.Background);
}
}