Files
dssc/vendor/textual/highlight.py
T
lucioandClaude Opus 5.5 60532a4043 Rebuild the UI on vendored Textual; make the checkout self-contained
The internal servers have no access to outside package repositories and
run Python 3.8/3.9, so the UI moves from hand-drawn curses to Textual
6.2.1 (the last release supporting 3.8.1), vendored with its pure-Python
dependencies in vendor/. The launcher puts vendor/ first on sys.path, so a
plain checkout runs with no pip and no network.

- dsflow/: the single script split into a package (pure moves of the
  backend sections); action rules moved to ops.py, free of Textual
- dsflow/ui/: tabs, detail pane, live sanity-run panel, log viewer,
  modal dialogs, filter, themes, mouse support
- tests/test_ui.py: headless UI tests through Textual's pilot
- tools/vendor.py (refresh vendor/), tools/screenshots.py (README images)
- README, CLAUDE.md, TODO and the feature doc updated for the new layout,
  the Python 3.8.1 floor and the vendoring rules

Verified: 11 tests green on Python 3.12 and 3.8.20; the shipped file set
runs under `python -I -S` on both.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-09-24 03:24:21 -07:00

154 lines
4.6 KiB
Python
Vendored

from __future__ import annotations
import os
from typing import Tuple
from pygments.lexer import Lexer
from pygments.lexers import get_lexer_by_name, guess_lexer_for_filename
from pygments.token import Token
from pygments.util import ClassNotFound
from textual.content import Content, Span
TokenType = Tuple[str, ...]
class HighlightTheme:
"""Contains the style definition for user with the highlight method."""
STYLES: dict[TokenType, str] = {
Token.Comment: "$text 60%",
Token.Error: "$text-error on $error-muted",
Token.Generic.Strong: "bold",
Token.Generic.Emph: "italic",
Token.Generic.Error: "$text-error on $error-muted",
Token.Generic.Heading: "$text-primary underline",
Token.Generic.Subheading: "$text-primary",
Token.Keyword: "$text-accent",
Token.Keyword.Constant: "bold $text-success 80%",
Token.Keyword.Namespace: "$text-error",
Token.Keyword.Type: "bold",
Token.Literal.Number: "$text-warning",
Token.Literal.String.Backtick: "$text 60%",
Token.Literal.String: "$text-success 90%",
Token.Literal.String.Doc: "$text-success 80% italic",
Token.Literal.String.Double: "$text-success 90%",
Token.Name: "$text-primary",
Token.Name.Attribute: "$text-warning",
Token.Name.Builtin: "$text-accent",
Token.Name.Builtin.Pseudo: "italic",
Token.Name.Class: "$text-warning bold",
Token.Name.Constant: "$text-error",
Token.Name.Decorator: "$text-primary bold",
Token.Name.Function: "$text-warning underline",
Token.Name.Function.Magic: "$text-warning underline",
Token.Name.Tag: "$text-primary bold",
Token.Name.Variable: "$text-secondary",
Token.Number: "$text-warning",
Token.Operator: "bold",
Token.Operator.Word: "bold $text-error",
Token.String: "$text-success",
Token.Whitespace: "",
}
def guess_language(code: str, path: str) -> str:
"""Guess the language based on the code and path.
The result may be used in the [highlight][textual.highlight.highlight] function.
Args:
code: The code to guess from.
path: A path to the code.
Returns:
The language, suitable for use with Pygments.
"""
if path is not None and os.path.splitext(path)[-1] == ".tcss":
# A special case for TCSS files which aren't known outside of Textual
return "scss"
lexer: Lexer | None = None
lexer_name = "default"
if code:
try:
lexer = guess_lexer_for_filename(path, code)
except ClassNotFound:
pass
if not lexer:
try:
_, ext = os.path.splitext(path)
if ext:
extension = ext.lstrip(".").lower()
lexer = get_lexer_by_name(extension)
except ClassNotFound:
pass
if lexer:
if lexer.aliases:
lexer_name = lexer.aliases[0]
else:
lexer_name = lexer.name
return lexer_name
def highlight(
code: str,
*,
language: str | None = None,
path: str | None = None,
theme: type[HighlightTheme] = HighlightTheme,
tab_size: int = 8,
) -> Content:
"""Apply syntax highlighting to a string.
Args:
code: A string to highlight.
language: The language to highlight.
theme: A HighlightTheme class (type not instance).
tab_size: Number of spaces in a tab.
Returns:
A Content instance which may be used in a widget.
"""
if language is None:
if path is None:
raise RuntimeError("One of 'language' or 'path' must be supplied.")
language = guess_language(code, path)
assert language is not None
code = "\n".join(code.splitlines())
try:
lexer = get_lexer_by_name(
language,
stripnl=False,
ensurenl=True,
tabsize=tab_size,
)
except ClassNotFound:
lexer = get_lexer_by_name(
"text",
stripnl=False,
ensurenl=True,
tabsize=tab_size,
)
token_start = 0
spans: list[Span] = []
styles = theme.STYLES
for token_type, token in lexer.get_tokens(code):
token_end = token_start + len(token)
while True:
if style := styles.get(token_type):
spans.append(Span(token_start, token_end, style))
break
if (token_type := token_type.parent) is None:
break
token_start = token_end
highlighted_code = Content(code, spans=spans).stylize_before("$text")
return highlighted_code