| 1 | #nullable enable |
| 2 | using CascadeIDE.Contracts; |
| 3 | using CascadeIDE.Features.HybridIndex.Application; |
| 4 | using CascadeIDE.Features.Search.DataAcquisition; |
| 5 | using CascadeIDE.Services; |
| 6 | |
| 7 | namespace CascadeIDE.Features.Search.Application; |
| 8 | |
| 9 | /// <summary><c>auto</c>: пробуем HCI при готовности индекса; при ошибке — ripgrep. ADR 0112 §7.</summary> |
| 10 | internal sealed class CompositeAutoCommandPaletteGoToSearchBackend( |
| 11 | IHybridIndexOrchestratorSearch orchestrator, |
| 12 | string hybridScopeMode, |
| 13 | ICommandPaletteGoToSearchBackend ripgrep, |
| 14 | HybridIndexCommandPaletteGoToSearchBackend hciExclusive) : ICommandPaletteGoToSearchBackend |
| 15 | { |
| 16 | public async Task<(IReadOnlyList<RipgrepWorkspaceMatch> Matches, string? Error)> SearchMatchesAsync( |
| 17 | GoToAllQuery query, |
| 18 | string workspaceRoot, |
| 19 | string? solutionPath, |
| 20 | int maxMatches, |
| 21 | string? rgExecutable, |
| 22 | CancellationToken cancellationToken) |
| 23 | { |
| 24 | try |
| 25 | { |
| 26 | var rootNorm = CanonicalFilePath.Normalize( |
| 27 | workspaceRoot.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)); |
| 28 | (var hciRoot, var hciSolution) = HybridIndexScopeResolver.ApplyScopeMode(hybridScopeMode, rootNorm, solutionPath); |
| 29 | var st = await orchestrator.GetIndexStatusAsync(hciRoot, hciSolution, cancellationToken).ConfigureAwait(false); |
| 30 | if (!st.DatabaseExists || st.DocumentCount <= 0 || !string.IsNullOrEmpty(st.LastReindexError)) |
| 31 | return await ripgrep |
| 32 | .SearchMatchesAsync(query, workspaceRoot, solutionPath, maxMatches, rgExecutable, cancellationToken) |
| 33 | .ConfigureAwait(false); |
| 34 | |
| 35 | var (hciMatches, hciErr) = await hciExclusive |
| 36 | .SearchMatchesAsync(query, workspaceRoot, solutionPath, maxMatches, rgExecutable, cancellationToken) |
| 37 | .ConfigureAwait(false); |
| 38 | |
| 39 | if (!string.IsNullOrEmpty(hciErr)) |
| 40 | return await ripgrep |
| 41 | .SearchMatchesAsync(query, workspaceRoot, solutionPath, maxMatches, rgExecutable, cancellationToken) |
| 42 | .ConfigureAwait(false); |
| 43 | |
| 44 | return (hciMatches, null); |
| 45 | } |
| 46 | catch (OperationCanceledException) |
| 47 | { |
| 48 | throw; |
| 49 | } |
| 50 | catch (Exception) |
| 51 | { |
| 52 | return await ripgrep |
| 53 | .SearchMatchesAsync(query, workspaceRoot, solutionPath, maxMatches, rgExecutable, cancellationToken) |
| 54 | .ConfigureAwait(false); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |