Files

125 lines
8.4 KiB
Swift

// SPDX-License-Identifier: GPL-3.0-or-later
import SwiftUI
import AppKit
struct DebugSourceView: View {
@ObservedObject var debugger: DebuggerModel
var body: some View {
VStack(alignment: .leading, spacing: 0) {
HStack {
Button(action: open) { Label("Open Listing…", systemImage: "folder") }.disabled(debugger.listingLoading)
.accessibilityIdentifier("debugger-source-open")
if let listing = debugger.listing {
Text(listing.name).lineLimit(1).truncationMode(.middle).help(listing.name)
Spacer(minLength: 4)
Button(action: debugger.unloadListing) { Label("Close", systemImage: "xmark") }.accessibilityIdentifier("debugger-source-close")
Toggle("Follow PC", isOn: $debugger.sourceFollowPC).toggleStyle(.checkbox)
.accessibilityIdentifier("debugger-source-follow")
} else { Spacer(); if debugger.listingLoading { ProgressView().controlSize(.small) } }
}.padding(10)
Divider()
if let listing = debugger.listing {
HStack {
TextField("Find source text or filename", text: $debugger.sourceSearch)
.textFieldStyle(.roundedBorder).onSubmit { debugger.findSource() }
.accessibilityLabel("Find source text or filename").accessibilityIdentifier("debugger-source-search")
Button(action: debugger.findSource) { Label("Find Next", systemImage: "magnifyingglass") }.accessibilityIdentifier("debugger-source-find")
Text("\(listing.addressedLineCount) addressed lines").font(.caption).foregroundStyle(.secondary)
}.padding(.horizontal, 10).padding(.vertical, 6)
ScrollViewReader { proxy in
ScrollView([.horizontal, .vertical]) {
LazyVStack(alignment: .leading, spacing: 0) {
ForEach(listing.lines) { line in row(line).id(line.id) }
}.padding(.vertical, 4)
}.accessibilityIdentifier("debugger-source-lines")
.onChange(of: debugger.visibleSourceLine) { _, index in if let index { proxy.scrollTo(index, anchor: .center) } }
.onChange(of: debugger.listing?.name) { _, _ in if let index = debugger.visibleSourceLine { proxy.scrollTo(index, anchor: .center) } }
.onAppear { if let index = debugger.visibleSourceLine { proxy.scrollTo(index, anchor: .center) } }
}
Divider()
VStack(alignment: .leading, spacing: 3) {
if let index = debugger.visibleSourceLine, listing.lines.indices.contains(index) {
let line = listing.lines[index]
Text(line.location).font(.caption).accessibilityIdentifier("debugger-source-location")
if let address = line.address, (listing.byAddress[address]?.count ?? 0) > 1 {
Text("Several listing lines share this address. Select the intended line; the breakpoint applies to every execution at that CPU address.")
.font(.caption).foregroundStyle(.secondary)
}
} else if debugger.sourceFollowPC {
Text("No source line at PC $\(DebuggerModel.hex(debugger.programCounter)).").font(.caption)
.accessibilityIdentifier("debugger-source-no-match")
}
Text("Logical CPU addresses · Load the matching program separately.")
.font(.caption).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true)
.help("Listing bytes are not verified against current memory. Use a final absolute listing for the loaded program and its current MMU mapping.")
}.padding(8)
} else {
VStack(alignment: .leading, spacing: 8) {
Label("Open a source listing", systemImage: "doc.text.magnifyingglass").font(.headline)
Text("Open a LWASM .lst or text listing generated with --list. View source, follow exact PC addresses, search, and share breakpoints with disassembly.")
Text("A listing is debugging information. Opening it does not load a program or change the machine.").foregroundStyle(.secondary)
}.padding(16).frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
}
if let message = debugger.sourceMessage {
Text(message).font(.caption).foregroundStyle(.orange).padding(8)
.accessibilityIdentifier("debugger-source-message")
}
}.background(Color(nsColor: .textBackgroundColor))
}
private func row(_ line: DebugSourceListing.Line) -> some View {
let isPC = line.id == debugger.currentSourceLine
let selected = line.id == debugger.visibleSourceLine
return HStack(spacing: 8) {
if let address = line.address {
let action = "\(debugger.allBreakpoints.contains(address) ? "Remove" : "Set") breakpoint at \(DebuggerModel.hex(address)), listing line \(line.id + 1)"
Button { debugger.toggleBreakpoint(at: address) } label: {
Image(systemName: debugger.breakpoints.contains(address) ? "circle.fill" : debugger.disabledBreakpoints.contains(address) ? "circle.dashed" : "circle")
.foregroundStyle(debugger.breakpoints.contains(address) ? Color.red : Color.secondary)
.frame(width: 22, height: 24)
}.buttonStyle(.plain).disabled(!debugger.commandsAvailable)
.help(action).accessibilityLabel(action)
.accessibilityIdentifier("debugger-source-breakpoint-\(line.id)")
} else { Color.clear.frame(width: 22, height: 24) }
Text(String(line.id + 1)).foregroundStyle(.secondary).frame(width: 42, alignment: .trailing)
Button {
debugger.selectSourceLine(line.id)
} label: {
HStack(spacing: 8) {
Text(line.address.map { DebuggerModel.hex($0) } ?? "—").frame(width: 42, alignment: .leading)
Image(systemName: "arrow.right").opacity(isPC ? 1 : 0).frame(width: 12)
Text(line.text.isEmpty ? " " : line.text).fixedSize(horizontal: true, vertical: false)
}.frame(maxWidth: .infinity, alignment: .leading).contentShape(Rectangle())
}.buttonStyle(.plain).accessibilityIdentifier("debugger-source-line-\(line.id)")
}.font(.system(size: 12, design: .monospaced)).padding(.horizontal, 8)
.background(selected ? Color.accentColor.opacity(0.16) : Color.clear)
.contextMenu {
if let address = line.address {
Button("Show Disassembly") { debugger.showDisassembly(at: address) }
Button("Show in Memory") { debugger.showMemory(at: address) }
Button("Set PC Here") { debugger.setPC(to: address) }.disabled(!debugger.canEdit)
Button(debugger.allBreakpoints.contains(address) ? "Remove Breakpoint" : "Add Breakpoint") { debugger.toggleBreakpoint(at: address) }
.disabled(!debugger.commandsAvailable)
if debugger.allBreakpoints.contains(address) {
Button(debugger.breakpoints.contains(address) ? "Disable Breakpoint" : "Enable Breakpoint") {
debugger.setBreakpoint(address, enabled: !debugger.breakpoints.contains(address))
}.disabled(!debugger.commandsAvailable)
}
}
Button("Copy Source Line") {
NSPasteboard.general.clearContents(); NSPasteboard.general.setString(line.text, forType: .string)
}
}
}
private func open() {
let panel = NSOpenPanel(); panel.title = "Open LWASM Source Listing"
panel.canChooseDirectories = false; panel.allowsMultipleSelection = false
// Allow .lst and text listings regardless of their Finder type association.
panel.begin { result in
guard result == .OK, let url = panel.url else { return }
Task { await debugger.loadListing(url) }
}
}
}