diff --git a/spec/atom/editor-spec.coffee b/spec/atom/editor-spec.coffee index 0f377433d..1905ceb74 100644 --- a/spec/atom/editor-spec.coffee +++ b/spec/atom/editor-spec.coffee @@ -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 diff --git a/src/atom/buffer.coffee b/src/atom/buffer.coffee index ea9d961c4..1e94010cc 100644 --- a/src/atom/buffer.coffee +++ b/src/atom/buffer.coffee @@ -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) diff --git a/src/atom/editor.coffee b/src/atom/editor.coffee index 8aaad5663..e9f3895c5 100644 --- a/src/atom/editor.coffee +++ b/src/atom/editor.coffee @@ -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())