MCPcopy
hub / github.com/Textualize/rich / _guess_lexer

Method _guess_lexer

rich/traceback.py:752–765  ·  view source on GitHub ↗
(cls, filename: str, code: str)

Source from the content-addressed store, hash-verified

750
751 @classmethod
752 def _guess_lexer(cls, filename: str, code: str) -> str:
753 ext = os.path.splitext(filename)[-1]
754 if not ext:
755 # No extension, look at first line to see if it is a hashbang
756 # Note, this is an educated guess and not a guarantee
757 # If it fails, the only downside is that the code is highlighted strangely
758 new_line_index = code.index("\n")
759 first_line = code[:new_line_index] if new_line_index != -1 else code
760 if first_line.startswith("#!") and "python" in first_line.lower():
761 return "python"
762 try:
763 return cls.LEXERS.get(ext) or guess_lexer_for_filename(filename, code).name
764 except ClassNotFound:
765 return "text"
766
767 @group()
768 def _render_stack(self, stack: Stack) -> RenderResult:

Callers 3

_render_stackMethod · 0.95
test_guess_lexerFunction · 0.80
test_guess_lexer_yaml_j2Function · 0.80

Calls 1

getMethod · 0.45

Tested by 2

test_guess_lexerFunction · 0.64
test_guess_lexer_yaml_j2Function · 0.64