-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
format.lua
167 lines (147 loc) · 4.5 KB
/
format.lua
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
-- Code formatting pipelines
local Methods = vim.lsp.protocol.Methods
---@param names string[]
local function notify(names)
if #names == 0 then
return
end
vim.notify("format", vim.log.levels.INFO, {
render = "compact",
title = ("[LSP] %s"):format(table.concat(names, ", ")),
})
end
local M = {}
-- ===========================================================================
---@TODO move this to individual ftplugins ?
---@type table<ft, function>
local pipelines = {}
pipelines["html"] = function()
require("dko.format.efm").format({ pipeline = "html" })
end
pipelines["javascript"] = function()
require("dko.format.javascript")(notify)
end
pipelines["javascriptreact"] = pipelines["javascript"]
pipelines["typescript"] = pipelines["javascript"]
pipelines["typescriptreact"] = pipelines["javascript"]
pipelines["json"] = function()
require("dko.format.efm").format_with("prettier", { pipeline = "json" })
end
pipelines["jsonc"] = pipelines["json"]
pipelines["lua"] = function()
require("dko.format.efm").format_with("stylua", { pipeline = "lua" })
end
pipelines["markdown"] = require("dko.format.markdown")
pipelines["yaml"] = function()
if vim.bo.filetype == "yaml.docker-compose" then
vim.lsp.buf.format({ name = "docker_compose_language_service" })
notify({ "docker_compose_language_service" })
return
end
require("dko.format.efm").format_with("yamlfmt", { pipeline = "yamlfmt" })
end
--- See options for vim.lsp.buf.format
M.run_pipeline = function(options)
local pipeline = pipelines[vim.bo.filetype]
if pipeline then
return pipeline()
end
local names = {}
options = vim.tbl_deep_extend("force", options or {}, {
filter = function(client)
if not client.supports_method(Methods.textDocument_formatting) then
return false
end
if client.name == "efm" then
local configs =
require("dko.tools").get_efm_languages()[vim.bo.filetype]
local filtered = vim.tbl_filter(function(config)
if config.formatCommand ~= nil then
table.insert(
names,
("efm[%s]"):format(
vim.fn.fnamemodify(config.formatCommand:match("([^%s]+)"), ":t")
)
)
return true
end
return false
end, configs)
return #filtered ~= 0
end
table.insert(names, client.name)
return true
end,
})
-- https://github.com/neovim/neovim/blob/master/runtime/lua/vim/lsp/buf.lua#L156-L196
vim.lsp.buf.format(options)
notify(names)
end
-- =============================================================================
-- Autocmd handlers
-- =============================================================================
-- LspAttach autocmd callback
M.enable_on_lspattach = function(args)
-- already enabled from a previous client
if vim.b.enable_format_on_save then
return
end
local bufnr = args.buf
local clients = vim.lsp.get_clients({
id = args.data.client_id,
bufnr = bufnr,
method = Methods.textDocument_formatting,
})
if #clients == 0 then -- just to shut up type checking
return
end
vim.b.enable_format_on_save = true
vim.notify(
("Format on save enabled using %s"):format(clients[1].name),
vim.log.levels.INFO,
{ title = "[LSP]", render = "compact" }
)
end
-- LspDetach autocmd callback
M.disable_on_lspdetach = function(args)
-- was already disabled manually?
if not vim.b.enable_format_on_save then
return
end
local bufnr = args.buf
local detached_client_id = args.data.client_id
-- @TODO could i just check using { bufnr, and method } here? Or does it still
-- include in the client being detached?
local capable_clients = vim.lsp.get_clients({
bufnr = bufnr,
method = Methods.textDocument_formatting,
})
local still_has_formatter = vim.iter(capable_clients):any(function(client)
return client.id ~= detached_client_id
end)
if still_has_formatter then
return
end
vim.b.enable_format_on_save = false
vim.notify(
"Format on save disabled, no capable clients attached",
vim.log.levels.INFO,
{ title = "[LSP]", render = "compact" }
)
end
-- autocmd callback for *WritePre
M.format_on_save = function()
-- callback gets arg
-- {
-- buf = 1,
-- event = "BufWritePre",
-- file = "nvim/lua/dko/behaviors.lua",
-- id = 127,
-- match = "/home/davidosomething/.dotfiles/nvim/lua/dko/behaviors.lua"
-- }
if not vim.b.enable_format_on_save then
return
end
M.run_pipeline({ async = false })
end
return M