Skip to content
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

Add default syntax mapping to CSS theme in TextArea #4149

Merged
merged 2 commits into from
Feb 12, 2024
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

### Added

- Add some syntax highlighting to TextArea default theme https://github.com/Textualize/textual/pull/4149

## [0.51.1] - 2024-02-09

Expand Down
2 changes: 1 addition & 1 deletion src/textual/_text_area_theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ def default(cls) -> TextAreaTheme:
},
)

_CSS_THEME = TextAreaTheme(name="css")
_CSS_THEME = TextAreaTheme(name="css", syntax_styles=_DARK_VS.syntax_styles)

_BUILTIN_THEMES = {
"css": _CSS_THEME,
Expand Down
30 changes: 15 additions & 15 deletions src/textual/widgets/_text_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -983,21 +983,6 @@ def render_line(self, widget_y: int) -> Strip:
selection_top_row, selection_top_column = selection_top
selection_bottom_row, selection_bottom_column = selection_bottom

highlights = self._highlights
if highlights and theme:
line_bytes = _utf8_encode(line_string)
byte_to_codepoint = build_byte_to_codepoint_dict(line_bytes)
get_highlight_from_theme = theme.syntax_styles.get
line_highlights = highlights[line_index]
for highlight_start, highlight_end, highlight_name in line_highlights:
node_style = get_highlight_from_theme(highlight_name)
if node_style is not None:
line.stylize(
node_style,
byte_to_codepoint.get(highlight_start, 0),
byte_to_codepoint.get(highlight_end) if highlight_end else None,
)

cursor_line_style = theme.cursor_line_style if theme else None
if cursor_line_style and cursor_row == line_index:
line.stylize(cursor_line_style)
Expand Down Expand Up @@ -1032,6 +1017,21 @@ def render_line(self, widget_y: int) -> Strip:
else:
line.stylize(selection_style, end=line_character_count)

highlights = self._highlights
Copy link
Member Author

Choose a reason for hiding this comment

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

Just reordering the style sandwich ingredients here, no actual change to the code that is inside the diff.

if highlights and theme:
line_bytes = _utf8_encode(line_string)
byte_to_codepoint = build_byte_to_codepoint_dict(line_bytes)
get_highlight_from_theme = theme.syntax_styles.get
line_highlights = highlights[line_index]
for highlight_start, highlight_end, highlight_name in line_highlights:
node_style = get_highlight_from_theme(highlight_name)
if node_style is not None:
line.stylize(
node_style,
byte_to_codepoint.get(highlight_start, 0),
byte_to_codepoint.get(highlight_end) if highlight_end else None,
)

# Highlight the cursor
matching_bracket = self._matching_bracket_location
match_cursor_bracket = self.match_cursor_bracket
Expand Down
Loading
Loading