Based on this TextKit 2 demo project I thought that I could implement syntax highlighting by parsing syntax block tokens (e.g. comments like <!-- --> or /* */) in processEditing and storing their locations, and then actually applying the rendering with NSTextContentStorageDelegate in textContentStorage(_:textParagraphWith:) by checking the location of each paragraph against the store of syntax tokens.
This sort of works except that the rendering is only updated for paragraphs which are changed.
Is there a way to trigger NSTextContentStorage to re-fetch paragraphs in a given range? Or is this a totally misguided approach to the problem?
Post
Replies
Boosts
Views
Activity
steps to reproduce:
create a new iPad Playgrounds project
implement the minimum viable app using DocumentGroup
import SwiftUI
import UniformTypeIdentifiers
struct TextDocument: FileDocument {
var text: String
init(text: String = "") {
self.text = text
}
static var readableContentTypes: [UTType] { [.plainText] }
init(configuration: ReadConfiguration) throws {
guard let data = configuration.file.regularFileContents,
let string = String(data: data, encoding: .utf8)
else {
throw CocoaError(.fileReadCorruptFile)
}
text = string
}
func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
let data = text.data(using: .utf8)!
return .init(regularFileWithContents: data)
}
}
@main
struct MyApp: App {
var body: some Scene {
DocumentGroup(newDocument: TextDocument()) { file in
TextEditor(text: file.$document.text)
}
}
}
I can compile and run the app, and use it to open and edit or create new plain text documents, but the App Preview crashes, saying "The requested index was outside the bounds of the array." and there's a red error badge on @main
I assume this is a bug, and I've submitted a feedback (FB12194610) but I'm looking for ways to work around it in the meantime. There are two annoyances
the red error badge obfuscates when i really do have errors in my code
I don't have previews
If I comment out the DocumentGroup code and insert a WindowGroup I can make the preview successfully load long enough that I can pause it. This fixes the error badge problem, but pausing the App Preview pauses all the view previews as well. And every time I run the project it restarts the previews. And I have to run the project pretty often because I can't use the previews!
So I'm hoping there's a better workaround, either by more permanently disabling App Preview, or with something like the App Preview equivalent of PreviewProvider to give me some control over how the App Preview renders.