Merge pull request #1115 from atom/refine-paste-normalization

Don't normalize indents on paste if there are preceding characters, fixes #1112
This commit is contained in:
Matt Colyer
2013-11-19 10:54:44 -08:00
3 changed files with 23 additions and 1 deletions

View File

@@ -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", ->

View File

@@ -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

View File

@@ -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)