From 783e9446e99443a47475ccb001f3b1c2d560dbb6 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 10 Jan 2012 19:16:46 -0800 Subject: [PATCH] Fix bug: Don't call handleKeyEvent with undefined event Ace triggers key handlers for both keydown (onCommandKey) and input (onTextInput) events. Input events don't pass the event, which was blowing up the keymap. --- spec/atom/editor-spec.coffee | 75 +++++++++++++++++++++--------------- src/atom/editor.coffee | 5 +-- 2 files changed, 47 insertions(+), 33 deletions(-) diff --git a/spec/atom/editor-spec.coffee b/spec/atom/editor-spec.coffee index 70297b9ac..3294c7d3b 100644 --- a/spec/atom/editor-spec.coffee +++ b/spec/atom/editor-spec.coffee @@ -111,47 +111,62 @@ describe "Editor", -> expect(fs.exists(selectedFilePath)).toBeFalsy() - describe "when a keydown event is handled by the ace editor", -> - returnValue = null + describe "key event handling", -> handler = null - event = null + returnValue = null beforeEach -> - event = keydownEvent 'x' - spyOn(event, 'stopPropagation') + handler = + handleKeyEvent: jasmine.createSpy('handleKeyEvent').andCallFake -> + returnValue + + describe "when onCommandKey is called on the aceEditor (triggered by a keydown event on the textarea)", -> + event = null - describe "when no key event handler has been assigned", -> beforeEach -> - expect(editor.keyEventHandler).toBeNull() + event = keydownEvent 'x' + spyOn(event, 'stopPropagation') - it "handles the event without crashing", -> - editor.aceEditor.onCommandKey event, 0, event.which + describe "when no key event handler has been assigned", -> + beforeEach -> + expect(editor.keyEventHandler).toBeNull() - describe "when a key event handler has been assigned", -> - beforeEach -> - handler = { - handleKeyEvent: jasmine.createSpy('handleKeyEvent').andCallFake -> - returnValue - } + it "handles the event without crashing", -> + editor.aceEditor.onCommandKey event, 0, event.which + + describe "when a key event handler has been assigned", -> + beforeEach -> + editor.keyEventHandler = handler + + it "asks the key event handler to handle the event", -> + editor.aceEditor.onCommandKey event, 0, event.which + expect(handler.handleKeyEvent).toHaveBeenCalled() + + describe "if the atom key event handler returns false, indicating that it did not handle the event", -> + beforeEach -> + returnValue = false + + it "does not stop the propagation of the event, allowing Ace to handle it as normal", -> + editor.aceEditor.onCommandKey event, 0, event.which + expect(event.stopPropagation).not.toHaveBeenCalled() + + describe "if the atom key event handler returns true, indicating that it handled the event", -> + beforeEach -> + returnValue = true + + it "stops propagation of the event, so Ace does not attempt to handle it", -> + editor.aceEditor.onCommandKey event, 0, event.which + expect(event.stopPropagation).toHaveBeenCalled() + + describe "when onTextInput is called on the aceEditor (triggered by an input event)", -> + it "does not call handleKeyEvent on the key event handler, because there is no event", -> editor.keyEventHandler = handler - it "asks the key event handler to handle the event", -> - editor.aceEditor.onCommandKey event, 0, event.which - expect(handler.handleKeyEvent).toHaveBeenCalled() + editor.aceEditor.onTextInput("x", false) + + expect(handler.handleKeyEvent).not.toHaveBeenCalled() - describe "if the atom key event handler returns false, indicating that it did not handle the event", -> - beforeEach -> - returnValue = false - it "does not stop the propagation of the event, allowing Ace to handle it as normal", -> - editor.aceEditor.onCommandKey event, 0, event.which - expect(event.stopPropagation).not.toHaveBeenCalled() - describe "if the atom key event handler returns true, indicating that it handled the event", -> - beforeEach -> - returnValue = true - it "stops propagation of the event, so Ace does not attempt to handle it", -> - editor.aceEditor.onCommandKey event, 0, event.which - expect(event.stopPropagation).toHaveBeenCalled() diff --git a/src/atom/editor.coffee b/src/atom/editor.coffee index 7a0ebbd60..32bf01f11 100644 --- a/src/atom/editor.coffee +++ b/src/atom/editor.coffee @@ -35,10 +35,9 @@ class Editor extends Template buildAceEditor: -> @aceEditor = ace.edit this[0] @aceEditor.setTheme(require "ace/theme/twilight") - @aceEditor.setKeyboardHandler - handleKeyboard: (data, hashId, keyString, keyCode, e) => - if @keyEventHandler?.handleKeyEvent(e) + handleKeyboard: (data, hashId, keyString, keyCode, event) => + if event and @keyEventHandler?.handleKeyEvent(event) {command: {exec: ->}} else null