3535ENCODING_RE : Final = re .compile (rb"([ \t\v]*#.*(\r\n?|\n))??[ \t\v]*#.*coding[:=][ \t]*([-\w.]+)" )
3636
3737DEFAULT_SOURCE_OFFSET : Final = 4
38+ CODE_START : Final = " " * DEFAULT_SOURCE_OFFSET
3839DEFAULT_COLUMNS : Final = 80
3940
4041# At least this number of columns will be shown on each side of
@@ -729,19 +730,27 @@ def style(
729730 start += self .DIM
730731 return start + self .colors [color ] + text + self .NORMAL
731732
733+ def is_marker_line (self , line : str ) -> bool :
734+ s_line = line .lstrip ()
735+ return (
736+ line .startswith (CODE_START )
737+ and s_line .startswith ("^" )
738+ and set (s_line ).issubset ({"^" , "~" })
739+ )
740+
732741 def fit_in_terminal (
733742 self , messages : list [str ], fixed_terminal_width : int | None = None
734743 ) -> list [str ]:
735744 """Improve readability by wrapping error messages and trimming source code."""
736745 width = fixed_terminal_width or get_terminal_width ()
737746 new_messages = messages .copy ()
738747 for i , error in enumerate (messages ):
739- if ": error:" in error :
748+ # TODO: detecting source code highlights through an indent can be surprising.
749+ if not error .startswith (CODE_START ) and ": error:" in error :
740750 loc , msg = error .split ("error:" , maxsplit = 1 )
741751 msg = soft_wrap (msg , width , first_offset = len (loc ) + len ("error: " ))
742752 new_messages [i ] = loc + "error:" + msg
743- if error .startswith (" " * DEFAULT_SOURCE_OFFSET ) and "^" not in error :
744- # TODO: detecting source code highlights through an indent can be surprising.
753+ elif error .startswith (CODE_START ) and not self .is_marker_line (error ):
745754 # Restore original error message and error location.
746755 error = error [DEFAULT_SOURCE_OFFSET :]
747756 marker_line = messages [i + 1 ]
@@ -768,7 +777,12 @@ def fit_in_terminal(
768777
769778 def colorize (self , error : str ) -> str :
770779 """Colorize an output line by highlighting the status and error code."""
771- if ": error:" in error :
780+ # TODO: detecting source code highlights through an indent can be surprising.
781+ if error .startswith (CODE_START ):
782+ if not self .is_marker_line (error ):
783+ return self .style (error , "none" , dim = True )
784+ return self .style (error , "red" )
785+ elif ": error:" in error :
772786 loc , msg = error .split ("error:" , maxsplit = 1 )
773787 if self .hide_error_codes :
774788 return (
@@ -790,11 +804,6 @@ def colorize(self, error: str) -> str:
790804 loc , msg = error .split ("note:" , maxsplit = 1 )
791805 formatted = self .highlight_quote_groups (self .underline_link (msg ))
792806 return loc + self .style ("note:" , "blue" ) + formatted
793- elif error .startswith (" " * DEFAULT_SOURCE_OFFSET ):
794- # TODO: detecting source code highlights through an indent can be surprising.
795- if "^" not in error :
796- return self .style (error , "none" , dim = True )
797- return self .style (error , "red" )
798807 else :
799808 return error
800809
0 commit comments