Sources/OrgEditorAppKit/ViewToggles.swift
112 lines · 4652 bytes
1#if os(macOS)
2import AppKit
3import OrgCore
4import OrgPresentation
5
6/// Spell checking, line truncation and mouse clicks on heading stars and checkboxes.
7extension OrgEditor {
8 // MARK: - Spell checking
9
10 public var spellChecking: Bool {
11 get { textView.isContinuousSpellCheckingEnabled }
12 set {
13 for view in textViews { view.isContinuousSpellCheckingEnabled = newValue }
14 if !newValue, let storage = textView.textStorage {
15 storage.removeAttribute(.spellingState, range: NSRange(location: 0, length: storage.length))
16 }
17 }
18 }
19
20 func toggleSpellCheck() {
21 spellChecking.toggle()
22 onMessage?(spellChecking ? "Spell checking enabled" : "Spell checking disabled")
23 }
24
25 /// Ranges spell-fu skips in Org, for this revision of the text.
26 func spellSkipped() -> [Range<Int>] {
27 if let cached = spellSkipCache, cached.text == document.text { return cached.ranges }
28 let ranges = isOrg ? SpellCheck.skipped(document.tree) : []
29 spellSkipCache = (document.text, ranges)
30 return ranges
31 }
32
33 // MARK: - Truncation
34
35 /// `toggle-truncate-lines`: long lines run off the right edge instead of wrapping.
36 public var truncatesLines: Bool {
37 get { !(textView.textContainer?.widthTracksTextView ?? true) }
38 set {
39 guard let container = textView.textContainer, newValue != truncatesLines else { return }
40 let scroll = textView.enclosingScrollView
41 keepingTopLine {
42 scroll?.hasHorizontalScroller = newValue
43 textView.isHorizontallyResizable = newValue
44 if newValue {
45 container.widthTracksTextView = false
46 container.size = NSSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
47 textView.autoresizingMask = [.width, .height]
48 } else {
49 let width = scroll?.contentSize.width ?? textView.frame.width
50 textView.setFrameSize(NSSize(width: width, height: textView.frame.height))
51 container.size = NSSize(width: width, height: CGFloat.greatestFiniteMagnitude)
52 container.widthTracksTextView = true
53 textView.autoresizingMask = [.width]
54 }
55 textLayoutManager.invalidateLayout(for: textLayoutManager.documentRange)
56 }
57 textView.needsDisplay = true
58 }
59 }
60
61 func toggleTruncateLines() {
62 truncatesLines.toggle()
63 onMessage?(truncatesLines ? "Truncate long lines enabled" : "Truncate long lines disabled")
64 }
65
66 // MARK: - Clicks
67
68 /// A click on a heading's stars cycles it; a click on a checkbox toggles it.
69 func click(at offset: Int) -> Bool {
70 guard isOrg else { return false }
71 var node = document.tree.root
72 while let child = node.child(containing: offset) ?? node.child(containing: offset - 1) {
73 node = child
74 if child.kind == .heading || child.kind == .item { break }
75 }
76 let tokens = node.tokens.filter { $0.range.lowerBound <= offset && offset <= $0.range.upperBound }
77 if node.kind == .heading, tokens.contains(where: { $0.kind == .stars }) {
78 setCaret(node.range.lowerBound)
79 _ = cycleAtCaret()
80 return true
81 }
82 if node.kind == .item, let box = tokens.first(where: { $0.kind == .checkbox }) {
83 setCaret(box.range.lowerBound)
84 run("org.checkbox.toggle")
85 return true
86 }
87 return false
88 }
89}
90
91extension OrgEditor: NSTextViewDelegate {
92 public func textViewDidChangeSelection(_ notification: Notification) {
93 // Another view's caret moving (a mapped selection, say) is its own business.
94 guard (notification.object as AnyObject?) === textView else { return }
95 selectionChanged()
96 if completionPopup != nil, let popup = completionPopup, textView.selectedRange().location != popup.completion.range.upperBound {
97 refreshCompletion()
98 }
99 }
100
101 public func textDidChange(_ notification: Notification) {
102 textTyped()
103 }
104
105 /// No spelling marks in code, links, dates, tags or keywords.
106 public func textView(_ textView: NSTextView, shouldSetSpellingState value: Int, range affectedCharRange: NSRange) -> Int {
107 guard value != 0 else { return value }
108 let range = affectedCharRange.location..<NSMaxRange(affectedCharRange)
109 return spellSkipped().contains { $0.overlaps(range) } ? 0 : value
110 }
111}
112#endif