@@ -17,6 +17,70 @@ require 'vm'
1717
1818local export = {}
1919
20+ local function clear_line ()
21+ -- Write out empty space to ensure that the previous lien is cleared.
22+ io.write (' \x0D ' , (' ' ):rep (80 ), ' \x0D ' )
23+ end
24+
25+ --- @param i integer
26+ --- @param max integer
27+ --- @param results table<string , table[]>
28+ local function report_progress (i , max , results )
29+ local filesWithErrors = 0
30+ local errors = 0
31+ for _ , diags in pairs (results ) do
32+ filesWithErrors = filesWithErrors + 1
33+ errors = errors + # diags
34+ end
35+
36+ clear_line ()
37+ io.write (
38+ (' >' ):rep (math.ceil (i / max * 20 )),
39+ (' =' ):rep (20 - math.ceil (i / max * 20 )),
40+ ' ' ,
41+ (' 0' ):rep (# tostring (max ) - # tostring (i )),
42+ tostring (i ),
43+ ' /' ,
44+ tostring (max )
45+ )
46+ if errors > 0 then
47+ io.write (' [' , lang .script (' CLI_CHECK_PROGRESS' , errors , filesWithErrors ), ' ]' )
48+ end
49+ io.flush ()
50+ end
51+
52+ --- @param uri string
53+ --- @param checkLevel integer
54+ local function apply_check_level (uri , checkLevel )
55+ local config_disables = util .arrayToHash (config .get (uri , ' Lua.diagnostics.disable' ))
56+ local config_severities = config .get (uri , ' Lua.diagnostics.severity' )
57+ for name , serverity in pairs (define .DiagnosticDefaultSeverity ) do
58+ serverity = config_severities [name ] or serverity
59+ if serverity :sub (- 1 ) == ' !' then
60+ serverity = serverity :sub (1 , - 2 )
61+ end
62+ if define .DiagnosticSeverity [serverity ] > checkLevel then
63+ config_disables [name ] = true
64+ end
65+ end
66+ config .set (uri , ' Lua.diagnostics.disable' , util .getTableKeys (config_disables , true ))
67+ end
68+
69+ local function downgrade_checks_to_opened (uri )
70+ local diagStatus = config .get (uri , ' Lua.diagnostics.neededFileStatus' )
71+ for d , status in pairs (diagStatus ) do
72+ if status == ' Any' or status == ' Any!' then
73+ diagStatus [d ] = ' Opened!'
74+ end
75+ end
76+ for d , status in pairs (protoDiag .getDefaultStatus ()) do
77+ if status == ' Any' or status == ' Any!' then
78+ diagStatus [d ] = ' Opened!'
79+ end
80+ end
81+ config .set (uri , ' Lua.diagnostics.neededFileStatus' , diagStatus )
82+ end
83+
2084function export .runCLI ()
2185 lang (LOCALE )
2286
@@ -36,18 +100,16 @@ function export.runCLI()
36100 end
37101 rootUri = rootUri :gsub (" /$" , " " )
38102
39- if CHECKLEVEL then
40- if not define .DiagnosticSeverity [CHECKLEVEL ] then
41- print (lang .script (' CLI_CHECK_ERROR_LEVEL' , ' Error, Warning, Information, Hint' ))
42- return
43- end
103+ if CHECKLEVEL and not define .DiagnosticSeverity [CHECKLEVEL ] then
104+ print (lang .script (' CLI_CHECK_ERROR_LEVEL' , ' Error, Warning, Information, Hint' ))
105+ return
44106 end
45107 local checkLevel = define .DiagnosticSeverity [CHECKLEVEL ] or define .DiagnosticSeverity .Warning
46108
47109 util .enableCloseFunction ()
48110
49111 local lastClock = os.clock ()
50- local results = {}
112+ local results = {} --- @type table<string , table[]>
51113
52114 local function errorhandler (err )
53115 print (err )
@@ -75,31 +137,12 @@ function export.runCLI()
75137
76138 ws .awaitReady (rootUri )
77139
78- local disables = util .arrayToHash (config .get (rootUri , ' Lua.diagnostics.disable' ))
79- for name , serverity in pairs (define .DiagnosticDefaultSeverity ) do
80- serverity = config .get (rootUri , ' Lua.diagnostics.severity' )[name ] or serverity
81- if serverity :sub (- 1 ) == ' !' then
82- serverity = serverity :sub (1 , - 2 )
83- end
84- if define .DiagnosticSeverity [serverity ] > checkLevel then
85- disables [name ] = true
86- end
87- end
88- config .set (rootUri , ' Lua.diagnostics.disable' , util .getTableKeys (disables , true ))
140+ -- Disable any diagnostics that are above the check level
141+ apply_check_level (rootUri , checkLevel )
89142
90- -- Downgrade file opened status to Opened for everything to avoid reporting during compilation on files that do not belong to this thread
91- local diagStatus = config .get (rootUri , ' Lua.diagnostics.neededFileStatus' )
92- for diag , status in pairs (diagStatus ) do
93- if status == ' Any' or status == ' Any!' then
94- diagStatus [diag ] = ' Opened!'
95- end
96- end
97- for diag , status in pairs (protoDiag .getDefaultStatus ()) do
98- if status == ' Any' or status == ' Any!' then
99- diagStatus [diag ] = ' Opened!'
100- end
101- end
102- config .set (rootUri , ' Lua.diagnostics.neededFileStatus' , diagStatus )
143+ -- Downgrade file opened status to Opened for everything to avoid
144+ -- reporting during compilation on files that do not belong to this thread
145+ downgrade_checks_to_opened (rootUri )
103146
104147 local uris = files .getChildFiles (rootUri )
105148 local max = # uris
@@ -108,28 +151,12 @@ function export.runCLI()
108151 if (i % numThreads + 1 ) == threadId then
109152 files .open (uri )
110153 diag .doDiagnostic (uri , true )
111- -- Print regularly but always print the last entry to ensure that logs written to files don't look incomplete.
112- if (os.clock () - lastClock > 0.2 or i == # uris ) and not QUIET then
154+ -- Print regularly but always print the last entry to ensure
155+ -- that logs written to files don't look incomplete.
156+ if not QUIET and (os.clock () - lastClock > 0.2 or i == # uris ) then
113157 lastClock = os.clock ()
114158 client :update ()
115- local output = ' \x0D '
116- .. (' >' ):rep (math.ceil (i / max * 20 ))
117- .. (' =' ):rep (20 - math.ceil (i / max * 20 ))
118- .. ' '
119- .. (' 0' ):rep (# tostring (max ) - # tostring (i ))
120- .. tostring (i ) .. ' /' .. tostring (max )
121- io.write (output )
122- local filesWithErrors = 0
123- local errors = 0
124- for _ , diags in pairs (results ) do
125- filesWithErrors = filesWithErrors + 1
126- errors = errors + # diags
127- end
128- if errors > 0 then
129- local errorDetails = ' [' .. lang .script (' CLI_CHECK_PROGRESS' , errors , filesWithErrors ) .. ' ]'
130- io.write (errorDetails )
131- end
132- io.flush ()
159+ report_progress (i , max , results )
133160 end
134161 end
135162 end
@@ -146,10 +173,8 @@ function export.runCLI()
146173 end
147174 end
148175
149- local outpath = CHECK_OUT_PATH
150- if outpath == nil then
151- outpath = LOGPATH .. ' /check.json'
152- end
176+ local outpath = CHECK_OUT_PATH or LOGPATH .. ' /check.json'
177+
153178 -- Always write result, even if it's empty to make sure no one accidentally looks at an old output after a successful run.
154179 util .saveFile (outpath , jsonb .beautify (results ))
155180
0 commit comments