Skip to content

chore(text area): prepare for tree-sitter breaking changes #5965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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.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' }}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps time to drop support for Python 3.8 - it's been end-of-life for a long time now.

- name: Upload snapshot report
if: always()
uses: actions/upload-artifact@v4
Expand Down
65 changes: 15 additions & 50 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ platformdirs = ">=3.6.0,<5"
# start of [syntax] extras
# Require tree-sitter >= 0.23.0 and python >= 3.9
# 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" }

# FIX: Temporarily install tree-sitter from GitHub to test against breaking changes before v0.25.0 is released on PyPI
tree-sitter = { git = "https://github.com/tree-sitter/py-tree-sitter.git", tag = "v0.25.0", python = ">=3.10" }
# 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" }
Expand Down
5 changes: 3 additions & 2 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 Down Expand Up @@ -88,7 +88,8 @@ def query_syntax_tree(
if end_point is not None:
captures_kwargs["end_point"] = end_point

captures = query.captures(self._syntax_tree.root_node, **captures_kwargs)
cursor = QueryCursor(query)
captures = cursor.captures(self._syntax_tree.root_node, **captures_kwargs)
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