Outdent works with Ace's Mode classes

This commit is contained in:
Corey Johnson 2012-03-06 11:08:46 -08:00
parent 79edb3fcdf
commit 668022fb3b
3 changed files with 36 additions and 4 deletions

View File

@ -441,13 +441,23 @@ describe "Editor", ->
expect(editor.getSelectedText()).toBe " if (items.length <= 1) return items;"
describe "auto indent/outdent", ->
beforeEach ->
editor.autoIndent = true
describe "when newline is inserted", ->
it "indents cursor based on the indentation of previous line", ->
editor.setCursorBufferPosition([4, 29])
editor.insertText("\n")
expect(editor.buffer.getLine(5)).toEqual(" ")
describe "when text that closes a scope entered", ->
it "outdents the text", ->
editor.setCursorBufferPosition([1, 30])
editor.insertText("\n")
expect(editor.buffer.getLine(2)).toEqual(" ")
editor.insertText("}")
expect(editor.buffer.getLine(2)).toEqual(" }")
describe "selection", ->
selection = null

View File

@ -105,5 +105,14 @@ class Buffer
@mode = new (require("ace/mode/#{modeName}").Mode)
# API to match Ace's Document class
findMatchingBracket: ({row, column}) ->
{row: 0, column: 0}
replace:(range, text) ->
# Only used to outdent lines
start = range.start
end = {row: range.start.row, column: range.start.column + atom.tabText.length}
@change(new Range(start, end), "")
_.extend(Buffer.prototype, EventEmitter)

View File

@ -31,6 +31,8 @@ class Editor extends View
highlighter: null
lineWrapper: null
undoManager: null
autoIndent: null
initialize: () ->
requireStylesheet 'editor.css'
@ -39,6 +41,7 @@ class Editor extends View
@buildCursorAndSelection()
@handleEvents()
@setBuffer(new Buffer)
@autoIndent = false
bindKeys: ->
window.keymap.bindKeys '*:not(.editor *)',
@ -301,14 +304,24 @@ class Editor extends View
@selection.selectToBufferPosition(position)
insertText: (text) ->
if not @autoIndent
@selection.insertText(text)
return
state = @lineWrapper.lineForScreenRow(@getCursorRow()).state
shouldOutdent = false
if text[0] == "\n"
tab = " "
state = @lineWrapper.lineForScreenRow(@getRow).state
indent = @buffer.mode.getNextLineIndent(state, @getCurrentLine(), tab)
indent = @buffer.mode.getNextLineIndent(state, @getCurrentLine(), atom.tabText)
text = text[0] + indent + text[1..]
else if @buffer.mode.checkOutdent(state, @getCurrentLine(), text)
shouldOutdent = true
@selection.insertText(text)
if shouldOutdent
@buffer.mode.autoOutdent(state, @buffer, @getCursorRow())
cutSelection: -> @selection.cut()
copySelection: -> @selection.copy()
paste: -> @selection.insertText($native.readFromPasteboard())