From dc662ed0711d19223a0461cadb3d9166db80cd5b Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Fri, 27 Jul 2012 13:08:43 -0600 Subject: [PATCH] When ( [ { ' or " is inserted, insert the matching character after the cursor --- spec/app/language-mode-spec.coffee | 20 ++++++++++++++++++++ src/app/language-mode.coffee | 15 +++++++++++---- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/spec/app/language-mode-spec.coffee b/spec/app/language-mode-spec.coffee index 915157c96..60d64f1df 100644 --- a/spec/app/language-mode-spec.coffee +++ b/spec/app/language-mode-spec.coffee @@ -19,6 +19,26 @@ describe "LanguageMode", -> editSession.insertText '(' expect(buffer.lineForRow(0)).toMatch /^\(\)/ + describe "when [ is inserted", -> + it "inserts a matching ] following the cursor", -> + editSession.insertText '[' + expect(buffer.lineForRow(0)).toMatch /^\[\]/ + + describe "when { is inserted", -> + it "inserts a matching ) following the cursor", -> + editSession.insertText '{' + expect(buffer.lineForRow(0)).toMatch /^\{\}/ + + describe "when \" is inserted", -> + it "inserts a matching \" following the cursor", -> + editSession.insertText '"' + expect(buffer.lineForRow(0)).toMatch /^""/ + + describe "when ' is inserted", -> + it "inserts a matching ' following the cursor", -> + editSession.insertText "'" + expect(buffer.lineForRow(0)).toMatch /^''/ + describe "javascript", -> beforeEach -> editSession = fixturesProject.buildEditSessionForPath('sample.js', autoIndent: false) diff --git a/src/app/language-mode.coffee b/src/app/language-mode.coffee index 8dcab80da..b31116df6 100644 --- a/src/app/language-mode.coffee +++ b/src/app/language-mode.coffee @@ -4,15 +4,22 @@ _ = require 'underscore' module.exports = class LanguageMode + matchingCharacters: + '(': ')' + '[': ']' + '{': '}' + '"': '"' + "'": "'" + constructor: (@editSession) -> @buffer = @editSession.buffer @aceMode = @requireAceMode() @aceAdaptor = new AceAdaptor(@editSession) - _.adviseBefore @editSession, 'insertText', (text) -> - if text == '(' - @insertText '()' - @moveCursorLeft() + _.adviseBefore @editSession, 'insertText', (text) => + if matchingCharacter = @matchingCharacters[text] + @editSession.insertText text + matchingCharacter + @editSession.moveCursorLeft() false requireAceMode: (fileExtension) ->