From 5184b90365e312c7ae2bb2b3b35f3723be4a9fbc Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Mon, 16 Jul 2012 10:25:28 -0700 Subject: [PATCH 1/4] Hitting tab on a line (containing only whitespace) will auto indent the line and set the cursor to the end --- spec/app/edit-session-spec.coffee | 25 +++++++++++++++++++------ src/app/edit-session.coffee | 10 +++++++++- src/app/tokenized-buffer.coffee | 5 +++++ 3 files changed, 33 insertions(+), 7 deletions(-) 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) From 453af489d914710e85c3f04d7bb2c1a808eb73d2 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Mon, 16 Jul 2012 10:28:52 -0700 Subject: [PATCH 2/4] un-log :shit: --- spec/app/edit-session-spec.coffee | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/app/edit-session-spec.coffee b/spec/app/edit-session-spec.coffee index cfcc7e61e..7eff8ae31 100644 --- a/spec/app/edit-session-spec.coffee +++ b/spec/app/edit-session-spec.coffee @@ -1071,7 +1071,6 @@ describe "EditSession", -> 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] From 95c3ea1b74a7e64e08bbb6dc59afcf4cba485d6f Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Mon, 16 Jul 2012 10:29:14 -0700 Subject: [PATCH 3/4] Rename insert tab event to indent --- spec/app/edit-session-spec.coffee | 10 +++++----- spec/extensions/snippets-spec.coffee | 2 +- src/app/edit-session.coffee | 2 +- src/app/editor.coffee | 4 ++-- src/app/keymaps/editor.coffee | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/spec/app/edit-session-spec.coffee b/spec/app/edit-session-spec.coffee index 7eff8ae31..e529b1c56 100644 --- a/spec/app/edit-session-spec.coffee +++ b/spec/app/edit-session-spec.coffee @@ -1055,13 +1055,13 @@ describe "EditSession", -> editSession.deleteToEndOfWord() expect(buffer.lineForRow(1)).toBe ' var sort = function(it) {' - describe ".insertTab()", -> + 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.insertTab() + editSession.indent() expect(buffer.lineForRow(0)).toMatch(tabRegex) describe "when auto-indent is on and there is no text after the cursor", -> @@ -1070,7 +1070,7 @@ describe "EditSession", -> editSession.tabText = " " editSession.setCursorBufferPosition [7, 2] editSession.setAutoIndent(true) - editSession.insertTab() + editSession.indent() expect(buffer.lineForRow(7)).toMatch /^\s+$/ expect(buffer.lineForRow(7).length).toBe 6 expect(editSession.getCursorBufferPosition()).toEqual [7, 6] @@ -1079,12 +1079,12 @@ describe "EditSession", -> 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 cc2556d2a..2a9718ae4 100644 --- a/src/app/edit-session.coffee +++ b/src/app/edit-session.coffee @@ -129,7 +129,7 @@ class EditSession @moveCursorToEndOfLine() @insertNewline() - insertTab: -> + indent: -> if @getSelection().isEmpty() whitespaceMatch = @lineForBufferRow(@getCursorBufferPosition().row).match /^\s*$/ if @autoIndent and whitespaceMatch diff --git a/src/app/editor.coffee b/src/app/editor.coffee index 4d2b9f3b9..c71be0e67 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' From ac04a8ed66ab09b57c3041fefa9fcf9dd359520d Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Mon, 16 Jul 2012 10:45:38 -0700 Subject: [PATCH 4/4] Allow additional indentation after line has been auto-indented --- spec/app/edit-session-spec.coffee | 10 ++++++++++ src/app/edit-session.coffee | 7 +++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/spec/app/edit-session-spec.coffee b/spec/app/edit-session-spec.coffee index e529b1c56..a3ecf9f2b 100644 --- a/spec/app/edit-session-spec.coffee +++ b/spec/app/edit-session-spec.coffee @@ -1075,6 +1075,16 @@ describe "EditSession", -> 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) diff --git a/src/app/edit-session.coffee b/src/app/edit-session.coffee index 2a9718ae4..2632abc31 100644 --- a/src/app/edit-session.coffee +++ b/src/app/edit-session.coffee @@ -134,8 +134,11 @@ class EditSession whitespaceMatch = @lineForBufferRow(@getCursorBufferPosition().row).match /^\s*$/ if @autoIndent and whitespaceMatch indentation = @indentationForRow(@getCursorBufferPosition().row) - @getSelection().selectLine() - @insertText(indentation) + if indentation.length > whitespaceMatch[0].length + @getSelection().selectLine() + @insertText(indentation) + else + @insertText(@tabText) else if @softTabs @insertText(@tabText) else