diff --git a/Packages/Shared/CmuxSyntaxHighlighting/Sources/CmuxSyntaxHighlighting/HighlightrThemeAdapter.swift b/Packages/Shared/CmuxSyntaxHighlighting/Sources/CmuxSyntaxHighlighting/HighlightrThemeAdapter.swift index a069ab1c1a7a..057c4eed9614 100644 --- a/Packages/Shared/CmuxSyntaxHighlighting/Sources/CmuxSyntaxHighlighting/HighlightrThemeAdapter.swift +++ b/Packages/Shared/CmuxSyntaxHighlighting/Sources/CmuxSyntaxHighlighting/HighlightrThemeAdapter.swift @@ -8,6 +8,10 @@ final class HighlightrThemeAdapter { /// Creates an adapter when Highlightr can initialize its JavaScript engine. init?() { guard let highlightr = Highlightr() else { return nil } + // File Preview handles source files that embed another language (for + // example dbt SQL with Jinja delimiters). Keep valid tokens around + // grammar-illegal characters instead of aborting the whole document. + highlightr.ignoreIllegals = true self.highlightr = highlightr } diff --git a/Packages/Shared/CmuxSyntaxHighlighting/Tests/CmuxSyntaxHighlightingTests/HighlightrSyntaxEngineTests.swift b/Packages/Shared/CmuxSyntaxHighlighting/Tests/CmuxSyntaxHighlightingTests/HighlightrSyntaxEngineTests.swift index e03c4c708338..7103906a053a 100644 --- a/Packages/Shared/CmuxSyntaxHighlighting/Tests/CmuxSyntaxHighlightingTests/HighlightrSyntaxEngineTests.swift +++ b/Packages/Shared/CmuxSyntaxHighlighting/Tests/CmuxSyntaxHighlightingTests/HighlightrSyntaxEngineTests.swift @@ -2,6 +2,10 @@ import Foundation import Testing +#if canImport(AppKit) +import AppKit +#endif + @Suite("Highlightr syntax engine") struct HighlightrSyntaxEngineTests { @Test("Policy rejection returns nil without coloring") @@ -15,4 +19,30 @@ struct HighlightrSyntaxEngineTests { #expect(highlighted == nil) } +#if canImport(AppKit) + @Test("SQL containing Jinja delimiters still highlights SQL tokens") + func sqlContainingJinjaDelimitersStillHighlightsTokens() async throws { + let engine = HighlightrSyntaxEngine() + let source = """ + select id, name + from {{ ref('users') }} + where active = true + """ + + let highlighted = try #require( + await engine.highlight(text: source, language: "sql", theme: .dark) + ) + let selectRange = (highlighted.value.string as NSString).range(of: "select") + #expect(selectRange.location != NSNotFound) + let color = highlighted.value.attribute( + .foregroundColor, + at: selectRange.location, + effectiveRange: nil + ) + #expect( + HighlightColorRemapper(theme: .dark).hexKey(from: color as Any) == "0091FF" + ) + } +#endif + }