From 61f63cd2196e6cec37395fe18996105361ccfafa Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Sun, 11 Feb 2024 20:22:30 +0000 Subject: [PATCH 1/2] Add default syntax mapping to CSS theme in TextArea --- src/textual/_text_area_theme.py | 2 +- src/textual/widgets/_text_area.py | 30 ++++----- .../__snapshots__/test_snapshots.ambr | 67 ++++++++++--------- 3 files changed, 52 insertions(+), 47 deletions(-) diff --git a/src/textual/_text_area_theme.py b/src/textual/_text_area_theme.py index da0ec60d4f..b9f920600b 100644 --- a/src/textual/_text_area_theme.py +++ b/src/textual/_text_area_theme.py @@ -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, diff --git a/src/textual/widgets/_text_area.py b/src/textual/widgets/_text_area.py index 2874c00d55..d5f4d8efef 100644 --- a/src/textual/widgets/_text_area.py +++ b/src/textual/widgets/_text_area.py @@ -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) @@ -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 diff --git a/tests/snapshot_tests/__snapshots__/test_snapshots.ambr b/tests/snapshot_tests/__snapshots__/test_snapshots.ambr index 4c00834c2a..f73d96309d 100644 --- a/tests/snapshot_tests/__snapshots__/test_snapshots.ambr +++ b/tests/snapshot_tests/__snapshots__/test_snapshots.ambr @@ -37227,77 +37227,82 @@ font-weight: 700; } - .terminal-3978829874-matrix { + .terminal-421307802-matrix { font-family: Fira Code, monospace; font-size: 20px; line-height: 24.4px; font-variant-east-asian: full-width; } - .terminal-3978829874-title { + .terminal-421307802-title { font-size: 18px; font-weight: bold; font-family: arial; } - .terminal-3978829874-r1 { fill: #1e1e1e } - .terminal-3978829874-r2 { fill: #0178d4 } - .terminal-3978829874-r3 { fill: #c5c8c6 } - .terminal-3978829874-r4 { fill: #7d7e7a } - .terminal-3978829874-r5 { fill: #f8f8f2 } - .terminal-3978829874-r6 { fill: #abaca9;font-weight: bold } - .terminal-3978829874-r7 { fill: #151515 } + .terminal-421307802-r1 { fill: #1e1e1e } + .terminal-421307802-r2 { fill: #0178d4 } + .terminal-421307802-r3 { fill: #c5c8c6 } + .terminal-421307802-r4 { fill: #7d7e7a } + .terminal-421307802-r5 { fill: #569cd6 } + .terminal-421307802-r6 { fill: #f8f8f2 } + .terminal-421307802-r7 { fill: #4ec9b0 } + .terminal-421307802-r8 { fill: #abaca9;font-weight: bold } + .terminal-421307802-r9 { fill: #b5cea8 } + .terminal-421307802-r10 { fill: #151515 } + .terminal-421307802-r11 { fill: #7daf9c } + .terminal-421307802-r12 { fill: #ce9178 } - + - + - + - + - + - + - + - + - + - TextAreaSnapshot + TextAreaSnapshot - - - - ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔ - 1  def hello(name): - 2      x = 123                               - 3      while not False:  - 4          print("hello " + name)  - 5          continue  - 6   - - ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ + + + + ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔ + 1  defhello(name): + 2      x =123 + 3  whilenotFalse + 4  print("hello "+ name)  + 5  continue + 6   + + ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ From fd8505dc9449006ad57e48a3b0cb07eace4bd322 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Mon, 12 Feb 2024 11:19:04 +0000 Subject: [PATCH 2/2] Update CHANGELOG --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a104dd737..2241508c9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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