When ( [ { ' or " is inserted, insert the matching character after the cursor

This commit is contained in:
Nathan Sobo 2012-07-27 13:08:43 -06:00
parent 87f4d8c6e9
commit dc662ed071
2 changed files with 31 additions and 4 deletions

View File

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

View File

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