|
| 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