Merge pull request #8025 from lpommers/copy-selection-to-clipboard

Copy selection command to only write to clipboard if text selected
This commit is contained in:
Max Brunsfeld
2015-08-18 09:25:59 -07:00
3 changed files with 32 additions and 0 deletions

View File

@@ -2920,6 +2920,28 @@ describe "TextEditor", ->
items
"""
describe ".copyOnlySelectedText()", ->
describe "when thee are multiple selections", ->
it "copies selected text onto the clipboard", ->
editor.setSelectedBufferRanges([[[0, 4], [0, 13]], [[1, 6], [1, 10]], [[2, 8], [2, 13]]])
editor.copyOnlySelectedText()
expect(buffer.lineForRow(0)).toBe "var quicksort = function () {"
expect(buffer.lineForRow(1)).toBe " var sort = function(items) {"
expect(buffer.lineForRow(2)).toBe " if (items.length <= 1) return items;"
expect(clipboard.readText()).toBe 'quicksort\nsort\nitems'
expect(atom.clipboard.read()).toEqual """
quicksort
sort
items
"""
describe "when no text is selected", ->
it "does not copy anything", ->
editor.setCursorBufferPosition([1, 5])
editor.copyOnlySelectedText()
expect(atom.clipboard.read()).toEqual "initial clipboard content"
describe ".pasteText()", ->
copyText = (text, {startColumn, textEditor}={}) ->
startColumn ?= 0

View File

@@ -302,6 +302,7 @@ atom.commands.add 'atom-text-editor', stopEventPropagationAndGroupUndo(
'editor:transpose': -> @transpose()
'editor:upper-case': -> @upperCase()
'editor:lower-case': -> @lowerCase()
'editor:copy-selection': -> @copyOnlySelectedText()
)
atom.commands.add 'atom-text-editor:not([mini])', stopEventPropagation(

View File

@@ -2608,6 +2608,15 @@ class TextEditor extends Model
maintainClipboard = true
return
# Private: For each selection, only copy highlighted text.
copyOnlySelectedText: ->
maintainClipboard = false
for selection in @getSelectionsOrderedByBufferPosition()
if not selection.isEmpty()
selection.copy(maintainClipboard, true)
maintainClipboard = true
return
# Essential: For each selection, cut the selected text.
cutSelectedText: ->
maintainClipboard = false