From fbabc6f45532a1dd822d6b78e2be6aacde5a1ea2 Mon Sep 17 00:00:00 2001 From: abe33 Date: Tue, 6 May 2014 23:19:39 +0200 Subject: [PATCH] Implements multiple selections clipboard paste Includes: - Passing the selection index in the `Editor::mutateSelectedText` method callback - Storing all the selections content on many calls of `Selection::copy` with `maintainClipboard = true` in a metadata `selections` array - Handling clipboard with a `selections` metadata in the`Editor::pasteText` method --- src/editor.coffee | 15 +++++++++++++-- src/selection.coffee | 15 ++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/editor.coffee b/src/editor.coffee index ba1dea2f3..10ec1687f 100644 --- a/src/editor.coffee +++ b/src/editor.coffee @@ -725,13 +725,24 @@ class Editor extends Model # Public: For each selection, replace the selected text with the contents of # the clipboard. # + # If the clipboard contains the same number of selection as the current + # editor, each selection will be replaced with the content of the + # corresponding clipboard selection text. + # # options - See {Selection::insertText}. pasteText: (options={}) -> {text, metadata} = atom.clipboard.readWithMetadata() containsNewlines = text.indexOf('\n') isnt -1 - if atom.config.get('editor.normalizeIndentOnPaste') and metadata + if metadata?.selections? and metadata.selections.length is @getSelections().length + @mutateSelectedText (selection, index) => + text = metadata.selections[index] + selection.insertText(text, options) + + return + + else if atom.config.get("editor.normalizeIndentOnPaste") and metadata?.indentBasis? if !@getCursor().hasPrecedingCharactersOnLine() or containsNewlines options.indentBasis ?= metadata.indentBasis @@ -1007,7 +1018,7 @@ class Editor extends Model # # fn - A {Function} that will be called with each {Selection}. mutateSelectedText: (fn) -> - @transact => fn(selection) for selection in @getSelections() + @transact => fn(selection,index) for selection,index in @getSelections() replaceSelectedText: (options={}, fn) -> {selectWordIfEmpty} = options diff --git a/src/selection.coffee b/src/selection.coffee index efaf25d7a..3c674db2b 100644 --- a/src/selection.coffee +++ b/src/selection.coffee @@ -503,11 +503,24 @@ class Selection extends Model @delete() # Public: Copies the current selection to the clipboard. + # + # If the `maintainClipboard` is set to `true`, a specific metadata property + # is created to store each content copied to the clipboard. The clipboard + # `text` still contains the concatenation of the clipboard with the + # current selection. copy: (maintainClipboard=false) -> return if @isEmpty() text = @editor.buffer.getTextInRange(@getBufferRange()) if maintainClipboard - text = "#{atom.clipboard.read()}\n#{text}" + {text: clipboardText, metadata: clipboardMetadata} = atom.clipboard.readWithMetadata() + + if clipboardMetadata? and clipboardMetadata.selections? + metadata = clipboardMetadata + clipboardMetadata.selections.push(text) + else + metadata = { selections: [clipboardText, text] } + + text = "" + (clipboardText) + "\n" + text else metadata = { indentBasis: @editor.indentationForBufferRow(@getBufferRange().start.row) }