-
Notifications
You must be signed in to change notification settings - Fork 486
Expand file tree
/
Copy pathformatting.lua
More file actions
205 lines (187 loc) · 5.59 KB
/
formatting.lua
File metadata and controls
205 lines (187 loc) · 5.59 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
local M = {}
local settings = require("core.settings")
local disabled_workspaces = settings.format_disabled_dirs
local format_on_save = settings.format_on_save
local format_notify = settings.format_notify
local format_modifications_only = settings.format_modifications_only
local server_formatting_block_list = settings.server_formatting_block_list
local format_timeout = settings.format_timeout
vim.api.nvim_create_user_command("Format", function()
M.format({
timeout = format_timeout,
filter = M.format_filter,
})
end, {})
vim.api.nvim_create_user_command("FormatToggle", function()
M.toggle_format_on_save()
end, {})
local block_list = settings.formatter_block_list
vim.api.nvim_create_user_command("FormatterToggleFt", function(opts)
if block_list[opts.args] == nil then
vim.notify(
string.format("[LSP] Formatter for [%s] has been recorded in list and disabled.", opts.args),
vim.log.levels.WARN,
{ title = "LSP Formatter Warning" }
)
block_list[opts.args] = true
else
block_list[opts.args] = not block_list[opts.args]
vim.notify(
string.format(
"[LSP] Formatter for [%s] has been %s.",
opts.args,
not block_list[opts.args] and "enabled" or "disabled"
),
not block_list[opts.args] and vim.log.levels.INFO or vim.log.levels.WARN,
{ title = string.format("LSP Formatter %s", not block_list[opts.args] and "Info" or "Warning") }
)
end
end, { nargs = 1, complete = "filetype" })
function M.enable_format_on_save(is_configured)
local opts = { pattern = "*", timeout = format_timeout }
vim.api.nvim_create_augroup("format_on_save", { clear = true })
vim.api.nvim_create_autocmd("BufWritePre", {
group = "format_on_save",
pattern = opts.pattern,
callback = function()
require("completion.formatting").format({
timeout_ms = opts.timeout,
filter = M.format_filter,
})
end,
})
if not is_configured then
vim.notify(
"Successfully enabled format-on-save",
vim.log.levels.INFO,
{ title = "Settings modification success" }
)
end
end
function M.disable_format_on_save(is_configured)
pcall(vim.api.nvim_del_augroup_by_name, "format_on_save")
if not is_configured then
vim.notify(
"Successfully disabled format-on-save",
vim.log.levels.INFO,
{ title = "Settings modification success" }
)
end
end
function M.configure_format_on_save()
if format_on_save then
M.enable_format_on_save(true)
else
M.disable_format_on_save(true)
end
end
function M.toggle_format_on_save()
local status = pcall(vim.api.nvim_get_autocmds, {
group = "format_on_save",
event = "BufWritePre",
})
if not status then
M.enable_format_on_save(false)
else
M.disable_format_on_save(false)
end
end
function M.format_filter(clients)
return vim.tbl_filter(function(client)
local status_ok, formatting_supported = pcall(function()
return client.supports_method("textDocument/formatting")
end)
if status_ok and formatting_supported and client.name == "null-ls" then
return "null-ls"
elseif not server_formatting_block_list[client.name] and status_ok and formatting_supported then
return client.name
end
end, clients)
end
function M.format(opts)
local filedir = vim.fn.expand("%:p:h")
for i = 1, #disabled_workspaces do
if vim.regex(vim.fs.normalize(disabled_workspaces[i])):match_str(filedir) ~= nil then
vim.notify(
string.format(
"[LSP] Formatting for all files under [%s] has been disabled.",
vim.fs.normalize(disabled_workspaces[i])
),
vim.log.levels.WARN,
{ title = "LSP Formatter Warning" }
)
return
end
end
local bufnr = opts.bufnr or vim.api.nvim_get_current_buf()
local clients = vim.lsp.get_clients({ bufnr = bufnr })
if opts.filter then
clients = opts.filter(clients)
elseif opts.id then
clients = vim.tbl_filter(function(client)
return client.id == opts.id
end, clients)
elseif opts.name then
clients = vim.tbl_filter(function(client)
return client.name == opts.name
end, clients)
end
clients = vim.tbl_filter(function(client)
return client.supports_method("textDocument/formatting")
end, clients)
if #clients == 0 then
vim.notify(
"[LSP] Format request failed, no matching language servers.",
vim.log.levels.WARN,
{ title = "Formatting Failed" }
)
end
local timeout_ms = opts.timeout_ms
for _, client in pairs(clients) do
if block_list[vim.bo.filetype] == true then
vim.notify(
string.format(
"[LSP][%s] Formatting for [%s] has been disabled. This file is not being processed.",
client.name,
vim.bo.filetype
),
vim.log.levels.WARN,
{ title = "LSP Formatter Warning" }
)
return
end
if
format_modifications_only
and require("lsp-format-modifications").format_modifications(client, bufnr).success
then
if format_notify then
vim.notify(
string.format("[LSP] Format changed lines successfully with %s!", client.name),
vim.log.levels.INFO,
{ title = "LSP Range Format Success" }
)
end
return
end
-- Fall back to format the whole buffer (even if partial formatting failed)
local params = vim.lsp.util.make_formatting_params(opts.formatting_options)
local result, err = client.request_sync("textDocument/formatting", params, timeout_ms, bufnr)
if result and result.result then
vim.lsp.util.apply_text_edits(result.result, bufnr, client.offset_encoding)
if format_notify then
vim.notify(
string.format("[LSP] Format successfully with %s!", client.name),
vim.log.levels.INFO,
{ title = "LSP Format Success" }
)
end
elseif err then
vim.notify(
string.format("[LSP][%s] %s", client.name, err),
vim.log.levels.ERROR,
{ title = "LSP Format Error" }
)
end
end
end
return M