Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ jobs:
cache: "poetry"
- name: Install dependencies
run: poetry install --no-interaction --extras syntax
if: ${{ matrix.python-version != '3.8' }}
- name: Install dependencies for 3.8
if: ${{ matrix.python-version != '3.8' && matrix.python-version != '3.9' }}
- name: Install dependencies for 3.8 and 3.9
run: poetry install --no-interaction
if: ${{ matrix.python-version == '3.8' }}
- name: Test with pytest (Py39+ - with syntax highlighting)
if: ${{ matrix.python-version == '3.8' || matrix.python-version == '3.9' }}
- name: Test with pytest (Py310+ - with syntax highlighting)
run: |
poetry run pytest tests -v --cov=./src/textual --cov-report=xml:./coverage.xml --cov-report term-missing
if: ${{ matrix.python-version != '3.8' }}
- name: Test with pytest (Py38 - without syntax highlighting)
if: ${{ matrix.python-version != '3.8' && matrix.python-version != '3.9' }}
- name: Test with pytest (Py39 - without syntax highlighting)
run: |
poetry run pytest tests -v --cov=./src/textual --cov-report=xml:./coverage.xml --cov-report term-missing -m 'not syntax'
if: ${{ matrix.python-version == '3.8' }}
if: ${{ matrix.python-version == '3.8' || matrix.python-version == '3.9' }}
- name: Upload snapshot report
if: always()
uses: actions/upload-artifact@v4
Expand Down
140 changes: 64 additions & 76 deletions poetry.lock

Large diffs are not rendered by default.

34 changes: 17 additions & 17 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,24 @@ typing-extensions = "^4.4.0"
platformdirs = ">=3.6.0,<5"

# start of [syntax] extras
# Require tree-sitter >= 0.23.0 and python >= 3.9
# Require tree-sitter >= 0.25.0 and python >= 3.10
# Windows, MacOS and Linux binary wheels are available for all of the languages below.
tree-sitter = { version = ">=0.23.0", optional = true, python = ">=3.9" }
tree-sitter-python = { version = ">=0.23.0", optional = true, python = ">=3.9" }
tree-sitter-markdown = { version = ">=0.3.0", optional = true, python = ">=3.9"}
tree-sitter-json = { version = ">=0.24.0", optional = true, python = ">=3.9" }
tree-sitter-toml = { version = ">=0.6.0", optional = true, python = ">=3.9" }
tree-sitter-yaml = { version = ">=0.6.0", optional = true, python = ">=3.9" }
tree-sitter-html = { version = ">=0.23.0", optional = true, python = ">=3.9" }
tree-sitter-css = { version = ">=0.23.0", optional = true, python = ">=3.9" }
tree-sitter-javascript = { version = ">=0.23.0", optional = true, python = ">=3.9" }
tree-sitter-rust = { version = ">=0.23.0,<=0.23.2", optional = true, python = ">=3.9" }
tree-sitter-go = { version = ">=0.23.0", optional = true, python = ">=3.9" }
tree-sitter-regex = { version = ">=0.24.0", optional = true, python = ">=3.9" }
tree-sitter-xml = { version = ">=0.7.0", optional = true, python = ">=3.9" }
tree-sitter-sql = { version = ">=0.3.0,<0.3.8", optional = true, python = ">=3.9" }
tree-sitter-java = { version = ">=0.23.0", optional = true, python = ">=3.9" }
tree-sitter-bash = { version = ">=0.23.0", optional = true, python = ">=3.9" }
tree-sitter = { version = ">=0.25.0", optional = true, python = ">=3.10" }
tree-sitter-python = { version = ">=0.23.0", optional = true, python = ">=3.10" }
tree-sitter-markdown = { version = ">=0.3.0", optional = true, python = ">=3.10"}
tree-sitter-json = { version = ">=0.24.0", optional = true, python = ">=3.10" }
tree-sitter-toml = { version = ">=0.6.0", optional = true, python = ">=3.10" }
tree-sitter-yaml = { version = ">=0.6.0", optional = true, python = ">=3.10" }
tree-sitter-html = { version = ">=0.23.0", optional = true, python = ">=3.10" }
tree-sitter-css = { version = ">=0.23.0", optional = true, python = ">=3.10" }
tree-sitter-javascript = { version = ">=0.23.0", optional = true, python = ">=3.10" }
tree-sitter-rust = { version = ">=0.23.0,<=0.23.2", optional = true, python = ">=3.10" }
tree-sitter-go = { version = ">=0.23.0", optional = true, python = ">=3.10" }
tree-sitter-regex = { version = ">=0.24.0", optional = true, python = ">=3.10" }
tree-sitter-xml = { version = ">=0.7.0", optional = true, python = ">=3.10" }
tree-sitter-sql = { version = ">=0.3.0,<0.3.8", optional = true, python = ">=3.10" }
tree-sitter-java = { version = ">=0.23.0", optional = true, python = ">=3.10" }
tree-sitter-bash = { version = ">=0.23.0", optional = true, python = ">=3.10" }
# end of [syntax] extras
pygments = "^2.19.2"

Expand Down
6 changes: 3 additions & 3 deletions src/textual/_tree_sitter.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from __future__ import annotations

from importlib import import_module

from textual import log


try:
from tree_sitter import Language

Expand All @@ -24,9 +24,9 @@ def get_language(language_name: str) -> Language | None:
if language_name == "xml":
# xml uses language_xml() instead of language()
# it's the only outlier amongst the languages in the `textual[syntax]` extra
language = Language(module.language_xml(), name=language_name)
language = Language(module.language_xml())
else:
language = Language(module.language(), name=language_name)
language = Language(module.language())
except (OSError, AttributeError):
log.warning(f"Could not load language {language_name!r}.")
return None
Expand Down
22 changes: 14 additions & 8 deletions src/textual/document/_syntax_aware_document.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

try:
from tree_sitter import Language, Node, Parser, Query, Tree
from tree_sitter import Language, Node, Parser, Query, QueryCursor, Tree

TREE_SITTER = True
except ImportError:
Expand All @@ -10,6 +10,8 @@

from textual.document._document import Document, EditResult, Location, _utf8_encode

_UINT32_MAX = 0xFFFFFFFF


class SyntaxAwareDocumentError(Exception):
"""General error raised when SyntaxAwareDocument is used incorrectly."""
Expand Down Expand Up @@ -60,7 +62,7 @@ def prepare_query(self, query: str) -> Query | None:
Returns:
The prepared query.
"""
return self.language.query(query)
return Query(self.language, query)

def query_syntax_tree(
self,
Expand All @@ -82,13 +84,17 @@ def query_syntax_tree(
Returns:
A tuple containing the nodes and text captured by the query.
"""
captures_kwargs = {}
if start_point is not None:
captures_kwargs["start_point"] = start_point
if end_point is not None:
captures_kwargs["end_point"] = end_point
cursor = QueryCursor(query)

if start_point is not None or end_point is not None:
if start_point is None:
start_point = (0, 0)
if end_point is None:
end_point = (_UINT32_MAX, _UINT32_MAX)

cursor.set_point_range(start_point, end_point)

captures = query.captures(self._syntax_tree.root_node, **captures_kwargs)
captures = cursor.captures(self._syntax_tree.root_node)
return captures

def replace_range(self, start: Location, end: Location, text: str) -> EditResult:
Expand Down
4 changes: 2 additions & 2 deletions src/textual/tree-sitter/highlights/css.scm
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@
(namespace_name) @namespace

((property_name) @type.definition
(#lua-match? @type.definition "^[-][-]"))
(#match? @type.definition "^[-][-]"))
((plain_value) @type
(#lua-match? @type "^[-][-]"))
(#match? @type "^[-][-]"))

[
(string_value)
Expand Down
8 changes: 4 additions & 4 deletions src/textual/tree-sitter/highlights/python.scm
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

;; Identifier naming conventions
((identifier) @type
(#lua-match? @type "^[A-Z].*[a-z]"))
(#match? @type "^[A-Z].*[a-z]"))
((identifier) @constant
(#lua-match? @constant "^[A-Z][A-Z_0-9]*$"))
(#match? @constant "^[A-Z][A-Z_0-9]*$"))

((attribute
attribute: (identifier) @field)
Expand Down Expand Up @@ -59,12 +59,12 @@

((call
function: (identifier) @constructor)
(#lua-match? @constructor "^[A-Z]"))
(#match? @constructor "^[A-Z]"))

((call
function: (attribute
attribute: (identifier) @constructor))
(#lua-match? @constructor "^[A-Z]"))
(#match? @constructor "^[A-Z]"))

;; Decorators

Expand Down
Loading
Loading