Skip to content

Commit

Permalink
Fix type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenburns committed Jan 25, 2022
1 parent c4df3a7 commit 781b41f
Showing 1 changed file with 11 additions and 10 deletions.
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

0 comments on commit 781b41f

Please sign in to comment.