From afceacefc818603fe36c8bf2e9334fc8e538e67f Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Tue, 10 Apr 2012 10:18:39 -0700 Subject: [PATCH] meta-[ outdents selected lines --- spec/app/selection-spec.coffee | 37 +++++++++++++++++++++++++++--- src/app/composite-selection.coffee | 3 +++ src/app/editor.coffee | 5 ++-- src/app/keymaps/editor.coffee | 1 + src/app/selection.coffee | 8 +++++++ 5 files changed, 49 insertions(+), 5 deletions(-) diff --git a/spec/app/selection-spec.coffee b/spec/app/selection-spec.coffee index 5e07bcc1b..4e3df5876 100644 --- a/spec/app/selection-spec.coffee +++ b/spec/app/selection-spec.coffee @@ -222,23 +222,54 @@ describe "Selection", -> tabLength = editor.tabText.length describe "when nothing is selected", -> - it "indents line cursor is and retains selection", -> + it "indents line and retains selection", -> selection.setBufferRange new Range([0,3], [0,3]) selection.indentSelectedRows() expect(editor.buffer.lineForRow(0)).toBe "#{editor.tabText}var quicksort = function () {" expect(selection.getBufferRange()).toEqual [[0, 3 + tabLength], [0, 3 + tabLength]] describe "when one line is selected", -> - it "indents line selection and retains selection", -> + it "indents line and retains selection", -> selection.setBufferRange new Range([0,4], [0,14]) selection.indentSelectedRows() expect(editor.buffer.lineForRow(0)).toBe "#{editor.tabText}var quicksort = function () {" expect(selection.getBufferRange()).toEqual [[0, 4 + tabLength], [0, 14 + tabLength]] describe "when multiple lines are selected", -> - it "indents selected lines with text and retains selection", -> + it "indents selected lines (that are not empty) and retains selection", -> selection.setBufferRange new Range([9,1], [11,15]) selection.indentSelectedRows() expect(editor.buffer.lineForRow(9)).toBe " };" expect(editor.buffer.lineForRow(10)).toBe "" expect(editor.buffer.lineForRow(11)).toBe " return sort(Array.apply(this, arguments));" + expect(selection.getBufferRange()).toEqual [[9, 1 + tabLength], [11, 15 + tabLength]] + + describe ".outdentSelectedRows()", -> + tabLength = null + + beforeEach -> + editor.tabText = " " + tabLength = editor.tabText.length + + describe "when nothing is selected", -> + it "outdents line and retains selection", -> + selection.setBufferRange new Range([1,3], [1,3]) + selection.outdentSelectedRows() + expect(editor.buffer.lineForRow(1)).toBe "var sort = function(items) {" + expect(selection.getBufferRange()).toEqual [[1, 3 - tabLength], [1, 3 - tabLength]] + + describe "when one line is selected", -> + it "outdents line and retains selection", -> + selection.setBufferRange new Range([1,4], [1,14]) + selection.outdentSelectedRows() + expect(editor.buffer.lineForRow(1)).toBe "var sort = function(items) {" + expect(selection.getBufferRange()).toEqual [[1, 4 - tabLength], [1, 14 - tabLength]] + + describe "when multiple lines are selected", -> + it "outdents selected lines and retains selection", -> + selection.setBufferRange new Range([0,1], [3,15]) + selection.outdentSelectedRows() + expect(editor.buffer.lineForRow(0)).toBe "var quicksort = function () {" + expect(editor.buffer.lineForRow(1)).toBe "var sort = function(items) {" + expect(editor.buffer.lineForRow(2)).toBe " if (items.length <= 1) return items;" + expect(selection.getBufferRange()).toEqual [[0, 1], [3, 15 - tabLength]] diff --git a/src/app/composite-selection.coffee b/src/app/composite-selection.coffee index 765fae369..ff8f6afa7 100644 --- a/src/app/composite-selection.coffee +++ b/src/app/composite-selection.coffee @@ -91,6 +91,9 @@ class CompositeSeleciton insertText: (text) -> @mutateSelectedText (selection) -> selection.insertText(text) + outdentSelectedRows: -> + @mutateSelectedText (selection) -> selection.outdentSelectedRows() + indentSelectedRows: -> @mutateSelectedText (selection) -> selection.indentSelectedRows() diff --git a/src/app/editor.coffee b/src/app/editor.coffee index 32e3be710..971c239e9 100644 --- a/src/app/editor.coffee +++ b/src/app/editor.coffee @@ -67,6 +67,7 @@ class Editor extends View @on 'newline', => @insertText("\n") @on 'tab', => @insertTab() @on 'indent-selected-rows', => @indentSelectedRows() + @on 'outdent-selected-rows', => @outdentSelectedRows() @on 'backspace', => @backspace() @on 'backspace-to-beginning-of-word', => @backspaceToBeginningOfWord() @on 'delete', => @delete() @@ -425,8 +426,8 @@ class Editor extends View else @compositeSelection.insertText('\t') - indentSelectedRows: -> - @compositeSelection.indentSelectedRows() + indentSelectedRows: -> @compositeSelection.indentSelectedRows() + outdentSelectedRows: -> @compositeSelection.outdentSelectedRows() cutSelection: -> @compositeSelection.cut() copySelection: -> @compositeSelection.copy() diff --git a/src/app/keymaps/editor.coffee b/src/app/keymaps/editor.coffee index 35bf5677c..91946d070 100644 --- a/src/app/keymaps/editor.coffee +++ b/src/app/keymaps/editor.coffee @@ -29,4 +29,5 @@ window.keymap.bindKeys '.editor', 'alt-meta-right': 'split-right' 'alt-meta-up': 'split-up' 'alt-meta-down': 'split-down' + 'meta-[': 'outdent-selected-rows' 'meta-]': 'indent-selected-rows' \ No newline at end of file diff --git a/src/app/selection.coffee b/src/app/selection.coffee index 623418664..7462d4728 100644 --- a/src/app/selection.coffee +++ b/src/app/selection.coffee @@ -110,6 +110,14 @@ class Selection extends View for row in [range.start.row..range.end.row] @editor.buffer.insert([row, 0], @editor.tabText) unless @editor.buffer.lineLengthForRow(row) == 0 + outdentSelectedRows: -> + range = @getBufferRange() + buffer = @editor.buffer + leadingTabRegex = new RegExp("^#{@editor.tabText}") + for row in [range.start.row..range.end.row] + if leadingTabRegex.test buffer.lineForRow(row) + buffer.delete [[row, 0], [row, @editor.tabText.length]] + autoIndentText: (text) -> if @editor.autoIndent mode = @editor.getCurrentMode()