Skip to content

Conversation

@nikhil2004-blip
Copy link

Problem

The current python.scm syntax highlighting queries are too aggressive. They use broad regular expressions to match any identifier starting with a capital letter as a @type or @constructor.

This results in incorrect syntax highlighting behavior, such as:

Variables like Final_Score

Function calls

Even commented text

…being highlighted as Types or Constructors simply because they start with a capital letter.
This leads to inconsistent "rainbow coloring" and reduces readability for Python code displayed inside TextArea.

Solution

The heuristic rules depending solely on regex matching for types and constructors have been disabled.
These were removed in:

File: src/textual/tree-sitter/highlights/python.scm

Removed queries:

((identifier) @type
(#match? @type "^[A-Z].*[a-z]"))

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

This ensures Tree-sitter relies more on proper semantic analysis instead of unreliable capitalization guessing.

The result:
✔ Cleaner syntax highlighting
✔ More accurate code representation
✔ No more distracting false-type coloring

  • Docstrings on all new or modified functions / classes
  • Updated documentation
  • Updated CHANGELOG.md (where appropriate)

@TomJGooding
Copy link
Collaborator

Even commented text [is incorrectly] highlighted as Types or Constructors simply because they start with a capital letter.

I can't reproduce this?

image
from textual.app import App, ComposeResult
from textual.widgets import TextArea

CODE = """\
# lower

# Title

# UPPER\
"""


class ExampleApp(App):
    CSS = """
    TextArea { 
        width: 40; 
        height: auto;
    }
    """

    def compose(self) -> ComposeResult:
        yield TextArea(CODE, language="python")


if __name__ == "__main__":
    app = ExampleApp()
    app.run()

Comment on lines 12 to -16
;; Identifier naming conventions
((identifier) @type
(#match? @type "^[A-Z].*[a-z]"))
((identifier) @constant
(#match? @constant "^[A-Z][A-Z_0-9]*$"))
Copy link
Collaborator

Choose a reason for hiding this comment

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

This follows naming conventions as mentioned in the comment above.

Here's a simple example of variables with different casings in Python:

score = 999
Score = 999
SCORE = 999

Notice the syntax highlighting on GitHub and the screenshot below from Neovim:

image

@nikhil2004-blip nikhil2004-blip deleted the fix/syntax-highlighting branch December 9, 2025 10:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants