import Foundation import OrgCore import OrgPresentation import Testing /// Highlights `yaml` and `swift` code as one keyword run over the whole code. struct WholeCodeHighlighter: CodeHighlighter { func highlights(language: String, code: String) -> [(range: Range, category: SyntaxCategory)] { ["yaml", "swift"].contains(language) ? [(0.., Set)] { #expect(Presentation.markdownHiddenRanges(document, folds: folds, bodyFolds: bodyFolds) == Presentation.hiddenRanges(tree, folds: folds, bodyFolds: bodyFolds), "\(folds) \(bodyFolds)") } // The `#` line in a fence isn't a heading. #expect(Presentation.heading(containing: 31, in: tree) == nil) #expect(Presentation.heading(containing: 40, in: tree) == 39) } func roles(_ text: String, highlighter: (any CodeHighlighter)? = nil) -> [String] { let document = MarkdownDocument(text) let ns = text as NSString return Presentation.markdownStyles(document, in: 0.. q\n", highlighter: WholeCodeHighlighter()) == [ "blockDelimiter ---⏎", "metadata k: v⏎", "syntax(OrgPresentation.SyntaxCategory.keyword) k: v⏎", "blockDelimiter ---⏎", "blockDelimiter ```swift⏎", "block let⏎", "syntax(OrgPresentation.SyntaxCategory.keyword) let⏎", "blockDelimiter ```⏎", "heading(level: 1) # H *e*", "italic *e*", "markup *", "markup *", "markup >", "italic q", ]) } @Test func stylesOnlyTheLinesAsked() { let text = "# A\n**b**\n```\n# c\n" let document = MarkdownDocument(text) let runs = Presentation.markdownStyles(document, in: 5..<6) #expect(runs.map(\.role) == [.bold, .markup, .markup]) // Inside an unclosed fence, nothing is a heading. #expect(Presentation.markdownStyles(document, in: 14..<17).map(\.role) == [.blockDelimiter, .block]) } @Test func restyleRangeFollowsFences() { let old = "a\n```\ncode\n```\nb\n# h\n" let oldBlocks = MarkdownDocument(old).structure.blocks // Deleting the closing fence opens the block to the end of the text. let edit = TextEdit(range: 11..<15, replacement: "") let new = "a\n```\ncode\nb\n# h\n" let range = Presentation.markdownRestyleRange(for: edit, oldBlocks: oldBlocks, new: MarkdownDocument(new), newText: new as NSString) #expect(range == 2..<(new as NSString).length) // Typing inside a paragraph restyles its line. let typed = TextEdit(range: 1..<1, replacement: "x") let after = "ax\n```\ncode\n```\nb\n# h\n" #expect(Presentation.markdownRestyleRange(for: typed, oldBlocks: oldBlocks, new: MarkdownDocument(after), newText: after as NSString) == 0..<3) } }