88 TREE_SITTER = False
99
1010
11- from textual ._tree_sitter import BUILTIN_LANGUAGES
1211from textual .document ._document import Document , EditResult , Location , _utf8_encode
1312
1413
@@ -17,59 +16,30 @@ class SyntaxAwareDocumentError(Exception):
1716
1817
1918class 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
2120 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.
3121 """
3222
3323 def __init__ (
3424 self ,
3525 text : str ,
36- language : str | Language ,
26+ language : Language ,
3727 ):
3828 """Construct a SyntaxAwareDocument.
3929
4030 Args:
4131 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.
4433 """
4534
4635 if not TREE_SITTER :
47- raise RuntimeError ("SyntaxAwareDocument unavailable." )
36+ raise RuntimeError (
37+ "SyntaxAwareDocument unavailable - tree-sitter is not installed."
38+ )
4839
4940 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."""
7343
7444 self ._parser = Parser (self .language )
7545 """The tree-sitter Parser or None if tree-sitter is unavailable."""
@@ -90,16 +60,6 @@ def prepare_query(self, query: str) -> Query | None:
9060 Returns:
9161 The prepared query.
9262 """
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-
10363 return self .language .query (query )
10464
10565 def query_syntax_tree (
@@ -122,12 +82,6 @@ def query_syntax_tree(
12282 Returns:
12383 A tuple containing the nodes and text captured by the query.
12484 """
125-
126- if not TREE_SITTER :
127- raise SyntaxAwareDocumentError (
128- "tree-sitter is not available on this architecture."
129- )
130-
13185 captures_kwargs = {}
13286 if start_point is not None :
13387 captures_kwargs ["start_point" ] = start_point
0 commit comments