Sources/Orgstar/LintPane.swift
86 lines · 4127 bytes
1import OrgApp
2import OrgCore
3import OrgPresentation
4import SwiftUI
5
6/// Lint File's panel under the editor: the problems in the open file, by line. Choosing a row
7/// moves the caret there. The list follows the file as it changes.
8struct LintPane: View {
9 let workspace: WorkspaceModel
10 let session: DocumentSession
11 /// Raised each time Lint File runs, so running it again lints again.
12 let run: Int
13 let close: () -> Void
14 @State private var findings: [LintFinding]?
15 @State private var linted: Key?
16 @State private var selection: LintFinding.ID?
17 @Environment(\.orgTheme) private var theme
18 @Environment(\.colorScheme) private var colorScheme
19
20 private struct Key: Equatable {
21 var path: String?
22 var revision: Int?
23 var run: Int
24 }
25
26 var body: some View {
27 VStack(spacing: 0) {
28 HStack(spacing: 8) {
29 Text("Lint File").font(.subheadline.weight(.semibold))
30 if let path = session.path { Text((path as NSString).lastPathComponent).foregroundStyle(.secondary).lineLimit(1) }
31 Spacer()
32 if let findings { Text(FileLint.summary(findings)).foregroundStyle(.secondary) } else { ProgressView().controlSize(.small) }
33 Button("Close", systemImage: "xmark", action: close)
34 .labelStyle(.iconOnly)
35 .buttonStyle(.borderless)
36 .help("Close the Lint File panel")
37 }
38 .padding(.horizontal, 10)
39 .padding(.vertical, 5)
40 Divider()
41 Table(findings ?? [], selection: $selection) {
42 TableColumn("Line") { finding in Text(verbatim: "\(finding.line)").monospacedDigit() }
43 .width(48)
44 TableColumn("Severity") { finding in
45 Label(FileLint.severity(finding), systemImage: finding.trust == .high ? "xmark.octagon.fill" : "exclamationmark.triangle.fill")
46 .foregroundStyle(finding.trust == .high ? Color.red : Color.orange)
47 .labelStyle(.titleAndIcon)
48 }
49 .width(96)
50 TableColumn("Problem") { finding in Text(finding.message).lineLimit(1).help(finding.message) }
51 .width(min: 200, ideal: 360)
52 TableColumn("Checker") { finding in Text(verbatim: finding.checker.rawValue).foregroundStyle(.secondary).lineLimit(1) }
53 .width(min: 120, ideal: 210, max: 260)
54 }
55 .tableStyle(.inset(alternatesRowBackgrounds: true))
56 .overlay {
57 if findings?.isEmpty == true { Text("No problems found").foregroundStyle(.secondary) }
58 }
59 }
60 .background(theme.color("sidebar-background", in: colorScheme) ?? .clear)
61 .onChange(of: selection) { _, id in
62 guard let id, let finding = findings?.first(where: { $0.id == id }) else { return }
63 session.reveal(finding.offset)
64 }
65 .task(id: Key(path: session.path, revision: session.document?.revision, run: run)) { await refresh() }
66 }
67
68 private func refresh() async {
69 let key = Key(path: session.path, revision: session.document?.revision, run: run)
70 // Typing relints after a pause; a new file or another run of the command at once.
71 if let linted, linted.path == key.path, linted.run == key.run {
72 try? await Task.sleep(for: .milliseconds(500))
73 guard !Task.isCancelled else { return }
74 }
75 guard let document = session.document, FileLint.applies(to: session.path) else {
76 findings = []
77 return
78 }
79 let environment = FileLint.environment(index: workspace.index, overlay: workspace.overlay(session.openDocuments))
80 let found = await FileLint.lint(document.text, tree: document.tree, path: session.path, environment: environment)
81 guard !Task.isCancelled else { return }
82 findings = found
83 linted = key
84 if let selection, !found.contains(where: { $0.id == selection }) { self.selection = nil }
85 }
86}