syntax.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  1. import os.path
  2. import re
  3. import sys
  4. import textwrap
  5. from abc import ABC, abstractmethod
  6. from pathlib import Path
  7. from typing import (
  8. Any,
  9. Dict,
  10. Iterable,
  11. List,
  12. NamedTuple,
  13. Optional,
  14. Sequence,
  15. Set,
  16. Tuple,
  17. Type,
  18. Union,
  19. )
  20. from pygments.lexer import Lexer
  21. from pygments.lexers import get_lexer_by_name, guess_lexer_for_filename
  22. from pygments.style import Style as PygmentsStyle
  23. from pygments.styles import get_style_by_name
  24. from pygments.token import (
  25. Comment,
  26. Error,
  27. Generic,
  28. Keyword,
  29. Name,
  30. Number,
  31. Operator,
  32. String,
  33. Token,
  34. Whitespace,
  35. )
  36. from pygments.util import ClassNotFound
  37. from rich.containers import Lines
  38. from rich.padding import Padding, PaddingDimensions
  39. from ._loop import loop_first
  40. from .cells import cell_len
  41. from .color import Color, blend_rgb
  42. from .console import Console, ConsoleOptions, JustifyMethod, RenderResult
  43. from .jupyter import JupyterMixin
  44. from .measure import Measurement
  45. from .segment import Segment, Segments
  46. from .style import Style, StyleType
  47. from .text import Text
  48. TokenType = Tuple[str, ...]
  49. WINDOWS = sys.platform == "win32"
  50. DEFAULT_THEME = "monokai"
  51. # The following styles are based on https://github.com/pygments/pygments/blob/master/pygments/formatters/terminal.py
  52. # A few modifications were made
  53. ANSI_LIGHT: Dict[TokenType, Style] = {
  54. Token: Style(),
  55. Whitespace: Style(color="white"),
  56. Comment: Style(dim=True),
  57. Comment.Preproc: Style(color="cyan"),
  58. Keyword: Style(color="blue"),
  59. Keyword.Type: Style(color="cyan"),
  60. Operator.Word: Style(color="magenta"),
  61. Name.Builtin: Style(color="cyan"),
  62. Name.Function: Style(color="green"),
  63. Name.Namespace: Style(color="cyan", underline=True),
  64. Name.Class: Style(color="green", underline=True),
  65. Name.Exception: Style(color="cyan"),
  66. Name.Decorator: Style(color="magenta", bold=True),
  67. Name.Variable: Style(color="red"),
  68. Name.Constant: Style(color="red"),
  69. Name.Attribute: Style(color="cyan"),
  70. Name.Tag: Style(color="bright_blue"),
  71. String: Style(color="yellow"),
  72. Number: Style(color="blue"),
  73. Generic.Deleted: Style(color="bright_red"),
  74. Generic.Inserted: Style(color="green"),
  75. Generic.Heading: Style(bold=True),
  76. Generic.Subheading: Style(color="magenta", bold=True),
  77. Generic.Prompt: Style(bold=True),
  78. Generic.Error: Style(color="bright_red"),
  79. Error: Style(color="red", underline=True),
  80. }
  81. ANSI_DARK: Dict[TokenType, Style] = {
  82. Token: Style(),
  83. Whitespace: Style(color="bright_black"),
  84. Comment: Style(dim=True),
  85. Comment.Preproc: Style(color="bright_cyan"),
  86. Keyword: Style(color="bright_blue"),
  87. Keyword.Type: Style(color="bright_cyan"),
  88. Operator.Word: Style(color="bright_magenta"),
  89. Name.Builtin: Style(color="bright_cyan"),
  90. Name.Function: Style(color="bright_green"),
  91. Name.Namespace: Style(color="bright_cyan", underline=True),
  92. Name.Class: Style(color="bright_green", underline=True),
  93. Name.Exception: Style(color="bright_cyan"),
  94. Name.Decorator: Style(color="bright_magenta", bold=True),
  95. Name.Variable: Style(color="bright_red"),
  96. Name.Constant: Style(color="bright_red"),
  97. Name.Attribute: Style(color="bright_cyan"),
  98. Name.Tag: Style(color="bright_blue"),
  99. String: Style(color="yellow"),
  100. Number: Style(color="bright_blue"),
  101. Generic.Deleted: Style(color="bright_red"),
  102. Generic.Inserted: Style(color="bright_green"),
  103. Generic.Heading: Style(bold=True),
  104. Generic.Subheading: Style(color="bright_magenta", bold=True),
  105. Generic.Prompt: Style(bold=True),
  106. Generic.Error: Style(color="bright_red"),
  107. Error: Style(color="red", underline=True),
  108. }
  109. RICH_SYNTAX_THEMES = {"ansi_light": ANSI_LIGHT, "ansi_dark": ANSI_DARK}
  110. NUMBERS_COLUMN_DEFAULT_PADDING = 2
  111. class SyntaxTheme(ABC):
  112. """Base class for a syntax theme."""
  113. @abstractmethod
  114. def get_style_for_token(self, token_type: TokenType) -> Style:
  115. """Get a style for a given Pygments token."""
  116. raise NotImplementedError # pragma: no cover
  117. @abstractmethod
  118. def get_background_style(self) -> Style:
  119. """Get the background color."""
  120. raise NotImplementedError # pragma: no cover
  121. class PygmentsSyntaxTheme(SyntaxTheme):
  122. """Syntax theme that delegates to Pygments theme."""
  123. def __init__(self, theme: Union[str, Type[PygmentsStyle]]) -> None:
  124. self._style_cache: Dict[TokenType, Style] = {}
  125. if isinstance(theme, str):
  126. try:
  127. self._pygments_style_class = get_style_by_name(theme)
  128. except ClassNotFound:
  129. self._pygments_style_class = get_style_by_name("default")
  130. else:
  131. self._pygments_style_class = theme
  132. self._background_color = self._pygments_style_class.background_color
  133. self._background_style = Style(bgcolor=self._background_color)
  134. def get_style_for_token(self, token_type: TokenType) -> Style:
  135. """Get a style from a Pygments class."""
  136. try:
  137. return self._style_cache[token_type]
  138. except KeyError:
  139. try:
  140. pygments_style = self._pygments_style_class.style_for_token(token_type)
  141. except KeyError:
  142. style = Style.null()
  143. else:
  144. color = pygments_style["color"]
  145. bgcolor = pygments_style["bgcolor"]
  146. style = Style(
  147. color="#" + color if color else "#000000",
  148. bgcolor="#" + bgcolor if bgcolor else self._background_color,
  149. bold=pygments_style["bold"],
  150. italic=pygments_style["italic"],
  151. underline=pygments_style["underline"],
  152. )
  153. self._style_cache[token_type] = style
  154. return style
  155. def get_background_style(self) -> Style:
  156. return self._background_style
  157. class ANSISyntaxTheme(SyntaxTheme):
  158. """Syntax theme to use standard colors."""
  159. def __init__(self, style_map: Dict[TokenType, Style]) -> None:
  160. self.style_map = style_map
  161. self._missing_style = Style.null()
  162. self._background_style = Style.null()
  163. self._style_cache: Dict[TokenType, Style] = {}
  164. def get_style_for_token(self, token_type: TokenType) -> Style:
  165. """Look up style in the style map."""
  166. try:
  167. return self._style_cache[token_type]
  168. except KeyError:
  169. # Styles form a hierarchy
  170. # We need to go from most to least specific
  171. # e.g. ("foo", "bar", "baz") to ("foo", "bar") to ("foo",)
  172. get_style = self.style_map.get
  173. token = tuple(token_type)
  174. style = self._missing_style
  175. while token:
  176. _style = get_style(token)
  177. if _style is not None:
  178. style = _style
  179. break
  180. token = token[:-1]
  181. self._style_cache[token_type] = style
  182. return style
  183. def get_background_style(self) -> Style:
  184. return self._background_style
  185. SyntaxPosition = Tuple[int, int]
  186. class _SyntaxHighlightRange(NamedTuple):
  187. """
  188. A range to highlight in a Syntax object.
  189. `start` and `end` are 2-integers tuples, where the first integer is the line number
  190. (starting from 1) and the second integer is the column index (starting from 0).
  191. """
  192. style: StyleType
  193. start: SyntaxPosition
  194. end: SyntaxPosition
  195. style_before: bool = False
  196. class Syntax(JupyterMixin):
  197. """Construct a Syntax object to render syntax highlighted code.
  198. Args:
  199. code (str): Code to highlight.
  200. lexer (Lexer | str): Lexer to use (see https://pygments.org/docs/lexers/)
  201. theme (str, optional): Color theme, aka Pygments style (see https://pygments.org/docs/styles/#getting-a-list-of-available-styles). Defaults to "monokai".
  202. dedent (bool, optional): Enable stripping of initial whitespace. Defaults to False.
  203. line_numbers (bool, optional): Enable rendering of line numbers. Defaults to False.
  204. start_line (int, optional): Starting number for line numbers. Defaults to 1.
  205. line_range (Tuple[int | None, int | None], optional): If given should be a tuple of the start and end line to render.
  206. A value of None in the tuple indicates the range is open in that direction.
  207. highlight_lines (Set[int]): A set of line numbers to highlight.
  208. code_width: Width of code to render (not including line numbers), or ``None`` to use all available width.
  209. tab_size (int, optional): Size of tabs. Defaults to 4.
  210. word_wrap (bool, optional): Enable word wrapping.
  211. background_color (str, optional): Optional background color, or None to use theme color. Defaults to None.
  212. indent_guides (bool, optional): Show indent guides. Defaults to False.
  213. padding (PaddingDimensions): Padding to apply around the syntax. Defaults to 0 (no padding).
  214. """
  215. _pygments_style_class: Type[PygmentsStyle]
  216. _theme: SyntaxTheme
  217. @classmethod
  218. def get_theme(cls, name: Union[str, SyntaxTheme]) -> SyntaxTheme:
  219. """Get a syntax theme instance."""
  220. if isinstance(name, SyntaxTheme):
  221. return name
  222. theme: SyntaxTheme
  223. if name in RICH_SYNTAX_THEMES:
  224. theme = ANSISyntaxTheme(RICH_SYNTAX_THEMES[name])
  225. else:
  226. theme = PygmentsSyntaxTheme(name)
  227. return theme
  228. def __init__(
  229. self,
  230. code: str,
  231. lexer: Union[Lexer, str],
  232. *,
  233. theme: Union[str, SyntaxTheme] = DEFAULT_THEME,
  234. dedent: bool = False,
  235. line_numbers: bool = False,
  236. start_line: int = 1,
  237. line_range: Optional[Tuple[Optional[int], Optional[int]]] = None,
  238. highlight_lines: Optional[Set[int]] = None,
  239. code_width: Optional[int] = None,
  240. tab_size: int = 4,
  241. word_wrap: bool = False,
  242. background_color: Optional[str] = None,
  243. indent_guides: bool = False,
  244. padding: PaddingDimensions = 0,
  245. ) -> None:
  246. self.code = code
  247. self._lexer = lexer
  248. self.dedent = dedent
  249. self.line_numbers = line_numbers
  250. self.start_line = start_line
  251. self.line_range = line_range
  252. self.highlight_lines = highlight_lines or set()
  253. self.code_width = code_width
  254. self.tab_size = tab_size
  255. self.word_wrap = word_wrap
  256. self.background_color = background_color
  257. self.background_style = (
  258. Style(bgcolor=background_color) if background_color else Style()
  259. )
  260. self.indent_guides = indent_guides
  261. self.padding = padding
  262. self._theme = self.get_theme(theme)
  263. self._stylized_ranges: List[_SyntaxHighlightRange] = []
  264. @classmethod
  265. def from_path(
  266. cls,
  267. path: str,
  268. encoding: str = "utf-8",
  269. lexer: Optional[Union[Lexer, str]] = None,
  270. theme: Union[str, SyntaxTheme] = DEFAULT_THEME,
  271. dedent: bool = False,
  272. line_numbers: bool = False,
  273. line_range: Optional[Tuple[int, int]] = None,
  274. start_line: int = 1,
  275. highlight_lines: Optional[Set[int]] = None,
  276. code_width: Optional[int] = None,
  277. tab_size: int = 4,
  278. word_wrap: bool = False,
  279. background_color: Optional[str] = None,
  280. indent_guides: bool = False,
  281. padding: PaddingDimensions = 0,
  282. ) -> "Syntax":
  283. """Construct a Syntax object from a file.
  284. Args:
  285. path (str): Path to file to highlight.
  286. encoding (str): Encoding of file.
  287. lexer (str | Lexer, optional): Lexer to use. If None, lexer will be auto-detected from path/file content.
  288. theme (str, optional): Color theme, aka Pygments style (see https://pygments.org/docs/styles/#getting-a-list-of-available-styles). Defaults to "emacs".
  289. dedent (bool, optional): Enable stripping of initial whitespace. Defaults to True.
  290. line_numbers (bool, optional): Enable rendering of line numbers. Defaults to False.
  291. start_line (int, optional): Starting number for line numbers. Defaults to 1.
  292. line_range (Tuple[int, int], optional): If given should be a tuple of the start and end line to render.
  293. highlight_lines (Set[int]): A set of line numbers to highlight.
  294. code_width: Width of code to render (not including line numbers), or ``None`` to use all available width.
  295. tab_size (int, optional): Size of tabs. Defaults to 4.
  296. word_wrap (bool, optional): Enable word wrapping of code.
  297. background_color (str, optional): Optional background color, or None to use theme color. Defaults to None.
  298. indent_guides (bool, optional): Show indent guides. Defaults to False.
  299. padding (PaddingDimensions): Padding to apply around the syntax. Defaults to 0 (no padding).
  300. Returns:
  301. [Syntax]: A Syntax object that may be printed to the console
  302. """
  303. code = Path(path).read_text(encoding=encoding)
  304. if not lexer:
  305. lexer = cls.guess_lexer(path, code=code)
  306. return cls(
  307. code,
  308. lexer,
  309. theme=theme,
  310. dedent=dedent,
  311. line_numbers=line_numbers,
  312. line_range=line_range,
  313. start_line=start_line,
  314. highlight_lines=highlight_lines,
  315. code_width=code_width,
  316. tab_size=tab_size,
  317. word_wrap=word_wrap,
  318. background_color=background_color,
  319. indent_guides=indent_guides,
  320. padding=padding,
  321. )
  322. @classmethod
  323. def guess_lexer(cls, path: str, code: Optional[str] = None) -> str:
  324. """Guess the alias of the Pygments lexer to use based on a path and an optional string of code.
  325. If code is supplied, it will use a combination of the code and the filename to determine the
  326. best lexer to use. For example, if the file is ``index.html`` and the file contains Django
  327. templating syntax, then "html+django" will be returned. If the file is ``index.html``, and no
  328. templating language is used, the "html" lexer will be used. If no string of code
  329. is supplied, the lexer will be chosen based on the file extension..
  330. Args:
  331. path (AnyStr): The path to the file containing the code you wish to know the lexer for.
  332. code (str, optional): Optional string of code that will be used as a fallback if no lexer
  333. is found for the supplied path.
  334. Returns:
  335. str: The name of the Pygments lexer that best matches the supplied path/code.
  336. """
  337. lexer: Optional[Lexer] = None
  338. lexer_name = "default"
  339. if code:
  340. try:
  341. lexer = guess_lexer_for_filename(path, code)
  342. except ClassNotFound:
  343. pass
  344. if not lexer:
  345. try:
  346. _, ext = os.path.splitext(path)
  347. if ext:
  348. extension = ext.lstrip(".").lower()
  349. lexer = get_lexer_by_name(extension)
  350. except ClassNotFound:
  351. pass
  352. if lexer:
  353. if lexer.aliases:
  354. lexer_name = lexer.aliases[0]
  355. else:
  356. lexer_name = lexer.name
  357. return lexer_name
  358. def _get_base_style(self) -> Style:
  359. """Get the base style."""
  360. default_style = self._theme.get_background_style() + self.background_style
  361. return default_style
  362. def _get_token_color(self, token_type: TokenType) -> Optional[Color]:
  363. """Get a color (if any) for the given token.
  364. Args:
  365. token_type (TokenType): A token type tuple from Pygments.
  366. Returns:
  367. Optional[Color]: Color from theme, or None for no color.
  368. """
  369. style = self._theme.get_style_for_token(token_type)
  370. return style.color
  371. @property
  372. def lexer(self) -> Optional[Lexer]:
  373. """The lexer for this syntax, or None if no lexer was found.
  374. Tries to find the lexer by name if a string was passed to the constructor.
  375. """
  376. if isinstance(self._lexer, Lexer):
  377. return self._lexer
  378. try:
  379. return get_lexer_by_name(
  380. self._lexer,
  381. stripnl=False,
  382. ensurenl=True,
  383. tabsize=self.tab_size,
  384. )
  385. except ClassNotFound:
  386. return None
  387. @property
  388. def default_lexer(self) -> Lexer:
  389. """A Pygments Lexer to use if one is not specified or invalid."""
  390. return get_lexer_by_name(
  391. "text",
  392. stripnl=False,
  393. ensurenl=True,
  394. tabsize=self.tab_size,
  395. )
  396. def highlight(
  397. self,
  398. code: str,
  399. line_range: Optional[Tuple[Optional[int], Optional[int]]] = None,
  400. ) -> Text:
  401. """Highlight code and return a Text instance.
  402. Args:
  403. code (str): Code to highlight.
  404. line_range(Tuple[int, int], optional): Optional line range to highlight.
  405. Returns:
  406. Text: A text instance containing highlighted syntax.
  407. """
  408. base_style = self._get_base_style()
  409. justify: JustifyMethod = (
  410. "default" if base_style.transparent_background else "left"
  411. )
  412. text = Text(
  413. justify=justify,
  414. style=base_style,
  415. tab_size=self.tab_size,
  416. no_wrap=not self.word_wrap,
  417. )
  418. _get_theme_style = self._theme.get_style_for_token
  419. lexer = self.lexer or self.default_lexer
  420. if lexer is None:
  421. text.append(code)
  422. else:
  423. if line_range:
  424. # More complicated path to only stylize a portion of the code
  425. # This speeds up further operations as there are less spans to process
  426. line_start, line_end = line_range
  427. def line_tokenize() -> Iterable[Tuple[Any, str]]:
  428. """Split tokens to one per line."""
  429. assert lexer # required to make MyPy happy - we know lexer is not None at this point
  430. for token_type, token in lexer.get_tokens(code):
  431. while token:
  432. line_token, new_line, token = token.partition("\n")
  433. yield token_type, line_token + new_line
  434. def tokens_to_spans() -> Iterable[Tuple[str, Optional[Style]]]:
  435. """Convert tokens to spans."""
  436. tokens = iter(line_tokenize())
  437. line_no = 0
  438. _line_start = line_start - 1 if line_start else 0
  439. # Skip over tokens until line start
  440. while line_no < _line_start:
  441. try:
  442. _token_type, token = next(tokens)
  443. except StopIteration:
  444. break
  445. yield (token, None)
  446. if token.endswith("\n"):
  447. line_no += 1
  448. # Generate spans until line end
  449. for token_type, token in tokens:
  450. yield (token, _get_theme_style(token_type))
  451. if token.endswith("\n"):
  452. line_no += 1
  453. if line_end and line_no >= line_end:
  454. break
  455. text.append_tokens(tokens_to_spans())
  456. else:
  457. text.append_tokens(
  458. (token, _get_theme_style(token_type))
  459. for token_type, token in lexer.get_tokens(code)
  460. )
  461. if self.background_color is not None:
  462. text.stylize(f"on {self.background_color}")
  463. if self._stylized_ranges:
  464. self._apply_stylized_ranges(text)
  465. return text
  466. def stylize_range(
  467. self,
  468. style: StyleType,
  469. start: SyntaxPosition,
  470. end: SyntaxPosition,
  471. style_before: bool = False,
  472. ) -> None:
  473. """
  474. Adds a custom style on a part of the code, that will be applied to the syntax display when it's rendered.
  475. Line numbers are 1-based, while column indexes are 0-based.
  476. Args:
  477. style (StyleType): The style to apply.
  478. start (Tuple[int, int]): The start of the range, in the form `[line number, column index]`.
  479. end (Tuple[int, int]): The end of the range, in the form `[line number, column index]`.
  480. style_before (bool): Apply the style before any existing styles.
  481. """
  482. self._stylized_ranges.append(
  483. _SyntaxHighlightRange(style, start, end, style_before)
  484. )
  485. def _get_line_numbers_color(self, blend: float = 0.3) -> Color:
  486. background_style = self._theme.get_background_style() + self.background_style
  487. background_color = background_style.bgcolor
  488. if background_color is None or background_color.is_system_defined:
  489. return Color.default()
  490. foreground_color = self._get_token_color(Token.Text)
  491. if foreground_color is None or foreground_color.is_system_defined:
  492. return foreground_color or Color.default()
  493. new_color = blend_rgb(
  494. background_color.get_truecolor(),
  495. foreground_color.get_truecolor(),
  496. cross_fade=blend,
  497. )
  498. return Color.from_triplet(new_color)
  499. @property
  500. def _numbers_column_width(self) -> int:
  501. """Get the number of characters used to render the numbers column."""
  502. column_width = 0
  503. if self.line_numbers:
  504. column_width = (
  505. len(str(self.start_line + self.code.count("\n")))
  506. + NUMBERS_COLUMN_DEFAULT_PADDING
  507. )
  508. return column_width
  509. def _get_number_styles(self, console: Console) -> Tuple[Style, Style, Style]:
  510. """Get background, number, and highlight styles for line numbers."""
  511. background_style = self._get_base_style()
  512. if background_style.transparent_background:
  513. return Style.null(), Style(dim=True), Style.null()
  514. if console.color_system in ("256", "truecolor"):
  515. number_style = Style.chain(
  516. background_style,
  517. self._theme.get_style_for_token(Token.Text),
  518. Style(color=self._get_line_numbers_color()),
  519. self.background_style,
  520. )
  521. highlight_number_style = Style.chain(
  522. background_style,
  523. self._theme.get_style_for_token(Token.Text),
  524. Style(bold=True, color=self._get_line_numbers_color(0.9)),
  525. self.background_style,
  526. )
  527. else:
  528. number_style = background_style + Style(dim=True)
  529. highlight_number_style = background_style + Style(dim=False)
  530. return background_style, number_style, highlight_number_style
  531. def __rich_measure__(
  532. self, console: "Console", options: "ConsoleOptions"
  533. ) -> "Measurement":
  534. _, right, _, left = Padding.unpack(self.padding)
  535. padding = left + right
  536. if self.code_width is not None:
  537. width = self.code_width + self._numbers_column_width + padding + 1
  538. return Measurement(self._numbers_column_width, width)
  539. lines = self.code.splitlines()
  540. width = (
  541. self._numbers_column_width
  542. + padding
  543. + (max(cell_len(line) for line in lines) if lines else 0)
  544. )
  545. if self.line_numbers:
  546. width += 1
  547. return Measurement(self._numbers_column_width, width)
  548. def __rich_console__(
  549. self, console: Console, options: ConsoleOptions
  550. ) -> RenderResult:
  551. segments = Segments(self._get_syntax(console, options))
  552. if self.padding:
  553. yield Padding(segments, style=self._get_base_style(), pad=self.padding)
  554. else:
  555. yield segments
  556. def _get_syntax(
  557. self,
  558. console: Console,
  559. options: ConsoleOptions,
  560. ) -> Iterable[Segment]:
  561. """
  562. Get the Segments for the Syntax object, excluding any vertical/horizontal padding
  563. """
  564. transparent_background = self._get_base_style().transparent_background
  565. code_width = (
  566. (
  567. (options.max_width - self._numbers_column_width - 1)
  568. if self.line_numbers
  569. else options.max_width
  570. )
  571. if self.code_width is None
  572. else self.code_width
  573. )
  574. ends_on_nl, processed_code = self._process_code(self.code)
  575. text = self.highlight(processed_code, self.line_range)
  576. if not self.line_numbers and not self.word_wrap and not self.line_range:
  577. if not ends_on_nl:
  578. text.remove_suffix("\n")
  579. # Simple case of just rendering text
  580. style = (
  581. self._get_base_style()
  582. + self._theme.get_style_for_token(Comment)
  583. + Style(dim=True)
  584. + self.background_style
  585. )
  586. if self.indent_guides and not options.ascii_only:
  587. text = text.with_indent_guides(self.tab_size, style=style)
  588. text.overflow = "crop"
  589. if style.transparent_background:
  590. yield from console.render(
  591. text, options=options.update(width=code_width)
  592. )
  593. else:
  594. syntax_lines = console.render_lines(
  595. text,
  596. options.update(width=code_width, height=None, justify="left"),
  597. style=self.background_style,
  598. pad=True,
  599. new_lines=True,
  600. )
  601. for syntax_line in syntax_lines:
  602. yield from syntax_line
  603. return
  604. start_line, end_line = self.line_range or (None, None)
  605. line_offset = 0
  606. if start_line:
  607. line_offset = max(0, start_line - 1)
  608. lines: Union[List[Text], Lines] = text.split("\n", allow_blank=ends_on_nl)
  609. if self.line_range:
  610. if line_offset > len(lines):
  611. return
  612. lines = lines[line_offset:end_line]
  613. if self.indent_guides and not options.ascii_only:
  614. style = (
  615. self._get_base_style()
  616. + self._theme.get_style_for_token(Comment)
  617. + Style(dim=True)
  618. + self.background_style
  619. )
  620. lines = (
  621. Text("\n")
  622. .join(lines)
  623. .with_indent_guides(self.tab_size, style=style + Style(italic=False))
  624. .split("\n", allow_blank=True)
  625. )
  626. numbers_column_width = self._numbers_column_width
  627. render_options = options.update(width=code_width)
  628. highlight_line = self.highlight_lines.__contains__
  629. _Segment = Segment
  630. new_line = _Segment("\n")
  631. line_pointer = "> " if options.legacy_windows else "❱ "
  632. (
  633. background_style,
  634. number_style,
  635. highlight_number_style,
  636. ) = self._get_number_styles(console)
  637. for line_no, line in enumerate(lines, self.start_line + line_offset):
  638. if self.word_wrap:
  639. wrapped_lines = console.render_lines(
  640. line,
  641. render_options.update(height=None, justify="left"),
  642. style=background_style,
  643. pad=not transparent_background,
  644. )
  645. else:
  646. segments = list(line.render(console, end=""))
  647. if options.no_wrap:
  648. wrapped_lines = [segments]
  649. else:
  650. wrapped_lines = [
  651. _Segment.adjust_line_length(
  652. segments,
  653. render_options.max_width,
  654. style=background_style,
  655. pad=not transparent_background,
  656. )
  657. ]
  658. if self.line_numbers:
  659. wrapped_line_left_pad = _Segment(
  660. " " * numbers_column_width + " ", background_style
  661. )
  662. for first, wrapped_line in loop_first(wrapped_lines):
  663. if first:
  664. line_column = str(line_no).rjust(numbers_column_width - 2) + " "
  665. if highlight_line(line_no):
  666. yield _Segment(line_pointer, Style(color="red"))
  667. yield _Segment(line_column, highlight_number_style)
  668. else:
  669. yield _Segment(" ", highlight_number_style)
  670. yield _Segment(line_column, number_style)
  671. else:
  672. yield wrapped_line_left_pad
  673. yield from wrapped_line
  674. yield new_line
  675. else:
  676. for wrapped_line in wrapped_lines:
  677. yield from wrapped_line
  678. yield new_line
  679. def _apply_stylized_ranges(self, text: Text) -> None:
  680. """
  681. Apply stylized ranges to a text instance,
  682. using the given code to determine the right portion to apply the style to.
  683. Args:
  684. text (Text): Text instance to apply the style to.
  685. """
  686. code = text.plain
  687. newlines_offsets = [
  688. # Let's add outer boundaries at each side of the list:
  689. 0,
  690. # N.B. using "\n" here is much faster than using metacharacters such as "^" or "\Z":
  691. *[
  692. match.start() + 1
  693. for match in re.finditer("\n", code, flags=re.MULTILINE)
  694. ],
  695. len(code) + 1,
  696. ]
  697. for stylized_range in self._stylized_ranges:
  698. start = _get_code_index_for_syntax_position(
  699. newlines_offsets, stylized_range.start
  700. )
  701. end = _get_code_index_for_syntax_position(
  702. newlines_offsets, stylized_range.end
  703. )
  704. if start is not None and end is not None:
  705. if stylized_range.style_before:
  706. text.stylize_before(stylized_range.style, start, end)
  707. else:
  708. text.stylize(stylized_range.style, start, end)
  709. def _process_code(self, code: str) -> Tuple[bool, str]:
  710. """
  711. Applies various processing to a raw code string
  712. (normalises it so it always ends with a line return, dedents it if necessary, etc.)
  713. Args:
  714. code (str): The raw code string to process
  715. Returns:
  716. Tuple[bool, str]: the boolean indicates whether the raw code ends with a line return,
  717. while the string is the processed code.
  718. """
  719. ends_on_nl = code.endswith("\n")
  720. processed_code = code if ends_on_nl else code + "\n"
  721. processed_code = (
  722. textwrap.dedent(processed_code) if self.dedent else processed_code
  723. )
  724. processed_code = processed_code.expandtabs(self.tab_size)
  725. return ends_on_nl, processed_code
  726. def _get_code_index_for_syntax_position(
  727. newlines_offsets: Sequence[int], position: SyntaxPosition
  728. ) -> Optional[int]:
  729. """
  730. Returns the index of the code string for the given positions.
  731. Args:
  732. newlines_offsets (Sequence[int]): The offset of each newline character found in the code snippet.
  733. position (SyntaxPosition): The position to search for.
  734. Returns:
  735. Optional[int]: The index of the code string for this position, or `None`
  736. if the given position's line number is out of range (if it's the column that is out of range
  737. we silently clamp its value so that it reaches the end of the line)
  738. """
  739. lines_count = len(newlines_offsets)
  740. line_number, column_index = position
  741. if line_number > lines_count or len(newlines_offsets) < (line_number + 1):
  742. return None # `line_number` is out of range
  743. line_index = line_number - 1
  744. line_length = newlines_offsets[line_index + 1] - newlines_offsets[line_index] - 1
  745. # If `column_index` is out of range: let's silently clamp it:
  746. column_index = min(line_length, column_index)
  747. return newlines_offsets[line_index] + column_index
  748. if __name__ == "__main__": # pragma: no cover
  749. import argparse
  750. import sys
  751. parser = argparse.ArgumentParser(
  752. description="Render syntax to the console with Rich"
  753. )
  754. parser.add_argument(
  755. "path",
  756. metavar="PATH",
  757. help="path to file, or - for stdin",
  758. )
  759. parser.add_argument(
  760. "-c",
  761. "--force-color",
  762. dest="force_color",
  763. action="store_true",
  764. default=None,
  765. help="force color for non-terminals",
  766. )
  767. parser.add_argument(
  768. "-i",
  769. "--indent-guides",
  770. dest="indent_guides",
  771. action="store_true",
  772. default=False,
  773. help="display indent guides",
  774. )
  775. parser.add_argument(
  776. "-l",
  777. "--line-numbers",
  778. dest="line_numbers",
  779. action="store_true",
  780. help="render line numbers",
  781. )
  782. parser.add_argument(
  783. "-w",
  784. "--width",
  785. type=int,
  786. dest="width",
  787. default=None,
  788. help="width of output (default will auto-detect)",
  789. )
  790. parser.add_argument(
  791. "-r",
  792. "--wrap",
  793. dest="word_wrap",
  794. action="store_true",
  795. default=False,
  796. help="word wrap long lines",
  797. )
  798. parser.add_argument(
  799. "-s",
  800. "--soft-wrap",
  801. action="store_true",
  802. dest="soft_wrap",
  803. default=False,
  804. help="enable soft wrapping mode",
  805. )
  806. parser.add_argument(
  807. "-t", "--theme", dest="theme", default="monokai", help="pygments theme"
  808. )
  809. parser.add_argument(
  810. "-b",
  811. "--background-color",
  812. dest="background_color",
  813. default=None,
  814. help="Override background color",
  815. )
  816. parser.add_argument(
  817. "-x",
  818. "--lexer",
  819. default=None,
  820. dest="lexer_name",
  821. help="Lexer name",
  822. )
  823. parser.add_argument(
  824. "-p", "--padding", type=int, default=0, dest="padding", help="Padding"
  825. )
  826. parser.add_argument(
  827. "--highlight-line",
  828. type=int,
  829. default=None,
  830. dest="highlight_line",
  831. help="The line number (not index!) to highlight",
  832. )
  833. args = parser.parse_args()
  834. from rich.console import Console
  835. console = Console(force_terminal=args.force_color, width=args.width)
  836. if args.path == "-":
  837. code = sys.stdin.read()
  838. syntax = Syntax(
  839. code=code,
  840. lexer=args.lexer_name,
  841. line_numbers=args.line_numbers,
  842. word_wrap=args.word_wrap,
  843. theme=args.theme,
  844. background_color=args.background_color,
  845. indent_guides=args.indent_guides,
  846. padding=args.padding,
  847. highlight_lines={args.highlight_line},
  848. )
  849. else:
  850. syntax = Syntax.from_path(
  851. args.path,
  852. lexer=args.lexer_name,
  853. line_numbers=args.line_numbers,
  854. word_wrap=args.word_wrap,
  855. theme=args.theme,
  856. background_color=args.background_color,
  857. indent_guides=args.indent_guides,
  858. padding=args.padding,
  859. highlight_lines={args.highlight_line},
  860. )
  861. console.print(syntax, soft_wrap=args.soft_wrap)