Sources/OrgstarMobile/LintScreen.swift
73 lines · 2839 bytes
1#if os(iOS)
2import OrgApp
3import OrgCore
4import SwiftUI
5
6/// Lint File: the problems in the open file, by line, as the Mac's panel lists them. Tapping
7/// one closes the list and moves the caret to its line.
8struct LintScreen: View {
9 let workspace: WorkspaceModel
10 let session: DocumentSession
11 let path: String
12 let reveal: (Int) -> Void
13 @State private var findings: [LintFinding]?
14 @Environment(\.dismiss) private var dismiss
15
16 var body: some View {
17 NavigationStack {
18 List {
19 if let findings {
20 Section {
21 ForEach(findings) { finding in
22 Button {
23 dismiss()
24 reveal(finding.offset)
25 } label: {
26 row(finding)
27 }
28 }
29 } header: {
30 Text(FileLint.summary(findings))
31 }
32 }
33 }
34 .overlay {
35 if findings == nil {
36 ProgressView()
37 } else if findings?.isEmpty == true {
38 ContentUnavailableView("No problems found", systemImage: "checkmark.circle")
39 }
40 }
41 .navigationTitle("Lint File")
42 .navigationBarTitleDisplayMode(.inline)
43 .toolbar { ToolbarItem(placement: .confirmationAction) { Button("Done") { dismiss() } } }
44 }
45 .task { await refresh() }
46 }
47
48 private func row(_ finding: LintFinding) -> some View {
49 HStack(alignment: .firstTextBaseline, spacing: 10) {
50 Image(systemName: finding.trust == .high ? "xmark.octagon.fill" : "exclamationmark.triangle.fill")
51 .foregroundStyle(finding.trust == .high ? Color.red : Color.orange)
52 .accessibilityLabel(FileLint.severity(finding))
53 VStack(alignment: .leading, spacing: 2) {
54 Text(finding.message).foregroundStyle(.primary)
55 Text("Line \(finding.line) · \(finding.checker.rawValue)", comment: "Lint File: a problem's line and checker")
56 .font(.caption)
57 .foregroundStyle(.secondary)
58 }
59 }
60 }
61
62 private func refresh() async {
63 guard let document = session.openDocuments.first(where: { $0.path == path })?.document else {
64 findings = []
65 return
66 }
67 let environment = FileLint.environment(index: workspace.index, overlay: workspace.overlay(session.openDocuments))
68 let found = await FileLint.lint(document.text, tree: document.tree, path: path, environment: environment)
69 guard !Task.isCancelled else { return }
70 findings = found
71 }
72}
73#endif