From 7e1a295f07cd6aaa159db12df2c17ab7d3d05ef7 Mon Sep 17 00:00:00 2001 From: Ivan Zuzak Date: Thu, 17 Sep 2015 19:24:52 +0200 Subject: [PATCH] Add method for cutting text to the end of the buffer line --- spec/text-editor-spec.coffee | 29 +++++++++++++++++++++++++++++ src/selection.coffee | 14 ++++++++++++-- src/text-editor-element.coffee | 1 + src/text-editor.coffee | 11 ++++++++++- 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/spec/text-editor-spec.coffee b/spec/text-editor-spec.coffee index 89acd4fa9..a3e1e6ebd 100644 --- a/spec/text-editor-spec.coffee +++ b/spec/text-editor-spec.coffee @@ -2899,6 +2899,35 @@ describe "TextEditor", -> expect(buffer.lineForRow(3)).toBe ' var pivot = item' expect(atom.clipboard.read()).toBe ' <= 1) ret\ns.shift(), current, left = [], right = [];' + describe ".cutToEndOfBufferLine()", -> + describe "when soft wrap is on", -> + it "cuts up to the end of the buffer line", -> + editor.setSoftWrapped(true) + editor.setEditorWidthInChars(10) + editor.setCursorScreenPosition([2, 2]) + editor.cutToEndOfBufferLine() + expect(editor.tokenizedLineForScreenRow(2).text).toBe '= ' + + describe "when soft wrap is off", -> + describe "when nothing is selected", -> + it "cuts up to the end of the buffer line", -> + editor.setCursorBufferPosition([2, 20]) + editor.addCursorAtBufferPosition([3, 20]) + editor.cutToEndOfBufferLine() + expect(buffer.lineForRow(2)).toBe ' if (items.length' + expect(buffer.lineForRow(3)).toBe ' var pivot = item' + expect(atom.clipboard.read()).toBe ' <= 1) return items;\ns.shift(), current, left = [], right = [];' + + describe "when text is selected", -> + it "only cuts the selected text, not to the end of the buffer line", -> + editor.setSelectedBufferRanges([[[2, 20], [2, 30]], [[3, 20], [3, 20]]]) + + editor.cutToEndOfBufferLine() + + expect(buffer.lineForRow(2)).toBe ' if (items.lengthurn items;' + expect(buffer.lineForRow(3)).toBe ' var pivot = item' + expect(atom.clipboard.read()).toBe ' <= 1) ret\ns.shift(), current, left = [], right = [];' + describe ".copySelectedText()", -> it "copies selected text onto the clipboard", -> editor.setSelectedBufferRanges([[[0, 4], [0, 13]], [[1, 6], [1, 10]], [[2, 8], [2, 13]]]) diff --git a/src/selection.coffee b/src/selection.coffee index ca87c5ac9..abf8640dc 100644 --- a/src/selection.coffee +++ b/src/selection.coffee @@ -257,10 +257,15 @@ class Selection extends Model @modifySelection => @cursor.moveToFirstCharacterOfLine() # Public: Selects all the text from the current cursor position to the end of - # the line. + # the screen line. selectToEndOfLine: -> @modifySelection => @cursor.moveToEndOfScreenLine() + # Public: Selects all the text from the current cursor position to the end of + # the buffer line. + selectToEndOfBufferLine: -> + @modifySelection => @cursor.moveToEndOfLine() + # Public: Selects all the text from the current cursor position to the # beginning of the word. selectToBeginningOfWord: -> @@ -572,11 +577,16 @@ class Selection extends Model toggleLineComments: -> @editor.toggleLineCommentsForBufferRows(@getBufferRowRange()...) - # Public: Cuts the selection until the end of the line. + # Public: Cuts the selection until the end of the screen line. cutToEndOfLine: (maintainClipboard) -> @selectToEndOfLine() if @isEmpty() @cut(maintainClipboard) + # Public: Cuts the selection until the end of the buffer line. + cutToEndOfBufferLine: (maintainClipboard) -> + @selectToEndOfBufferLine() if @isEmpty() + @cut(maintainClipboard) + # Public: Copies the selection to the clipboard and then deletes it. # # * `maintainClipboard` {Boolean} (default: false) See {::copy} diff --git a/src/text-editor-element.coffee b/src/text-editor-element.coffee index fc027b0d8..db911307c 100644 --- a/src/text-editor-element.coffee +++ b/src/text-editor-element.coffee @@ -299,6 +299,7 @@ atom.commands.add 'atom-text-editor', stopEventPropagationAndGroupUndo( 'editor:delete-to-end-of-subword': -> @deleteToEndOfSubword() 'editor:delete-line': -> @deleteLine() 'editor:cut-to-end-of-line': -> @cutToEndOfLine() + 'editor:cut-to-end-of-buffer-line': -> @cutToEndOfBufferLine() 'editor:transpose': -> @transpose() 'editor:upper-case': -> @upperCase() 'editor:lower-case': -> @lowerCase() diff --git a/src/text-editor.coffee b/src/text-editor.coffee index e1b379c39..05f44ec2c 100644 --- a/src/text-editor.coffee +++ b/src/text-editor.coffee @@ -2664,7 +2664,7 @@ class TextEditor extends Model @emitter.emit 'did-insert-text', didInsertEvent # Essential: For each selection, if the selection is empty, cut all characters - # of the containing line following the cursor. Otherwise cut the selected + # of the containing screen line following the cursor. Otherwise cut the selected # text. cutToEndOfLine: -> maintainClipboard = false @@ -2672,6 +2672,15 @@ class TextEditor extends Model selection.cutToEndOfLine(maintainClipboard) maintainClipboard = true + # Essential: For each selection, if the selection is empty, cut all characters + # of the containing buffer line following the cursor. Otherwise cut the + # selected text. + cutToEndOfBufferLine: -> + maintainClipboard = false + @mutateSelectedText (selection) -> + selection.cutToEndOfBufferLine(maintainClipboard) + maintainClipboard = true + ### Section: Folds ###