diff --git a/spec/app/edit-session-spec.coffee b/spec/app/edit-session-spec.coffee index 17d6f78a0..b708e32b8 100644 --- a/spec/app/edit-session-spec.coffee +++ b/spec/app/edit-session-spec.coffee @@ -1096,24 +1096,46 @@ describe "EditSession", -> editSession.deleteToEndOfWord() 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 ".indent()", -> + 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.indent() + 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.indent() + expect(buffer.lineForRow(7)).toMatch /^\s+$/ + expect(buffer.lineForRow(7).length).toBe 6 + expect(editSession.getCursorBufferPosition()).toEqual [7, 6] + + it "allows for additional indentation if the cursor is beyond the proper indentation point", -> + buffer.insert([7, 0], " \n") + editSession.tabText = " " + editSession.setCursorBufferPosition [7, 6] + editSession.setAutoIndent(true) + editSession.indent() + expect(buffer.lineForRow(7)).toMatch /^\s+$/ + expect(buffer.lineForRow(7).length).toBe 8 + expect(editSession.getCursorBufferPosition()).toEqual [7, 8] describe "if editSession.softTabs is false", -> it "inserts a tab character into the buffer", -> editSession.setSoftTabs(false) expect(buffer.lineForRow(0)).not.toMatch(/^\t/) - editSession.insertTab() + editSession.indent() expect(buffer.lineForRow(0)).toMatch(/^\t/) expect(editSession.getCursorBufferPosition()).toEqual [0, 1] expect(editSession.getCursorScreenPosition()).toEqual [0, editSession.tabText.length] - editSession.insertTab() + editSession.indent() expect(buffer.lineForRow(0)).toMatch(/^\t\t/) expect(editSession.getCursorBufferPosition()).toEqual [0, 2] expect(editSession.getCursorScreenPosition()).toEqual [0, editSession.tabText.length * 2] diff --git a/spec/extensions/snippets-spec.coffee b/spec/extensions/snippets-spec.coffee index f56cf7a2e..68d9e0ed7 100644 --- a/spec/extensions/snippets-spec.coffee +++ b/spec/extensions/snippets-spec.coffee @@ -145,7 +145,7 @@ describe "Snippets extension", -> editor.insertText("xte") expect(editor.getCursorScreenPosition()).toEqual [0, 3] - editor.trigger 'tab' + editor.trigger keydownEvent('tab', target: editor[0]) expect(buffer.lineForRow(0)).toBe "xte var quicksort = function () {" expect(editor.getCursorScreenPosition()).toEqual [0, 5] diff --git a/src/app/edit-session.coffee b/src/app/edit-session.coffee index 2d457b28b..7786b7ac1 100644 --- a/src/app/edit-session.coffee +++ b/src/app/edit-session.coffee @@ -124,9 +124,17 @@ class EditSession @moveCursorToEndOfLine() @insertNewline() - insertTab: -> + indent: -> if @getSelection().isEmpty() - if @softTabs + whitespaceMatch = @lineForBufferRow(@getCursorBufferPosition().row).match /^\s*$/ + if @autoIndent and whitespaceMatch + indentation = @indentationForRow(@getCursorBufferPosition().row) + if indentation.length > whitespaceMatch[0].length + @getSelection().selectLine() + @insertText(indentation) + else + @insertText(@tabText) + else if @softTabs @insertText(@tabText) else @insertText('\t') @@ -220,6 +228,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/editor.coffee b/src/app/editor.coffee index e9a0b926d..e0ed5fa1d 100644 --- a/src/app/editor.coffee +++ b/src/app/editor.coffee @@ -105,7 +105,7 @@ class Editor extends View 'select-down': @selectDown 'select-word': @selectWord 'newline': @insertNewline - 'tab': @insertTab + 'indent': @indent 'indent-selected-rows': @indentSelectedRows 'outdent-selected-rows': @outdentSelectedRows 'backspace': @backspace @@ -211,7 +211,7 @@ class Editor extends View insertText: (text) -> @activeEditSession.insertText(text) insertNewline: -> @activeEditSession.insertNewline() insertNewlineBelow: -> @activeEditSession.insertNewlineBelow() - insertTab: -> @activeEditSession.insertTab() + indent: -> @activeEditSession.indent() indentSelectedRows: -> @activeEditSession.indentSelectedRows() outdentSelectedRows: -> @activeEditSession.outdentSelectedRows() cutSelection: -> @activeEditSession.cutSelectedText() diff --git a/src/app/keymaps/editor.coffee b/src/app/keymaps/editor.coffee index 4ff9ca96a..6bdce72c3 100644 --- a/src/app/keymaps/editor.coffee +++ b/src/app/keymaps/editor.coffee @@ -7,7 +7,7 @@ window.keymap.bindKeys '.editor', 'meta-a': 'select-all' 'enter': 'newline' 'meta-enter': 'newline-below' - 'tab': 'tab' + 'tab': 'indent' 'backspace': 'backspace' 'delete': 'delete' 'meta-x': 'cut' 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)