MCPcopy Index your code
hub / github.com/philc/vimium / reverseSelection

Method reverseSelection

content_scripts/mode_visual.js:112–132  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

110 // Swap the anchor node/offset and the focus node/offset. This allows us to work with both ends of
111 // the selection, and implements "o" for visual mode.
112 reverseSelection() {
113 const direction = this.getDirection();
114 const element = document.activeElement;
115 if (element && DomUtils.isEditable(element) && !element.isContentEditable) {
116 // Note(smblott). This implementation is expensive if the selection is large. We only use it
117 // here because the normal method (below) does not work within text areas, etc.
118 const length = this.selection.toString().length;
119 this.collapseSelectionToFocus();
120 for (let i = 0, end = length; i < end; i++) {
121 this.runMovement(this.opposite[direction], character);
122 }
123 } else {
124 // Normal method.
125 const original = this.selection.getRangeAt(0).cloneRange();
126 const range = original.cloneRange();
127 range.collapse(direction === backward);
128 this.setSelectionRange(range);
129 const which = direction === forward ? "start" : "end";
130 this.selection.extend(original[`${which}Container`], original[`${which}Offset`]);
131 }
132 }
133
134 // Try to extend the selection by one character in direction. Return positive, negative or 0,
135 // indicating whether the selection got bigger, or smaller, or is unchanged.

Callers 4

selectLineMethod · 0.95
"o"Function · 0.80
commandHandlerMethod · 0.80
extendSelectionMethod · 0.80

Calls 4

getDirectionMethod · 0.95
runMovementMethod · 0.95
setSelectionRangeMethod · 0.95

Tested by

no test coverage detected