From 875a6749ec19f4156f1b74d9079e763d50f59be3 Mon Sep 17 00:00:00 2001 From: watermarkhu Date: Mon, 6 May 2024 17:07:33 +0800 Subject: [PATCH] type and other small fixes --- .../grammars/matlab/__init__.py | 18 +++++++++--------- src/textmate_grammar/language.py | 15 +++++---------- src/textmate_grammar/utils/handler.py | 2 +- test/unit/matlab/test_pre_processor.py | 3 +-- 4 files changed, 16 insertions(+), 22 deletions(-) diff --git a/src/textmate_grammar/grammars/matlab/__init__.py b/src/textmate_grammar/grammars/matlab/__init__.py index 3a16436..6143ba5 100644 --- a/src/textmate_grammar/grammars/matlab/__init__.py +++ b/src/textmate_grammar/grammars/matlab/__init__.py @@ -21,19 +21,19 @@ if tmLanguageFile.exists(): - with open(tmLanguageFile, "rb") as file: - GRAMMAR = plistlib.load(file, fmt=plistlib.FMT_XML) - with open(tmLanguageYAML, "w") as f: - f.write(yaml.dump(GRAMMAR, indent=2)) + with open(tmLanguageFile, "rb") as tmFile: + GRAMMAR = plistlib.load(tmFile, fmt=plistlib.FMT_XML) + with open(tmLanguageYAML, "w") as ymlFile: + ymlFile.write(yaml.dump(GRAMMAR, indent=2)) else: - with open(tmLanguageYAML) as file: + with open(tmLanguageYAML) as ymlFile: try: - GRAMMAR = yaml.load(file.read(), Loader=yaml.CLoader) + GRAMMAR = yaml.load(ymlFile.read(), Loader=yaml.CLoader) except ImportError: - GRAMMAR = yaml.load(file.read(), Loader=yaml.Loader) + GRAMMAR = yaml.load(ymlFile.read(), Loader=yaml.Loader) -class PreProcessor(BasePreProcessor): +class RemoveLineContinationsPreProcessor(BasePreProcessor): """The pre-processor for the MATLAB language.""" def process(self, input: str) -> str: @@ -45,4 +45,4 @@ def process(self, input: str) -> str: output += split[matching.span()[1]:] else: output += split - return output \ No newline at end of file + return output diff --git a/src/textmate_grammar/language.py b/src/textmate_grammar/language.py index edfcd16..22ff597 100644 --- a/src/textmate_grammar/language.py +++ b/src/textmate_grammar/language.py @@ -33,12 +33,7 @@ def _parse(self, *args, **kwargs): class LanguageParser(PatternsParser): """The parser of a language grammar.""" - def __init__( - self, - grammar: dict, - pre_processor: BasePreProcessor | None = None, - **kwargs - ): + def __init__(self, grammar: dict, pre_processor: BasePreProcessor | None = None, **kwargs): """ Initialize a Language object. @@ -58,7 +53,7 @@ def __init__( """ super().__init__(grammar, key=grammar.get("name", "myLanguage"), language=self, **kwargs) - self.pre_processor = pre_processor + self.pre_processor = pre_processor self.name = grammar.get("name", "") self.uuid = grammar.get("uuid", "") self.file_types = grammar.get("fileTypes", []) @@ -128,9 +123,8 @@ def parse_file(self, filePath: str | Path, **kwargs) -> ContentElement | None: if self._cache.cache_valid(filePath): element = self._cache.load(filePath) else: - handler = ContentHandler.from_path(filePath, pre_processor=self.pre_processor, **kwargs) - if handler.source == "": + if handler.content == "": return None # Configure logger @@ -150,7 +144,8 @@ def parse_string(self, input: str, **kwargs) -> ContentElement | None: :return: The result of parsing the input string. """ handler = ContentHandler(input, pre_processor=self.pre_processor, **kwargs) - # Configure loggerf + + # Configure logger LOGGER.configure(self, height=len(handler.lines), width=max(handler.line_lengths)) element = self._parse_language(handler, **kwargs) diff --git a/src/textmate_grammar/utils/handler.py b/src/textmate_grammar/utils/handler.py index 45f41eb..28f2e46 100644 --- a/src/textmate_grammar/utils/handler.py +++ b/src/textmate_grammar/utils/handler.py @@ -6,9 +6,9 @@ from onigurumacffi import _Pattern as Pattern from onigurumacffi import compile +from ..grammars import BasePreProcessor from .exceptions import FileNotFound, ImpossibleSpan from .logger import LOGGER -from ..grammars import BasePreProcessor POS = tuple[int, int] diff --git a/test/unit/matlab/test_pre_processor.py b/test/unit/matlab/test_pre_processor.py index 995baa5..d85c697 100644 --- a/test/unit/matlab/test_pre_processor.py +++ b/test/unit/matlab/test_pre_processor.py @@ -6,7 +6,7 @@ logging.getLogger().setLevel(logging.DEBUG) logging.getLogger("textmate_grammar").setLevel(logging.INFO) -processor = matlab.PreProcessor() +processor = matlab.RemoveLineContinationsPreProcessor() def test_preprocessor(): @@ -23,4 +23,3 @@ def test_preprocessor(): result = processor.process(input_string) assert result == output_string, "Incorrect pre-processed string" - \ No newline at end of file