-
Notifications
You must be signed in to change notification settings - Fork 404
Issues with Code Highlighting when Highlighting needs Async #338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Does the UI freeze, or does it simply not update? When you call the code from a Task (in the freeze demo), you're updating the Try something like:
This transfers execution of that block back to the main thread, where SwiftUI can see that it happened. Also, instead of the semaphore, try |
edit - The UI just froze, like the navigation page would freeze when trying to navigate to a sub-page containing code-blocks, and not respond to touch on other elements until the app was force closed, which was strange behavior to me, at least, and only on iOS / visionOS. macOS was fine. Appreciate the feedback! I ended up forking the repository and changing the protocol to be async myself which fixed my issue. It also let me adjust the code block behavior on macOS to not use scroll views since the nested ScrollViews kept messing up my vertical scrolling of my implementation on macOS, but it worked fine on other targets. Here's the commit from the fork: This issue can be closed if ultimately there isn't value to an async code styling adjustment to this protocol in the main repository |
I tried the solutions in this comment and wanted to report back on what I found:
@State var asyncLoadedAttributedText: AttributedString?
var body: some View {
VStack {
//fails on iOS
Markdown(demoMarkdown)
.markdownCodeSyntaxHighlighter(HighlightSyntaxHighligher(isDarkMode: true))
//works cross platform
Text(asyncLoadedAttributedText ?? AttributedString())
//Static Test
Text("Hello World")
}
.padding()
.task {
let loadedAttributedText = try? await highlighter.syntaxHighlighter.attributedText(demoSwift, language: .swift)
DispatchQueue.main.async {
asyncLoadedAttributedText = loadedAttributedText
}
}
}
func highlightCode(_ content: String, language: String?) -> Text {
guard let language = language else {
return Text(content)
}
let result: AttributedString
do {
//xcode error here
result = try withCheckedThrowingContinuation { continuation in
Task {
var detectedLanguage: HighlightLanguage? = nil
switch language {
case "swift": detectedLanguage = .swift
case "csharp": detectedLanguage = .cSharp
default: break
}
do {
let attributedText: AttributedString
if let detectedLanguage = detectedLanguage {
attributedText = try await syntaxHighlighter.attributedText(content, language: detectedLanguage, colors: isDarkMode ? .dark(.github) : .light(.github))
} else {
attributedText = try await syntaxHighlighter.attributedText(content, colors: isDarkMode ? .dark(.github) : .light(.github))
}
continuation.resume(returning: attributedText)
} catch {
continuation.resume(throwing: error)
}
}
}
} catch {
print(error)
return Text(content)
}
return Text(result)
} |
You could try creating your own theme instead with something like the following: public static func theme() -> Theme {
let theme = Theme()
// Copy everything that is in Theme+Github.swift, but swapped in `MyCoolAsyncTextView`
// ...Other stuff left out for brevity
theme.codeBlock { configuration in
ScrollView(.horizontal) {
// Key part
MyCoolAsyncTextView(content: configuration.content, language: configuration.language ?? "")
// Below copied from Theme+Github.swift
.fixedSize(horizontal: false, vertical: true)
.relativeLineSpacing(.em(0.225))
.markdownTextStyle {
FontFamilyVariant(.monospaced)
FontSize(.em(0.85))
}
.padding(16)
}
.background(Color.codeBlockBackground)
.clipShape(RoundedRectangle(cornerRadius: 6))
.markdownMargin(top: 0, bottom: 16)
return theme
} Later Markdown(content)
.markdownTheme(MyApp.theme) |
Description
I am trying to use a from-source version of HighlightSwift to perform my code highlighting as I want a macOS / iOS / visionOS unified solution for highlighting, which this library seems closest to providing. It's based in HighlightJS though, and the calls to the JS Core are done via async, which means it's root level call to get attributed text for given code is async.
CodeSyntaxHighlighter
from MarkdownUI only provides a synchronoushighlightCode
method.I was able to make this kind of work, it works on macOS, but it freezes on iOS and visionOS. I ripped this down to a sample project (See SyntaxHighlightFreezeDemo ) and I think I've narrowed it down to the code that interfaces with MarkdownUI, not the underlying highlighting code, which seems to work cross platform as expected.
The offending code seems to be my
highlightCode
function, which I had to finagle to work with the async call inside the synchronous function. Maybe there's a much better way to do this? I am getting a lot of warnings regarding Swift 6 Concurrency but I don't know of a better way to do this without getting an async call from MarkdownUI's protocol.What's weirder is that the function executes, and it gets to the
Highlight.swift
function call to create NSMutableAttributedString from the HTML data generated by HighlightJS, but my debug execution stops after that call:Checklist
main
branch of this package.Steps to reproduce
See SyntaxHighlightFreezeDemo
Expected behavior
MarkdownUI performing syntax highlighting cross platform
Resulting Behavior
Syntax highlighting on macOS, but a freeze on iOS and visionOS
Version information
The text was updated successfully, but these errors were encountered: