From ebc9a6bf516b3e596fa8db2fe677fb4794514654 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 17 Nov 2014 08:53:14 -0800 Subject: [PATCH] Don't auto-indent current line when pasting after non-whitespace characters Signed-off-by: Nathan Sobo --- spec/text-editor-spec.coffee | 25 ++++++++++++++++++++----- src/selection.coffee | 9 +++++++-- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/spec/text-editor-spec.coffee b/spec/text-editor-spec.coffee index 7c5804bc5..4ef7a3c2d 100644 --- a/spec/text-editor-spec.coffee +++ b/spec/text-editor-spec.coffee @@ -3341,11 +3341,26 @@ describe "TextEditor", -> expect(editor.indentationForBufferRow(2)).toBe editor.indentationForBufferRow(1) + 1 describe "when pasting", -> - it "auto-indents the pasted text", -> - atom.clipboard.write("console.log(x);\n") - editor.setCursorBufferPosition([5, 2]) - editor.pasteText() - expect(editor.lineTextForBufferRow(5)).toBe(" console.log(x);") + describe "when only whitespace precedes the cursor", -> + it "auto-indents the lines spanned by the pasted text", -> + atom.clipboard.write("console.log(x);\nconsole.log(y);\n") + editor.setCursorBufferPosition([5, 2]) + editor.pasteText() + expect(editor.lineTextForBufferRow(5)).toBe(" console.log(x);") + expect(editor.lineTextForBufferRow(6)).toBe(" console.log(y);") + + describe "when non-whitespace characters precede the cursor", -> + it "does not auto-indent the first line being pasted", -> + editor.setText """ + if (x) { + y(); + } + """ + + atom.clipboard.write(" z();") + editor.setCursorBufferPosition([1, Infinity]) + editor.pasteText() + expect(editor.lineTextForBufferRow(1)).toBe(" y(); z();") describe 'when scoped settings are used', -> coffeeEditor = null diff --git a/src/selection.coffee b/src/selection.coffee index 45f913e43..39013e996 100644 --- a/src/selection.coffee +++ b/src/selection.coffee @@ -4,6 +4,8 @@ {Emitter} = require 'event-kit' Grim = require 'grim' +NonWhitespaceRegExp = /\S/ + # Extended: Represents a selection in the {TextEditor}. module.exports = class Selection extends Model @@ -368,13 +370,16 @@ class Selection extends Model @cursor.setBufferPosition(newBufferRange.end, skipAtomicTokens: true) if wasReversed if options.autoIndent - @editor.autoIndentBufferRow(row) for row in newBufferRange.getRows() + precedingText = @editor.getTextInBufferRange([[newBufferRange.start.row, 0], newBufferRange.start]) + unless NonWhitespaceRegExp.test(precedingText) + @editor.autoIndentBufferRow(newBufferRange.getRows()[0]) + @editor.autoIndentBufferRow(row) for row, i in newBufferRange.getRows() when i > 0 else if options.autoIndentNewline and text == '\n' currentIndentation = @editor.indentationForBufferRow(newBufferRange.start.row) @editor.autoIndentBufferRow(newBufferRange.end.row, preserveLeadingWhitespace: true) if @editor.indentationForBufferRow(newBufferRange.end.row) < currentIndentation @editor.setIndentationForBufferRow(newBufferRange.end.row, currentIndentation) - else if options.autoDecreaseIndent and /\S/.test text + else if options.autoDecreaseIndent and NonWhitespaceRegExp.test(text) @editor.autoDecreaseIndentForBufferRow(newBufferRange.start.row) newBufferRange