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..551edef0e 100644 --- a/src/cursor.coffee +++ b/src/cursor.coffee @@ -466,3 +466,15 @@ class Cursor # Returns an {Array} of {String}s. getScopes: -> @editSession.scopesForBufferPosition(@getBufferPosition()) + + # Public: Returns true if this cursor has no non-whitespace characters before + # its current position. + hasPrecedingCharactersOnLine: -> + bufferPosition = @getBufferPosition() + line = @editSession.lineForBufferRow(bufferPosition.row) + firstCharacterColumn = line.search(/\S/) + + if firstCharacterColumn is -1 + false + else + bufferPosition.column > firstCharacterColumn diff --git a/src/edit-session.coffee b/src/edit-session.coffee index 42c08c5ff..b12628ba0 100644 --- a/src/edit-session.coffee +++ b/src/edit-session.coffee @@ -564,8 +564,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().hasPrecedingCharactersOnLine() or containsNewlines + options.indentBasis ?= metadata.indentBasis @insertText(text, options)