Skip to content

Commit 6bc64ea

Browse files
committed
fix: improve option parsing
1 parent 61054ac commit 6bc64ea

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

source/prettier.lua

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,23 @@ local prettierFileTypes = {
2828
-- The default configuration for prettier
2929
-- See https://prettier.io/docs/en/options.html
3030
local preferences = {
31-
tabWidth = "2",
32-
useTabs = "false",
33-
semi = "true",
34-
singleQuote = "false",
31+
tabWidth = 2,
32+
useTabs = false,
33+
semi = true,
34+
singleQuote = false,
3535
quoteProps = "as-needed",
36-
jsxSingleQuotes = "false",
36+
jsxSingleQuotes = false,
3737
trailingComma = "es5",
38-
bracketSpacing = "true",
39-
bracketSameLine = "false",
38+
bracketSpacing = true,
39+
bracketSameLine = false,
4040
arrowParens = "always",
4141
proseWrap = "preserve",
4242
endOfLine = "lf",
4343
embeddedLanguageFormatting = "auto",
44-
vueIndentScriptAndStyle = "false",
44+
vueIndentScriptAndStyle = false,
4545
htmlWhitespaceSensitivity = "css",
46-
requirePragma = "false",
47-
insertPragma = "false"
46+
requirePragma = false,
47+
insertPragma = false
4848
}
4949

5050
-- Check if a table has a certain value
@@ -58,6 +58,19 @@ local function hasValue(tab, val)
5858
return false
5959
end
6060

61+
-- Retrieve a setting
62+
function getOption(file, key, default)
63+
if file.Buf.Settings["prettier" .. "." .. key] ~= nil then
64+
-- If the local buffer's settings has a value set for that option, return it
65+
return file.Buf.Settings["prettier" .. "." .. key]
66+
elseif config.GetGlobalOption("prettier" .. "." .. key) ~= nil then
67+
-- If the global settings has a value set for that option, return it
68+
return config.GetGlobalOption("prettier" .. "." .. key)
69+
else
70+
return default
71+
end
72+
end
73+
6174
-- Format the given file
6275
function format(file)
6376
-- Save the file first
@@ -70,20 +83,27 @@ function format(file)
7083

7184
-- If not, then use the preferences set for this plugin
7285
local options = ""
73-
if string.sub(pathToConfig, 1, 7) == "[error]" then
86+
if error ~= nil then
87+
-- Loop through the options and create a long options string to pass to the
88+
-- prettier cli
7489
for key, value in pairs(preferences) do
75-
value = file.Buf.Settings["prettier" .. "." .. key] or config.GetGlobalOption("prettier" .. "." .. key) or value
76-
90+
-- Check if the user has set a different value in:
91+
-- - local settings
92+
-- - global settings
93+
-- else use the default value
94+
value = tostring(getOption(file, key, value))
95+
96+
-- Convert the option name to kebab case and append it to the existing
97+
-- option string
7798
options = options .. " --" .. key:gsub("%u", function (word)
7899
return "-" .. string.lower(word)
79100
end) .. "=" .. tostring(value)
80101
end
81102
end
82103

83-
-- Run prettier
84-
local message, error = shell.RunCommand(
85-
"prettier" .. options .. " --no-color --write " .. file.Buf.Path
86-
)
104+
-- Run prettier on the file
105+
local command = "prettier" .. options .. " --no-color --write " .. file.Buf.Path
106+
local message, error = shell.RunCommand(command)
87107
-- If we encounter an error, flash a message on the bar at the bottom and dump
88108
-- the message in the logs
89109
if error ~= nil then
@@ -112,7 +132,7 @@ end
112132
-- Editor initialization hook
113133
function init()
114134
-- Register prettier specific configuration
115-
for key, value in ipairs(preferences) do
135+
for key, value in pairs(preferences) do
116136
config.RegisterCommonOption("prettier", key, value)
117137
end
118138
-- Register plugin specific options

0 commit comments

Comments
 (0)