@@ -12,19 +12,19 @@ struct HighlightingTextEditor: NSViewRepresentable {
1212 @Binding var text : String
1313 var font : NSFont = . systemFont( ofSize: 15 , weight: . regular)
1414 var isFocused : FocusState < Bool > . Binding ?
15-
15+
1616 func makeCoordinator( ) -> Coordinator {
1717 Coordinator ( self )
1818 }
19-
19+
2020 func makeNSView( context: Context ) -> NSScrollView {
2121 let scrollView = NSScrollView ( )
2222 scrollView. hasVerticalScroller = true
2323 scrollView. hasHorizontalScroller = false
2424 scrollView. autohidesScrollers = true
2525 scrollView. borderType = . noBorder
2626 scrollView. drawsBackground = false
27-
27+
2828 let textView = NSTextView ( )
2929 textView. isEditable = true
3030 textView. isSelectable = true
@@ -41,79 +41,79 @@ struct HighlightingTextEditor: NSViewRepresentable {
4141 textView. isVerticallyResizable = true
4242 textView. isHorizontallyResizable = false
4343 textView. autoresizingMask = [ . width]
44-
44+
4545 scrollView. documentView = textView
4646 context. coordinator. textView = textView
47-
47+
4848 // Set initial text and apply highlighting
4949 textView. string = text
5050 context. coordinator. applyHighlighting ( textView)
51-
51+
5252 return scrollView
5353 }
54-
54+
5555 func updateNSView( _ scrollView: NSScrollView , context: Context ) {
5656 guard let textView = scrollView. documentView as? NSTextView else { return }
57-
57+
5858 if textView. string != text {
5959 let selectedRanges = textView. selectedRanges
6060 textView. string = text
6161 textView. selectedRanges = selectedRanges
6262 context. coordinator. applyHighlighting ( textView)
6363 }
6464 }
65-
65+
6666 class Coordinator : NSObject , NSTextViewDelegate {
6767 var parent : HighlightingTextEditor
6868 weak var textView : NSTextView ?
69-
69+
7070 private static let annotationPattern = try ! NSRegularExpression (
7171 pattern: " \\ [[^ \\ ]]+ \\ ] " ,
7272 options: [ ]
7373 )
74-
74+
7575 init ( _ parent: HighlightingTextEditor ) {
7676 self . parent = parent
7777 }
78-
78+
7979 func textDidChange( _ notification: Notification ) {
8080 guard let textView = notification. object as? NSTextView else { return }
8181 parent. text = textView. string
8282 applyHighlighting ( textView)
8383 }
84-
84+
8585 func applyHighlighting( _ textView: NSTextView ) {
8686 guard let textStorage = textView. textStorage else { return }
8787 let fullRange = NSRange ( location: 0 , length: textStorage. length)
8888 let text = textStorage. string
89-
89+
9090 // Preserve selection
9191 let selectedRanges = textView. selectedRanges
92-
92+
9393 textStorage. beginEditing ( )
94-
94+
9595 // Reset to default style
9696 let defaultAttributes : [ NSAttributedString . Key : Any ] = [
9797 . font: parent. font,
9898 . foregroundColor: NSColor . labelColor
9999 ]
100100 textStorage. setAttributes ( defaultAttributes, range: fullRange)
101-
101+
102102 // Highlight [bracket] annotations
103103 let matches = Self . annotationPattern. matches ( in: text, options: [ ] , range: fullRange)
104104 for match in matches {
105105 let annotationAttributes : [ NSAttributedString . Key : Any ] = [
106106 . font: NSFontManager . shared. convert ( parent. font, toHaveTrait: . italicFontMask) ,
107107 . foregroundColor: NSColor . secondaryLabelColor,
108- . backgroundColor: NSColor . secondaryLabelColor. withAlphaComponent ( 0 .08)
108+ . backgroundColor: NSColor . secondaryLabelColor. withAlphaComponent ( 1 .08)
109109 ]
110110 textStorage. addAttributes ( annotationAttributes, range: match. range)
111111 }
112-
112+
113113 textStorage. endEditing ( )
114-
114+
115115 // Restore selection
116116 textView. selectedRanges = selectedRanges
117117 }
118118 }
119- }
119+ }
0 commit comments