Skip to content

Commit 287e893

Browse files
committed
fix!: make option names consistent with rest of micro
- add options list (description, default, possible values) to `readme.md` - refactor the way the `prettier` command is generated
1 parent d7a2fcb commit 287e893

File tree

2 files changed

+117
-39
lines changed

2 files changed

+117
-39
lines changed

readme.md

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
# <div align="center"> Micro <3 Prettier </div>
22

3-
> A [`micro`](https://github.com/zyedidia/micro) plugin that formats files using
4-
> [`prettier`](https://github.com/prettier/prettier).
3+
> A [`Micro`](https://github.com/zyedidia/micro) editor plugin that formats
4+
> files using [`Prettier`](https://github.com/prettier/prettier).
55
66
## Installation and Usage
77

8+
Simply run the following in terminal to install the plugin:
9+
810
```
911
micro --plugin install prettier
1012
```
1113

12-
The next time you edit and save a file using micro, it will get automatically
14+
The next time you edit and save a file using Micro, it will get automatically
1315
formatted using Prettier.
1416

1517
## Configuration
1618

17-
If prettier detects a configuration file, the options set for the plugin will
19+
If Prettier detects a configuration file, the options set for the plugin will
1820
not apply. If there is no configuration present, then the options set for the
19-
plugin (or the default values) will be used.
21+
plugin (or the default values take from
22+
[here](https://prettier.io/docs/en/options.html)) will be used.
2023

2124
You can set an option by running the following command (Press `Ctrl+E`, then run
2225
the following):
@@ -25,9 +28,30 @@ the following):
2528
set prettier.<name> <value>
2629
```
2730

28-
> See
29-
> [the official Prettier documentation](https://prettier.io/docs/en/options.html)
30-
> for a list of options and their possible values.
31+
Where `name` is an option from the table, and `value` is one of the possible
32+
values for that option. See the official Prettier documentation for an even mroe
33+
detail explanation of each option and its possible values.
34+
35+
| Option | Description | Default Value | Possible Values |
36+
| --------------------------- | ------------------------------------------------------------------------------------------------------------------------- | ------------- | ------------------------------------- |
37+
| `tabwidth` | Specify the number of spaces per indentation-level. | `2` | `<number>` |
38+
| `usetabs` | Indent lines with tabs instead of spaces. | `false` | `true`, `false` |
39+
| `semi` | Print semicolons at the ends of statements. | `true` | `true`, `false` |
40+
| `singlequote` | Use single quotes instead of double quotes. | `false` | `true`, `false` |
41+
| `quoteProps` | Change when properties in objects are quoted. | `as-needed` | `as-needed`, `consistent`, `preserve` |
42+
| `jsxsinglequote` | Use single quotes instead of double quotes in JSX. | `false` | `true`, `false` |
43+
| `trailingcomma` | Print trailing commas wherever possible in multi-line comma-separated syntactic structures. | `es5` | `none`, `es5`, `always` |
44+
| `bracketspacing` | Print spaces between brackets in object literals. | `true` | `true`, `false` |
45+
| `bracketsameline` | Put the > of a multi-line HTML element at the end of the last line instead of being alone on the next line. | `false` | `true`, `false` |
46+
| `arrowparens` | Include parentheses around a sole arrow function parameter. | `always` | `always` , `avoid` |
47+
| `wrap` | Hard wrap lines at the column specified by the `cols` setting. | `preserve` | `always`, `never`, `preserve` |
48+
| `cols` | Specify the line length that the printer will wrap on. | `80` | `<number>` |
49+
| `eol` | The line feed character to use in all files. | `lf` | `lf`, `crlf`, `cr`, `auto` |
50+
| `formatembeddedcode` | Whether or not to format quoted code embedded in the file. | `auto` | `auto`, `off` |
51+
| `indentvuecode` | Whether or not to indent the code inside `<script>` and `<style>` tags in Vue files. | `false` | `true`, `false` |
52+
| `htmlwhitespacesensitivity` | Specify the global whitespace sensitivity for HTML, Vue, Angular, and Handlebars. | `css` | `css`, `strict`, `ignore` |
53+
| `requirepragma` | Whether to only format files that contain a special comment, called a pragma, at the top of the file. | `false` | `true`, `false` |
54+
| `insertpragma` | Whether to insert a special @format marker at the top of files specifying that the file has been formatted with Prettier. | `false` | `true`, `false` |
3155

3256
You can also format the document without saving it by running the following
3357
command (Press `Ctrl+E`, then run the following):
@@ -40,7 +64,7 @@ You can switch off the format-on-save feature by running the following command
4064
(Press `Ctrl+E`, then run the following):
4165

4266
```
43-
set prettier.formatOnSave off
67+
set prettier.autoformat off
4468
```
4569

4670
## Issues and Contributing

source/prettier.lua

Lines changed: 84 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ VERSION = "0.1.3"
33
-- prettier
44
-- A micro plugin that automatically formats files using prettier on save
55

6-
-- Plugin source: https://github.com/gamemaker1/micro-plugin-prettier
7-
-- Prettier source: https://github.com/prettier/prettier
6+
-- plugin source: https://github.com/gamemaker1/micro-plugin-prettier
7+
-- prettier source: https://github.com/prettier/prettier
88

99
local micro = import("micro")
1010
local shell = import("micro/shell")
@@ -28,23 +28,78 @@ 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,
35-
quoteProps = "as-needed",
36-
jsxSingleQuotes = false,
37-
trailingComma = "es5",
38-
bracketSpacing = true,
39-
bracketSameLine = false,
40-
arrowParens = "always",
41-
proseWrap = "preserve",
42-
endOfLine = "lf",
43-
embeddedLanguageFormatting = "auto",
44-
vueIndentScriptAndStyle = false,
45-
htmlWhitespaceSensitivity = "css",
46-
requirePragma = false,
47-
insertPragma = false
31+
tabwidth = {
32+
flag = "--tab-width",
33+
default = 2
34+
},
35+
usetabs = {
36+
flag = "--use-tabs",
37+
default = false
38+
},
39+
semi = {
40+
flag = "--semi",
41+
default = true
42+
},
43+
singlequote = {
44+
flag = "--single-quote",
45+
default = false
46+
},
47+
quoteProps = {
48+
flag = "--quote-props",
49+
default = "as-needed",
50+
},
51+
jsxsinglequote = {
52+
flag = "--jsx-single-quote",
53+
default = false
54+
},
55+
trailingcomma = {
56+
flag = "--trailing-comma",
57+
default = "es5"
58+
},
59+
bracketspacing = {
60+
flag = "--bracket-spacing",
61+
default = true
62+
},
63+
bracketsameline = {
64+
flag = "--bracket-same-line",
65+
default = false
66+
},
67+
arrowparens = {
68+
flag = "--arrow-parens",
69+
default = "always"
70+
},
71+
wrap = {
72+
flag = "--prose-wrap",
73+
default = "preserve"
74+
},
75+
cols = {
76+
flag = "--print-width",
77+
default = 80
78+
},
79+
eol = {
80+
flag = "--end-of-line",
81+
default = "lf"
82+
},
83+
formatembeddedcode = {
84+
flag = "--embedded-language-formatting",
85+
default = "auto"
86+
},
87+
indentvuecode = {
88+
flag = "--vue-indent-script-and-style",
89+
default = false
90+
},
91+
htmlwhitespacesensitivity = {
92+
flag = "--html-whitespace-sensitivity",
93+
default = "css"
94+
},
95+
requirepragma = {
96+
flag = "--require-pragma",
97+
default = false
98+
},
99+
insertpragma = {
100+
flag = "--insert-pragma",
101+
default = false
102+
}
48103
}
49104

50105
-- Check if a table has a certain value
@@ -61,7 +116,7 @@ end
61116
-- Retrieve a setting
62117
function getOption(file, key, default)
63118
if file.Buf.Settings["prettier" .. "." .. key] ~= nil then
64-
-- If the local buffer's settings has a value set for that option, return it
119+
-- If the local buffer"s settings has a value set for that option, return it
65120
return file.Buf.Settings["prettier" .. "." .. key]
66121
elseif config.GetGlobalOption("prettier" .. "." .. key) ~= nil then
67122
-- If the global settings has a value set for that option, return it
@@ -86,20 +141,19 @@ function format(file)
86141
if error ~= nil then
87142
-- Loop through the options and create a long options string to pass to the
88143
-- prettier cli
89-
for key, value in pairs(preferences) do
144+
for option, value in pairs(preferences) do
90145
-- Check if the user has set a different value in:
91146
-- - local settings
92147
-- - global settings
93148
-- else use the default value
94-
value = tostring(getOption(file, key, value))
149+
value.default = tostring(getOption(file, option, value.default))
95150

96151
-- Convert the option name to kebab case and append it to the existing
97152
-- option string
98-
options = options .. " --" .. key:gsub("%u", function (word)
99-
return "-" .. string.lower(word)
100-
end) .. "=" .. tostring(value)
153+
options = options .. " " .. value.flag .. "=" .. tostring(value.default)
101154
end
102155
end
156+
buffer.Log(options)
103157

104158
-- Run prettier on the file
105159
local command = "prettier" .. options .. " --no-color --write " .. file.Buf.Path
@@ -121,8 +175,8 @@ end
121175
-- On file save hook
122176
function onSave(file)
123177
-- If the current file is of any of the above types and the user has enabled
124-
-- `formatOnSave`, then format away!
125-
if hasValue(prettierFileTypes, file.Buf:FileType()) and config.GetGlobalOption("prettier.formatOnSave") then
178+
-- `autoformat`, then format away!
179+
if hasValue(prettierFileTypes, file.Buf:FileType()) and config.GetGlobalOption("prettier.autoformat") then
126180
format(file)
127181
end
128182

@@ -132,11 +186,11 @@ end
132186
-- Editor initialization hook
133187
function init()
134188
-- Register prettier specific configuration
135-
for key, value in pairs(preferences) do
136-
config.RegisterCommonOption("prettier", key, value)
189+
for option, value in pairs(preferences) do
190+
config.RegisterCommonOption("prettier", option, value.default)
137191
end
138192
-- Register plugin specific options
139-
config.RegisterCommonOption("prettier", "formatOnSave", true)
193+
config.RegisterCommonOption("prettier", "autoformat", true)
140194

141195
-- Register the `format` command
142196
config.MakeCommand("format", format, config.NoComplete)

0 commit comments

Comments
 (0)