8
8
TREE_SITTER = False
9
9
10
10
11
- from textual ._tree_sitter import BUILTIN_LANGUAGES
12
11
from textual .document ._document import Document , EditResult , Location , _utf8_encode
13
12
14
13
@@ -17,59 +16,30 @@ class SyntaxAwareDocumentError(Exception):
17
16
18
17
19
18
class SyntaxAwareDocument (Document ):
20
- """A wrapper around a Document which also maintains a tree-sitter syntax
19
+ """A subclass of Document which also maintains a tree-sitter syntax
21
20
tree when the document is edited.
22
-
23
- The primary reason for this split is actually to keep tree-sitter stuff separate,
24
- since it isn't supported in Python 3.7. By having the tree-sitter code
25
- isolated in this subclass, it makes it easier to conditionally import. However,
26
- it does come with other design flaws (e.g. Document is required to have methods
27
- which only really make sense on SyntaxAwareDocument).
28
-
29
- If you're reading this and Python 3.7 is no longer supported by Textual,
30
- consider merging this subclass into the `Document` superclass.
31
21
"""
32
22
33
23
def __init__ (
34
24
self ,
35
25
text : str ,
36
- language : str | Language ,
26
+ language : Language ,
37
27
):
38
28
"""Construct a SyntaxAwareDocument.
39
29
40
30
Args:
41
31
text: The initial text contained in the document.
42
- language: The language to use. You can pass a string to use a supported
43
- language, or pass in your own tree-sitter `Language` object.
32
+ language: The tree-sitter language to use.
44
33
"""
45
34
46
35
if not TREE_SITTER :
47
- raise RuntimeError ("SyntaxAwareDocument unavailable." )
36
+ raise RuntimeError (
37
+ "SyntaxAwareDocument unavailable - tree-sitter is not installed."
38
+ )
48
39
49
40
super ().__init__ (text )
50
- self .language : Language | None = None
51
- """The tree-sitter Language or None if tree-sitter is unavailable."""
52
-
53
- self ._parser : Parser | None = None
54
-
55
- from textual ._tree_sitter import get_language
56
-
57
- # If the language is `None`, then avoid doing any parsing related stuff.
58
- if isinstance (language , str ):
59
- if language not in BUILTIN_LANGUAGES :
60
- raise SyntaxAwareDocumentError (f"Invalid language { language !r} " )
61
- # If tree-sitter-languages is not installed properly, get_language
62
- # and get_parser may raise an OSError when unable to load their
63
- # resources
64
-
65
- try :
66
- self .language = get_language (language )
67
- except OSError as e :
68
- raise SyntaxAwareDocumentError (
69
- f"Could not find binaries for { language !r} "
70
- ) from e
71
- else :
72
- self .language = language
41
+ self .language : Language = language
42
+ """The tree-sitter Language."""
73
43
74
44
self ._parser = Parser (self .language )
75
45
"""The tree-sitter Parser or None if tree-sitter is unavailable."""
@@ -90,16 +60,6 @@ def prepare_query(self, query: str) -> Query | None:
90
60
Returns:
91
61
The prepared query.
92
62
"""
93
- if not TREE_SITTER :
94
- raise SyntaxAwareDocumentError (
95
- "Couldn't prepare query - tree-sitter is not available on this architecture."
96
- )
97
-
98
- if self .language is None :
99
- raise SyntaxAwareDocumentError (
100
- "Couldn't prepare query - no language assigned."
101
- )
102
-
103
63
return self .language .query (query )
104
64
105
65
def query_syntax_tree (
@@ -122,12 +82,6 @@ def query_syntax_tree(
122
82
Returns:
123
83
A tuple containing the nodes and text captured by the query.
124
84
"""
125
-
126
- if not TREE_SITTER :
127
- raise SyntaxAwareDocumentError (
128
- "tree-sitter is not available on this architecture."
129
- )
130
-
131
85
captures_kwargs = {}
132
86
if start_point is not None :
133
87
captures_kwargs ["start_point" ] = start_point
0 commit comments