MCPcopy
hub / github.com/pytest-dev/pytest / _highlight

Method _highlight

src/_pytest/_io/terminalwriter.py:235–258  ·  view source on GitHub ↗

Highlight the given source if we have markup support.

(
        self, source: str, lexer: Literal["diff", "python"] = "python"
    )

Source from the content-addressed store, hash-verified

233 ) from e
234
235 def _highlight(
236 self, source: str, lexer: Literal["diff", "python"] = "python"
237 ) -> str:
238 """Highlight the given source if we have markup support."""
239 if not source or not self.hasmarkup or not self.code_highlight:
240 return source
241
242 pygments_lexer = self._get_pygments_lexer(lexer)
243 pygments_formatter = self._get_pygments_formatter()
244
245 highlighted: str = pygments.highlight(
246 source, pygments_lexer, pygments_formatter
247 )
248 # pygments terminal formatter may add a newline when there wasn't one.
249 # We don't want this, remove.
250 if highlighted[-1] == "\n" and source[-1] != "\n":
251 highlighted = highlighted[:-1]
252
253 # Some lexers will not set the initial color explicitly
254 # which may lead to the previous color being propagated to the
255 # start of the expression, so reset first.
256 highlighted = "\x1b[0m" + highlighted
257
258 return highlighted

Callers 1

_write_sourceMethod · 0.95

Calls 2

_get_pygments_lexerMethod · 0.95

Tested by

no test coverage detected