Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add automatic tabsize detection #114

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion babi/buf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import bisect
import contextlib
import re
from typing import Callable
from typing import Generator
from typing import Iterator
Expand Down Expand Up @@ -60,7 +61,7 @@ class Buf:
def __init__(self, lines: List[str], tab_size: int = 4) -> None:
self._lines = lines
self.expandtabs = True
self.tab_size = tab_size
self.tab_size = self.get_tab_size(tab_size)
self.file_y = self.y = self._x = self._x_hint = 0

self._set_callbacks: List[SetCallback] = [self._set_cb]
Expand Down Expand Up @@ -138,6 +139,16 @@ def restore_eof_invariant(self) -> None:
if self[-1] != '':
self.append('')

def get_tab_size(self, tab_size: int) -> int:
for line in self._lines:
file_tab_size = re.search(r'(?=.*\S)^\s+', line)
if file_tab_size is not None:
# if hard tabs are detected use default
if not file_tab_size.group(0).startswith('\t'):
tab_size = len(file_tab_size.group(0))
break
return tab_size

def set_tab_size(self, tab_size: int) -> None:
self.tab_size = tab_size
self._positions = [None]
Expand Down
17 changes: 17 additions & 0 deletions tests/buf_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,20 @@ def test_buf_pop_idx():
buf.apply(modifications)

assert lst == ['a', 'b', 'c']


@pytest.mark.parametrize(
('lines', 'exp_tab_size'),
(
pytest.param(['a', ' b', ' c'], 2, id='not_default'),
pytest.param(['a', '\tb', 'c'], 4, id='hard_tab'),
pytest.param(['a', 'b', 'c'], 4, id='unknown'),
pytest.param(['a', ' ', ' b'], 2, id='tr_ws_first'),
pytest.param(['a', '\t\t', '\tb'], 4, id='tr_ws_first_hard_tab'),
pytest.param(['a', ' ', 'b'], 4, id='tr_ws_unknown'),
pytest.param([], 4, id='empty'),
),
)
def test_buf_get_tabsize(lines, exp_tab_size):
buf = Buf(lines)
assert buf.tab_size == exp_tab_size
4 changes: 2 additions & 2 deletions tests/features/indent_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ def test_dedent_exactly_one_indent(run):

def test_dedent_selection(run, tmpdir):
f = tmpdir.join('f')
f.write('1\n 2\n 3\n')
f.write('1\n 2\n 3\n 4\n')
with run(str(f)) as h, and_exit(h):
for _ in range(3):
h.press('S-Down')
h.press('BTab')
h.await_text('\n1\n2\n 3\n')
h.await_text('\n1\n2\n 3\n4\n')


def test_dedent_selection_with_noexpandtabs(run, tmpdir):
Expand Down