diff options
| author | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-07-07 22:01:24 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-07-07 22:01:24 +0200 |
| commit | 0c28a09091075ebfb0003efe2cfc32e1cc163d75 (patch) | |
| tree | e0cc3e9f286aa5a7e3ce46f2e1efdcd4ed2c98fe | |
| parent | 129bca332b68bd6106079f58bd0de77baf21ec68 (diff) | |
Improve performance of syntax highlighting
| -rw-r--r-- | Map/Presentation/Base Components/MapTextEditor.swift | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/Map/Presentation/Base Components/MapTextEditor.swift b/Map/Presentation/Base Components/MapTextEditor.swift index 5c7d74c..a2070e4 100644 --- a/Map/Presentation/Base Components/MapTextEditor.swift +++ b/Map/Presentation/Base Components/MapTextEditor.swift @@ -79,6 +79,10 @@ class MapTextEditorController: NSViewController { override func viewDidAppear() { self.view.window?.makeFirstResponder(self.view) updateHighlights() + // Initial colorization of the entire document + if let textView = currentTextView, let textStorage = textView.textStorage { + colorizeText(textStorage: textStorage) + } } var currentTextView: NSTextView? { @@ -150,12 +154,32 @@ extension MapTextEditorController: NSTextStorageDelegate { override func textStorageDidProcessEditing(_ obj: Notification) { if let textStorage = obj.object as? NSTextStorage { - self.colorizeText(textStorage: textStorage) + // Only colorize the edited range instead of the entire document + let editedRange = textStorage.editedRange + if editedRange.location != NSNotFound { + self.colorizeEditedText(textStorage: textStorage, editedRange: editedRange) + } } } + nonisolated private func colorizeEditedText(textStorage: NSTextStorage, editedRange: NSRange) { + // Expand range to include the entire line(s) that were edited + let string = textStorage.string as NSString + let lineRange = string.lineRange(for: editedRange) + + // Clear existing attributes in the range + textStorage.removeAttribute(.foregroundColor, range: lineRange) + + // Apply syntax highlighting only to the affected lines + self.applySyntaxHighlighting(textStorage: textStorage, range: lineRange) + } + nonisolated private func colorizeText(textStorage: NSTextStorage) { let range = NSMakeRange(0, textStorage.length) + self.applySyntaxHighlighting(textStorage: textStorage, range: range) + } + + nonisolated private func applySyntaxHighlighting(textStorage: NSTextStorage, range: NSRange) { var matches = vertexRegex.matches(in: textStorage.string, options: [], range: range) for match in matches { |