Skip to content

Commit 84f6f3e

Browse files
author
Nathan Sobo
authored
Merge pull request atom#15976 from atom/paste-without-reformatting
Add command to paste without reformatting
2 parents 7637bc3 + fd85c1b commit 84f6f3e

11 files changed

+44
-8
lines changed

keymaps/darwin.cson

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
'ctrl-shift-w': 'editor:select-word'
133133
'cmd-ctrl-left': 'editor:move-selection-left'
134134
'cmd-ctrl-right': 'editor:move-selection-right'
135+
'cmd-shift-V': 'editor:paste-without-reformatting'
135136

136137
# Emacs
137138
'alt-f': 'editor:move-to-end-of-word'

keymaps/linux.cson

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
'alt-shift-right': 'editor:select-to-next-subword-boundary'
106106
'alt-backspace': 'editor:delete-to-beginning-of-subword'
107107
'alt-delete': 'editor:delete-to-end-of-subword'
108+
'ctrl-shift-V': 'editor:paste-without-reformatting'
108109

109110
# Sublime Parity
110111
'ctrl-a': 'core:select-all'

keymaps/win32.cson

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
'alt-shift-right': 'editor:select-to-next-subword-boundary'
111111
'alt-backspace': 'editor:delete-to-beginning-of-subword'
112112
'alt-delete': 'editor:delete-to-end-of-subword'
113+
'ctrl-shift-V': 'editor:paste-without-reformatting'
113114

114115
# Sublime Parity
115116
'ctrl-a': 'core:select-all'

menus/darwin.cson

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
{ label: 'Copy', command: 'core:copy' }
6666
{ label: 'Copy Path', command: 'editor:copy-path' }
6767
{ label: 'Paste', command: 'core:paste' }
68+
{ label: 'Paste Without Reformatting', command: 'editor:paste-without-reformatting' }
6869
{ label: 'Select All', command: 'core:select-all' }
6970
{ type: 'separator' }
7071
{ label: 'Toggle Comments', command: 'editor:toggle-line-comments' }

menus/linux.cson

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
{ label: 'C&opy', command: 'core:copy' }
3939
{ label: 'Copy Pat&h', command: 'editor:copy-path' }
4040
{ label: '&Paste', command: 'core:paste' }
41+
{ label: 'Paste Without Reformatting', command: 'editor:paste-without-reformatting' }
4142
{ label: 'Select &All', command: 'core:select-all' }
4243
{ type: 'separator' }
4344
{ label: '&Toggle Comments', command: 'editor:toggle-line-comments' }

menus/win32.cson

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
{ label: '&Copy', command: 'core:copy' }
4747
{ label: 'Copy Pat&h', command: 'editor:copy-path' }
4848
{ label: '&Paste', command: 'core:paste' }
49+
{ label: 'Paste Without Reformatting', command: 'editor:paste-without-reformatting' }
4950
{ label: 'Select &All', command: 'core:select-all' }
5051
{ type: 'separator' }
5152
{ label: '&Toggle Comments', command: 'editor:toggle-line-comments' }

spec/selection-spec.coffee

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ describe "Selection", ->
103103
selection.insertText("\r\n", autoIndent: true)
104104
expect(buffer.lineForRow(2)).toBe " "
105105

106+
it "does not adjust the indent of trailing lines if preserveTrailingLineIndentation is true", ->
107+
selection.setBufferRange [[5, 0], [5, 0]]
108+
selection.insertText(' foo\n bar\n', preserveTrailingLineIndentation: true, indentBasis: 1)
109+
expect(buffer.lineForRow(6)).toBe(' bar')
110+
106111
describe ".fold()", ->
107112
it "folds the buffer range spanned by the selection", ->
108113
selection.setBufferRange([[0, 3], [1, 6]])

spec/text-editor-spec.coffee

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4222,6 +4222,19 @@ describe "TextEditor", ->
42224222
expect(editor.lineTextForBufferRow(3)).toBe(" if (items.length <= 1) return items;")
42234223
expect(editor.getCursorBufferPosition()).toEqual([3, 13])
42244224

4225+
it "respects options that preserve the formatting of the pasted text", ->
4226+
editor.update({autoIndentOnPaste: true})
4227+
atom.clipboard.write("a(x);\n b(x);\r\nc(x);\n", indentBasis: 0)
4228+
editor.setCursorBufferPosition([5, 0])
4229+
editor.insertText(' ')
4230+
editor.pasteText({autoIndent: false, preserveTrailingLineIndentation: true, normalizeLineEndings: false})
4231+
4232+
expect(editor.lineTextForBufferRow(5)).toBe " a(x);"
4233+
expect(editor.lineTextForBufferRow(6)).toBe " b(x);"
4234+
expect(editor.buffer.lineEndingForRow(6)).toBe "\r\n"
4235+
expect(editor.lineTextForBufferRow(7)).toBe "c(x);"
4236+
expect(editor.lineTextForBufferRow(8)).toBe " current = items.shift();"
4237+
42254238
describe ".indentSelectedRows()", ->
42264239
describe "when nothing is selected", ->
42274240
describe "when softTabs is enabled", ->

src/register-default-commands.coffee

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ module.exports = ({commandRegistry, commandInstaller, config, notificationManage
174174
'core:cut': -> @cutSelectedText()
175175
'core:copy': -> @copySelectedText()
176176
'core:paste': -> @pasteText()
177+
'editor:paste-without-reformatting': -> @pasteText({
178+
normalizeLineEndings: false,
179+
autoIndent: false,
180+
preserveTrailingLineIndentation: true
181+
})
177182
'editor:delete-to-previous-word-boundary': -> @deleteToPreviousWordBoundary()
178183
'editor:delete-to-next-word-boundary': -> @deleteToNextWordBoundary()
179184
'editor:delete-to-beginning-of-word': -> @deleteToBeginningOfWord()

src/selection.coffee

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -356,13 +356,19 @@ class Selection extends Model
356356
#
357357
# * `text` A {String} representing the text to add
358358
# * `options` (optional) {Object} with keys:
359-
# * `select` if `true`, selects the newly added text.
360-
# * `autoIndent` if `true`, indents all inserted text appropriately.
361-
# * `autoIndentNewline` if `true`, indent newline appropriately.
362-
# * `autoDecreaseIndent` if `true`, decreases indent level appropriately
359+
# * `select` If `true`, selects the newly added text.
360+
# * `autoIndent` If `true`, indents all inserted text appropriately.
361+
# * `autoIndentNewline` If `true`, indent newline appropriately.
362+
# * `autoDecreaseIndent` If `true`, decreases indent level appropriately
363363
# (for example, when a closing bracket is inserted).
364+
# * `preserveTrailingLineIndentation` By default, when pasting multiple
365+
# lines, Atom attempts to preserve the relative indent level between the
366+
# first line and trailing lines, even if the indent level of the first
367+
# line has changed from the copied text. If this option is `true`, this
368+
# behavior is suppressed.
369+
# level between the first lines and the trailing lines.
364370
# * `normalizeLineEndings` (optional) {Boolean} (default: true)
365-
# * `undo` if `skip`, skips the undo stack for this operation.
371+
# * `undo` If `skip`, skips the undo stack for this operation.
366372
insertText: (text, options={}) ->
367373
oldBufferRange = @getBufferRange()
368374
wasReversed = @isReversed()
@@ -373,7 +379,7 @@ class Selection extends Model
373379
remainingLines = text.split('\n')
374380
firstInsertedLine = remainingLines.shift()
375381

376-
if options.indentBasis?
382+
if options.indentBasis? and not options.preserveTrailingLineIndentation
377383
indentAdjustment = @editor.indentLevelForLine(precedingText) - options.indentBasis
378384
@adjustIndent(remainingLines, indentAdjustment)
379385

src/text-editor.coffee

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3247,12 +3247,13 @@ class TextEditor extends Model
32473247
# corresponding clipboard selection text.
32483248
#
32493249
# * `options` (optional) See {Selection::insertText}.
3250-
pasteText: (options={}) ->
3250+
pasteText: (options) ->
3251+
options = Object.assign({}, options)
32513252
{text: clipboardText, metadata} = @constructor.clipboard.readWithMetadata()
32523253
return false unless @emitWillInsertTextEvent(clipboardText)
32533254

32543255
metadata ?= {}
3255-
options.autoIndent = @shouldAutoIndentOnPaste()
3256+
options.autoIndent ?= @shouldAutoIndentOnPaste()
32563257

32573258
@mutateSelectedText (selection, index) =>
32583259
if metadata.selections?.length is @getSelections().length

0 commit comments

Comments
 (0)