11from enum import Flag
22from pathlib import Path
33from textwrap import indent
4- from typing import List , Optional , Set , Tuple
4+ from typing import List , Optional , Set , Tuple , Union
55
66import click
77
@@ -56,16 +56,49 @@ class ReturnCode(Flag):
5656
5757class Statistic :
5858 def __init__ (self ) -> None :
59- self .folders : Set [WorkspaceFolder ] = set ()
60- self .files : Set [TextDocument ] = set ()
61- self .errors = 0
62- self .warnings = 0
63- self .infos = 0
64- self .hints = 0
59+ self ._folders : Set [WorkspaceFolder ] = set ()
60+ self ._files : Set [TextDocument ] = set ()
61+ self ._diagnostics : List [Union [DocumentDiagnosticReport , FolderDiagnosticReport ]] = []
62+
63+ @property
64+ def errors (self ) -> int :
65+ return sum (
66+ len ([i for i in e .items if i .severity == DiagnosticSeverity .ERROR ]) for e in self ._diagnostics if e .items
67+ )
68+
69+ @property
70+ def warnings (self ) -> int :
71+ return sum (
72+ len ([i for i in e .items if i .severity == DiagnosticSeverity .WARNING ]) for e in self ._diagnostics if e .items
73+ )
74+
75+ @property
76+ def infos (self ) -> int :
77+ return sum (
78+ len ([i for i in e .items if i .severity == DiagnosticSeverity .INFORMATION ])
79+ for e in self ._diagnostics
80+ if e .items
81+ )
82+
83+ @property
84+ def hints (self ) -> int :
85+ return sum (
86+ len ([i for i in e .items if i .severity == DiagnosticSeverity .HINT ]) for e in self ._diagnostics if e .items
87+ )
88+
89+ def add_diagnostics_report (
90+ self , diagnostics_report : Union [DocumentDiagnosticReport , FolderDiagnosticReport ]
91+ ) -> None :
92+ self ._diagnostics .append (diagnostics_report )
93+
94+ if isinstance (diagnostics_report , FolderDiagnosticReport ):
95+ self ._folders .add (diagnostics_report .folder )
96+ elif isinstance (diagnostics_report , DocumentDiagnosticReport ):
97+ self ._files .add (diagnostics_report .document )
6598
6699 def __str__ (self ) -> str :
67100 return (
68- f"Files: { len (self .files )} , Errors: { self .errors } , Warnings: { self .warnings } , "
101+ f"Files: { len (self ._files )} , Errors: { self .errors } , Warnings: { self .warnings } , "
69102 f"Infos: { self .infos } , Hints: { self .hints } "
70103 )
71104
@@ -192,7 +225,6 @@ def code(
192225
193226 The return code is a bitwise combination of the following values:
194227
195- \b
196228 - `0`: **SUCCESS** - No issues detected.
197229 - `1`: **ERRORS** - Critical issues found.
198230 - `2`: **WARNINGS** - Non-critical issues detected.
@@ -284,20 +316,17 @@ def code(
284316 robot_profile = robot_profile ,
285317 root_folder = root_folder ,
286318 ).run (paths = paths , filter = filter ):
287- if isinstance (e , FolderDiagnosticReport ):
288- statistics .folders .add (e .folder )
319+ statistics .add_diagnostics_report (e )
289320
321+ if isinstance (e , FolderDiagnosticReport ):
290322 if e .items :
291- _print_diagnostics (app , root_folder , statistics , e .items , e .folder .uri .to_path ())
292-
323+ _print_diagnostics (app , root_folder , e .items , e .folder .uri .to_path ())
293324 elif isinstance (e , DocumentDiagnosticReport ):
294- statistics .files .add (e .document )
295-
296325 doc_path = (
297326 e .document .uri .to_path ().relative_to (root_folder ) if root_folder else e .document .uri .to_path ()
298327 )
299328 if e .items :
300- _print_diagnostics (app , root_folder , statistics , e .items , doc_path )
329+ _print_diagnostics (app , root_folder , e .items , doc_path )
301330
302331 statistics_str = str (statistics )
303332 if statistics .errors > 0 :
@@ -314,23 +343,13 @@ def code(
314343def _print_diagnostics (
315344 app : Application ,
316345 root_folder : Optional [Path ],
317- statistics : Statistic ,
318346 diagnostics : List [Diagnostic ],
319347 folder_path : Optional [Path ],
320348 print_range : bool = True ,
321349) -> None :
322350 for item in diagnostics :
323351 severity = item .severity if item .severity is not None else DiagnosticSeverity .ERROR
324352
325- if severity == DiagnosticSeverity .ERROR :
326- statistics .errors += 1
327- elif severity == DiagnosticSeverity .WARNING :
328- statistics .warnings += 1
329- elif severity == DiagnosticSeverity .INFORMATION :
330- statistics .infos += 1
331- elif severity == DiagnosticSeverity .HINT :
332- statistics .hints += 1
333-
334353 app .echo (
335354 (
336355 (
0 commit comments