-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy patheditor_helper.lua
More file actions
2425 lines (1957 loc) · 59.3 KB
/
editor_helper.lua
File metadata and controls
2425 lines (1957 loc) · 59.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--DONT_ANALYZE
--[[HOTRELOAD
run_test("test/tests/editor_helper.lua")
--os.execute("luajit nattlua.lua build fast && luajit nattlua.lua install")
]]
local pairs = _G.pairs
local ipairs = _G.ipairs
local type = _G.type
local table = _G.table
local setmetatable = _G.setmetatable
local pcall = _G.pcall
local debug = _G.debug
local assert = _G.assert
local Compiler = require("nattlua.compiler").New
local formating = require("nattlua.other.formating")
local Union = require("nattlua.types.union").Union
local Table = require("nattlua.types.table").Table
local runtime_syntax = require("nattlua.syntax.runtime")
local typesystem_syntax = require("nattlua.syntax.typesystem")
local callstack = require("nattlua.other.callstack")
local bit = require("nattlua.other.bit")
local class = require("nattlua.other.class")
local fs = require("nattlua.other.fs")
local Code = require("nattlua.code").New
local Emitter = require("nattlua.emitter.emitter").New
local BuildBaseEnvironment = require("nattlua.base_environment").BuildBaseEnvironment
local runtime_env, typesystem_env, base_node_to_type = BuildBaseEnvironment()
local path_util = require("nattlua.other.path")
local Token = require("nattlua.lexer.token")
local META = class.CreateTemplate("editor_helper")
META:GetSet("WorkingDirectory", "./")
local format_emitter_defaults = {
pretty_print = true,
string_quote = "\"",
no_semicolon = true,
comment_type_annotations = true,
type_annotations = "explicit",
force_parenthesis = true,
omit_parentheses_for_single_table_call = true,
trailing_newline = true,
}
local function apply_format_emitter_defaults(emitter_config)
for k, v in pairs(format_emitter_defaults) do
if emitter_config[k] == nil then emitter_config[k] = v end
end
end
function META:SetWorkingDirectory(dir)
self.WorkingDirectory = dir
end
META:GetSet("ConfigFunction", function()
return
end)
function META:GetProjectConfig(what, path)
local get_config = self.ConfigFunction
local config = get_config(path)
if config then
local entry = config[what]
if not entry and config.commands then entry = config.commands[what] end
local sub_config
if type(entry) == "function" then
sub_config = entry()
elseif type(entry) == "table" then
if type(entry.cb) == "function" then
sub_config = entry.cb()
else
sub_config = entry
end
end
if sub_config then
if sub_config.ignorefiles == nil and config.ignorefiles ~= nil then
sub_config.ignorefiles = config.ignorefiles
end
sub_config.root_directory = config.config_dir
return sub_config
end
end
end
function META.New()
local node_to_type = {}
for k, v in pairs(base_node_to_type) do
node_to_type[k] = v
end
return META.NewObject(
{
TempFiles = {},
LoadedFiles = {},
DirtyFiles = {},
DirtySince = {},
OpenFiles = {},
VisibleFiles = {},
PublishedDiagnostics = {},
UseVisibleFilesForOpen = false,
InteractiveRefreshDelay = 0.25,
OnYield = false,
debug = false,
node_to_type = node_to_type,
},
true
)
end
function META:NodeToType(obj)
local node = self.node_to_type[obj]
if node and node.GetStartStop then return node end
return nil
end
function META:GetCompilerConfig(path)
local project_config = self.ConfigFunction(path) or {}
local cfg = self:GetProjectConfig("get-compiler-config", path) or {}
cfg.lexer = cfg.lexer or {}
cfg.emitter = cfg.emitter or {}
cfg.analyzer = cfg.analyzer or {}
cfg.lsp = cfg.lsp or {}
cfg.parser = cfg.parser or {}
cfg.root_directory = cfg.root_directory or project_config.config_dir or self:GetWorkingDirectory()
if
cfg.root_directory and
cfg.root_directory ~= "" and
cfg.root_directory:sub(-1) ~= "/"
then
cfg.root_directory = cfg.root_directory .. "/"
end
cfg.parser.root_directory = cfg.parser.root_directory or cfg.root_directory
cfg.analyzer.root_directory = cfg.analyzer.root_directory or cfg.root_directory
if self.workspace_config then
if self.workspace_config.removeUnusedOnSave then
cfg.emitter.remove_unused = true
cfg.analyzer.remove_unused = true
end
end
if cfg.emitter.type_annotations == nil then
cfg.emitter.type_annotations = true
end
if cfg.analyzer.should_crawl_untyped_functions == nil then
cfg.analyzer.should_crawl_untyped_functions = true
end
local function wrap_check_timeout(stage_cfg)
if stage_cfg.__nattlua_lsp_check_timeout_wrapped then return end
local previous = stage_cfg.check_timeout
stage_cfg.check_timeout = function(...)
if previous then previous(...) end
self:Yield()
end
stage_cfg.__nattlua_lsp_check_timeout_wrapped = true
end
wrap_check_timeout(cfg.lexer)
wrap_check_timeout(cfg.parser)
wrap_check_timeout(cfg.analyzer)
return cfg
end
function META:IsLightLspMode(path)
local cfg = self:GetCompilerConfig(path)
return cfg.lsp and (cfg.lsp.analyze == false or cfg.lsp.parse_only) or false
end
function META:DebugLog(str)
if self.debug then print(coroutine.running(), str) end
end
function META:Yield()
if self.OnYield then return self:OnYield() end
end
function META:GetTime()
if self.Now then return self:Now() end
return os.clock()
end
do
function META:IsLoaded(path)
path = path_util.Normalize(path)
return self.LoadedFiles[path] ~= nil
end
function META:IsDirty(path)
path = path_util.Normalize(path)
return self.DirtyFiles[path] == true
end
function META:MarkDirty(path)
path = path_util.Normalize(path)
self.DirtyFiles[path] = true
self.DirtySince[path] = self:GetTime()
end
function META:ClearDirty(path)
path = path_util.Normalize(path)
self.DirtyFiles[path] = nil
self.DirtySince[path] = nil
end
function META:ShouldDeferInteractiveRefresh(path)
path = path_util.Normalize(path)
if not self:IsDirty(path) then return false end
local dirty_since = self.DirtySince[path]
if not dirty_since then return false end
return self:GetTime() - dirty_since < (self.InteractiveRefreshDelay or 0)
end
function META:GetFile(path)
path = path_util.Normalize(path)
if not self.LoadedFiles[path] then
self:DebugLog("[ " .. path .. " ] is not loaded")
self:DebugLog("=== these are loaded ===")
for k, v in pairs(self.LoadedFiles) do
self:DebugLog("[ " .. k .. " ] is loaded")
end
self:DebugLog("===")
error(path .. " not loaded", 2)
end
return self.LoadedFiles[path]
end
function META:IsParsed(path)
path = path_util.Normalize(path)
local data = self.LoadedFiles[path]
return data ~= nil and data.parsed == true
end
function META:IsAnalyzed(path)
path = path_util.Normalize(path)
local data = self.LoadedFiles[path]
return data ~= nil and data.analyzed == true
end
function META:LoadFile(path, code, tokens, compiler, analyzed)
path = path_util.Normalize(path)
self.LoadedFiles[path] = {
code = code,
tokens = tokens,
compiler = compiler,
parsed = true,
analyzed = analyzed == true,
}
end
function META:MarkAnalyzed(path)
path = path_util.Normalize(path)
local data = self.LoadedFiles[path]
if data then data.analyzed = true end
end
function META:UnloadFile(path)
path = path_util.Normalize(path)
self.LoadedFiles[path] = nil
end
end
do
function META:SetFileContent(path, code)
path = path_util.Normalize(path)
self.TempFiles[path] = code
end
function META:GetFileContent(path)
path = path_util.Normalize(path)
if not self.TempFiles[path] then
self:DebugLog("[ " .. path .. " ] content is not loaded")
self:DebugLog("=== these are loaded ===")
for k, v in pairs(self.TempFiles) do
self:DebugLog("[ " .. k .. " ] content is loaded")
end
self:DebugLog("===")
error(path .. " is not loaded", 2)
end
return self.TempFiles[path]
end
function META:GetCurrentContent(path)
path = path_util.Normalize(path)
if self.TempFiles[path] ~= nil then return self.TempFiles[path] end
return fs.read(path)
end
end
local function normalize_path(path)
if not path then return nil end
if path:sub(1, 1) == "@" then path = path:sub(2) end
return path_util.Normalize(path)
end
local function has_force_analyze_directive(content)
return content and
(
content:match("^%s*%-%-%s*ANALYZE") or
content:match("^%#%!.-%s*%-%-%s*ANALYZE")
)
end
function META:SetVisibleFiles(paths)
local next_visible = {}
local newly_visible = {}
for _, path in ipairs(paths or {}) do
path = normalize_path(path)
if path then
next_visible[path] = true
if not self.VisibleFiles[path] then table.insert(newly_visible, path) end
end
end
self.VisibleFiles = next_visible
self.UseVisibleFilesForOpen = true
return newly_visible
end
function META:IsVisibleFile(path)
path = normalize_path(path)
return path ~= nil and self.VisibleFiles[path] or false
end
function META:ShouldRecompileOnOpen(path)
if not self.UseVisibleFilesForOpen then return true end
return self:IsVisibleFile(path)
end
function META:Recompile(path, lol, diagnostics, on_save_path, target_state)
if path then path = normalize_path(path) end
on_save_path = on_save_path or path
if on_save_path then on_save_path = normalize_path(on_save_path) end
local cfg = self:GetCompilerConfig(path)
diagnostics = diagnostics or {}
local light_lsp_mode = cfg.lsp and (cfg.lsp.analyze == false or cfg.lsp.parse_only)
local syntax_only = target_state == "syntax"
if not path and light_lsp_mode then return true end
if not path and not lol and not light_lsp_mode then
if type(cfg.lsp.entry_point) == "table" then
if self.debug then print("recompiling entry points from: " .. path) end
local ok = true
local reasons = {}
for _, path in ipairs(cfg.lsp.entry_point) do
local new_path = path_util.Resolve(path, cfg.parser.root_directory, cfg.parser.working_directory)
if self.debug then
print(path, "->", new_path)
table.print(cfg)
end
local b, reason = self:Recompile(new_path, true, diagnostics, on_save_path)
if not b then
ok = false
table.insert(reasons, reason)
end
end
if path and not self:IsLoaded(path) then
local b, reason = self:Recompile(path, true, diagnostics, on_save_path)
if not b then
ok = false
table.insert(reasons, reason)
end
end
return ok, table.concat(reasons, "\n")
elseif type(cfg.lsp.entry_point) == "string" then
local path = path_util.Resolve(cfg.lsp.entry_point, cfg.parser.root_directory, cfg.parser.working_directory)
if self.debug then
print("recompiling entry point: " .. path)
print(cfg.lsp.entry_point, "->", path)
table.print(cfg)
end
local ok, reason = self:Recompile(path, true, diagnostics, on_save_path)
return ok, reason
end
end
local entry_point = path or cfg.lsp.entry_point
if not entry_point then return false, "no entry point" end
if light_lsp_mode then
entry_point = path or entry_point
cfg.parser.import_validation_only = cfg.parser.import_validation_only ~= false
end
if syntax_only then
entry_point = path or entry_point
cfg.parser.skip_import = true
cfg.parser.import_validation_only = false
end
cfg.parser.pre_read_file = function(parser, path)
path = path_util.Normalize(path)
if self.TempFiles[path] then return self:GetFileContent(path) end
if path:sub(1, 1) ~= "/" then
local workspace_path = path_util.Normalize(self.WorkingDirectory .. "/" .. path)
if self.TempFiles[workspace_path] then
return self:GetFileContent(workspace_path)
end
end
return fs.read(path)
end
cfg.analyzer.pre_read_file = function(parser, path)
if self.TempFiles[path] then return self:GetFileContent(path) end
return fs.read(path)
end
cfg.analyzer.on_read_file = function(parser, path, content)
if not self.TempFiles[path] then self:SetFileContent(path, content) end
end
cfg.parser.on_parsed_file = function(path, compiler)
self:LoadFile(path, compiler.Code, compiler.Tokens, compiler)
end
cfg.parser.inline_require = not light_lsp_mode and not syntax_only
self:DebugLog("[ " .. entry_point .. " ] compiling")
local compiler
if path == entry_point or entry_point == on_save_path then
local content = self.TempFiles[entry_point] or fs.read(entry_point)
compiler = Compiler(content or "", entry_point, cfg)
else
compiler = Compiler([[return import("]] .. entry_point .. [[")]], entry_point, cfg)
end
compiler:SetEnvironments(self:GetEnvironment())
function compiler.OnDiagnostic(_, code, msg, severity, start, stop, node, level, ...)
local name = code:GetName()
local str_msg = formating.FormatMessage(msg, ...)
if severity == "fatal" then
self:DebugLog("[ " .. entry_point .. " ] " .. str_msg)
end
diagnostics[name] = diagnostics[name] or {}
table.insert(
diagnostics[name],
{
severity = severity,
code = code,
start = start,
stop = stop,
message = str_msg,
trace = callstack.traceback(),
}
)
end
local ok, err = compiler:Parse()
if ok then
if compiler.SyntaxTree.imports then
for _, root_node in ipairs(compiler.SyntaxTree.imports) do
self:Yield()
local root = root_node.RootStatement
if root_node.RootStatement then
if not root_node.RootStatement.parser then
root = root_node.RootStatement.RootStatement
end
-- if root is false it failed to import and will be reported shortly after
if root then
self:SetFileContent(root.parser.config.file_path, root.code:GetString())
self:LoadFile(root.parser.config.file_path, root.code, root.lexer_tokens, root.parser.compiler or compiler)
diagnostics[root.parser.config.file_path] = diagnostics[root.parser.config.file_path] or {}
end
end
end
end
-- Always load the main file itself
if path then
self:SetFileContent(path, compiler.Code:GetString())
self:LoadFile(path, compiler.Code, compiler.Tokens, compiler)
diagnostics[path] = diagnostics[path] or {}
end
local should_analyze = false
local lsp_analyze = not (cfg.lsp and (cfg.lsp.analyze == false or cfg.lsp.parse_only))
local force_analyze = false
if path then
local content = self.TempFiles[path] or fs.read(path)
force_analyze = has_force_analyze_directive(content)
if force_analyze then
should_analyze = true
elseif not lsp_analyze then
should_analyze = false
else
local ignored = false
local project_cfg = self:GetProjectConfig("get-compiler-config", path)
if project_cfg and project_cfg.ignorefiles then
for _, pattern in ipairs(project_cfg.ignorefiles) do
if path:find(pattern) then
if path:find("test_focus%.nlua") then
self:DebugLog("not ignoring test_focus.nlua")
else
ignored = true
break
end
end
end
end
if not ignored then
if path:find("%.nlua$") then should_analyze = true end
end
end
elseif lsp_analyze and cfg.lsp.entry_point then
should_analyze = true
end
if target_state == "parsed" or syntax_only then should_analyze = false end
if should_analyze then
local restore_import_roots
local restore_should_crawl_untyped_functions
if
force_analyze and
not lsp_analyze and
compiler.SyntaxTree and
compiler.SyntaxTree.imports
then
local imports = {}
for _, import_node in ipairs(compiler.SyntaxTree.imports) do
imports[#imports + 1] = {node = import_node, root = import_node.RootStatement}
import_node.RootStatement = false
end
restore_import_roots = function()
for _, data in ipairs(imports) do
data.node.RootStatement = data.root
end
end
end
if force_analyze and not lsp_analyze and cfg.analyzer then
local previous = cfg.analyzer.should_crawl_untyped_functions
cfg.analyzer.should_crawl_untyped_functions = false
restore_should_crawl_untyped_functions = function()
cfg.analyzer.should_crawl_untyped_functions = previous
end
end
local ok, err = compiler:Analyze(nil, cfg.analyzer)
if restore_import_roots then restore_import_roots() end
if restore_should_crawl_untyped_functions then
restore_should_crawl_untyped_functions()
end
local name = compiler:GetCode():GetName()
if not ok then
diagnostics[name] = diagnostics[name] or {}
local node = cfg.analyzer and
cfg.analyzer.GetCurrentExpression and
(
cfg.analyzer:GetCurrentExpression() or
cfg.analyzer:GetCurrentStatement()
)
local start, stop = 1, compiler:GetCode():GetByteSize()
if node then start, stop = node:GetStartStop() end
table.insert(
diagnostics[name],
{
severity = "fatal",
code = compiler:GetCode(),
start = start,
stop = stop,
message = err,
}
)
end
for typ, node in pairs(compiler.analyzer:GetTypeToNodeMap()) do
self.node_to_type[typ] = node
end
self:MarkAnalyzed(name)
if compiler.SyntaxTree.imports then
for _, root_node in ipairs(compiler.SyntaxTree.imports) do
self:Yield()
local root = root_node.RootStatement
if root then
if not root.parser and root.RootStatement then
root = root.RootStatement
end
if root and root.parser and root.parser.config and root.parser.config.file_path then
self:MarkAnalyzed(root.parser.config.file_path)
end
end
end
end
if
on_save_path and
cfg and
(
cfg.analyzer.remove_unused or
(
cfg.emitter and
cfg.emitter.remove_unused
)
)
then
self:DebugLog("checking roots for on_save_path: " .. tostring(on_save_path))
local roots = {}
if compiler.SyntaxTree.code then
self:DebugLog(
"compiler.SyntaxTree.code:GetName() = " .. tostring(compiler.SyntaxTree.code:GetName())
)
if
normalize_path(compiler.SyntaxTree.code:GetName()) == normalize_path(on_save_path)
then
self:DebugLog("found compiler.SyntaxTree match")
table.insert(roots, compiler.SyntaxTree)
end
end
if compiler.SyntaxTree.imports then
for _, root_node in ipairs(compiler.SyntaxTree.imports) do
local root = root_node.RootStatement
if root_node.RootStatement then
if not root_node.RootStatement.parser then
root = root_node.RootStatement.RootStatement
end
if root and root.code then
if normalize_path(root.code:GetName()) == normalize_path(on_save_path) then
self:DebugLog("found import match: " .. tostring(root.code:GetName()))
table.insert(roots, root)
-- else
-- self:DebugLog("not matching import: " .. tostring(root.code:GetName()))
end
end
end
end
end
for _, root in ipairs(roots) do
self:DebugLog("REMOVING UNUSED IN: " .. tostring(root.code:GetName()))
local ok, new_code = pcall(function()
local cfg_copy = {}
for k, v in pairs(cfg.emitter) do
cfg_copy[k] = v
end
if cfg_copy.trailing_newline == nil then cfg_copy.trailing_newline = true end
cfg_copy.remove_unused = true
cfg_copy.skip_import = true
cfg_copy.no_pipeline = true
cfg_copy.comment_type_annotations = on_save_path:sub(-#".lua") == ".lua"
local emitter = Emitter(cfg_copy)
return emitter:BuildCode(root)
end)
if not ok then
self:DebugLog("[ " .. entry_point .. " ] emitter failed: " .. new_code)
elseif new_code ~= root.code:GetString() then
local path = on_save_path
if root.parser and root.parser.config then
path = root.parser.config.file_path
end
fs.write(path, new_code)
self:SetFileContent(path, new_code)
end
end
end
else
self:DebugLog("[ " .. entry_point .. " ] skipped analysis")
end
end
-- Add diagnostics for unreachable code (dimmed with Unnecessary tag)
local file_data = self:IsLoaded(path) and self:GetFile(path)
if file_data and file_data.compiler and file_data.compiler.SyntaxTree then
local code_obj = file_data.code
local diag_name = code_obj and code_obj:GetName()
if diag_name then
local function collect_unreachable(node)
if not node then return end
if node.statements then
for _, child in ipairs(node.statements) do
if type(child) == "table" then
if child.Type then
-- It's a statement node
if child.Unreachable == true then
local start, stop = child:GetStartStop()
if start and stop then
diagnostics[diag_name] = diagnostics[diag_name] or {}
table.insert(
diagnostics[diag_name],
{
severity = "hint",
code = code_obj,
start = start,
stop = stop,
message = "unreachable code",
unreachable = true,
}
)
end
end
collect_unreachable(child)
else
-- It's an array of statements (e.g., if branch)
for _, stmt in ipairs(child) do
if type(stmt) == "table" and stmt.Type then
if stmt.Unreachable == true then
local start, stop = stmt:GetStartStop()
if start and stop then
diagnostics[diag_name] = diagnostics[diag_name] or {}
table.insert(
diagnostics[diag_name],
{
severity = "hint",
code = code_obj,
start = start,
stop = stop,
message = "unreachable code",
unreachable = true,
}
)
end
end
collect_unreachable(stmt)
end
end
end
end
end
end
end
collect_unreachable(file_data.compiler.SyntaxTree)
end
end
local next_published = {}
local normalized_path = normalize_path(path)
local normalized_entry_point = normalize_path(entry_point)
local normalized_on_save_path = normalize_path(on_save_path)
local function should_publish(name)
name = normalize_path(name)
return self.OpenFiles[name] or
(
normalized_path and
name == normalized_path
)
or
(
normalized_entry_point and
name == normalized_entry_point
)
or
(
normalized_on_save_path and
name == normalized_on_save_path
)
end
for name, data in pairs(diagnostics) do
self:Yield()
if should_publish(name) then
next_published[normalize_path(name)] = true
if #data > 0 then
self:OnDiagnostics(name, data)
else
self:OnClearDiagnostics(name)
end
end
end
for name in pairs(self.PublishedDiagnostics) do
self:Yield()
if not next_published[name] and not self.OpenFiles[name] then
self:OnClearDiagnostics(name)
end
end
for name in pairs(self.OpenFiles) do
if not next_published[name] and diagnostics[name] and #diagnostics[name] == 0 then
next_published[name] = true
end
end
self.PublishedDiagnostics = next_published
if path then self:ClearDirty(path) end
return true
end
function META:GetEnvironment()
return runtime_env, typesystem_env
end
function META:OnDiagnostics(name, data) end
function META:OnClearDiagnostics(name) end
function META:OnResponse(response) end
function META:Initialize()
local ok, reason = self:Recompile()
if not ok and reason ~= "no entry point" then
if not _G.TEST then print(":Recompile() failed: " .. reason) end
end
end
function META:Format(code, path, extra_emitter_config)
path = path_util.Normalize(path)
local config = self:GetCompilerConfig(path)
apply_format_emitter_defaults(config.emitter)
config.parser = {skip_import = true}
config.emitter.comment_type_annotations = path:sub(-#".lua") == ".lua"
config.emitter.transpile_extensions = path:sub(-#".lua") == ".lua"
config.emitter.skip_import = true
config.emitter.no_pipeline = true
-- remove_unused should only be triggered by explicit request (e.g. code action),
-- not by the workspace-level removeUnusedOnSave setting (that goes through the Recompile save path)
local do_remove_unused = extra_emitter_config and extra_emitter_config.remove_unused
config.emitter.remove_unused = do_remove_unused or false
config.analyzer.remove_unused = do_remove_unused or false
local file_data = self:IsLoaded(path) and self:GetFile(path)
local loaded_parser_cfg = file_data and
file_data.compiler and
file_data.compiler.Config and
file_data.compiler.Config.parser or
{}
local can_reuse_loaded_tree = file_data and
file_data.compiler and
file_data.code:GetString() == code and
loaded_parser_cfg.skip_import == config.parser.skip_import
if can_reuse_loaded_tree then
local cfg_copy = {}
for k, v in pairs(config.emitter) do
cfg_copy[k] = v
end
if do_remove_unused then file_data.compiler:Analyze() end
local emitter = Emitter(cfg_copy)
if extra_emitter_config then
for k, v in pairs(extra_emitter_config) do
emitter.config[k] = v
end
end
return emitter:BuildCode(file_data.compiler.SyntaxTree)
end
local compiler = Compiler(code, "@" .. path, config)
if do_remove_unused then compiler:Analyze() end
local code, err = compiler:Emit()
return code
end
function META:OpenFile(path, code, should_recompile)
path = path_util.Normalize(path)
self.OpenFiles[path] = true
self:SetFileContent(path, code)
if should_recompile == nil then
should_recompile = self:ShouldRecompileOnOpen(path)
end
if should_recompile and not self:IsLightLspMode(path) then
assert(self:Recompile(path))
end
end
function META:CloseFile(path)
path = path_util.Normalize(path)
self.OpenFiles[path] = nil
self.PublishedDiagnostics[path] = nil
self:ClearDirty(path)
self:SetFileContent(path, nil)
self:UnloadFile(path)
self:OnClearDiagnostics(path)
end
function META:UpdateFile(path, code)
path = path_util.Normalize(path)
self:SetFileContent(path, code)
self:MarkDirty(path)
end
function META:SaveFile(path)
path = path_util.Normalize(path)
local cfg = self:GetCompilerConfig(path)
local content = self.TempFiles[path] or fs.read(path) or ""
local needs_full_save_refresh = has_force_analyze_directive(content) or
(
cfg.analyzer and
cfg.analyzer.remove_unused
)
or
(
cfg.emitter and
cfg.emitter.remove_unused
)
local target_state = "syntax"
if needs_full_save_refresh then target_state = nil end
self:SetFileContent(path, nil)
assert(self:Recompile(path, nil, nil, path, target_state))
end
function META:EnsureLoaded(path)