From f5ee676e5e25a62c9128d276a37db097ae21799d Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Wed, 9 Jan 2013 15:24:04 -0800 Subject: [PATCH] Pass autoIndent as an option flag Instead of querying EditSession for autoIndenting --- spec/app/edit-session-spec.coffee | 56 +++++++++++-------------------- src/app/edit-session.coffee | 22 ++++++++---- src/app/editor.coffee | 4 +-- src/app/selection.coffee | 8 ++--- 4 files changed, 41 insertions(+), 49 deletions(-) diff --git a/spec/app/edit-session-spec.coffee b/spec/app/edit-session-spec.coffee index 4b70927d9..f82d7b263 100644 --- a/spec/app/edit-session-spec.coffee +++ b/spec/app/edit-session-spec.coffee @@ -675,10 +675,7 @@ describe "EditSession", -> editSession.insertText('holy cow') expect(editSession.lineForScreenRow(2).fold).toBeUndefined() - describe "when auto-indent is enabled and the `autoIndent` option is true", -> - beforeEach -> - editSession.setAutoIndent(true) - + describe "when auto-indent is enabled", -> describe "when a single newline is inserted", -> describe "when the newline is inserted on a line that starts a new level of indentation", -> it "auto-indents the new line to one additional level of indentation beyond the preceding line", -> @@ -739,16 +736,13 @@ describe "EditSession", -> removeLeadingWhitespace = (text) -> text.replace(/^\s*/, '') describe "when the cursor is preceded only by whitespace", -> - describe "when auto-indent is enabled", -> - beforeEach -> - editSession.setAutoIndent(true) - + describe "when auto-indent is enabled", -> describe "when the cursor's current column is less than the suggested indent level", -> describe "when the indentBasis is inferred from the first line", -> it "indents all lines relative to the suggested indent", -> - editSession.insertText('\n xx') + editSession.insertText('\n xx', autoIndent: true) editSession.setCursorBufferPosition([3, 1]) - editSession.insertText(text, normalizeIndent: true) + editSession.insertText(text, normalizeIndent: true, autoIndent: true) expect(editSession.lineForBufferRow(3)).toBe " while (true) {" expect(editSession.lineForBufferRow(4)).toBe " foo();" @@ -759,7 +753,7 @@ describe "EditSession", -> it "indents all lines relative to the suggested indent", -> editSession.insertText('\n xx') editSession.setCursorBufferPosition([3, 1]) - editSession.insertText(removeLeadingWhitespace(text), normalizeIndent: true, indentBasis: 2) + editSession.insertText(removeLeadingWhitespace(text), normalizeIndent: true, indentBasis: 2, autoIndent: true) expect(editSession.lineForBufferRow(3)).toBe " while (true) {" expect(editSession.lineForBufferRow(4)).toBe " foo();" @@ -775,7 +769,7 @@ describe "EditSession", -> """ editSession.setCursorBufferPosition([1, 0]) - editSession.insertText(text, normalizeIndent: true) + editSession.insertText(text, normalizeIndent: true, autoIndent: true) expect(editSession.lineForBufferRow(1)).toBe "\t\t\twhile (true) {" expect(editSession.lineForBufferRow(2)).toBe "\t\t\t\tfoo();" @@ -791,7 +785,7 @@ describe "EditSession", -> """ editSession.setCursorBufferPosition([1, 0]) - editSession.insertText(text, normalizeIndent: true) + editSession.insertText(text, normalizeIndent: true, autoIndent: true) expect(editSession.lineForBufferRow(1)).toBe "\t\twhile (true) {" expect(editSession.lineForBufferRow(2)).toBe "\t\t\tfoo();" @@ -820,9 +814,6 @@ describe "EditSession", -> expect(editSession.lineForBufferRow(6)).toBe " bar();" describe "if auto-indent is disabled", -> - beforeEach -> - expect(editSession.autoIndent).toBeFalsy() - describe "when the indentBasis is inferred from the first line", -> it "always normalizes indented lines to the cursor's current indentation level", -> editSession.insertText('\n ') @@ -845,7 +836,6 @@ describe "EditSession", -> describe "when the cursor is preceded by non-whitespace characters", -> describe "when the indentBasis is inferred from the first line", -> it "normalizes the indentation level of all lines based on the level of the existing first line", -> - editSession.setAutoIndent(true) editSession.buffer.delete([[2, 0], [2, 2]]) editSession.insertText(text, normalizeIndent:true) @@ -856,7 +846,6 @@ describe "EditSession", -> describe "when an indentBasis is provided", -> it "normalizes the indentation level of all lines based on the level of the existing first line", -> - editSession.setAutoIndent(true) editSession.buffer.delete([[2, 0], [2, 2]]) editSession.insertText(removeLeadingWhitespace(text), normalizeIndent:true, indentBasis: 2) @@ -1311,8 +1300,7 @@ describe "EditSession", -> it "moves the cursor to the end of the leading whitespace and inserts enough whitespace to bring the line to the suggested level of indentaion", -> buffer.insert([5, 0], " \n") editSession.setCursorBufferPosition [5, 0] - editSession.setAutoIndent(true) - editSession.indent() + editSession.indent(autoIndent: true) expect(buffer.lineForRow(5)).toMatch /^\s+$/ expect(buffer.lineForRow(5).length).toBe 6 expect(editSession.getCursorBufferPosition()).toEqual [5, 6] @@ -1323,8 +1311,7 @@ describe "EditSession", -> editSession.softTabs = false buffer.insert([5, 0], "\t\n") editSession.setCursorBufferPosition [5, 0] - editSession.setAutoIndent(true) - editSession.indent() + editSession.indent(autoIndent: true) expect(buffer.lineForRow(5)).toMatch /^\t\t\t$/ expect(editSession.getCursorBufferPosition()).toEqual [5, 3] @@ -1333,8 +1320,7 @@ describe "EditSession", -> it "moves the cursor to the end of the leading whitespace and inserts 'tabLength' spaces into the buffer", -> buffer.insert([7, 0], " \n") editSession.setCursorBufferPosition [7, 2] - editSession.setAutoIndent(true) - editSession.indent() + editSession.indent(autoIndent: true) expect(buffer.lineForRow(7)).toMatch /^\s+$/ expect(buffer.lineForRow(7).length).toBe 8 expect(editSession.getCursorBufferPosition()).toEqual [7, 8] @@ -1345,8 +1331,7 @@ describe "EditSession", -> editSession.softTabs = false buffer.insert([7, 0], "\t\t\t\n") editSession.setCursorBufferPosition [7, 1] - editSession.setAutoIndent(true) - editSession.indent() + editSession.indent(autoIndent: true) expect(buffer.lineForRow(7)).toMatch /^\t\t\t\t$/ expect(editSession.getCursorBufferPosition()).toEqual [7, 4] @@ -1421,17 +1406,16 @@ describe "EditSession", -> expect(editSession.buffer.lineForRow(0)).toBe "var first = function () {" expect(buffer.lineForRow(1)).toBe " var first = function(items) {" - it "preserves the indent level when copying and pasting multiple lines", -> - editSession.setAutoIndent(true) - editSession.setSelectedBufferRange([[4, 4], [7, 5]]) - editSession.copySelectedText() - editSession.setCursorBufferPosition([10, 0]) - editSession.pasteText() + it "preserves the indent level when copying and pasting multiple lines", -> + editSession.setSelectedBufferRange([[4, 4], [7, 5]]) + editSession.copySelectedText() + editSession.setCursorBufferPosition([10, 0]) + editSession.pasteText(autoIndent: true) - expect(editSession.lineForBufferRow(10)).toBe " while(items.length > 0) {" - expect(editSession.lineForBufferRow(11)).toBe " current = items.shift();" - expect(editSession.lineForBufferRow(12)).toBe " current < pivot ? left.push(current) : right.push(current);" - expect(editSession.lineForBufferRow(13)).toBe " }" + expect(editSession.lineForBufferRow(10)).toBe " while(items.length > 0) {" + expect(editSession.lineForBufferRow(11)).toBe " current = items.shift();" + expect(editSession.lineForBufferRow(12)).toBe " current < pivot ? left.push(current) : right.push(current);" + expect(editSession.lineForBufferRow(13)).toBe " }" describe ".indentSelectedRows()", -> describe "when nothing is selected", -> diff --git a/src/app/edit-session.coffee b/src/app/edit-session.coffee index 2eb241a89..761c6dba8 100644 --- a/src/app/edit-session.coffee +++ b/src/app/edit-session.coffee @@ -92,7 +92,6 @@ class EditSession getScrollLeft: -> @scrollLeft setSoftWrapColumn: (@softWrapColumn) -> @displayBuffer.setSoftWrapColumn(@softWrapColumn) - setAutoIndent: (@autoIndent) -> setSoftTabs: (@softTabs) -> getSoftWrap: -> @softWrap @@ -158,18 +157,23 @@ class EditSession getCursorScopes: -> @getCursor().getScopes() logScreenLines: (start, end) -> @displayBuffer.logLines(start, end) - insertText: (text, options) -> + shouldAutoIndent: -> + false + + insertText: (text, options={}) -> + options.autoIndent ?= @shouldAutoIndent() @mutateSelectedText (selection) -> selection.insertText(text, options) insertNewline: -> - @insertText('\n', autoIndent: true) + @insertText('\n') insertNewlineBelow: -> @moveCursorToEndOfLine() @insertNewline() - indent: -> - @mutateSelectedText (selection) -> selection.indent() + indent: (options={})-> + options.autoIndent ?= @shouldAutoIndent() + @mutateSelectedText (selection) -> selection.indent(options) backspace: -> @mutateSelectedText (selection) -> selection.backspace() @@ -216,9 +220,13 @@ class EditSession selection.copy(maintainPasteboard) maintainPasteboard = true - pasteText: -> + pasteText: (options={})-> + options.normalizeIndent ?= true + [text, metadata] = pasteboard.read() - @insertText(text, _.extend(metadata ? {}, normalizeIndent: true)) + _.extend(options, metadata) if metadata + + @insertText(text, options) undo: -> @buffer.undo(this) diff --git a/src/app/editor.coffee b/src/app/editor.coffee index 651811574..40b64d64d 100644 --- a/src/app/editor.coffee +++ b/src/app/editor.coffee @@ -243,7 +243,7 @@ class Editor extends View insertText: (text, options) -> @activeEditSession.insertText(text, options) insertNewline: -> @activeEditSession.insertNewline() insertNewlineBelow: -> @activeEditSession.insertNewlineBelow() - indent: -> @activeEditSession.indent() + indent: (options) -> @activeEditSession.indent(options) indentSelectedRows: -> @activeEditSession.indentSelectedRows() outdentSelectedRows: -> @activeEditSession.outdentSelectedRows() cutSelection: -> @activeEditSession.cutSelectedText() @@ -380,7 +380,7 @@ class Editor extends View @selectOnMousemoveUntilMouseup() @on "textInput", (e) => - @insertText(e.originalEvent.data, autoIndent: true) + @insertText(e.originalEvent.data) false @scrollView.on 'mousewheel', (e) => diff --git a/src/app/selection.coffee b/src/app/selection.coffee index e7f7c23db..dce6f1b3e 100644 --- a/src/app/selection.coffee +++ b/src/app/selection.coffee @@ -179,13 +179,13 @@ class Selection else @cursor.setBufferPosition(newBufferRange.end, skipAtomicTokens: true) if wasReversed - if @editSession.autoIndent and options.autoIndent + if options.autoIndent if text == '\n' @editSession.autoIndentBufferRow(newBufferRange.end.row) else @editSession.autoDecreaseIndentForRow(newBufferRange.start.row) - indent: -> + indent: ({ autoIndent }={})-> { row, column } = @cursor.getBufferPosition() if @isEmpty() @@ -193,7 +193,7 @@ class Selection desiredIndent = @editSession.suggestedIndentForBufferRow(row) delta = desiredIndent - @cursor.getIndentLevel() - if @editSession.autoIndent and delta > 0 + if autoIndent and delta > 0 @insertText(@editSession.buildIndentString(delta)) else @insertText(@editSession.getTabText()) @@ -221,7 +221,7 @@ class Selection if insideExistingLine desiredBasis = @editSession.indentationForBufferRow(currentBufferRow) - else if @editSession.autoIndent + else if options.autoIndent desiredBasis = @editSession.suggestedIndentForBufferRow(currentBufferRow) else desiredBasis = @cursor.getIndentLevel()