properly detect hidden (.extension-only) files

This commit is contained in:
Anthony Sottile
2020-03-15 19:23:46 -07:00
parent 31e7c9345b
commit d5376ca6f2
2 changed files with 8 additions and 8 deletions

View File

@@ -196,16 +196,10 @@ class Grammar(NamedTuple):
@classmethod @classmethod
def blank(cls) -> 'Grammar': def blank(cls) -> 'Grammar':
return cls( return cls.from_data({'scopeName': 'source.unknown', 'patterns': []})
scope_name='source.unknown',
first_line_match=None,
file_types=frozenset(),
patterns=(),
repository=FDict({}),
)
def matches_file(self, filename: str, first_line: str) -> bool: 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: if ext.lstrip('.') in self.file_types:
return True return True
elif self.first_line_match is not None: elif self.first_line_match is not None:

View File

@@ -4,6 +4,12 @@ from babi.highlight import highlight_line
from babi.highlight import Region 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): def _compiler_state(grammar_dct, *others):
grammar = Grammar.from_data(grammar_dct) grammar = Grammar.from_data(grammar_dct)
grammars = [grammar, *(Grammar.from_data(dct) for dct in others)] grammars = [grammar, *(Grammar.from_data(dct) for dct in others)]