From c6c822f5396655fd298f8a8e473f1e5a568ad13d Mon Sep 17 00:00:00 2001 From: Ezequiel Rosas Date: Wed, 2 Aug 2017 14:17:02 -0500 Subject: [PATCH] Refactor NeoVimView.keydown --- SwiftNeoVim/NeoVimView+Key.swift | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/SwiftNeoVim/NeoVimView+Key.swift b/SwiftNeoVim/NeoVimView+Key.swift index bd060231..2ad7ed0d 100644 --- a/SwiftNeoVim/NeoVimView+Key.swift +++ b/SwiftNeoVim/NeoVimView+Key.swift @@ -27,22 +27,19 @@ extension NeoVimView { ? event.charactersIgnoringModifiers!.lowercased() : event.charactersIgnoringModifiers! - if KeyUtils.isSpecial(key: charsIgnoringModifiers) { - if let vimModifiers = self.vimModifierFlags(modifierFlags) { - self.agent.vimInput( - self.wrapNamedKeys(vimModifiers + KeyUtils.namedKeyFrom(key: charsIgnoringModifiers)) - ) - } else { - self.agent.vimInput(self.wrapNamedKeys(KeyUtils.namedKeyFrom(key: charsIgnoringModifiers))) - } - } else { - if let vimModifiers = self.vimModifierFlags(modifierFlags) { - self.agent.vimInput(self.wrapNamedKeys(vimModifiers + charsIgnoringModifiers)) - } else { - self.agent.vimInput(self.vimPlainString(chars)) - } - } + let flags = self.vimModifierFlags(modifierFlags) ?? "" + let isNamedKey = KeyUtils.isSpecial(key: charsIgnoringModifiers) + let isPlain = flags.isEmpty && !isNamedKey + let isWrapNeeded = !isPlain + let namedChars = isNamedKey + ? KeyUtils.namedKeyFrom(key: charsIgnoringModifiers) + : charsIgnoringModifiers + let finalInput = isWrapNeeded + ? self.wrapNamedKeys(flags + namedChars) + : self.vimPlainString(chars) + + self.agent.vimInput(finalInput) self.keyDownDone = true }