diff --git a/sublime_lib/_util/simple_yaml.py b/sublime_lib/_util/simple_yaml.py deleted file mode 100644 index 7fd789b..0000000 --- a/sublime_lib/_util/simple_yaml.py +++ /dev/null @@ -1,34 +0,0 @@ -from __future__ import annotations -import sublime -import re -from typing import Dict, Union - - -YamlScalar = Union[str, bool, None] - -__all__ = ['parse_simple_top_level_keys'] - - -def parse_simple_top_level_keys(text: str) -> Dict[YamlScalar, YamlScalar]: - return { - _parse_yaml_value(match.group(1)): - _parse_yaml_value(match.group(2)) - for match in re.finditer(r'(?m)^([^\s#].*?\s*): *(.+) *$', text) - } - - -def _parse_yaml_value(value: str) -> YamlScalar: - if value.startswith("'"): - return value[1:-1].replace("''", "'") - elif value.startswith('"'): - # JSON and YAML quotation rules are very similar, if not identical - return sublime.decode_value(value) - elif value == "true": - return True - elif value == "false": - return False - elif value == "null": - return None - else: - # Does not handle numbers because we don't expect any - return value diff --git a/sublime_lib/syntax.py b/sublime_lib/syntax.py index d6a23c5..aeb9048 100644 --- a/sublime_lib/syntax.py +++ b/sublime_lib/syntax.py @@ -1,101 +1,18 @@ from __future__ import annotations -from collections import namedtuple -import plistlib - -from ._util.simple_yaml import parse_simple_top_level_keys -from .resource_path import ResourcePath -from typing import List +import sublime __all__ = ['list_syntaxes', 'get_syntax_for_scope'] -SyntaxInfo = namedtuple('SyntaxInfo', ['path', 'name', 'scope', 'hidden']) -SyntaxInfo.__new__.__defaults__ = (None, None, False) # type: ignore - - -def get_sublime_syntax_metadata(path: ResourcePath) -> dict: - yaml = parse_simple_top_level_keys(path.read_text()) - return { - 'name': yaml.get('name') or path.stem, - 'hidden': yaml.get('hidden', False), - 'scope': yaml.get('scope'), - } - - -def get_tmlanguage_metadata(path: ResourcePath) -> dict: - tree = plistlib.readPlistFromBytes(path.read_bytes()) - - return { - 'name': tree.get('name') or path.stem, - 'hidden': tree.get('hidden', False), - 'scope': tree.get('scopeName'), - } - - -def get_hidden_tmlanguage_metadata(path: ResourcePath) -> dict: - tree = plistlib.readPlistFromBytes(path.read_bytes()) - - return { - 'name': path.stem, # `name` key is ignored - 'hidden': True, # `hidden` key is ignored - 'scope': tree.get('scopeName'), - } - - -SYNTAX_TYPES = { - '.sublime-syntax': get_sublime_syntax_metadata, - '.tmLanguage': get_tmlanguage_metadata, - '.hidden-tmLanguage': get_hidden_tmlanguage_metadata, -} - - -def get_syntax_metadata(path: ResourcePath) -> SyntaxInfo: - return SyntaxInfo( - path=str(path), - **SYNTAX_TYPES[path.suffix](path) - ) - - -def list_syntaxes() -> List[SyntaxInfo]: - """Return a list of all loaded syntax definitions. - - Each item is a :class:`namedtuple` with the following properties: - - path - The resource path to the syntax definition file. - - name - The display name of the syntax definition. - - scope - The top-level scope of the syntax. - - hidden - Whether the syntax will appear in the syntax menus and the command palette. - """ - syntax_definition_paths = [ - path for path in ResourcePath.glob_resources('') - if path.suffix in SYNTAX_TYPES - ] - - return [ - get_syntax_metadata(path) - for path in syntax_definition_paths - if not ( - path.suffix in {'.tmLanguage', '.hidden-tmLanguage'} - and path.with_suffix('.sublime-syntax') in syntax_definition_paths - ) - ] +def list_syntaxes() -> list[sublime.Syntax]: + print("sublime_lib.list_syntaxes() is deprecated," + " use sublime.list_syntaxes() instead!") + return sublime.list_syntaxes() def get_syntax_for_scope(scope: str) -> str: - """Returns the last syntax in load order that matches `scope`.""" - try: - return next( - syntax.path - for syntax in reversed(list_syntaxes()) - if syntax.scope == scope - ) - except StopIteration: - raise ValueError("Cannot find syntax for scope {!r}.".format(scope)) from None + print("sublime_lib.get_syntax_for_scope() is deprecated," + " use sublime.find_syntax_by_scope() instead!") + syntax = sublime.find_syntax_by_scope(scope) + return syntax[0].path if syntax else "" diff --git a/tests/test_yaml_util.py b/tests/test_yaml_util.py deleted file mode 100644 index 6b1a233..0000000 --- a/tests/test_yaml_util.py +++ /dev/null @@ -1,32 +0,0 @@ -from sublime_lib._util.simple_yaml import parse_simple_top_level_keys - -from unittest import TestCase - - -TEXT = r""" -# A comment -'single': 'test '' value' -"double": "test\nvalue" -unquoted: test\value -true: true -false: false -null: null -multiline: - not: parsed -""" - - -class TestSettingsDict(TestCase): - - def test_parse_simple_top_level_keys(self): - self.assertEqual( - parse_simple_top_level_keys(TEXT), - { - 'single': 'test \' value', - 'double': 'test\nvalue', - 'unquoted': 'test\\value', - True: True, - False: False, - None: None, - } - )