Skip to content

Commit 9d18242

Browse files
authored
Sunset syntax module (#187)
Turn functions from sublime_lib.syntax module into aliases of sublime API calls and prints deprecation warnings when being called. ST4 provides compatible API calls, which can be used as drop-in replacement. This change assumes SyntaxInfo members are accessed in sane ways, which don't cause trouble by sublime.Syntax being a normal class. This change fixes a compatibility issue with python 3.13, as plistlib removed `readPlistFromBytes` function. Deprecation is commmunicated via plump prints as warnings.deprecated is not supported by Python 3.8.
1 parent 93df33a commit 9d18242

File tree

3 files changed

+9
-158
lines changed

3 files changed

+9
-158
lines changed

sublime_lib/_util/simple_yaml.py

Lines changed: 0 additions & 34 deletions
This file was deleted.

sublime_lib/syntax.py

Lines changed: 9 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,18 @@
11
from __future__ import annotations
22

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
94

105
__all__ = ['list_syntaxes', 'get_syntax_for_scope']
116

127

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()
9012

9113

9214
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 ""

tests/test_yaml_util.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)