def test_find_T(): generator = FindMotionGenerator(None) output = list(generator._find(View(1, 5, 5), "T")) assert output == [ (View(1, 3, 3), Motion("T", "c")), (View(1, 2, 2), Motion("T", "b")), (View(1, 1, 1), Motion("T", "a")), ]
def test_find_t(): generator = FindMotionGenerator(None) output = list(generator._find(View(1, 0, 0), "t")) assert output == [ (View(1, 1, 1), Motion("t", "c")), (View(1, 2, 2), Motion("t", "d")), (View(1, 4, 4), Motion("t", "e")), ]
def test_find_F(): generator = FindMotionGenerator(None) output = list(generator._find(View(1, 5, 5), "F")) assert output == [ (View(1, 4, 4), Motion("F", "d")), (View(1, 2, 2), Motion("F", "c")), (View(1, 1, 1), Motion("F", "b")), (View(1, 0, 0), Motion("F", "a")), ]
def test_find_f(): generator = FindMotionGenerator(None) output = list(generator._find(View(1, 0, 0), "f")) assert output == [ (View(1, 1, 1), Motion("f", "b")), (View(1, 2, 2), Motion("f", "c")), (View(1, 3, 3), Motion("f", "d")), (View(1, 5, 5), Motion("f", "e")), ]
def test_explained_motions(): motion1 = Motion("j", None) motion2 = Motion("f", "g") with mock.patch( "pathfinder.client.output.vim.vars", { "pf_descriptions": { "j": "Down {count} lines", "f": "To occurence {count} of {argument}", } }, ): assert list(explained_motions([motion1, motion2, motion2])) == [ "j Down 1 lines", "2fg To occurence 2 of g", ]
def test_generate(try_motion, create_node): generator = SimpleMotionGenerator(None) output = list(generator.generate(INPUT_VIEW)) assert len(output) == len(SimpleMotionGenerator.MOTIONS) for motion in SimpleMotionGenerator.MOTIONS: try_motion.assert_any_call(INPUT_VIEW, motion) create_node.assert_any_call(OUTPUT_VIEW, Motion(motion, None))
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))
def test_compact_motions(): motion1 = Motion("j", None) motion2 = Motion("k", None) assert compact_motions([motion1, motion2, motion2, motion2]) == "j 3k"
def test_get_count(motion, count, expected): motion = Motion(motion, None) assert get_count(motion, count) == expected
def generate(self, view): for motion in self.MOTIONS: result_view = self._try_motion(view, motion) if result_view is not None and result_view != view: yield self._create_node(result_view, Motion(motion, None))
def test_search_when_target_is_before_start(): generator = SearchMotionGenerator(None) assert generator._search("abcde", 2, 0) == Motion("/", "a") assert generator._search("bcdabc", 3, 0) == Motion("?", "b")
def test_search_finds_shortest_possible_query(): generator = SearchMotionGenerator(None) assert generator._search("abcde", 0, 2) == Motion("/", "c") assert generator._search("abcbcdebbc", 0, 3) == Motion("/", "bcd")
def _create_motion(self, search_query, motion="/"): return Motion(motion, self._escape_magic(search_query))