|
| 1 | +import UIKit |
| 2 | + |
| 3 | +/// Syntax highlights a string. |
| 4 | +/// |
| 5 | +/// An instance of `StringSyntaxHighlighter` can be used to syntax highlight a string without needing to create a `TextView`. |
| 6 | +public final class StringSyntaxHighlighter { |
| 7 | + /// The theme to use when syntax highlighting the text. |
| 8 | + public var theme: Theme |
| 9 | + /// The language to use when parsing the text. |
| 10 | + public var language: TreeSitterLanguage |
| 11 | + /// Object that can provide embedded languages on demand. A strong reference will be stored to the language provider. |
| 12 | + public var languageProvider: TreeSitterLanguageProvider? |
| 13 | + /// The number of points by which to adjust kern. |
| 14 | + /// |
| 15 | + /// The default value is 0 meaning that kerning is disabled. |
| 16 | + public var kern: CGFloat = 0 |
| 17 | + /// The tab length determines the width of the tab measured in space characers. |
| 18 | + /// |
| 19 | + /// The default value is 4 meaning that a tab is four spaces wide. |
| 20 | + public var tabLength: Int = 4 |
| 21 | + /// The line-height is multiplied with the value. |
| 22 | + public var lineHeightMultiplier: CGFloat = 1 |
| 23 | + |
| 24 | + /// Creates an object that can syntax highlight a text. |
| 25 | + /// - Parameters: |
| 26 | + /// - theme: The theme to use when syntax highlighting the text. |
| 27 | + /// - language: The language to use when parsing the text |
| 28 | + /// - languageProvider: Object that can provide embedded languages on demand. A strong reference will be stored to the language provider.. |
| 29 | + public init( |
| 30 | + theme: Theme = DefaultTheme(), |
| 31 | + language: TreeSitterLanguage, |
| 32 | + languageProvider: TreeSitterLanguageProvider? = nil |
| 33 | + ) { |
| 34 | + self.theme = theme |
| 35 | + self.language = language |
| 36 | + self.languageProvider = languageProvider |
| 37 | + } |
| 38 | + |
| 39 | + required init?(coder: NSCoder) { |
| 40 | + fatalError("init(coder:) has not been implemented") |
| 41 | + } |
| 42 | + |
| 43 | + /// Syntax highlights the text using the configured syntax highlighter. |
| 44 | + /// - Parameter text: The text to be syntax highlighted. |
| 45 | + /// - Returns: An attributed string containing the syntax highlighted text. |
| 46 | + public func syntaxHighlight(_ text: String) -> NSAttributedString { |
| 47 | + let mutableString = NSMutableString(string: text) |
| 48 | + let stringView = StringView(string: mutableString) |
| 49 | + let lineManager = LineManager(stringView: stringView) |
| 50 | + lineManager.rebuild() |
| 51 | + let languageMode = TreeSitterLanguageMode(language: language, languageProvider: languageProvider) |
| 52 | + let internalLanguageMode = languageMode.makeInternalLanguageMode( |
| 53 | + stringView: stringView, |
| 54 | + lineManager: lineManager |
| 55 | + ) |
| 56 | + internalLanguageMode.parse(mutableString) |
| 57 | + let tabWidth = TabWidthMeasurer.tabWidth(tabLength: tabLength, font: theme.font) |
| 58 | + let mutableAttributedString = NSMutableAttributedString(string: text) |
| 59 | + let defaultAttributes = DefaultStringAttributes( |
| 60 | + textColor: theme.textColor, |
| 61 | + font: theme.font, |
| 62 | + kern: kern, |
| 63 | + tabWidth: tabWidth |
| 64 | + ) |
| 65 | + defaultAttributes.apply(to: mutableAttributedString) |
| 66 | + applyLineHeightMultiplier(to: mutableAttributedString) |
| 67 | + let byteRange = ByteRange(from: 0, to: text.byteCount) |
| 68 | + let syntaxHighlighter = internalLanguageMode.createLineSyntaxHighlighter() |
| 69 | + syntaxHighlighter.theme = theme |
| 70 | + let syntaxHighlighterInput = LineSyntaxHighlighterInput( |
| 71 | + attributedString: mutableAttributedString, |
| 72 | + byteRange: byteRange |
| 73 | + ) |
| 74 | + syntaxHighlighter.syntaxHighlight(syntaxHighlighterInput) |
| 75 | + return mutableAttributedString |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +private extension StringSyntaxHighlighter { |
| 80 | + private func applyLineHeightMultiplier(to attributedString: NSMutableAttributedString) { |
| 81 | + let scaledLineHeight = theme.font.totalLineHeight * lineHeightMultiplier |
| 82 | + let mutableParagraphStyle = getMutableParagraphStyle(from: attributedString) |
| 83 | + mutableParagraphStyle.lineSpacing = scaledLineHeight - theme.font.totalLineHeight |
| 84 | + let range = NSRange(location: 0, length: attributedString.length) |
| 85 | + attributedString.beginEditing() |
| 86 | + attributedString.removeAttribute(.paragraphStyle, range: range) |
| 87 | + attributedString.addAttribute(.paragraphStyle, value: mutableParagraphStyle, range: range) |
| 88 | + attributedString.endEditing() |
| 89 | + } |
| 90 | + |
| 91 | + private func getMutableParagraphStyle( |
| 92 | + from attributedString: NSMutableAttributedString |
| 93 | + ) -> NSMutableParagraphStyle { |
| 94 | + guard let attributeValue = attributedString.attribute(.paragraphStyle, at: 0, effectiveRange: nil) else { |
| 95 | + return NSMutableParagraphStyle() |
| 96 | + } |
| 97 | + guard let paragraphStyle = attributeValue as? NSParagraphStyle else { |
| 98 | + fatalError("Expected .paragraphStyle attribute to be instance of NSParagraphStyle") |
| 99 | + } |
| 100 | + guard let mutableParagraphStyle = paragraphStyle.mutableCopy() as? NSMutableParagraphStyle else { |
| 101 | + fatalError("Expected mutableCopy() to return an instance of NSMutableParagraphStyle") |
| 102 | + } |
| 103 | + return mutableParagraphStyle |
| 104 | + } |
| 105 | +} |
0 commit comments