| 1 | #nullable enable |
| 2 | |
| 3 | using CascadeIDE.Features.CasaField.Application; |
| 4 | using CascadeIDE.Features.WorkspaceNavigation.Application; |
| 5 | using CascadeIDE.Models; |
| 6 | using CascadeIDE.Services; |
| 7 | using CascadeIDE.ViewModels; |
| 8 | |
| 9 | namespace CascadeIDE.Features.CasaField.Application; |
| 10 | |
| 11 | /// <summary>Open first CASA query target in MFD Markdown Preview.</summary> |
| 12 | public static class CasaFieldNavigationOpener |
| 13 | { |
| 14 | public static bool TryOpenFirstTarget(MainWindowViewModel vm, CasaFieldQueryResult result) |
| 15 | { |
| 16 | var target = result.Targets.FirstOrDefault(); |
| 17 | if (target is null) |
| 18 | return false; |
| 19 | |
| 20 | if (target.Kind == "code") |
| 21 | return CasaFieldCodeNavigationOpener.TryOpenCodeTarget(vm, target); |
| 22 | |
| 23 | if (string.IsNullOrWhiteSpace(target.DocPath)) |
| 24 | return false; |
| 25 | |
| 26 | var ws = vm.GetWorkspacePath(); |
| 27 | if (string.IsNullOrWhiteSpace(ws)) |
| 28 | return false; |
| 29 | |
| 30 | var notesWs = McpAgentNotesService.ResolveNotesWorkspacePath(ws); |
| 31 | if (string.IsNullOrEmpty(notesWs)) |
| 32 | return false; |
| 33 | |
| 34 | try |
| 35 | { |
| 36 | var storage = new AgentNotes.Core.NotesStorage(); |
| 37 | var content = storage.ReadKnowledgeFile(knowledgePath: null, filePath: target.DocPath); |
| 38 | var title = $"KB: {target.ConceptId}"; |
| 39 | vm.MarkdownPreviewTool.SetContent(title, content, target.DocPath, target.SectionLine); |
| 40 | vm.ApplyMfdRegionExpanded(true); |
| 41 | vm.TryNavigateToMfdShellPage(MfdShellPage.MarkdownPreview); |
| 42 | return true; |
| 43 | } |
| 44 | catch |
| 45 | { |
| 46 | if (WorkspaceMarkdownPreviewOpener.TryOpenRepoDocument( |
| 47 | ws, |
| 48 | target.DocPath, |
| 49 | (title, content, source) => vm.MarkdownPreviewTool.SetContent(title, content, source, target.SectionLine), |
| 50 | out _)) |
| 51 | { |
| 52 | vm.ApplyMfdRegionExpanded(true); |
| 53 | vm.TryNavigateToMfdShellPage(MfdShellPage.MarkdownPreview); |
| 54 | return true; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | return false; |
| 59 | } |
| 60 | } |
| 61 | |