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