Skip to content

Commit 2439c81

Browse files
committed
fix format on save bug causing loadfile calls and other related calls to become empty
1 parent d8ba75c commit 2439c81

4 files changed

Lines changed: 55 additions & 2 deletions

File tree

language_server/editor_helper.lua

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,8 +604,17 @@ function META:Format(code, path, extra_emitter_config)
604604
config.emitter.remove_unused = do_remove_unused or false
605605
config.analyzer.remove_unused = do_remove_unused or false
606606
local file_data = self:IsLoaded(path) and self:GetFile(path)
607-
608-
if file_data and file_data.compiler and file_data.code:GetString() == code then
607+
local loaded_parser_cfg = file_data and
608+
file_data.compiler and
609+
file_data.compiler.Config and
610+
file_data.compiler.Config.parser or
611+
{}
612+
local can_reuse_loaded_tree = file_data and
613+
file_data.compiler and
614+
file_data.code:GetString() == code and
615+
loaded_parser_cfg.skip_import == config.parser.skip_import
616+
617+
if can_reuse_loaded_tree then
609618
local cfg_copy = {}
610619

611620
for k, v in pairs(config.emitter) do

nattlua/parser/parser.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ function META:ParseFile(path--[[#: string]], config--[[#: nil | any]])
387387
config.file_path = config.file_path or path
388388
config.file_name = config.file_name or "@" .. path
389389
return self:ParseString(code, config)
390+
390391
end
391392

392393
local imported_index = nil

test/tests/language_server/editor_helper.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,23 @@ do
175175
helper:Recompile("./main.nlua")
176176
end
177177

178+
do
179+
local helper = EditorHelper.New()
180+
helper:Initialize()
181+
local format_path = "./main.lua"
182+
local code = [[
183+
assert(loadfile("game/run.lua"))()
184+
local f = assert(loadfile("test/run.lua"))
185+
assert(loadfile("game/run.lua"))()
186+
]]
187+
helper:OpenFile(format_path, code)
188+
local formatted = helper:Format(code, format_path)
189+
assert(formatted:find("assert%(loadfile%(\"game/run%.lua\"%)%)%(") ~= nil)
190+
assert(formatted:find("local f = assert%(loadfile%(\"test/run%.lua\"%)%)") ~= nil)
191+
assert(formatted:find("assert%(loadfile%)%(") == nil)
192+
assert(formatted:find("local f = assert%(loadfile%)") == nil)
193+
end
194+
178195
do
179196
local helper = EditorHelper.New()
180197
helper:Initialize()

test/tests/nattlua/emitter.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,32 @@ check(
607607
608608
local x = import("platforms/windows/filesystem.nlua")]=]
609609
)
610+
do
611+
local input = [[
612+
assert(loadfile("game/run.lua"))()
613+
local f = assert(loadfile("test/run.lua"))
614+
assert(loadfile("game/run.lua"))()
615+
]]
616+
local output = assert(
617+
nl.Compiler(
618+
input,
619+
nil,
620+
{
621+
parser = {skip_import = true},
622+
emitter = {
623+
pretty_print = true,
624+
force_parenthesis = true,
625+
string_quote = "\"",
626+
skip_import = true,
627+
},
628+
}
629+
):Emit()
630+
)
631+
assert(output:find("assert%(loadfile%(\"game/run%.lua\"%)%)%(") ~= nil)
632+
assert(output:find("local f = assert%(loadfile%(\"test/run%.lua\"%)%)") ~= nil)
633+
assert(output:find("assert%(loadfile%)%(") == nil)
634+
assert(output:find("local f = assert%(loadfile%)") == nil)
635+
end
610636
identical([[hook.Add("Foo", "bar_foo", function(ply, pos)
611637
for i = 1, 10 do
612638
ply:SetPos(pos + VectorRand())

0 commit comments

Comments
 (0)