-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: create MotionGenerator classes
BREAKING CHANGE: `g:pf_motions` has been removed. Descriptions are now stored in `g:pf_descriptions`. Weights are inferred from the number of characters in the motion (configurable weights may return with #41). It is no longer possible to disable motions individually.
- Loading branch information
Showing
12 changed files
with
206 additions
and
258 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from collections import namedtuple | ||
import abc | ||
|
||
from pathfinder.server.node import Node | ||
|
||
|
||
# motion - The motion such as h,j,k,f,T,gM | ||
# argument - Used for the additional argument to f,t,/ etc | ||
Motion = namedtuple("Motion", "motion argument") | ||
|
||
|
||
class MotionGenerator(abc.ABC): | ||
def __init__(self, dijkstra): | ||
self.dijkstra = dijkstra | ||
|
||
@abc.abstractmethod | ||
def generate(self, view): | ||
"""Yield all neighbouring nodes found from the given view.""" | ||
pass | ||
|
||
def _create_node(self, *args, **kwargs): | ||
return Node(self.dijkstra, *args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import vim | ||
|
||
from pathfinder.server.motions import MotionGenerator, Motion | ||
|
||
|
||
class FindMotionGenerator(MotionGenerator): | ||
MOTIONS = {"f", "t", "F", "T"} | ||
|
||
def generate(self, view): | ||
for motion in self.MOTIONS: | ||
yield from self._find(view, motion) | ||
|
||
def _find(self, view, motion): | ||
line_text = vim.current.buffer[view.lnum - 1] | ||
seen_characters = set() | ||
|
||
# characters = string of characters which may be accessible using this motion | ||
# column = lambda function which converts index in `characters` to a column number | ||
if motion == "f" and view.col < len(line_text): | ||
column = lambda i: view.col + i + 1 | ||
characters = line_text[view.col + 1 :] | ||
elif motion == "t" and view.col < len(line_text) - 1: | ||
column = lambda i: view.col + i + 1 | ||
characters = line_text[view.col + 2 :] | ||
seen_characters.add(line_text[view.col + 1]) | ||
elif motion == "F" and view.col > 0 and len(line_text) > view.col: | ||
column = lambda i: view.col - i - 1 | ||
# Characters are reversed because we are looking backwards | ||
characters = line_text[: view.col][::-1] | ||
elif motion == "T" and view.col > 1 and len(line_text) > view.col: | ||
column = lambda i: view.col - i - 1 | ||
characters = line_text[: view.col - 1][::-1] | ||
seen_characters.add(line_text[view.col - 1]) | ||
else: | ||
return | ||
|
||
for i, character in enumerate(characters): | ||
# Only use each unique character once | ||
if character in seen_characters: | ||
continue | ||
seen_characters.add(character) | ||
|
||
new_col = column(i) | ||
new_view = view._replace(col=new_col, curswant=new_col) | ||
yield self._create_node(new_view, Motion(motion, character)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.