Skip to content

Commit

Permalink
type and other small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
watermarkhu committed May 6, 2024
1 parent 3a9f64e commit 875a674
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 22 deletions.
18 changes: 9 additions & 9 deletions src/textmate_grammar/grammars/matlab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -45,4 +45,4 @@ def process(self, input: str) -> str:
output += split[matching.span()[1]:]
else:
output += split
return output
return output
15 changes: 5 additions & 10 deletions src/textmate_grammar/language.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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", [])
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/textmate_grammar/utils/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
3 changes: 1 addition & 2 deletions test/unit/matlab/test_pre_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():

Expand All @@ -23,4 +23,3 @@ def test_preprocessor():

result = processor.process(input_string)
assert result == output_string, "Incorrect pre-processed string"

0 comments on commit 875a674

Please sign in to comment.