Skip to content

Commit fe6c5fb

Browse files
John Diamondphilc
John Diamond
authored andcommitted
Simplify runMovement a little
1 parent af13cdf commit fe6c5fb

File tree

1 file changed

+7
-33
lines changed

1 file changed

+7
-33
lines changed

content_scripts/mode_visual.js

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ class Movement {
3838

3939
// Run a movement. This is the core movement method, all movements happen here. For convenience, the
4040
// following three argument forms are supported:
41-
// @runMovement "forward word"
42-
// @runMovement ["forward", "word"]
43-
// @runMovement "forward", "word"
41+
// runMovement("forward word")
42+
// runMovement(["forward", "word"])
43+
// runMovement("forward", "word")
4444
//
4545
// The granularities are word, "character", "line", "lineboundary", "sentence" and "paragraph". In addition,
4646
// we implement the pseudo granularity "vimword", which implements vim-like word movement (e.g. "w").
@@ -56,10 +56,10 @@ class Movement {
5656
// character-by-character.
5757
if ((granularity === vimword) && (direction === forward)) {
5858
while (this.nextCharacterIsWordCharacter())
59-
if (!this.runMovements([forward, character]))
59+
if (this.extendByOneCharacter(forward) === 0)
6060
return;
6161
while (this.getNextForwardCharacter() && !this.nextCharacterIsWordCharacter())
62-
if (!this.runMovements([forward, character]))
62+
if (this.extendByOneCharacter(forward) === 0)
6363
return;
6464
} else if (granularity === vimword) {
6565
this.selection.modify(this.alterMethod, backward, word);
@@ -68,42 +68,16 @@ class Movement {
6868
// As above, we implement this character-by-character to get consistent behavior on Windows and Linux.
6969
if ((granularity === word) && (direction === forward)) {
7070
while (this.getNextForwardCharacter() && !this.nextCharacterIsWordCharacter())
71-
if (!this.runMovements([forward, character]))
71+
if (this.extendByOneCharacter(forward) === 0)
7272
return;
7373
while (this.nextCharacterIsWordCharacter())
74-
if (!this.runMovements([forward, character]))
74+
if (this.extendByOneCharacter(forward) === 0)
7575
return;
7676
} else {
7777
return this.selection.modify(this.alterMethod, direction, granularity);
7878
}
7979
}
8080

81-
// Return a simple comparable value which depends on various aspects of the selection. This is used to
82-
// detect, after a movement, whether the selection has changed.
83-
hashSelection() {
84-
const range = this.selection.getRangeAt(0);
85-
return [this.selection.toString().length,
86-
range.anchorOffset,
87-
range.focusOffset,
88-
this.selection.extentOffset,
89-
this.selection.baseOffset].join("/");
90-
}
91-
92-
// Call a function; return true if the selection changed, false otherwise.
93-
selectionChanged(func) {
94-
const before = this.hashSelection();
95-
func();
96-
return this.hashSelection() !== before;
97-
}
98-
99-
// Run a sequence of movements, stopping if a movement fails to change the selection.
100-
runMovements(...movements) {
101-
for (var movement of movements)
102-
if (!this.selectionChanged(() => this.runMovement(movement)))
103-
return false;
104-
return true;
105-
}
106-
10781
// Swap the anchor node/offset and the focus node/offset. This allows us to work with both ends of the
10882
// selection, and implements "o" for visual mode.
10983
reverseSelection() {

0 commit comments

Comments
 (0)