syntax.py 34 KB

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