Skip to content

Commit

Permalink
[closebrackets addon] When wrapping selection, create new selection w…
Browse files Browse the repository at this point in the history
…ithout brackets

Right now if you have a selection, and you enter a bracket/quote,
surrounds it with quotation marks, and selects the entire quote. This
change should change the behaviour to selecting the interior of the
quote instead.

The motivating example for me was autocomplete, where after
autocompleting a function the arguments are selected, so if you want
to insert a string literal right now you have to delete the selection
before you can enter the quotes. With the proposed fix, it'll be
rather more fluid.
  • Loading branch information
increpare authored and marijnh committed Sep 17, 2015
1 parent 0c9a2ff commit c65244d
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions addon/edit/closebrackets.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@
});
}

function contractSelection(sel){
if (sel.anchor.line<sel.head.line || (sel.anchor.line===sel.head.line&&sel.anchor.ch<sel.head.ch)){
sel.anchor.ch++;
sel.head.ch--;
}
if (sel.anchor.line>sel.head.line || (sel.anchor.line===sel.head.line&&sel.anchor.ch>sel.head.ch)){
sel.head.ch++;
sel.anchor.ch--;
}
}
function handleChar(cm, ch) {
var conf = getConfig(cm);
if (!conf || cm.getOption("disableInput")) return CodeMirror.Pass;
Expand Down Expand Up @@ -145,6 +155,11 @@
for (var i = 0; i < sels.length; i++)
sels[i] = left + sels[i] + right;
cm.replaceSelections(sels, "around");
var selections = cm.listSelections();
for (var i=0;i<selections.length;i++){
contractSelection(selections[i]);
}
cm.setSelections(selections);
} else if (type == "both") {
cm.replaceSelection(left + right, null);
cm.triggerElectric(left + right);
Expand Down

1 comment on commit c65244d

@mihailik
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brilliant!

I've noticed myself fighting a similar trouble in Chrome DevTools lately, but after search. I want to replace the found text with enquoted string, or array/brackets — and get caught in the selection muddle.

I wish Chrome picked CodeMirror changes soon, great improvement.

Please sign in to comment.