| 1 | #nullable enable |
| 2 | using CascadeIDE.Contracts; |
| 3 | using CascadeIDE.Features.Search.DataAcquisition; |
| 4 | using HybridCodebaseIndex.Core; |
| 5 | |
| 6 | namespace CascadeIDE.Features.Search.Application; |
| 7 | |
| 8 | internal static class CommandPaletteHybridIndexHitMapper |
| 9 | { |
| 10 | internal static RipgrepWorkspaceMatch ToRipgrepCompatibleMatch(IndexHit hit, string workspaceRootNorm) |
| 11 | { |
| 12 | var root = CanonicalFilePath.Normalize(workspaceRootNorm.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)); |
| 13 | var relative = hit.Path.Replace('/', Path.DirectorySeparatorChar); |
| 14 | var path = Path.IsPathRooted(relative) |
| 15 | ? CanonicalFilePath.Normalize(relative) |
| 16 | : CanonicalFilePath.Normalize(Path.Combine(root, relative)); |
| 17 | |
| 18 | var lineNum = Math.Max(hit.LineStart, 1); |
| 19 | var lineText = (hit.Snippet ?? "").TrimEnd('\r', '\n'); |
| 20 | return new RipgrepWorkspaceMatch(path, lineNum, lineText); |
| 21 | } |
| 22 | |
| 23 | internal static IReadOnlyList<RipgrepWorkspaceMatch> MapHits(IReadOnlyList<IndexHit> hits, string workspaceRootNorm) |
| 24 | { |
| 25 | if (hits.Count == 0) |
| 26 | return Array.Empty<RipgrepWorkspaceMatch>(); |
| 27 | |
| 28 | var list = new RipgrepWorkspaceMatch[hits.Count]; |
| 29 | for (var i = 0; i < hits.Count; i++) |
| 30 | list[i] = ToRipgrepCompatibleMatch(hits[i], workspaceRootNorm); |
| 31 | return list; |
| 32 | } |
| 33 | } |
| 34 | |