From 0ef6757e655f1c09cd793d910fb13bcd58a2d72c Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 7 Jan 2014 18:17:34 -0800 Subject: [PATCH 1/5] Add Editor::splitSelectionIntoLines --- spec/editor-spec.coffee | 21 +++++++++++++++++++++ src/editor-view.coffee | 4 ++++ src/editor.coffee | 20 ++++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/spec/editor-spec.coffee b/spec/editor-spec.coffee index 821c9becb..353b6ad66 100644 --- a/spec/editor-spec.coffee +++ b/spec/editor-spec.coffee @@ -1181,6 +1181,27 @@ describe "Editor", -> [[10, 0], [10, 0]] ] + describe ".splitSelectionIntoLines()", -> + it "splits all multi-line selections into one selection per line", -> + editor.setSelectedBufferRange([[0, 3], [2, 4]]) + editor.splitSelectionIntoLines() + expect(editor.getSelectedBufferRanges()).toEqual [ + [[0, 3], [0, 29]] + [[1, 0], [1, 30]] + [[2, 0], [2, 4]] + ] + + editor.setSelectedBufferRange([[0, 3], [1, 10]]) + editor.splitSelectionIntoLines() + expect(editor.getSelectedBufferRanges()).toEqual [ + [[0, 3], [0, 29]] + [[1, 0], [1, 10]] + ] + + editor.setSelectedBufferRange([[0, 0], [0, 3]]) + editor.splitSelectionIntoLines() + expect(editor.getSelectedBufferRanges()).toEqual [[[0, 0], [0, 3]]] + describe ".consolidateSelections()", -> it "destroys all selections but the most recent, returning true if any selections were destroyed", -> editor.setSelectedBufferRange([[3, 16], [3, 21]]) diff --git a/src/editor-view.coffee b/src/editor-view.coffee index 9c51a5554..f017967d8 100644 --- a/src/editor-view.coffee +++ b/src/editor-view.coffee @@ -183,6 +183,7 @@ class EditorView extends View 'editor:newline-above': @insertNewlineAbove 'editor:add-selection-below': @addSelectionBelow 'editor:add-selection-above': @addSelectionAbove + 'editor:split-selection-into-lines': @splitSelectionIntoLines 'editor:toggle-soft-tabs': @toggleSoftTabs 'editor:toggle-soft-wrap': @toggleSoftWrap 'editor:fold-all': @foldAll @@ -375,6 +376,9 @@ class EditorView extends View # {Delegates to: Editor.addSelectionAbove} addSelectionAbove: -> @editor.addSelectionAbove() + # {Delegates to: Editor.splitSelectionIntoLines} + splitSelectionIntoLines: -> @editor.splitSelectionIntoLines() + # {Delegates to: Editor.selectToBeginningOfWord} selectToBeginningOfWord: -> @editor.selectToBeginningOfWord() diff --git a/src/editor.coffee b/src/editor.coffee index cff06ddfc..f2efe5c37 100644 --- a/src/editor.coffee +++ b/src/editor.coffee @@ -1190,6 +1190,26 @@ class Editor extends Model addSelectionAbove: -> @expandSelectionsBackward (selection) => selection.addSelectionAbove() + # Public: Split any multi-line selections into one selection per line. + # + # This method breaks apart multi-line selections by adding new selections + # that select the entire line for all completely selected lines and leaves + # any partially selected lines at the start and end of the selection as-is. + # + # This method will not affect any single line selections. + splitSelectionIntoLines: -> + for selection in @getSelections() + range = selection.getBufferRange() + continue if range.isSingleLine() + + selection.destroy() + {start, end} = range + @addSelectionForBufferRange([start, [start.row, Infinity]]) + {row} = start + while ++row < end.row + @addSelectionForBufferRange([[row, 0], [row, Infinity]]) + @addSelectionForBufferRange([[end.row, 0], [end.row, end.column]]) + # Public: Transposes the current text selections. # # The text in each selection is reversed so `abcd` would become `dcba`. The From fe0184d0672bebb134ea44bb5bbaa11e5de31cc8 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 7 Jan 2014 18:20:03 -0800 Subject: [PATCH 2/5] Add Split into Lines menu item --- menus/darwin.cson | 1 + menus/win32.cson | 1 + 2 files changed, 2 insertions(+) diff --git a/menus/darwin.cson b/menus/darwin.cson index 8cd314c1e..c20379e54 100644 --- a/menus/darwin.cson +++ b/menus/darwin.cson @@ -99,6 +99,7 @@ submenu: [ { label: 'Add Selection Above', command: 'editor:add-selection-above' } { label: 'Add Selection Below', command: 'editor:add-selection-below' } + { label: 'Split into Lines', command: 'editor:split-selection-into-lines'} { type: 'separator' } { label: 'Select to Top', command: 'core:select-to-top' } { label: 'Select to Bottom', command: 'core:select-to-bottom' } diff --git a/menus/win32.cson b/menus/win32.cson index e4017fc42..8263f99a5 100644 --- a/menus/win32.cson +++ b/menus/win32.cson @@ -106,6 +106,7 @@ submenu: [ { label: 'Add Selection &Above', command: 'editor:add-selection-above' } { label: 'Add Selection &Below', command: 'editor:add-selection-below' } + { label: 'S&plit into Lines', command: 'editor:split-selection-into-lines'} { type: 'separator' } { label: 'Select to &Top', command: 'core:select-to-top' } { label: 'Select to Botto&m', command: 'core:select-to-bottom' } From 82c73c9911e44a4d9fdf7dd74d0374de9f061def Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 7 Jan 2014 18:24:41 -0800 Subject: [PATCH 3/5] :memo: Tweak comment for clarity --- src/editor.coffee | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/editor.coffee b/src/editor.coffee index f2efe5c37..4b9842e6d 100644 --- a/src/editor.coffee +++ b/src/editor.coffee @@ -1192,9 +1192,8 @@ class Editor extends Model # Public: Split any multi-line selections into one selection per line. # - # This method breaks apart multi-line selections by adding new selections - # that select the entire line for all completely selected lines and leaves - # any partially selected lines at the start and end of the selection as-is. + # This methods break apart all multi-line selections to create multiple + # single-line selections that cumulatively cover the same original area. # # This method will not affect any single line selections. splitSelectionIntoLines: -> From 32fdf0b681ab9a455e9601f85587d32f4532e187 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 7 Jan 2014 18:29:42 -0800 Subject: [PATCH 4/5] :memo: Drop line about single line selections --- src/editor.coffee | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/editor.coffee b/src/editor.coffee index 4b9842e6d..49ba87518 100644 --- a/src/editor.coffee +++ b/src/editor.coffee @@ -1194,8 +1194,6 @@ class Editor extends Model # # This methods break apart all multi-line selections to create multiple # single-line selections that cumulatively cover the same original area. - # - # This method will not affect any single line selections. splitSelectionIntoLines: -> for selection in @getSelections() range = selection.getBufferRange() From c8f3e056e93ec508cee1661b8c7fb655d5b0f3ee Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 8 Jan 2014 14:51:50 -0800 Subject: [PATCH 5/5] Pluralize selections in method name --- spec/editor-spec.coffee | 8 ++++---- src/editor-view.coffee | 5 +---- src/editor.coffee | 2 +- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/spec/editor-spec.coffee b/spec/editor-spec.coffee index 353b6ad66..93d19e815 100644 --- a/spec/editor-spec.coffee +++ b/spec/editor-spec.coffee @@ -1181,10 +1181,10 @@ describe "Editor", -> [[10, 0], [10, 0]] ] - describe ".splitSelectionIntoLines()", -> + describe ".splitSelectionsIntoLines()", -> it "splits all multi-line selections into one selection per line", -> editor.setSelectedBufferRange([[0, 3], [2, 4]]) - editor.splitSelectionIntoLines() + editor.splitSelectionsIntoLines() expect(editor.getSelectedBufferRanges()).toEqual [ [[0, 3], [0, 29]] [[1, 0], [1, 30]] @@ -1192,14 +1192,14 @@ describe "Editor", -> ] editor.setSelectedBufferRange([[0, 3], [1, 10]]) - editor.splitSelectionIntoLines() + editor.splitSelectionsIntoLines() expect(editor.getSelectedBufferRanges()).toEqual [ [[0, 3], [0, 29]] [[1, 0], [1, 10]] ] editor.setSelectedBufferRange([[0, 0], [0, 3]]) - editor.splitSelectionIntoLines() + editor.splitSelectionsIntoLines() expect(editor.getSelectedBufferRanges()).toEqual [[[0, 0], [0, 3]]] describe ".consolidateSelections()", -> diff --git a/src/editor-view.coffee b/src/editor-view.coffee index f017967d8..f198b2df4 100644 --- a/src/editor-view.coffee +++ b/src/editor-view.coffee @@ -183,7 +183,7 @@ class EditorView extends View 'editor:newline-above': @insertNewlineAbove 'editor:add-selection-below': @addSelectionBelow 'editor:add-selection-above': @addSelectionAbove - 'editor:split-selection-into-lines': @splitSelectionIntoLines + 'editor:split-selection-into-lines': => @editor.splitSelectionsIntoLines() 'editor:toggle-soft-tabs': @toggleSoftTabs 'editor:toggle-soft-wrap': @toggleSoftWrap 'editor:fold-all': @foldAll @@ -376,9 +376,6 @@ class EditorView extends View # {Delegates to: Editor.addSelectionAbove} addSelectionAbove: -> @editor.addSelectionAbove() - # {Delegates to: Editor.splitSelectionIntoLines} - splitSelectionIntoLines: -> @editor.splitSelectionIntoLines() - # {Delegates to: Editor.selectToBeginningOfWord} selectToBeginningOfWord: -> @editor.selectToBeginningOfWord() diff --git a/src/editor.coffee b/src/editor.coffee index 49ba87518..bbefa2f69 100644 --- a/src/editor.coffee +++ b/src/editor.coffee @@ -1194,7 +1194,7 @@ class Editor extends Model # # This methods break apart all multi-line selections to create multiple # single-line selections that cumulatively cover the same original area. - splitSelectionIntoLines: -> + splitSelectionsIntoLines: -> for selection in @getSelections() range = selection.getBufferRange() continue if range.isSingleLine()