|
1 | 1 | local LSPClient = require("test.helpers.lsp_client") |
2 | 2 | local lsp = require("language_server.lsp") |
3 | 3 | local Compiler = require("nattlua.compiler") |
| 4 | +local fs = require("nattlua.other.fs") |
4 | 5 |
|
5 | 6 | local function find_position(code, pattern) |
6 | 7 | local marker = "|" |
|
337 | 338 | end) |
338 | 339 | end |
339 | 340 |
|
| 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 | + |
340 | 376 | do |
341 | 377 | local client = LSPClient.New() |
342 | 378 | client:SetWorkingDirectory("/workspace") |
|
0 commit comments