|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -from collections import namedtuple |
4 | | -import plistlib |
5 | | - |
6 | | -from ._util.simple_yaml import parse_simple_top_level_keys |
7 | | -from .resource_path import ResourcePath |
8 | | -from typing import List |
| 3 | +import sublime |
9 | 4 |
|
10 | 5 | __all__ = ['list_syntaxes', 'get_syntax_for_scope'] |
11 | 6 |
|
12 | 7 |
|
13 | | -SyntaxInfo = namedtuple('SyntaxInfo', ['path', 'name', 'scope', 'hidden']) |
14 | | -SyntaxInfo.__new__.__defaults__ = (None, None, False) # type: ignore |
15 | | - |
16 | | - |
17 | | -def get_sublime_syntax_metadata(path: ResourcePath) -> dict: |
18 | | - yaml = parse_simple_top_level_keys(path.read_text()) |
19 | | - return { |
20 | | - 'name': yaml.get('name') or path.stem, |
21 | | - 'hidden': yaml.get('hidden', False), |
22 | | - 'scope': yaml.get('scope'), |
23 | | - } |
24 | | - |
25 | | - |
26 | | -def get_tmlanguage_metadata(path: ResourcePath) -> dict: |
27 | | - tree = plistlib.readPlistFromBytes(path.read_bytes()) |
28 | | - |
29 | | - return { |
30 | | - 'name': tree.get('name') or path.stem, |
31 | | - 'hidden': tree.get('hidden', False), |
32 | | - 'scope': tree.get('scopeName'), |
33 | | - } |
34 | | - |
35 | | - |
36 | | -def get_hidden_tmlanguage_metadata(path: ResourcePath) -> dict: |
37 | | - tree = plistlib.readPlistFromBytes(path.read_bytes()) |
38 | | - |
39 | | - return { |
40 | | - 'name': path.stem, # `name` key is ignored |
41 | | - 'hidden': True, # `hidden` key is ignored |
42 | | - 'scope': tree.get('scopeName'), |
43 | | - } |
44 | | - |
45 | | - |
46 | | -SYNTAX_TYPES = { |
47 | | - '.sublime-syntax': get_sublime_syntax_metadata, |
48 | | - '.tmLanguage': get_tmlanguage_metadata, |
49 | | - '.hidden-tmLanguage': get_hidden_tmlanguage_metadata, |
50 | | -} |
51 | | - |
52 | | - |
53 | | -def get_syntax_metadata(path: ResourcePath) -> SyntaxInfo: |
54 | | - return SyntaxInfo( |
55 | | - path=str(path), |
56 | | - **SYNTAX_TYPES[path.suffix](path) |
57 | | - ) |
58 | | - |
59 | | - |
60 | | -def list_syntaxes() -> List[SyntaxInfo]: |
61 | | - """Return a list of all loaded syntax definitions. |
62 | | -
|
63 | | - Each item is a :class:`namedtuple` with the following properties: |
64 | | -
|
65 | | - path |
66 | | - The resource path to the syntax definition file. |
67 | | -
|
68 | | - name |
69 | | - The display name of the syntax definition. |
70 | | -
|
71 | | - scope |
72 | | - The top-level scope of the syntax. |
73 | | -
|
74 | | - hidden |
75 | | - Whether the syntax will appear in the syntax menus and the command palette. |
76 | | - """ |
77 | | - syntax_definition_paths = [ |
78 | | - path for path in ResourcePath.glob_resources('') |
79 | | - if path.suffix in SYNTAX_TYPES |
80 | | - ] |
81 | | - |
82 | | - return [ |
83 | | - get_syntax_metadata(path) |
84 | | - for path in syntax_definition_paths |
85 | | - if not ( |
86 | | - path.suffix in {'.tmLanguage', '.hidden-tmLanguage'} |
87 | | - and path.with_suffix('.sublime-syntax') in syntax_definition_paths |
88 | | - ) |
89 | | - ] |
| 8 | +def list_syntaxes() -> list[sublime.Syntax]: |
| 9 | + print("sublime_lib.list_syntaxes() is deprecated," |
| 10 | + " use sublime.list_syntaxes() instead!") |
| 11 | + return sublime.list_syntaxes() |
90 | 12 |
|
91 | 13 |
|
92 | 14 | def get_syntax_for_scope(scope: str) -> str: |
93 | | - """Returns the last syntax in load order that matches `scope`.""" |
94 | | - try: |
95 | | - return next( |
96 | | - syntax.path |
97 | | - for syntax in reversed(list_syntaxes()) |
98 | | - if syntax.scope == scope |
99 | | - ) |
100 | | - except StopIteration: |
101 | | - raise ValueError("Cannot find syntax for scope {!r}.".format(scope)) from None |
| 15 | + print("sublime_lib.get_syntax_for_scope() is deprecated," |
| 16 | + " use sublime.find_syntax_by_scope() instead!") |
| 17 | + syntax = sublime.find_syntax_by_scope(scope) |
| 18 | + return syntax[0].path if syntax else "" |
0 commit comments