Skip to content

Commit 51b3353

Browse files
tom-ludwigaustincondiff
authored andcommitted
Handle Tab and Shift+Tab
1 parent c24c533 commit 51b3353

1 file changed

Lines changed: 33 additions & 12 deletions

File tree

Sources/CodeEditSourceEditor/Controller/TextViewController+LoadView.swift

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,22 +115,43 @@ extension TextViewController {
115115
}
116116
self.localEvenMonitor = NSEvent.addLocalMonitorForEvents(matching: .keyDown) { [weak self] event in
117117
guard self?.view.window?.firstResponder == self?.textView else { return event }
118-
let commandKey = NSEvent.ModifierFlags.command.rawValue
118+
119+
let tabKey: UInt16 = 0x30
119120
let modifierFlags = event.modifierFlags.intersection(.deviceIndependentFlagsMask).rawValue
120121

121-
switch (modifierFlags, event.charactersIgnoringModifiers) {
122-
case (commandKey, "/"):
123-
self?.handleCommandSlash()
124-
return nil
125-
case (commandKey, "["):
126-
self?.handleIndent(inwards: true)
127-
return nil
128-
case (commandKey, "]"):
129-
self?.handleIndent()
122+
if event.keyCode == tabKey {
123+
self?.handleTab(modifierFalgs: modifierFlags)
130124
return nil
131-
default:
132-
return event
125+
} else {
126+
return self?.handleCommand(event: event, modifierFlags: modifierFlags)
133127
}
134128
}
135129
}
130+
func handleCommand(event: NSEvent, modifierFlags: UInt) -> NSEvent? {
131+
let commandKey = NSEvent.ModifierFlags.command.rawValue
132+
133+
switch (modifierFlags, event.charactersIgnoringModifiers) {
134+
case (commandKey, "/"):
135+
handleCommandSlash()
136+
return nil
137+
case (commandKey, "["):
138+
handleIndent(inwards: true)
139+
return nil
140+
case (commandKey, "]"):
141+
handleIndent()
142+
return nil
143+
case (_, _):
144+
return event
145+
}
146+
}
147+
148+
func handleTab(modifierFalgs: UInt) {
149+
let shiftKey = NSEvent.ModifierFlags.shift.rawValue
150+
151+
if modifierFalgs == shiftKey {
152+
handleIndent(inwards: true)
153+
} else {
154+
handleIndent()
155+
}
156+
}
136157
}

0 commit comments

Comments
 (0)