Skip to content

Commit 61054ac

Browse files
committed
feat: create micro-plugin-prettier
0 parents  commit 61054ac

File tree

4 files changed

+178
-0
lines changed

4 files changed

+178
-0
lines changed

assets/repo.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[
2+
{
3+
"Name": "prettier",
4+
"Description": "Format your code using Prettier",
5+
"Website": "https://github.com/gamemaker1/micro-plugin-prettier",
6+
"Tags": ["javascript", "node", "nodejs", "typescript", "formatting"],
7+
"Versions": [
8+
{
9+
"Version": "0.1.0",
10+
"Url": "https://github.com/gamemaker1/micro-plugin-prettier/releases/latest/download/prettier-0.1.0.zip",
11+
"Require": {
12+
"micro": ">=1.0.3"
13+
}
14+
}
15+
]
16+
}
17+
]

license.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# ISC License
2+
3+
Copyright 2021 Vedant K <https://github.com/gamemaker1>
4+
5+
Permission to use, copy, modify, and/or distribute this software for any purpose
6+
with or without fee is hereby granted, provided that the above copyright notice
7+
and this permission notice appear in all copies.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
11+
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
13+
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
14+
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
15+
THIS SOFTWARE.

readme.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# <div align="center"> Micro <3 Prettier </div>
2+
3+
> A [`micro`](https://github.com/zyedidia/micro) plugin that formats files using
4+
> [`prettier`](https://github.com/prettier/prettier).
5+
6+
## Installation and Usage
7+
8+
```
9+
micro --plugin install prettier
10+
```
11+
12+
The next time you edit and save a file using micro, it will get automatically
13+
formatted using Prettier.
14+
15+
## Issues and Contributing
16+
17+
Found a bug? Don't like something? Want to see something added/changed? Open
18+
[an issue](https://github.com/gamemaker1/micro-plugin-prettier/issues/new)!
19+
20+
## License
21+
22+
This plugin is licensed under the ISC license. See the [license file](license.md)
23+
for the complete license text.

source/prettier.lua

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
VERSION = "0.1.0"
2+
3+
-- prettier
4+
-- A micro plugin that automatically formats files using prettier on save
5+
6+
-- Plugin source: https://github.com/gamemaker1/micro-plugin-prettier
7+
-- Prettier source: https://github.com/prettier/prettier
8+
9+
local micro = import("micro")
10+
local shell = import("micro/shell")
11+
local buffer = import("micro/buffer")
12+
local config = import("micro/config")
13+
14+
-- Run prettier on any of these files
15+
local prettierFileTypes = {
16+
"javascript",
17+
"typescript",
18+
"jsx", "tsx",
19+
"angular",
20+
"vue", "svelte",
21+
"flow",
22+
"css", "less", "scss",
23+
"html",
24+
"json", "yaml",
25+
"graphql",
26+
"markdown"
27+
}
28+
-- The default configuration for prettier
29+
-- See https://prettier.io/docs/en/options.html
30+
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"
48+
}
49+
50+
-- Check if a table has a certain value
51+
local function hasValue(tab, val)
52+
for index, value in ipairs(tab) do
53+
if value == val then
54+
return true
55+
end
56+
end
57+
58+
return false
59+
end
60+
61+
-- Format the given file
62+
function format(file)
63+
-- Save the file first
64+
file:Save()
65+
66+
-- Check if prettier can find user provided configuration
67+
local pathToConfig, error = shell.RunCommand(
68+
"prettier" .. " --no-color --find-config-path " .. file.Buf.Path
69+
)
70+
71+
-- If not, then use the preferences set for this plugin
72+
local options = ""
73+
if string.sub(pathToConfig, 1, 7) == "[error]" then
74+
for key, value in pairs(preferences) do
75+
value = file.Buf.Settings["prettier" .. "." .. key] or config.GetGlobalOption("prettier" .. "." .. key) or value
76+
77+
options = options .. " --" .. key:gsub("%u", function (word)
78+
return "-" .. string.lower(word)
79+
end) .. "=" .. tostring(value)
80+
end
81+
end
82+
83+
-- Run prettier
84+
local message, error = shell.RunCommand(
85+
"prettier" .. options .. " --no-color --write " .. file.Buf.Path
86+
)
87+
-- If we encounter an error, flash a message on the bar at the bottom and dump
88+
-- the message in the logs
89+
if error ~= nil then
90+
micro.InfoBar():Error(
91+
"Prettier: an error occurred while formatting the file. Check the logs (Ctrl+E > log) for the full error."
92+
)
93+
buffer.Log(message)
94+
95+
return
96+
end
97+
98+
file.Buf:ReOpen()
99+
end
100+
101+
-- On file save hook
102+
function onSave(file)
103+
-- If the current file is of any of the above types and the user has enabled
104+
-- `formatOnSave`, then format away!
105+
if hasValue(prettierFileTypes, file.Buf:FileType()) and config.GetGlobalOption("prettier.formatOnSave") then
106+
format(file)
107+
end
108+
109+
return true
110+
end
111+
112+
-- Editor initialization hook
113+
function init()
114+
-- Register prettier specific configuration
115+
for key, value in ipairs(preferences) do
116+
config.RegisterCommonOption("prettier", key, value)
117+
end
118+
-- Register plugin specific options
119+
config.RegisterCommonOption("prettier", "formatOnSave", true)
120+
121+
-- Register the `format` command
122+
config.MakeCommand("format", format, config.NoComplete)
123+
end

0 commit comments

Comments
 (0)