meta-c copies text in selection

This commit is contained in:
Corey Johnson 2012-01-30 17:46:03 -08:00
parent 99cbb5e0a2
commit 7dba7dc3de
5 changed files with 41 additions and 0 deletions

View File

@ -88,3 +88,20 @@ describe "Selection", ->
expect(selection.regions.length).toBe 3
expect(selection.find('.selection').length).toBe 3
describe ".copy()", ->
beforeEach ->
atom.native.writeToPasteboard('first')
expect(atom.native.readFromPasteboard()).toBe 'first'
it "places selected text on the clipboard", ->
selection.setRange new Range([0,4], [0,13])
selection.copy()
expect(atom.native.readFromPasteboard()).toBe 'quicksort'
it "places nothing on the clipboard when there is no selection", ->
selection.setRange new Range([0,4], [0,4])
selection.copy()
expect(atom.native.readFromPasteboard()).toBe 'first'

View File

@ -17,6 +17,15 @@ class Buffer
setText: (text) ->
@lines = text.split('\n')
getTextInRange: (range) ->
if range.start.row == range.end.row
return @lines[range.start.row][range.start.column...range.end.column]
text = @lines[range.start.row][range.start.column..]
for row in [range.start.row...range.end.row]
text += @lines[row]
text += @lines[range.end.row][0...range.end.column]
getLines: ->
@lines

View File

@ -41,6 +41,7 @@ class Editor extends Template
enter: 'newline'
backspace: 'backspace'
delete: 'delete'
'meta-c': 'copy'
@on 'move-right', => @moveCursorRight()
@on 'move-left', => @moveCursorLeft()
@ -53,6 +54,7 @@ class Editor extends Template
@on 'newline', => @insertNewline()
@on 'backspace', => @backspace()
@on 'delete', => @delete()
@on 'copy', => @copyText()
buildCursorAndSelection: ->
@ -190,4 +192,6 @@ class Editor extends Template
insertNewline: -> @selection.insertNewline()
backspace: -> @selection.backspace()
delete: -> @selection.delete()
copySelection: -> @selection().copy()

View File

@ -150,3 +150,9 @@ class Selection extends Template
moveCursorToLineStart: ->
@cursor.moveToLineStart()
copy: ->
return if @isEmpty()
text = @editor.buffer.getTextInRange @getRange()
atom.native.writeToPasteboard text

View File

@ -42,6 +42,11 @@ class Native
pb.declareTypes_owner [OSX.NSStringPboardType], null
pb.setString_forType text, OSX.NSStringPboardType
readFromPasteboard: (text) ->
pb = OSX.NSPasteboard.generalPasteboard
results = pb.readObjectsForClasses_options [OSX.NSString], null
results?[0]?.toString()
resetMainMenu: (menu) ->
OSX.NSApp.resetMainMenu