From 8a77acb51e99371ebcfe46238608e9367e263138 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Wed, 5 Mar 2014 23:29:28 -0700 Subject: [PATCH] Duplicate multiple selections and multiple lines; folds are broken --- spec/editor-spec.coffee | 24 ++++++++++++++++++++++++ src/editor.coffee | 27 ++++++++------------------- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/spec/editor-spec.coffee b/spec/editor-spec.coffee index d02ed2b35..0b22cc380 100644 --- a/spec/editor-spec.coffee +++ b/spec/editor-spec.coffee @@ -2669,6 +2669,30 @@ describe "Editor", -> expect(editor.lineForBufferRow(9)).toBe ' }; return sort(Array.apply(this, arguments)); };' expect(editor.getSelectedBufferRange()).toEqual [[9, 3], [9, 49]] + describe ".duplicateLines()", -> + it "for each selection, duplicates all buffer lines intersected by the selection", -> + editor.foldBufferRow(4) + editor.setCursorBufferPosition([2, 5]) + editor.addSelectionForBufferRange([[3, 0], [8, 0]]) + + editor.duplicateLine() + + expect(editor.getTextInBufferRange([[2, 0], [13, 5]])).toBe """ + \ if (items.length <= 1) return items; + if (items.length <= 1) return items; + var pivot = items.shift(), current, left = [], right = []; + while(items.length > 0) { + current = items.shift(); + current < pivot ? left.push(current) : right.push(current); + } + var pivot = items.shift(), current, left = [], right = []; + while(items.length > 0) { + current = items.shift(); + current < pivot ? left.push(current) : right.push(current); + } + """ + expect(editor.getSelectedBufferRanges()).toEqual [[[3, 5], [3, 5]], [[9, 0], [14, 0]]] + describe ".shouldPromptToSave()", -> it "returns false when an edit session's buffer is in use by more than one session", -> jasmine.unspy(editor, 'shouldPromptToSave') diff --git a/src/editor.coffee b/src/editor.coffee index 94856c9a9..7b5543d5c 100644 --- a/src/editor.coffee +++ b/src/editor.coffee @@ -949,27 +949,16 @@ class Editor extends Model # Duplicate the most recent cursor's current line. duplicateLine: -> - return unless @getSelection().isEmpty() - @transact => - cursorPosition = @getCursorBufferPosition() - cursorRowFolded = @isFoldedAtCursorRow() - if cursorRowFolded - screenRow = @screenPositionForBufferPosition(cursorPosition).row - bufferRange = @bufferRangeForScreenRange([[screenRow], [screenRow + 1]]) - else - bufferRange = new Range([cursorPosition.row], [cursorPosition.row + 1]) + for selection in @getSelectionsOrderedByBufferPosition().reverse() + selectedBufferRange = selection.getBufferRange() + [startRow, endRow] = selection.getBufferRowRange() + endRow++ + rangeToDuplicate = [[startRow, 0], [endRow, 0]] + textToDuplicate = @getTextInBufferRange(rangeToDuplicate) + @buffer.insert([endRow, 0], textToDuplicate) - insertPosition = new Point(bufferRange.end.row) - if insertPosition.row > @buffer.getLastRow() - @unfoldCurrentRow() if cursorRowFolded - @buffer.append("\n#{@getTextInBufferRange(bufferRange)}") - @foldCurrentRow() if cursorRowFolded - else - @buffer.insert(insertPosition, @getTextInBufferRange(bufferRange)) - - @setCursorScreenPosition(@getCursorScreenPosition().translate([1])) - @foldCurrentRow() if cursorRowFolded + selection.setBufferRange(selectedBufferRange.translate([endRow - startRow, 0])) mutateSelectedText: (fn) -> @transact => fn(selection) for selection in @getSelections()