Skip to content

Commit db980e9

Browse files
committed
fix format on save error on unopened buffers
1 parent 2f3e250 commit db980e9

3 files changed

Lines changed: 46 additions & 2 deletions

File tree

language_server/editor_helper.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,14 @@ do
305305

306306
return self.TempFiles[path]
307307
end
308+
309+
function META:GetCurrentContent(path)
310+
path = path_util.Normalize(path)
311+
312+
if self.TempFiles[path] ~= nil then return self.TempFiles[path] end
313+
314+
return fs.read(path)
315+
end
308316
end
309317

310318
local function normalize_path(path)

language_server/lsp.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ lsp.methods["textDocument/codeAction"] = function(params)
414414

415415
if has_unused then
416416
local path = to_fs_path(params.textDocument.uri)
417-
local code = editor_helper:GetFileContent(path)
417+
local code = editor_helper:GetCurrentContent(path)
418418
local new_code = editor_helper:Format(code, path, {remove_unused = true})
419419

420420
if new_code ~= code then
@@ -453,7 +453,7 @@ lsp.methods["nattlua/format"] = function(params)
453453
end
454454
lsp.methods["textDocument/formatting"] = function(params)
455455
local path = to_fs_path(params.textDocument.uri)
456-
local code = editor_helper:GetFileContent(path)
456+
local code = editor_helper:GetCurrentContent(path)
457457
local new_code = editor_helper:Format(code, path)
458458

459459
if new_code ~= code then

test/tests/language_server/integration.lua

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local LSPClient = require("test.helpers.lsp_client")
22
local lsp = require("language_server.lsp")
33
local Compiler = require("nattlua.compiler")
4+
local fs = require("nattlua.other.fs")
45

56
local function find_position(code, pattern)
67
local marker = "|"
@@ -337,6 +338,41 @@ do
337338
end)
338339
end
339340

341+
do
342+
local old_fs_read = fs.read
343+
local client = LSPClient.New()
344+
client:SetWorkingDirectory("/workspace")
345+
local root_uri = "file:///workspace"
346+
client:Initialize(lsp, root_uri)
347+
local file_uri = root_uri .. "/format_from_disk.nlua"
348+
local file_path = "/workspace/format_from_disk.nlua"
349+
local code = [[
350+
local a=1
351+
local b=2]]
352+
fs.read = function(read_path)
353+
if read_path == file_path then return code end
354+
355+
return old_fs_read(read_path)
356+
end
357+
358+
local edits = client:Call(
359+
lsp,
360+
"textDocument/formatting",
361+
{
362+
textDocument = {uri = file_uri},
363+
options = {
364+
tabSize = 4,
365+
insertSpaces = true,
366+
},
367+
}
368+
)
369+
370+
fs.read = old_fs_read
371+
assert(#edits > 0, "Formatting should return edits for files loaded from disk")
372+
assert(edits[1].newText:find("local a = 1", 1, true))
373+
assert(not lsp.editor_helper.OpenFiles[file_path], "Formatting should not require opening the document")
374+
end
375+
340376
do
341377
local client = LSPClient.New()
342378
client:SetWorkingDirectory("/workspace")

0 commit comments

Comments
 (0)