Skip to content

Commit

Permalink
Add default syntax mapping to CSS theme in TextArea (#4149)
Browse files Browse the repository at this point in the history
* Add default syntax mapping to CSS theme in TextArea

* Update CHANGELOG
  • Loading branch information
darrenburns authored Feb 12, 2024
1 parent 6b14305 commit db4760b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 47 deletions.
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
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

0 comments on commit db4760b

Please sign in to comment.