Sources/Orgstar/FolderLintWindow.swift
86 lines · 3741 bytes
1import OrgApp
2import OrgCore
3import SwiftUI
4
5/// Lint All Folders: the problems in every indexed Org file, grouped by file. Choosing a row
6/// opens the file in the main window at that line.
7struct FolderLintWindow: View {
8 let workspace: WorkspaceModel
9 let session: DocumentSession
10 let commands: AppCommands
11 @State private var selection: String?
12 @Environment(\.openWindow) private var openWindow
13
14 private var lint: FolderLint { commands.folderLint }
15
16 var body: some View {
17 let names = FileLint.names(workspace.files)
18 return VStack(spacing: 0) {
19 HStack(spacing: 8) {
20 if lint.isRunning {
21 ProgressView(value: Double(lint.checked), total: Double(max(1, lint.total)))
22 .frame(width: 120)
23 }
24 Text(lint.summary).foregroundStyle(.secondary).lineLimit(1)
25 Spacer()
26 if lint.isRunning {
27 Button("Stop") { lint.stop() }
28 } else {
29 Button("Check Again") { lint.run(workspace: workspace, session: session) }
30 }
31 }
32 .padding(.horizontal, 10)
33 .padding(.vertical, 6)
34 Divider()
35 List(selection: $selection) {
36 ForEach(lint.files ?? []) { file in
37 Section {
38 ForEach(file.findings) { finding in
39 row(finding).tag(Self.tag(file.path, finding))
40 }
41 } header: {
42 HStack {
43 Text(verbatim: names[file.path] ?? file.path).lineLimit(1).truncationMode(.middle)
44 Spacer()
45 Text(verbatim: "\(file.findings.count)").monospacedDigit().foregroundStyle(.secondary)
46 }
47 .help(file.path)
48 }
49 }
50 }
51 .overlay {
52 if lint.files?.isEmpty == true { Text("No problems found").foregroundStyle(.secondary) }
53 }
54 }
55 .frame(minWidth: 520, minHeight: 300)
56 .navigationTitle("Lint All Folders")
57 .onAppear { if lint.files == nil, !lint.isRunning { lint.run(workspace: workspace, session: session) } }
58 .onChange(of: selection) { _, tag in
59 guard let tag, let (path, finding) = find(tag) else { return }
60 if !commands.mainWindowOpen { openWindow(id: "main") }
61 commands.jump(path: path, offset: finding.offset)
62 }
63 }
64
65 private func row(_ finding: LintFinding) -> some View {
66 HStack(alignment: .firstTextBaseline, spacing: 8) {
67 Text(verbatim: "\(finding.line)").monospacedDigit().foregroundStyle(.secondary).frame(width: 44, alignment: .trailing)
68 Image(systemName: finding.trust == .high ? "xmark.octagon.fill" : "exclamationmark.triangle.fill")
69 .foregroundStyle(finding.trust == .high ? Color.red : Color.orange)
70 .help(FileLint.severity(finding))
71 .accessibilityLabel(FileLint.severity(finding))
72 Text(finding.message).lineLimit(1).help(finding.message)
73 Spacer()
74 Text(verbatim: finding.checker.rawValue).foregroundStyle(.secondary).lineLimit(1)
75 }
76 }
77
78 private static func tag(_ path: String, _ finding: LintFinding) -> String { path + "\n" + finding.id }
79
80 private func find(_ tag: String) -> (String, LintFinding)? {
81 for file in lint.files ?? [] {
82 if let finding = file.findings.first(where: { Self.tag(file.path, $0) == tag }) { return (file.path, finding) }
83 }
84 return nil
85 }
86}