@@ -54,13 +54,13 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
5454
5555 Editor::FilteredSearchResult FilteredSearchResult::CreateNoResultsItem (const winrt::hstring& query)
5656 {
57- return winrt::make<FilteredSearchResult>(nullptr , nullptr , hstring{ fmt::format (fmt::runtime (std::wstring{ RS_ (L" Search_NoResults" ) }), query) });
57+ return winrt::make<FilteredSearchResult>(nullptr , nullptr , nullptr , hstring{ fmt::format (fmt::runtime (std::wstring{ RS_ (L" Search_NoResults" ) }), query) });
5858 }
5959
6060 // Creates a FilteredSearchResult with the given search index entry and runtime object.
6161 // The resulting search result will have a label like "<ProfileName>: <display text>" or "<ProfileName>".
6262 // This is so that we can reuse the display text from the search index, but also add additional context to which runtime object this search result maps to.
63- Editor::FilteredSearchResult FilteredSearchResult::CreateRuntimeObjectItem (const LocalizedIndexEntry* searchIndexEntry, const Windows::Foundation::IInspectable& runtimeObj)
63+ Editor::FilteredSearchResult FilteredSearchResult::CreateRuntimeObjectItem (std::shared_ptr< const IndexData> indexData, const LocalizedIndexEntry* searchIndexEntry, const Windows::Foundation::IInspectable& runtimeObj)
6464 {
6565 hstring runtimeObjLabel{};
6666 hstring runtimeObjContext{};
@@ -96,14 +96,14 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
9696 // - primaryText: <displayText>
9797 // - secondaryText: <runtimeObjLabel>
9898 // navigates to setting container
99- return winrt::make<FilteredSearchResult>(searchIndexEntry, runtimeObj, displayText, runtimeObjLabel);
99+ return winrt::make<FilteredSearchResult>(indexData, searchIndexEntry, runtimeObj, displayText, runtimeObjLabel);
100100 }
101101 // Partial index entry (for runtime object main pages)
102102 // - primaryText: <runtimeObjLabel>
103103 // - secondaryText: <runtimeObjContext>
104104 // "PowerShell" --> navigates to main runtime object page (i.e. Profiles_Base)
105105 // "SSH" | "Extension" --> navigates to main runtime object page (i.e. Extensions > SSH)
106- return winrt::make<FilteredSearchResult>(searchIndexEntry, runtimeObj, runtimeObjLabel, runtimeObjContext);
106+ return winrt::make<FilteredSearchResult>(indexData, searchIndexEntry, runtimeObj, runtimeObjLabel, runtimeObjContext);
107107 }
108108
109109 winrt::hstring FilteredSearchResult::AccessibleName () const
@@ -277,7 +277,7 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
277277 indexData.colorSchemeIndexEntry .Entry = &PartialColorSchemeIndexEntry ();
278278 indexData.actionIndexEntry .Entry = &PartialActionIndexEntry ();
279279
280- _index = std::move (indexData);
280+ _index. store (std::make_shared< const IndexData>( std::move (indexData)) );
281281 }
282282
283283 // Method Description:
@@ -301,6 +301,16 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
301301 const IVectorView<Editor::ExtensionPackageViewModel> extensionPkgVMs,
302302 const IVectorView<Editor::CommandViewModel> commandVMs)
303303 {
304+ // Snapshot the current index so that Reset() can safely swap
305+ // it without invalidating pointers used by this search.
306+ const auto index = _index.load ();
307+ wil::hide_name _index;
308+ if (!index)
309+ {
310+ // invalid search; do not return any results
311+ co_return single_threaded_observable_vector<Windows::Foundation::IInspectable>();
312+ }
313+
304314 co_await winrt::resume_background ();
305315
306316 // The search can be cancelled at any time by the caller.
@@ -309,7 +319,7 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
309319 const auto filter = fzf::matcher::ParsePattern (std::wstring_view{ query });
310320
311321 std::vector<std::pair<int , Editor::FilteredSearchResult>> scoredResults;
312- for (const auto & entry : _index. mainIndex )
322+ for (const auto & entry : index-> mainIndex )
313323 {
314324 if (cancellationToken ())
315325 {
@@ -331,7 +341,7 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
331341
332342 if (bestScore >= MinimumMatchScore)
333343 {
334- scoredResults.emplace_back (bestScore, winrt::make<FilteredSearchResult>(&entry));
344+ scoredResults.emplace_back (bestScore, winrt::make<FilteredSearchResult>(index, &entry));
335345 }
336346 }
337347
@@ -358,7 +368,7 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
358368 {
359369 // navigates to runtime object main page (i.e. "PowerShell" Profiles_Base page)
360370 scoredResults.emplace_back (objNameMatch->Score * WeightRuntimeObjectMatch,
361- FilteredSearchResult::CreateRuntimeObjectItem (&partialSearchIndexEntry, runtimeObj));
371+ FilteredSearchResult::CreateRuntimeObjectItem (index, &partialSearchIndexEntry, runtimeObj));
362372 }
363373
364374 for (const auto & entry : searchIndex)
@@ -409,15 +419,15 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
409419 {
410420 // navigates to runtime object's setting (i.e. "PowerShell: Command line")
411421 scoredResults.emplace_back (bestScore * WeightRuntimeObjectSetting,
412- FilteredSearchResult::CreateRuntimeObjectItem (&entry, runtimeObj));
422+ FilteredSearchResult::CreateRuntimeObjectItem (index, &entry, runtimeObj));
413423 }
414424 }
415425 }
416426 };
417427
418- appendRuntimeObjectResults (profileVMs, _index. profileIndex , _index. profileIndexEntry );
419- appendRuntimeObjectResults (ntmFolderVMs, _index. ntmFolderIndex , _index. ntmFolderIndexEntry );
420- appendRuntimeObjectResults (colorSchemeVMs, _index. colorSchemeIndex , _index. colorSchemeIndexEntry );
428+ appendRuntimeObjectResults (profileVMs, index-> profileIndex , index-> profileIndexEntry );
429+ appendRuntimeObjectResults (ntmFolderVMs, index-> ntmFolderIndex , index-> ntmFolderIndexEntry );
430+ appendRuntimeObjectResults (colorSchemeVMs, index-> colorSchemeIndex , index-> colorSchemeIndexEntry );
421431
422432 // Simple runtime object matching (no associated search index, just match by display name)
423433 auto appendSimpleRuntimeObjectResults = [&](const auto & runtimeObjectList, const LocalizedIndexEntry& indexEntry, auto getDisplayName) {
@@ -432,13 +442,13 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
432442 {
433443 // navigates to runtime object page (i.e. "Copy Text" --> Actions > EditAction page)
434444 scoredResults.emplace_back (match->Score * WeightRuntimeObjectMatch,
435- FilteredSearchResult::CreateRuntimeObjectItem (&indexEntry, runtimeObj));
445+ FilteredSearchResult::CreateRuntimeObjectItem (index, &indexEntry, runtimeObj));
436446 }
437447 }
438448 };
439449
440- appendSimpleRuntimeObjectResults (extensionPkgVMs, _index. extensionIndexEntry , [](const auto & ext) { return ext.DisplayName (); });
441- appendSimpleRuntimeObjectResults (commandVMs, _index. actionIndexEntry , [](const auto & cmd) { return cmd.DisplayName (); });
450+ appendSimpleRuntimeObjectResults (extensionPkgVMs, index-> extensionIndexEntry , [](const auto & ext) { return ext.DisplayName (); });
451+ appendSimpleRuntimeObjectResults (commandVMs, index-> actionIndexEntry , [](const auto & cmd) { return cmd.DisplayName (); });
442452
443453 // must be IInspectable to be used as ItemsSource in XAML
444454 std::vector<Windows::Foundation::IInspectable> results;
0 commit comments