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) ->