From d5376ca6f256f51ea6462c56aa076e95a130e643 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sun, 15 Mar 2020 19:23:46 -0700 Subject: [PATCH] properly detect hidden (.extension-only) files --- babi/highlight.py | 10 ++-------- tests/highlight_test.py | 6 ++++++ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/babi/highlight.py b/babi/highlight.py index 4fd779f..0c5a98d 100644 --- a/babi/highlight.py +++ b/babi/highlight.py @@ -196,16 +196,10 @@ class Grammar(NamedTuple): @classmethod def blank(cls) -> 'Grammar': - return cls( - scope_name='source.unknown', - first_line_match=None, - file_types=frozenset(), - patterns=(), - repository=FDict({}), - ) + return cls.from_data({'scopeName': 'source.unknown', 'patterns': []}) def matches_file(self, filename: str, first_line: str) -> bool: - _, ext = os.path.splitext(filename) + _, _, ext = os.path.basename(filename).rpartition('.') if ext.lstrip('.') in self.file_types: return True elif self.first_line_match is not None: diff --git a/tests/highlight_test.py b/tests/highlight_test.py index 3317113..47a3684 100644 --- a/tests/highlight_test.py +++ b/tests/highlight_test.py @@ -4,6 +4,12 @@ from babi.highlight import highlight_line from babi.highlight import Region +def test_grammar_matches_extension_only_name(): + data = {'scopeName': 'test', 'patterns': [], 'fileTypes': ['bashrc']} + grammar = Grammar.from_data(data) + assert grammar.matches_file('.bashrc', 'alias nano=babi') + + def _compiler_state(grammar_dct, *others): grammar = Grammar.from_data(grammar_dct) grammars = [grammar, *(Grammar.from_data(dct) for dct in others)]