Publish VCC Plus OSS source distribution for 3.6.2 build 60
This commit is contained in:
commit
c652a5276d
1192 files changed
+272368
No files matched your search
@@ -0,0 +1,251 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
import AppKit
|
||||
import SwiftUI
|
||||
|
||||
enum AssemblyEditorSyntax {
|
||||
enum Kind { case label, mnemonic, directive, literal, operand, comment }
|
||||
struct Token { let range: NSRange; let kind: Kind }
|
||||
private static let words = try! NSRegularExpression(pattern:
|
||||
#"(?:"[^"\r\n]*"|'[^'\r\n]*'|;[^\r\n]*|\$[0-9A-Fa-f]+|%[01]+|[A-Za-z_.@?][A-Za-z0-9_.$@?]*:?|\d+)"#)
|
||||
static func tokens(in line: String, vocabulary: Set<String>) -> [Token] {
|
||||
let ns = line as NSString
|
||||
if line.trimmingCharacters(in: .whitespaces).hasPrefix("*") {
|
||||
return [.init(range: NSRange(location: 0, length: ns.length), kind: .comment)]
|
||||
}
|
||||
var opcodeSeen = false, labelSeen = false
|
||||
return words.matches(in: line, range: NSRange(location: 0, length: ns.length)).map { match in
|
||||
let word = ns.substring(with: match.range)
|
||||
let kind: Kind
|
||||
if word.hasPrefix(";") { kind = .comment }
|
||||
else if ["\"", "'", "$", "%"].contains(where: { word.hasPrefix($0) }) || word.first?.isNumber == true { kind = .literal }
|
||||
else if !opcodeSeen && !labelSeen && (word.hasSuffix(":") || match.range.location == 0) {
|
||||
kind = .label
|
||||
labelSeen = true
|
||||
} else if !opcodeSeen {
|
||||
opcodeSeen = true
|
||||
let upper = word.uppercased()
|
||||
kind = AssemblyReference.shared.isDirective(upper) ? .directive : (vocabulary.contains(upper) ? .mnemonic : .operand)
|
||||
} else { kind = .operand }
|
||||
return Token(range: match.range, kind: kind)
|
||||
}
|
||||
}
|
||||
|
||||
static func token(in source: String, offset: Int, vocabulary: Set<String>) -> String? {
|
||||
let text = source as NSString
|
||||
let pos = min(max(0, offset), text.length)
|
||||
let line = text.lineRange(for: NSRange(location: pos, length: 0))
|
||||
let contents = text.substring(with: line) as NSString
|
||||
let local = pos - line.location
|
||||
guard let token = tokens(in: contents as String, vocabulary: vocabulary).first(where: {
|
||||
NSLocationInRange(local, $0.range) || local == NSMaxRange($0.range)
|
||||
}), [.operand, .mnemonic, .label, .directive].contains(token.kind) else { return nil }
|
||||
return contents.substring(with: token.range).trimmingCharacters(in: CharacterSet(charactersIn: ":"))
|
||||
}
|
||||
|
||||
static func labels(in text: String, vocabulary: Set<String>) -> [(String, Int)] {
|
||||
text.components(separatedBy: "\n").enumerated().compactMap { index, line in
|
||||
guard let label = tokens(in: line, vocabulary: vocabulary).first, label.kind == .label else { return nil }
|
||||
return ((line as NSString).substring(with: label.range).trimmingCharacters(in: CharacterSet(charactersIn: ":")), index + 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
private final class AssemblyTextView: NSTextView, SourceFindEditor {
|
||||
weak var sourceFind: SourceFind?
|
||||
let documentUndo = UndoManager()
|
||||
override var undoManager: UndoManager? { documentUndo }
|
||||
var lookup: (() -> Void)?
|
||||
var definition: (() -> Void)?
|
||||
var target6309 = false
|
||||
override func performKeyEquivalent(with event: NSEvent) -> Bool {
|
||||
let modifiers = event.modifierFlags.intersection([.command, .control, .option, .shift])
|
||||
if event.keyCode == 49 && modifiers == [.control] { complete(nil); return true }
|
||||
if event.charactersIgnoringModifiers == "j", modifiers == [.command, .control] { definition?(); return true }
|
||||
return super.performKeyEquivalent(with: event)
|
||||
}
|
||||
override func menu(for event: NSEvent) -> NSMenu? {
|
||||
guard let menu = super.menu(for: event)?.copy() as? NSMenu else { return nil }
|
||||
menu.addItem(.separator())
|
||||
for (title, action) in [("Look Up in Assembly Quick Reference", #selector(lookUpInstruction)),
|
||||
("Go to Label Definition", #selector(goToLabel)),
|
||||
("Complete", #selector(complete(_:)))] {
|
||||
let item = NSMenuItem(title: title, action: action, keyEquivalent: "")
|
||||
item.target = self
|
||||
menu.addItem(item)
|
||||
}
|
||||
return menu
|
||||
}
|
||||
@objc private func lookUpInstruction() { lookup?() }
|
||||
@objc private func goToLabel() { definition?() }
|
||||
override func cancelOperation(_ sender: Any?) {
|
||||
if sourceFind?.cancel(from: self) != true {
|
||||
_ = nextResponder?.tryToPerform(#selector(NSResponder.cancelOperation(_:)), with: sender)
|
||||
}
|
||||
}
|
||||
override func completions(forPartialWordRange charRange: NSRange,
|
||||
indexOfSelectedItem index: UnsafeMutablePointer<Int>?) -> [String]? {
|
||||
guard selectedRange().length == 0, !hasMarkedText() else { return [] }
|
||||
index?.pointee = -1
|
||||
return AssemblyReference.shared.completions(in: string, at: selectedRange().location, target6309: target6309)
|
||||
}
|
||||
override func insertCompletion(_ word: String, forPartialWordRange charRange: NSRange, movement: Int, isFinal flag: Bool) {
|
||||
guard flag, movement != NSTextMovement.cancel.rawValue, !hasMarkedText(),
|
||||
selectedRange().length == 0, NSMaxRange(charRange) == selectedRange().location,
|
||||
completions(forPartialWordRange: charRange, indexOfSelectedItem: nil)?.contains(word) == true else { return }
|
||||
breakUndoCoalescing()
|
||||
insertText(word, replacementRange: charRange)
|
||||
breakUndoCoalescing()
|
||||
undoManager?.setActionName("Complete Assembly")
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
final class AssemblyEditorPool: ObservableObject {
|
||||
fileprivate var editors: [UUID: AssemblyTextView] = [:]
|
||||
func complete(_ id: UUID) {
|
||||
guard let editor = editors[id] else { return }
|
||||
editor.window?.makeFirstResponder(editor); editor.complete(nil)
|
||||
}
|
||||
func canReplace(_ buffers: [AssemblyBuffer]) -> Bool {
|
||||
buffers.allSatisfy { buffer in
|
||||
guard let editor = editors[buffer.id], editor.delegate != nil else { return true }
|
||||
return !editor.hasMarkedText() && editor.string.utf8.elementsEqual(buffer.text.utf8)
|
||||
}
|
||||
}
|
||||
func replaceReviewedLine(in buffer: AssemblyBuffer, range: NSRange, with replacement: String) -> Bool {
|
||||
guard let editor = editors[buffer.id], editor.delegate != nil, !editor.hasMarkedText(),
|
||||
editor.string.utf8.elementsEqual(buffer.text.utf8) else { return false }
|
||||
editor.breakUndoCoalescing()
|
||||
editor.insertText(replacement, replacementRange: range)
|
||||
editor.breakUndoCoalescing()
|
||||
editor.documentUndo.setActionName("Assistant Assembly Edit")
|
||||
return true
|
||||
}
|
||||
func forget(except ids: Set<UUID>) { editors = editors.filter { ids.contains($0.key) } }
|
||||
}
|
||||
|
||||
struct AssemblyEditor: NSViewRepresentable {
|
||||
let finder: SourceFind
|
||||
@ObservedObject var model: AssemblyDevelopmentModel
|
||||
let pool: AssemblyEditorPool
|
||||
let lookup: () -> Void
|
||||
func makeCoordinator() -> Coordinator { Coordinator(model: model) }
|
||||
func makeNSView(context: Context) -> NSScrollView {
|
||||
let scroll = NSScrollView()
|
||||
scroll.hasVerticalScroller = true
|
||||
scroll.borderType = .bezelBorder
|
||||
scroll.autohidesScrollers = true
|
||||
return scroll
|
||||
}
|
||||
func updateNSView(_ scroll: NSScrollView, context: Context) {
|
||||
let doc = model.buffer
|
||||
let editor: AssemblyTextView
|
||||
let coord = context.coordinator
|
||||
if let existing = pool.editors[doc.id] { editor = existing }
|
||||
else {
|
||||
editor = AssemblyTextView(frame: .zero)
|
||||
editor.isRichText = false
|
||||
editor.isAutomaticQuoteSubstitutionEnabled = false
|
||||
editor.isAutomaticDashSubstitutionEnabled = false
|
||||
editor.isAutomaticTextReplacementEnabled = false
|
||||
editor.isAutomaticSpellingCorrectionEnabled = false
|
||||
editor.isContinuousSpellCheckingEnabled = false
|
||||
editor.allowsUndo = true
|
||||
editor.font = .monospacedSystemFont(ofSize: 13, weight: .regular)
|
||||
editor.textContainerInset = NSSize(width: 8, height: 8)
|
||||
editor.isVerticallyResizable = true; editor.autoresizingMask = [.width]
|
||||
editor.textContainer?.widthTracksTextView = true
|
||||
editor.setAccessibilityLabel("Assembly source")
|
||||
editor.setAccessibilityIdentifier("assembly-source")
|
||||
pool.editors[doc.id] = editor
|
||||
}
|
||||
finder.editor = editor
|
||||
let changedDocument = scroll.documentView !== editor
|
||||
if changedDocument {
|
||||
if let previous = scroll.documentView as? AssemblyTextView {
|
||||
previous.delegate = nil; previous.lookup = nil; previous.definition = nil
|
||||
}
|
||||
scroll.documentView = editor
|
||||
editor.frame.size.width = scroll.contentSize.width
|
||||
coord.bufferID = doc.id
|
||||
}
|
||||
coord.updating = true
|
||||
defer { coord.updating = false }
|
||||
let changedSource = !editor.string.utf8.elementsEqual(doc.text.utf8)
|
||||
let target = model.cpu == .hd6309
|
||||
let changedTarget = editor.target6309 != target
|
||||
editor.target6309 = target
|
||||
if changedSource { editor.string = doc.text; editor.documentUndo.removeAllActions() }
|
||||
editor.delegate = coord
|
||||
editor.lookup = lookup; editor.definition = model.goToDefinition
|
||||
if changedDocument || changedSource || changedTarget { coord.color(editor) }
|
||||
if let request = model.selection, request.buffer == doc.id, request.id != coord.lastSelection {
|
||||
coord.lastSelection = request.id
|
||||
let range = NSIntersectionRange(request.range, NSRange(location: 0, length: editor.string.utf16.count))
|
||||
editor.setSelectedRange(range); editor.scrollRangeToVisible(range)
|
||||
}
|
||||
let active = model.executionLine
|
||||
if changedDocument || changedSource || coord.markedLine != active {
|
||||
coord.markedLine = active
|
||||
let length = editor.string.utf16.count
|
||||
editor.layoutManager?.removeTemporaryAttribute(.backgroundColor, forCharacterRange: NSRange(location: 0, length: length))
|
||||
if let active, active.path == doc.path, let range = AssemblyDevelopmentModel.lineRange(active.line, text: doc.text) {
|
||||
editor.layoutManager?.addTemporaryAttribute(.backgroundColor, value: NSColor.systemGreen.withAlphaComponent(0.22), forCharacterRange: range)
|
||||
}
|
||||
}
|
||||
}
|
||||
static func dismantleNSView(_ scroll: NSScrollView, coordinator: Coordinator) {
|
||||
if let editor = scroll.documentView as? AssemblyTextView {
|
||||
editor.delegate = nil; editor.lookup = nil; editor.definition = nil
|
||||
}
|
||||
}
|
||||
@MainActor final class Coordinator: NSObject, NSTextViewDelegate {
|
||||
let model: AssemblyDevelopmentModel
|
||||
var updating = false
|
||||
var bufferID: UUID?
|
||||
var lastSelection: UUID?
|
||||
var markedLine: AssemblyDevelopmentModel.SourceLine?
|
||||
init(model: AssemblyDevelopmentModel) { self.model = model }
|
||||
private var changedCharacters: NSRange?
|
||||
func textView(_ textView: NSTextView, shouldChangeTextIn affectedCharRange: NSRange, replacementString: String?) -> Bool {
|
||||
changedCharacters = replacementString.map { NSRange(location: affectedCharRange.location, length: $0.utf16.count) }
|
||||
return true
|
||||
}
|
||||
func textDidChange(_ notification: Notification) {
|
||||
guard !updating, let view = notification.object as? AssemblyTextView, bufferID == model.buffer.id else { return }
|
||||
model.edit(view.string)
|
||||
color(view, changed: changedCharacters)
|
||||
changedCharacters = nil
|
||||
}
|
||||
func textViewDidChangeSelection(_ notification: Notification) {
|
||||
guard !updating, let view = notification.object as? NSTextView, bufferID == model.buffer.id else { return }
|
||||
model.moveCursor(view.selectedRange().location)
|
||||
}
|
||||
fileprivate func color(_ editor: AssemblyTextView, changed: NSRange? = nil) {
|
||||
let ns = editor.string as NSString
|
||||
let layout = editor.layoutManager
|
||||
let range = changed.flatMap { NSMaxRange($0) <= ns.length ? ns.lineRange(for: $0) : nil } ?? NSRange(location: 0, length: ns.length)
|
||||
layout?.removeTemporaryAttribute(.foregroundColor, forCharacterRange: range)
|
||||
let words = AssemblyReference.shared.vocabulary(target6309: editor.target6309)
|
||||
var pos = range.location
|
||||
while pos < NSMaxRange(range) {
|
||||
let row = ns.lineRange(for: NSRange(location: pos, length: 0))
|
||||
for token in AssemblyEditorSyntax.tokens(in: ns.substring(with: row), vocabulary: words) {
|
||||
let color: NSColor
|
||||
switch token.kind {
|
||||
case .label: color = .systemPurple
|
||||
case .mnemonic: color = .systemBlue
|
||||
case .directive: color = .systemTeal
|
||||
case .literal: color = .systemOrange
|
||||
case .operand: color = .labelColor
|
||||
case .comment: color = .secondaryLabelColor
|
||||
}
|
||||
layout?.addTemporaryAttribute(.foregroundColor, value: color,
|
||||
forCharacterRange: NSRange(location: pos + token.range.location, length: token.range.length))
|
||||
}
|
||||
pos = NSMaxRange(row)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user