diff --git a/spec/app/edit-session-spec.coffee b/spec/app/edit-session-spec.coffee index 2bdc18d68..cfcc7e61e 100644 --- a/spec/app/edit-session-spec.coffee +++ b/spec/app/edit-session-spec.coffee @@ -1056,12 +1056,25 @@ describe "EditSession", -> expect(buffer.lineForRow(1)).toBe ' var sort = function(it) {' describe ".insertTab()", -> - describe "if 'softTabs' is true (the default)", -> - it "inserts the value of 'tabText' into the buffer", -> - tabRegex = new RegExp("^#{editSession.tabText}") - expect(buffer.lineForRow(0)).not.toMatch(tabRegex) - editSession.insertTab() - expect(buffer.lineForRow(0)).toMatch(tabRegex) + describe "when nothing is selected", -> + describe "if 'softTabs' is true (the default)", -> + it "inserts the value of 'tabText' into the buffer", -> + tabRegex = new RegExp("^#{editSession.tabText}") + expect(buffer.lineForRow(0)).not.toMatch(tabRegex) + editSession.insertTab() + expect(buffer.lineForRow(0)).toMatch(tabRegex) + + describe "when auto-indent is on and there is no text after the cursor", -> + it "properly indents the line", -> + buffer.insert([7, 0], " \n") + editSession.tabText = " " + editSession.setCursorBufferPosition [7, 2] + editSession.setAutoIndent(true) + editSession.insertTab() + buffer.logLines() + expect(buffer.lineForRow(7)).toMatch /^\s+$/ + expect(buffer.lineForRow(7).length).toBe 6 + expect(editSession.getCursorBufferPosition()).toEqual [7, 6] describe "if editSession.softTabs is false", -> it "inserts a tab character into the buffer", -> diff --git a/src/app/edit-session.coffee b/src/app/edit-session.coffee index 6f2ececfe..cc2556d2a 100644 --- a/src/app/edit-session.coffee +++ b/src/app/edit-session.coffee @@ -131,7 +131,12 @@ class EditSession insertTab: -> if @getSelection().isEmpty() - if @softTabs + whitespaceMatch = @lineForBufferRow(@getCursorBufferPosition().row).match /^\s*$/ + if @autoIndent and whitespaceMatch + indentation = @indentationForRow(@getCursorBufferPosition().row) + @getSelection().selectLine() + @insertText(indentation) + else if @softTabs @insertText(@tabText) else @insertText('\t') @@ -225,6 +230,9 @@ class EditSession largestFoldStartingAtScreenRow: (screenRow) -> @displayBuffer.largestFoldStartingAtScreenRow(screenRow) + indentationForRow: (row) -> + @tokenizedBuffer.indentationForRow(row) + autoIndentTextAfterBufferPosition: (text, bufferPosition) -> return { text } unless @autoIndent @tokenizedBuffer.autoIndentTextAfterBufferPosition(text, bufferPosition) diff --git a/src/app/tokenized-buffer.coffee b/src/app/tokenized-buffer.coffee index 80649b8e4..15dc42a02 100644 --- a/src/app/tokenized-buffer.coffee +++ b/src/app/tokenized-buffer.coffee @@ -48,6 +48,11 @@ class TokenizedBuffer else null + indentationForRow: (row) -> + state = @stateForRow(row) + previousRowText = @buffer.lineForRow(row - 1) + @aceMode.getNextLineIndent(state, previousRowText, @tabText) + autoIndentTextAfterBufferPosition: (text, bufferPosition) -> { row, column} = bufferPosition state = @stateForRow(row)