From 52680bd63f218bcc653548e18e979f0f4af06003 Mon Sep 17 00:00:00 2001 From: Matt Colyer Date: Wed, 13 Nov 2013 17:18:43 -0800 Subject: [PATCH] Don't normalize indents on paste if there are preceding characters --- spec/edit-session-spec.coffee | 7 +++++++ src/cursor.coffee | 8 ++++++++ src/edit-session.coffee | 5 ++++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/spec/edit-session-spec.coffee b/spec/edit-session-spec.coffee index cd2f50430..2107c3928 100644 --- a/spec/edit-session-spec.coffee +++ b/spec/edit-session-spec.coffee @@ -2525,6 +2525,13 @@ describe "EditSession", -> editSession.insertText("foo", indentBasis: 5) expect(editSession.lineForBufferRow(5)).toBe " foo current = items.shift();" + it "does not adjust the whitespace if there are preceding characters", -> + copyText(" foo") + editSession.setCursorBufferPosition([5, 30]) + editSession.pasteText() + + expect(editSession.lineForBufferRow(5)).toBe " current = items.shift(); foo" + describe "when the inserted text contains newlines", -> describe "when the cursor is preceded only by whitespace characters", -> it "normalizes indented lines to the cursor's current indentation level", -> diff --git a/src/cursor.coffee b/src/cursor.coffee index 4e7402015..300d3fd36 100644 --- a/src/cursor.coffee +++ b/src/cursor.coffee @@ -466,3 +466,11 @@ class Cursor # Returns an {Array} of {String}s. getScopes: -> @editSession.scopesForBufferPosition(@getBufferPosition()) + + # Public: Returns true if this cursor has no non-whitespace characters before + # it's current position. + hasNoPrecedingCharacters: -> + bufferPosition = @getBufferPosition() + line = @editSession.lineForBufferRow(bufferPosition.row) + firstCharacterColumn = line.search(/\S/) + noPrecedingCharacters = bufferPosition.column < firstCharacterColumn or firstCharacterColumn is -1 diff --git a/src/edit-session.coffee b/src/edit-session.coffee index a3bb2673c..a0afab1bf 100644 --- a/src/edit-session.coffee +++ b/src/edit-session.coffee @@ -567,8 +567,11 @@ class EditSession pasteText: (options={}) -> [text, metadata] = atom.pasteboard.read() + containsNewlines = text.indexOf('\n') isnt -1 + if atom.config.get('editor.normalizeIndentOnPaste') and metadata - options.indentBasis ?= metadata.indentBasis + if @getCursor().hasNoPrecedingCharacters() or containsNewlines + options.indentBasis ?= metadata.indentBasis @insertText(text, options)