diff --git a/babi/highlight.py b/babi/highlight.py index 3eb31f5..a32eb53 100644 --- a/babi/highlight.py +++ b/babi/highlight.py @@ -12,6 +12,8 @@ from typing import Sequence from typing import Tuple from typing import TypeVar +from identify.identify import tags_from_filename + from babi._types import Protocol from babi.fdict import FDict from babi.reg import _Reg @@ -665,6 +667,10 @@ class Grammars: return self.compiler_for_scope('source.unknown') def compiler_for_file(self, filename: str, first_line: str) -> Compiler: + for tag in tags_from_filename(filename) - {'text'}: + with contextlib.suppress(KeyError): + return self.compiler_for_scope(f'source.{tag}') + _, _, ext = os.path.basename(filename).rpartition('.') for extensions, first_line_match, scope_name in self._find_scope: if ( diff --git a/setup.cfg b/setup.cfg index 89ae8e1..d006f45 100644 --- a/setup.cfg +++ b/setup.cfg @@ -22,6 +22,7 @@ classifiers = [options] packages = find: install_requires = + identify onigurumacffi>=0.0.10 python_requires = >=3.6.1 diff --git a/tests/highlight_test.py b/tests/highlight_test.py index d0a4fa1..5377ca3 100644 --- a/tests/highlight_test.py +++ b/tests/highlight_test.py @@ -10,6 +10,13 @@ def test_grammar_matches_extension_only_name(): assert compiler.root_state.entries[0].scope[0] == 'shell' +def test_grammar_matches_via_identify_tag(): + data = {'scopeName': 'source.ini', 'patterns': []} + grammars = Grammars([data]) + compiler = grammars.compiler_for_file('setup.cfg', '') + assert compiler.root_state.entries[0].scope[0] == 'source.ini' + + def _compiler_state(*grammar_dcts): grammars = Grammars(grammar_dcts) compiler = grammars.compiler_for_scope(grammar_dcts[0]['scopeName'])