Skip to content

Commit c66cee6

Browse files
committed
fix format on save vs project format obeying different newline rules
1 parent d23f5a7 commit c66cee6

4 files changed

Lines changed: 99 additions & 15 deletions

File tree

language_server/editor_helper.lua

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,23 @@ local Token = require("nattlua.lexer.token")
3030
local META = class.CreateTemplate("editor_helper")
3131
META:GetSet("WorkingDirectory", "./")
3232

33+
local format_emitter_defaults = {
34+
pretty_print = true,
35+
string_quote = "\"",
36+
no_semicolon = true,
37+
comment_type_annotations = true,
38+
type_annotations = "explicit",
39+
force_parenthesis = true,
40+
omit_parentheses_for_single_table_call = true,
41+
trailing_newline = true,
42+
}
43+
44+
local function apply_format_emitter_defaults(emitter_config)
45+
for k, v in pairs(format_emitter_defaults) do
46+
if emitter_config[k] == nil then emitter_config[k] = v end
47+
end
48+
end
49+
3350
function META:SetWorkingDirectory(dir)
3451
self.WorkingDirectory = dir
3552
end
@@ -522,6 +539,9 @@ function META:Recompile(path, lol, diagnostics, on_save_path)
522539
cfg_copy[k] = v
523540
end
524541

542+
543+
if cfg_copy.trailing_newline == nil then cfg_copy.trailing_newline = true end
544+
525545
cfg_copy.remove_unused = true
526546
cfg_copy.skip_import = true
527547
cfg_copy.no_pipeline = true
@@ -653,19 +673,7 @@ end
653673
function META:Format(code, path, extra_emitter_config)
654674
path = path_util.Normalize(path)
655675
local config = self:GetCompilerConfig(path)
656-
local emitter_cfg = {
657-
pretty_print = true,
658-
string_quote = "\"",
659-
no_semicolon = true,
660-
comment_type_annotations = true,
661-
type_annotations = "explicit",
662-
force_parenthesis = true,
663-
omit_parentheses_for_single_table_call = true,
664-
}
665-
666-
for k, v in pairs(emitter_cfg) do
667-
if config.emitter[k] == nil then config.emitter[k] = v end
668-
end
676+
apply_format_emitter_defaults(config.emitter)
669677

670678
config.parser = {skip_import = true}
671679
config.emitter.comment_type_annotations = path:sub(-#".lua") == ".lua"

language_server/lsp.lua

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,6 @@ lsp.methods["nattlua/format"] = function(params)
254254
local path = to_fs_path(params.textDocument.uri)
255255
local code = editor_helper:Format(params.code, path)
256256

257-
if code:sub(#code, #code) ~= "\n" then code = code .. "\n" end
258-
259257
return {
260258
code = b64.encode(code),
261259
}

test/tests/language_server/editor_helper.lua

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,34 @@ do
204204
assert(formatted:find("local f = assert%(loadfile%)") == nil)
205205
end
206206

207+
do
208+
local helper = EditorHelper.New()
209+
helper:Initialize()
210+
local format_path = "./main.nlua"
211+
local code = [[
212+
local a=1
213+
local b=2]]
214+
helper:OpenFile(format_path, code)
215+
local formatted = helper:Format(code, format_path)
216+
assert(formatted:sub(#formatted, #formatted) == "\n")
217+
218+
helper:SetConfigFunction(function(path)
219+
return {
220+
["get-compiler-config"] = function()
221+
return {
222+
emitter = {
223+
trailing_newline = false,
224+
},
225+
lsp = {entry_point = path},
226+
}
227+
end,
228+
}
229+
end)
230+
231+
formatted = helper:Format(code, format_path)
232+
assert(formatted:sub(#formatted, #formatted) ~= "\n")
233+
end
234+
207235
do
208236
local helper = EditorHelper.New()
209237
helper:Initialize()

test/tests/language_server/integration.lua

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,56 @@ do
8585
assert(#highlights > 0, "Should have highlights for 'a'")
8686
end
8787

88+
do
89+
lsp.editor_helper:SetConfigFunction(function(path)
90+
return {
91+
["get-compiler-config"] = function()
92+
return {
93+
lsp = {entry_point = path},
94+
}
95+
end,
96+
}
97+
end)
98+
99+
local client = LSPClient.New()
100+
client:SetWorkingDirectory("/workspace")
101+
local root_uri = "file:///workspace"
102+
client:Initialize(lsp, root_uri)
103+
local file_uri = root_uri .. "/format.nlua"
104+
local code = [[
105+
local a=1
106+
local b=2]]
107+
client:Notify(
108+
lsp,
109+
"textDocument/didOpen",
110+
{
111+
textDocument = {
112+
uri = file_uri,
113+
languageId = "nattlua",
114+
version = 1,
115+
text = code,
116+
},
117+
}
118+
)
119+
local edits = client:Call(
120+
lsp,
121+
"textDocument/formatting",
122+
{
123+
textDocument = {uri = file_uri},
124+
options = {
125+
tabSize = 4,
126+
insertSpaces = true,
127+
},
128+
}
129+
)
130+
assert(#edits > 0, "Formatting should return edits for unformatted code")
131+
assert(edits[1].newText:sub(#edits[1].newText, #edits[1].newText) == "\n")
132+
133+
lsp.editor_helper:SetConfigFunction(function()
134+
return
135+
end)
136+
end
137+
88138
do
89139
local client = LSPClient.New()
90140
client:SetWorkingDirectory("/workspace")

0 commit comments

Comments
 (0)