Skip to content

Commit 0846724

Browse files
committed
feat: create stylua plugin
1 parent b9702e3 commit 0846724

2 files changed

Lines changed: 108 additions & 0 deletions

File tree

lua/stylua/init.lua

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
local Job = require("plenary.job")
2+
local Path = require("plenary.path")
3+
4+
local lspconfig_util = require("lspconfig.util")
5+
6+
local DEFAULT_OPTS = {
7+
column_width = 120,
8+
line_endings = "Unix",
9+
indent_type = "Tabs",
10+
indent_width = 4,
11+
quote_style = "AutoPreferDouble",
12+
call_parentheses = "Always",
13+
}
14+
15+
local M = {}
16+
M._config = {}
17+
18+
local root_finder = lspconfig_util.root_pattern(".git")
19+
20+
local function find_stylua(path)
21+
if M._config[path] == nil then
22+
local file_path = Path:new(path)
23+
local root_path = Path:new(root_finder(path))
24+
25+
local file_parents = file_path:parents()
26+
local root_parents = root_path:parents()
27+
28+
local relative_diff = #file_parents - #root_parents
29+
for index, dir in ipairs(file_parents) do
30+
if index > relative_diff then
31+
break
32+
end
33+
34+
local stylua_path = Path:new { dir, "stylua.toml" }
35+
if stylua_path:exists() then
36+
M._config[path] = stylua_path:absolute()
37+
break
38+
end
39+
40+
stylua_path = Path:new { dir, ".stylua.toml" }
41+
if stylua_path:exists() then
42+
M._config[path] = stylua_path:absolute()
43+
break
44+
end
45+
end
46+
end
47+
48+
return M._config[path]
49+
end
50+
function M.setup(opts)
51+
local conf = vim.tbl_deep_extend("force", DEFAULT_OPTS, opts or {})
52+
53+
M._config.column_width = conf.column_width
54+
M._config.line_endings = conf.line_endings
55+
M._config.indent_type = conf.indent_type
56+
M._config.indent_width = conf.indent_width
57+
M._config.quote_style = conf.quote_style
58+
M._config.call_parentheses = conf.call_parentheses
59+
end
60+
61+
function M.format(bufnr)
62+
bufnr = bufnr or vim.api.nvim_get_current_buf()
63+
local filepath = Path:new(vim.api.nvim_buf_get_name(bufnr)):absolute()
64+
local stylua_toml = find_stylua(filepath)
65+
66+
local args
67+
if stylua_toml then
68+
args = { "--config-path", stylua_toml }
69+
else
70+
args = {
71+
"--column-width", M._config.column_width,
72+
"--line-endings", M._config.line_endings,
73+
"--indent-type", M._config.indent_type,
74+
"--indent-width", M._config.indent_width,
75+
"--quote-style", M._config.quote_style,
76+
"--call-parentheses", M._config.call_parentheses,
77+
}
78+
end
79+
80+
table.insert(args, "-")
81+
82+
local errors = {}
83+
local job = Job:new({
84+
command = "stylua",
85+
args = args,
86+
writer = vim.api.nvim_buf_get_lines(0, 0, -1, false),
87+
on_stderr = function (_, data)
88+
table.insert(errors, data)
89+
end
90+
})
91+
92+
local output = job:sync()
93+
if job.code ~= 0 then
94+
vim.schedule(function()
95+
error(string.format("[stylua] %s", errors[0] or "Failed to format due to errors"))
96+
end)
97+
98+
return
99+
end
100+
101+
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, output)
102+
end
103+
104+
return M

stylua.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
indent_type = "Spaces"
2+
indent_width = 2
3+
column_width = 80
4+
quote_style = "AutoPreferDouble"

0 commit comments

Comments
 (0)