From 8e62d772b39044f0e1bd8389646673e30b4e936f Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 18 Mar 2014 19:02:35 -0700 Subject: [PATCH 1/7] Ignore leading whitespace when auto-indenting a newline Refs atom/language-less#1 --- spec/editor-spec.coffee | 10 ++++++++++ src/editor.coffee | 12 +++++++++--- src/language-mode.coffee | 7 ++++--- src/selection.coffee | 2 +- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/spec/editor-spec.coffee b/spec/editor-spec.coffee index 3d45d176b..ced211574 100644 --- a/spec/editor-spec.coffee +++ b/spec/editor-spec.coffee @@ -2529,6 +2529,16 @@ describe "Editor", -> expect(editor.indentationForBufferRow(1)).toBe 1 expect(editor.indentationForBufferRow(2)).toBe 1 + describe "when the cursor is before whitespace", -> + it "indents that whitespace properly in the next line", -> + editor.setText(" var sort = function() {}") + editor.setCursorScreenPosition([0, 23]) + editor.insertNewline() + + expect(buffer.lineForRow(0)).toBe ' var sort = function()' + expect(buffer.lineForRow(1)).toBe ' {}' + expect(editor.getCursorScreenPosition()).toEqual [1, 2] + describe "when inserted text matches a decrease indent pattern", -> describe "when the preceding line matches an increase indent pattern", -> it "decreases the indentation to match that of the preceding line", -> diff --git a/src/editor.coffee b/src/editor.coffee index 1d7c6dde5..7a58c8c5e 100644 --- a/src/editor.coffee +++ b/src/editor.coffee @@ -373,10 +373,16 @@ class Editor extends Model # # bufferRow - A {Number} indicating the buffer row. # newLevel - A {Number} indicating the new indentation level. - setIndentationForBufferRow: (bufferRow, newLevel) -> - currentIndentLength = @lineForBufferRow(bufferRow).match(/^\s*/)[0].length + # options - An {Object} with the following keys: + # :ignoreLeadingWhitespace - true to not replace any of the leading + # whitespace already on the line (default: false) + setIndentationForBufferRow: (bufferRow, newLevel, {ignoreLeadingWhitespace}={}) -> + if ignoreLeadingWhitespace + endColumn = 0 + else + endColumn = @lineForBufferRow(bufferRow).match(/^\s*/)[0].length newIndentString = @buildIndentString(newLevel) - @buffer.change([[bufferRow, 0], [bufferRow, currentIndentLength]], newIndentString) + @buffer.change([[bufferRow, 0], [bufferRow, endColumn]], newIndentString) # Public: Get the indentation level of the given line of text. # diff --git a/src/language-mode.coffee b/src/language-mode.coffee index 093995947..f13a6c6b8 100644 --- a/src/language-mode.coffee +++ b/src/language-mode.coffee @@ -265,10 +265,11 @@ class LanguageMode # Given a buffer row, this indents it. # - # bufferRow - The row {Number} - autoIndentBufferRow: (bufferRow) -> + # bufferRow - The row {Number}. + # options - An options {Object} to pass to {Editor::setIndentationForBufferRow}. + autoIndentBufferRow: (bufferRow, options) -> indentLevel = @suggestedIndentForBufferRow(bufferRow) - @editor.setIndentationForBufferRow(bufferRow, indentLevel) + @editor.setIndentationForBufferRow(bufferRow, indentLevel, options) # Given a buffer row, this decreases the indentation. # diff --git a/src/selection.coffee b/src/selection.coffee index 13a949c51..7c357cdb1 100644 --- a/src/selection.coffee +++ b/src/selection.coffee @@ -301,7 +301,7 @@ class Selection if options.autoIndent @editor.autoIndentBufferRow(row) for row in newBufferRange.getRows() else if options.autoIndentNewline and text == '\n' - @editor.autoIndentBufferRow(newBufferRange.end.row) + @editor.autoIndentBufferRow(newBufferRange.end.row, ignoreLeadingWhitespace: true) else if options.autoDecreaseIndent and /\S/.test text @editor.autoDecreaseIndentForBufferRow(newBufferRange.start.row) From 2e8c0234bbeba58ee4567affca3e48c56d013c79 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 18 Mar 2014 19:05:30 -0700 Subject: [PATCH 2/7] Tweak spec description --- spec/editor-spec.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/editor-spec.coffee b/spec/editor-spec.coffee index ced211574..2a0eff51f 100644 --- a/spec/editor-spec.coffee +++ b/spec/editor-spec.coffee @@ -2530,7 +2530,7 @@ describe "Editor", -> expect(editor.indentationForBufferRow(2)).toBe 1 describe "when the cursor is before whitespace", -> - it "indents that whitespace properly in the next line", -> + it "indents the whitespace properly on the new line", -> editor.setText(" var sort = function() {}") editor.setCursorScreenPosition([0, 23]) editor.insertNewline() From 61f01f47139130290ec001f2e7de5bd9cfffc8d4 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 18 Mar 2014 19:07:51 -0700 Subject: [PATCH 3/7] :memo: Add through --- src/language-mode.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/language-mode.coffee b/src/language-mode.coffee index f13a6c6b8..bc075a662 100644 --- a/src/language-mode.coffee +++ b/src/language-mode.coffee @@ -266,7 +266,7 @@ class LanguageMode # Given a buffer row, this indents it. # # bufferRow - The row {Number}. - # options - An options {Object} to pass to {Editor::setIndentationForBufferRow}. + # options - An options {Object} to pass through to {Editor::setIndentationForBufferRow}. autoIndentBufferRow: (bufferRow, options) -> indentLevel = @suggestedIndentForBufferRow(bufferRow) @editor.setIndentationForBufferRow(bufferRow, indentLevel, options) From ff560bc3a360d23edef5615be5a464c2455ae194 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 19 Mar 2014 09:00:08 -0700 Subject: [PATCH 4/7] Update spec to not be before whitespace --- spec/editor-spec.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/editor-spec.coffee b/spec/editor-spec.coffee index 2a0eff51f..8af1b05f2 100644 --- a/spec/editor-spec.coffee +++ b/spec/editor-spec.coffee @@ -2506,7 +2506,7 @@ describe "Editor", -> describe "when the line preceding the newline does't add a level of indentation", -> it "indents the new line to the same level a as the preceding line", -> - editor.setCursorBufferPosition([5, 13]) + editor.setCursorBufferPosition([5, 14]) editor.insertText('\n') expect(editor.indentationForBufferRow(6)).toBe editor.indentationForBufferRow(5) From 7c17c99b9e9ca98d254163005e01f5b6de7e00a7 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 19 Mar 2014 10:02:14 -0700 Subject: [PATCH 5/7] Update spec description --- spec/editor-spec.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/editor-spec.coffee b/spec/editor-spec.coffee index 8af1b05f2..de8d55bfb 100644 --- a/spec/editor-spec.coffee +++ b/spec/editor-spec.coffee @@ -2530,7 +2530,7 @@ describe "Editor", -> expect(editor.indentationForBufferRow(2)).toBe 1 describe "when the cursor is before whitespace", -> - it "indents the whitespace properly on the new line", -> + it "retains the whitespace following the cursor on the new line", -> editor.setText(" var sort = function() {}") editor.setCursorScreenPosition([0, 23]) editor.insertNewline() From 9fce23d5acfea922aba0f43a78d3197a9cada4bd Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 19 Mar 2014 10:05:29 -0700 Subject: [PATCH 6/7] Update option to preserveLeadingWhitespace --- src/editor.coffee | 8 ++++---- src/selection.coffee | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/editor.coffee b/src/editor.coffee index 7a58c8c5e..d06f964e7 100644 --- a/src/editor.coffee +++ b/src/editor.coffee @@ -374,10 +374,10 @@ class Editor extends Model # bufferRow - A {Number} indicating the buffer row. # newLevel - A {Number} indicating the new indentation level. # options - An {Object} with the following keys: - # :ignoreLeadingWhitespace - true to not replace any of the leading - # whitespace already on the line (default: false) - setIndentationForBufferRow: (bufferRow, newLevel, {ignoreLeadingWhitespace}={}) -> - if ignoreLeadingWhitespace + # :preserveLeadingWhitespace - true to preserve any whitespace already on + # the line (default: false). + setIndentationForBufferRow: (bufferRow, newLevel, {preserveLeadingWhitespace}={}) -> + if preserveLeadingWhitespace endColumn = 0 else endColumn = @lineForBufferRow(bufferRow).match(/^\s*/)[0].length diff --git a/src/selection.coffee b/src/selection.coffee index 7c357cdb1..795e93004 100644 --- a/src/selection.coffee +++ b/src/selection.coffee @@ -301,7 +301,7 @@ class Selection if options.autoIndent @editor.autoIndentBufferRow(row) for row in newBufferRange.getRows() else if options.autoIndentNewline and text == '\n' - @editor.autoIndentBufferRow(newBufferRange.end.row, ignoreLeadingWhitespace: true) + @editor.autoIndentBufferRow(newBufferRange.end.row, preserveLeadingWhitespace: true) else if options.autoDecreaseIndent and /\S/.test text @editor.autoDecreaseIndentForBufferRow(newBufferRange.start.row) From 5783350484d5537f2ad124d53511ca2b2d53b4ec Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 19 Mar 2014 10:06:10 -0700 Subject: [PATCH 7/7] :memo: Tweak option comment --- src/editor.coffee | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/editor.coffee b/src/editor.coffee index d06f964e7..800eb3ba0 100644 --- a/src/editor.coffee +++ b/src/editor.coffee @@ -374,8 +374,8 @@ class Editor extends Model # bufferRow - A {Number} indicating the buffer row. # newLevel - A {Number} indicating the new indentation level. # options - An {Object} with the following keys: - # :preserveLeadingWhitespace - true to preserve any whitespace already on - # the line (default: false). + # :preserveLeadingWhitespace - true to preserve any whitespace already at + # the beginning of the line (default: false). setIndentationForBufferRow: (bufferRow, newLevel, {preserveLeadingWhitespace}={}) -> if preserveLeadingWhitespace endColumn = 0