From ed12f66b1397febc30571afab182d638abcc2f45 Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Wed, 1 Aug 2012 13:20:34 -0700 Subject: [PATCH] Do not insert matching bracket unless the following character is whitespace --- spec/app/language-mode-spec.coffee | 25 +++++++++++++++++-------- src/app/language-mode.coffee | 6 +++--- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/spec/app/language-mode-spec.coffee b/spec/app/language-mode-spec.coffee index cdc506295..3850e6e10 100644 --- a/spec/app/language-mode-spec.coffee +++ b/spec/app/language-mode-spec.coffee @@ -14,6 +14,17 @@ describe "LanguageMode", -> { buffer, languageMode } = editSession describe "matching character insertion", -> + beforeEach -> + editSession.buffer.setText("") + + describe "when there is non-whitespace after the cursor", -> + it "does not insert a matching bracket", -> + editSession.buffer.setText("ab") + editSession.setCursorBufferPosition([0, 1]) + editSession.insertText("(") + + expect(editSession.buffer.getText()).toBe "a(b" + describe "when there are multiple cursors", -> it "inserts ) at each cursor", -> editSession.buffer.setText("()\nab\n[]\n12") @@ -51,14 +62,12 @@ describe "LanguageMode", -> expect(buffer.lineForRow(0)).toMatch /^''/ describe "when ) is inserted before a )", -> - describe "when there is whitespace after the )", -> - it "moves the cursor one column to the right instead of inserting a new )", -> - editSession.buffer.setText("") - editSession.insertText '() ' - editSession.setCursorBufferPosition([0, 1]) - editSession.insertText ')' - expect(buffer.lineForRow(0)).toBe "() " - expect(editSession.getCursorBufferPosition().column).toBe 2 + it "moves the cursor one column to the right instead of inserting a new )", -> + editSession.insertText '() ' + editSession.setCursorBufferPosition([0, 1]) + editSession.insertText ')' + expect(buffer.lineForRow(0)).toBe "() " + expect(editSession.getCursorBufferPosition().column).toBe 2 describe "javascript", -> beforeEach -> diff --git a/src/app/language-mode.coffee b/src/app/language-mode.coffee index a456c05b6..4f90fac6e 100644 --- a/src/app/language-mode.coffee +++ b/src/app/language-mode.coffee @@ -20,12 +20,12 @@ class LanguageMode return true if @editSession.hasMultipleCursors() cursorBufferPosition = @editSession.getCursorBufferPosition() - nextCharachter = @editSession.getTextInBufferRange([cursorBufferPosition, cursorBufferPosition.add([0, 1])]) + nextCharacter = @editSession.getTextInBufferRange([cursorBufferPosition, cursorBufferPosition.add([0, 1])]) - if @isCloseBracket(text) and text == nextCharachter + if @isCloseBracket(text) and text == nextCharacter @editSession.moveCursorRight() false - else if matchingCharacter = @matchingCharacters[text] + else if /^\s*$/.test(nextCharacter) and matchingCharacter = @matchingCharacters[text] @editSession.insertText text + matchingCharacter @editSession.moveCursorLeft() false