Skip to content

Commit

Permalink
Fix search command
Browse files Browse the repository at this point in the history
  • Loading branch information
emersonmx committed Aug 16, 2022
1 parent 94df5b1 commit cbc5c6a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
16 changes: 8 additions & 8 deletions tests/search_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def lines() -> list[str]:

@pytest.fixture
def context(lines: list[str]) -> Context:
return Context(2, lines.copy())
return Context(3, lines.copy())


def test_should_move_cursor_to_found_line(
Expand All @@ -27,7 +27,7 @@ def test_should_move_cursor_to_found_line(

result = command(context)

assert result == Context(4, lines)
assert result == Context(5, lines)


def test_should_move_cursor_to_found_line_with_regex(
Expand All @@ -38,7 +38,7 @@ def test_should_move_cursor_to_found_line_with_regex(

result = command(context)

assert result == Context(4, lines)
assert result == Context(5, lines)


def test_should_start_search_from_current_line(
Expand All @@ -49,7 +49,7 @@ def test_should_start_search_from_current_line(

result = command(context)

assert result == Context(2, lines)
assert result == Context(3, lines)


def test_should_make_a_loop_search_until_current_line(
Expand All @@ -60,7 +60,7 @@ def test_should_make_a_loop_search_until_current_line(

result = command(context)

assert result == Context(0, lines)
assert result == Context(1, lines)


def test_should_move_cursor_to_found_line_in_reverse(
Expand All @@ -71,7 +71,7 @@ def test_should_move_cursor_to_found_line_in_reverse(

result = command(context)

assert result == Context(0, lines)
assert result == Context(1, lines)


def test_should_start_reverse_search_from_previous_line(
Expand All @@ -82,7 +82,7 @@ def test_should_start_reverse_search_from_previous_line(

result = command(context)

assert result == Context(0, lines)
assert result == Context(1, lines)


def test_should_make_a_reverse_loop_search_until_current_line(
Expand All @@ -93,7 +93,7 @@ def test_should_make_a_reverse_loop_search_until_current_line(

result = command(context)

assert result == Context(1, lines)
assert result == Context(2, lines)


def test_should_raise_error_when_pattern_not_found(
Expand Down
13 changes: 7 additions & 6 deletions yaex/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def __call__(self, context: Context) -> Context:
def _search_line(self) -> LineNumber:
for line_number, line_text in self._make_lines_iterator():
if self._match_pattern(line_text):
return self._to_line_number(line_number)
return self._to_valid_line(line_number)
raise InvalidOperation("Pattern not found.")

def _make_lines_iterator(self) -> Iterable[tuple[LineNumber, str]]:
Expand All @@ -183,22 +183,23 @@ def _make_reverse_lines_iterator(self) -> Iterable[tuple[LineNumber, str]]:
return reversed(list(iterator))

def _make_forward_lines_iterator(self) -> Iterable[tuple[LineNumber, str]]:
size = len(self._context.lines)
cursor = self._context.cursor
index = to_index(cursor)
size = len(self._context.lines)
return enumerate(
islice(cycle(self._context.lines), cursor, cursor + size),
islice(cycle(self._context.lines), index, index + size),
cursor,
)

def _match_pattern(self, line: str) -> bool:
return bool(self._pattern.search(line))

def _to_line_number(self, line: LineNumber) -> LineNumber:
return line % len(self._context.lines)
def _to_valid_line(self, line: LineNumber) -> LineNumber:
return to_line(to_index(line) % len(self._context.lines))

def _resolve_line(self, context: Context) -> LineNumber:
self._context = context
return to_line(self._search_line())
return self._search_line()


class SubstituteCommand:
Expand Down

0 comments on commit cbc5c6a

Please sign in to comment.