File tree Expand file tree Collapse file tree 2 files changed +42
-1
lines changed
Expand file tree Collapse file tree 2 files changed +42
-1
lines changed Original file line number Diff line number Diff line change @@ -104,11 +104,17 @@ def readline():
104104
105105 if token_type == tokenize .ENDMARKER :
106106 # End of source
107+ if current_type is None :
108+ current_type = self .TOKEN_DEFAULT
109+
107110 line += "<{}>{}</>" .format (self ._theme [current_type ], buffer )
108111 lines .append (line )
109112 break
110113
111114 if lineno > current_line :
115+ if current_type is None :
116+ current_type = self .TOKEN_DEFAULT
117+
112118 diff = lineno - current_line
113119 if diff > 1 :
114120 lines += ["" ] * (diff - 1 )
@@ -154,8 +160,9 @@ def readline():
154160
155161 if lineno < end [0 ]:
156162 # The token spans multiple lines
157- lines .append (line )
158163 token_lines = token_string .split ("\n " )
164+ line += "<{}>{}</>" .format (self ._theme [current_type ], token_lines [0 ])
165+ lines .append (line )
159166 for token_line in token_lines [1 :- 1 ]:
160167 lines .append (
161168 "<{}>{}</>" .format (self ._theme [current_type ], token_line )
Original file line number Diff line number Diff line change @@ -443,3 +443,37 @@ def call():
443443 )
444444
445445 assert expected == io .fetch_output ()
446+
447+
448+ def test_empty_source_file_do_not_break_highlighter ():
449+ from cleo .ui .exception_trace import Highlighter
450+
451+ highlighter = Highlighter ()
452+ highlighter .highlighted_lines ("" )
453+
454+
455+ def test_doctrings_are_corrrectly_rendered ():
456+ from cleo .formatters .formatter import Formatter
457+ from cleo .ui .exception_trace import Highlighter
458+
459+ source = '''
460+ def test():
461+ """
462+ Doctring
463+ """
464+ ...
465+ '''
466+
467+ formatter = Formatter ()
468+ highlighter = Highlighter ()
469+ lines = highlighter .highlighted_lines (source )
470+
471+ assert [formatter .format (l ) for l in lines ] == [
472+ "" ,
473+ "def test():" ,
474+ ' """' ,
475+ " Doctring" ,
476+ ' """' ,
477+ " ..." ,
478+ "" ,
479+ ]
You can’t perform that action at this time.
0 commit comments