Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Syntax.guess_lexer, add support for more lexers (e.g. Django templates etc.) #1869

Merged
merged 6 commits into from
Jan 25, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fix type checking
  • Loading branch information
darrenburns committed Jan 25, 2022
commit 781b41f4a780e70196692d7a43881eef0b0b81e0
21 changes: 11 additions & 10 deletions rich/syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ def from_path(
)

@classmethod
def guess_lexer(cls, path: AnyStr, code: Optional[str] = None) -> str:
def guess_lexer(cls, path: str, code: Optional[str] = None) -> str:
"""Guess the alias of the Pygments lexer to use based on a path and an optional string of code.
If code is supplied, it will use a combination of the code and the filename to determine the
best lexer to use. For example, if the file is ``index.html`` and the file contains Django
Expand All @@ -341,7 +341,7 @@ def guess_lexer(cls, path: AnyStr, code: Optional[str] = None) -> str:
lexer_name = "default"
if code:
try:
lexer: Lexer = guess_lexer_for_filename(path, code)
lexer = guess_lexer_for_filename(path, code)
except ClassNotFound:
pass

Expand Down Expand Up @@ -399,7 +399,9 @@ def lexer(self) -> Optional[Lexer]:
return None

def highlight(
self, code: str, line_range: Optional[Tuple[int, int]] = None
self,
code: str,
line_range: Optional[Tuple[Optional[int], Optional[int]]] = None,
) -> Text:
"""Highlight code and return a Text instance.

Expand Down Expand Up @@ -447,7 +449,7 @@ def tokens_to_spans() -> Iterable[Tuple[str, Optional[Style]]]:
"""Convert tokens to spans."""
tokens = iter(line_tokenize())
line_no = 0
_line_start = line_start - 1
_line_start = line_start - 1 if line_start else 0

# Skip over tokens until line start
while line_no < _line_start:
Expand All @@ -460,7 +462,7 @@ def tokens_to_spans() -> Iterable[Tuple[str, Optional[Style]]]:
yield (token, _get_theme_style(token_type))
if token.endswith("\n"):
line_no += 1
if line_no >= line_end:
if line_end and line_no >= line_end:
break

text.append_tokens(tokens_to_spans())
Expand Down Expand Up @@ -543,11 +545,6 @@ def __rich_console__(
else self.code_width
)

line_offset = 0
if self.line_range:
start_line, end_line = self.line_range
line_offset = max(0, start_line - 1)

ends_on_nl = self.code.endswith("\n")
code = self.code if ends_on_nl else self.code + "\n"
code = textwrap.dedent(code) if self.dedent else code
Expand Down Expand Up @@ -589,6 +586,10 @@ def __rich_console__(
yield from syntax_line
return

start_line, end_line = self.line_range or (None, None)
line_offset = 0
if start_line:
line_offset = max(0, start_line - 1)
lines: Union[List[Text], Lines] = text.split("\n", allow_blank=ends_on_nl)
if self.line_range:
lines = lines[line_offset:end_line]
Expand Down