Atom list-edit

The list-edit package for the Atom editor provides list-aware cut, copy, and paste operations that automatically handle separators and whitespace, while taking into account strings and comments. For example, to cut the first element from [Three, One, Two] and paste it at the end:

[Three, One, Two]list-cut[One, Two][One, Two]list-paste[One, Two, Three]

On list-cut the list element that contains the cursor is removed, together with its trailing separator and whitespace. After moving the cursor past the last element, list-paste inserts the cut element and puts a separator and whitespace in front of it. List-edit also works in a vertical layout and on multiple elements:

o = { p2: 'B' , p3: 'C' , p1: 'A' }list-cuto = { p1: 'A' }  o = { p1: 'A' }  list-pasteo = { p1: 'A' , p2: 'B' , p3: 'C' }

And even between different lists (with yet another layout):

o = { radius: 32, z: 2, pos:{ x: 4, y: 3 } }list-cuto = { radius: 32, pos:{ x: 4, y: 3 } }o = { radius: 32, pos:{ x: 4, y: 3 } }list-pasteo = { radius: 32, pos:{ x: 4, y: 3, z: 2 } }

Live demo

The editable area below contains a functional version of the list-edit package (without comment/string support). You can try it out by using the keyboard shortcuts below.

 
 
Emulated Atom editor with list-edit package

Key bindings

These are Mac key bindings, for Windows and Linux use ctrl-alt instead of alt-cmd.

alt-cmd-slist-selectSelect element at cursor, or range of elements in selection
alt-cmd-xlist-cutCut elements (and separator+whitespace) at cursor/selection
alt-cmd-clist-copyCopy elements at cursor/selection to the clipboard
alt-cmd-vlist-pastePaste elements (and separator+whitespace) at cursor/selection

How selection works

The edited parent list is the smallest list that contains the entire text selection. To select an element, the selection/cursor needs to be inside the element or around it. A text selection that contains only a separator or whitespace represents an empty range, which is only used for paste.

Some examples should clarify selection:

[ Inky, Dinky, Pinky, Clyde ]list-select[ Inky, Dinky, Pinky, Clyde ] (Empty range)
[ Inky, Dinky, Pinky, Clyde ]list-select[ Inky, Dinky, Pinky, Clyde ](Single element)
[ Inky, Dinky, Pinky, Clyde ]list-select[ Inky, Dinky, Pinky, Clyde ](Multiple elements)
[ (0,0), (1,1), (2,2), (3,3) ]list-select[ (0,0), (1,1), (2,2), (3,3) ](Selection in nested lists)
{ x: 120, y: 14 }list-select { x: 120, y: 14 }(Selection in object)

Separator and whitespace handling

List-edit uses the grammar of the edited file to ignore strings and comments, but list detection takes place purely on a lexical level. Currently, , [], and () are brackets, and , and ; are separators.

Any separators and whitespace inserted on list-paste are copies of the first separator in the target list, unless this has fewer than two elements (and hence no separators,) in which case they are taken from the source list. If the original source list also has fewer than two elements, a default separator based on the list brackets is inserted with a single space. For [] and () the default separator is ,, and for {} it is ;.

Package on Atom.io: list-edit, GitHub: github.com/Oblosys/atom-list-edit