|
| 1 | +VERSION = "0.3.0" |
| 2 | + |
| 3 | +local micro = import("micro") |
| 4 | +local microBuffer = import("micro/buffer") |
| 5 | +local config = import("micro/config") |
| 6 | +local shell = import("micro/shell") |
| 7 | + |
| 8 | +local verbose = config.GetGlobalOption("editorconfigverbose") or false |
| 9 | + |
| 10 | +local function errlog(msg) |
| 11 | + -- TODO: automatically open the log buffer like plugin list |
| 12 | + microBuffer.Log(("editorconfig error: %s\n"):format(msg)) |
| 13 | +end |
| 14 | + |
| 15 | +-- for debugging; use micro -debug, and then inspect log.txt |
| 16 | +local function log(msg) |
| 17 | + micro.Log(("editorconfig debug: %s"):format(msg)) |
| 18 | +end |
| 19 | + |
| 20 | +local function setSafely(key, value, buffer) |
| 21 | + if value == nil then |
| 22 | + -- log(("Ignore nil for %s"):format(key)) |
| 23 | + else |
| 24 | + if config.GetGlobalOption(key) ~= value then |
| 25 | + log(("Set %s = %s"):format(key, value)) |
| 26 | + buffer:SetOptionNative(key, value) |
| 27 | + end |
| 28 | + end |
| 29 | +end |
| 30 | + |
| 31 | +local function setIndentation(properties, buffer) |
| 32 | + local indent_size_str = properties["indent_size"] |
| 33 | + local tab_width_str = properties["tab_width"] |
| 34 | + local indent_style = properties["indent_style"] |
| 35 | + |
| 36 | + local indent_size = tonumber(indent_size_str, 10) |
| 37 | + local tab_width = tonumber(tab_width_str, 10) |
| 38 | + |
| 39 | + if indent_size_str == "tab" then |
| 40 | + indent_size = tab_width |
| 41 | + elseif tab_width == nil then |
| 42 | + tab_width = indent_size |
| 43 | + end |
| 44 | + |
| 45 | + if indent_style == "space" then |
| 46 | + setSafely("tabstospaces", true, buffer) |
| 47 | + setSafely("tabsize", indent_size, buffer) |
| 48 | + elseif indent_style == "tab" then |
| 49 | + setSafely("tabstospaces", false, buffer) |
| 50 | + setSafely("tabsize", tab_width, buffer) |
| 51 | + elseif indent_style ~= nil then |
| 52 | + errlog(("Unknown value for editorconfig property indent_style: %s"):format(indent_style or "nil")) |
| 53 | + end |
| 54 | +end |
| 55 | + |
| 56 | +local function setEndOfLine(properties, buffer) |
| 57 | + local end_of_line = properties["end_of_line"] |
| 58 | + if end_of_line == "lf" then |
| 59 | + setSafely("fileformat", "unix", buffer) |
| 60 | + elseif end_of_line == "crlf" then |
| 61 | + setSafely("fileformat", "dos", buffer) |
| 62 | + elseif end_of_line == "cr" then |
| 63 | + -- See https://github.com/zyedidia/micro/blob/master/runtime/help/options.md for supported runtime options. |
| 64 | + errlog(("Value %s for editorconfig property end_of_line is not currently supported by micro."):format(end_of_line)) |
| 65 | + elseif end_of_line ~= nil then |
| 66 | + errlog(("Unknown value for editorconfig property end_of_line: %s"):format(end_of_line)) |
| 67 | + end |
| 68 | +end |
| 69 | + |
| 70 | +local function setCharset(properties, buffer) |
| 71 | + local charset = properties["charset"] |
| 72 | + if charset ~= "utf-8" and charset ~= nil then |
| 73 | + -- TODO: I believe micro 2.0 added support for more charsets, so this is gonna have to be updated accordingly. |
| 74 | + -- Also now we need to actually set the charset since it isn't just utf-8. |
| 75 | + errlog(("Value %s for editorconfig property charset is not currently supported by micro."):format(charset)) |
| 76 | + end |
| 77 | +end |
| 78 | + |
| 79 | +local function setTrimTrailingWhitespace(properties, buffer) |
| 80 | + local val = properties["trim_trailing_whitespace"] |
| 81 | + if val == "true" then |
| 82 | + setSafely("rmtrailingws", true, buffer) |
| 83 | + elseif val == "false" then |
| 84 | + setSafely("rmtrailingws", false, buffer) |
| 85 | + elseif val ~= nil then |
| 86 | + errlog(("Unknown value for editorconfig property trim_trailing_whitespace: %s"):format(val)) |
| 87 | + end |
| 88 | +end |
| 89 | + |
| 90 | +local function setInsertFinalNewline(properties, buffer) |
| 91 | + local val = properties["insert_final_newline"] |
| 92 | + if val == "true" then |
| 93 | + setSafely("eofnewline", true, buffer) |
| 94 | + elseif val == "false" then |
| 95 | + setSafely("eofnewline", false, buffer) |
| 96 | + elseif val ~= nil then |
| 97 | + errlog(("Unknown value for editorconfig property insert_final_newline: %s"):format(val)) |
| 98 | + end |
| 99 | +end |
| 100 | + |
| 101 | +local function applyProperties(properties, buffer) |
| 102 | + setIndentation(properties, buffer) |
| 103 | + setEndOfLine(properties, buffer) |
| 104 | + setCharset(properties, buffer) |
| 105 | + setTrimTrailingWhitespace(properties, buffer) |
| 106 | + setInsertFinalNewline(properties, buffer) |
| 107 | +end |
| 108 | + |
| 109 | +function onEditorConfigExit(output, args) |
| 110 | + if verbose then |
| 111 | + log(("Output: \n%s"):format(output)) |
| 112 | + end |
| 113 | + |
| 114 | + local properties = {} |
| 115 | + for line in output:gmatch('([^\n]+)') do |
| 116 | + local key, value = line:match('([^=]*)=(.*)') |
| 117 | + if key == nil or value == nil then |
| 118 | + errlog(("Failed to parse editorconfig output: %s"):format(line)) |
| 119 | + return |
| 120 | + end |
| 121 | + key = key:gsub('^%s(.-)%s*$', '%1') |
| 122 | + value = value:gsub('^%s(.-)%s*$', '%1') |
| 123 | + properties[key] = value |
| 124 | + end |
| 125 | + |
| 126 | + local buffer = args[1] |
| 127 | + applyProperties(properties, buffer) |
| 128 | + |
| 129 | + if verbose then |
| 130 | + log("Running editorconfig done") |
| 131 | + end |
| 132 | +end |
| 133 | + |
| 134 | +function getApplyProperties(bufpane) |
| 135 | + buffer = bufpane.Buf |
| 136 | + if (buffer.Path or "") == "" then |
| 137 | + -- Current buffer does not visit any file |
| 138 | + return |
| 139 | + end |
| 140 | + |
| 141 | + local fullpath = buffer.AbsPath |
| 142 | + if fullpath == nil then |
| 143 | + messenger:Message("editorconfig: AbsPath is empty") |
| 144 | + return |
| 145 | + end |
| 146 | + |
| 147 | + if verbose then; |
| 148 | + log(("Running editorconfig %s"):format(fullpath)) |
| 149 | + end |
| 150 | + |
| 151 | + shell.JobSpawn("editorconfig", {fullpath}, "", "", "editorconfig.onEditorConfigExit", buffer) |
| 152 | +end |
| 153 | + |
| 154 | +function onBufPaneOpen(bp) |
| 155 | + getApplyProperties(bp) |
| 156 | +end |
| 157 | + |
| 158 | +function onSave(bp) |
| 159 | + getApplyProperties(bp) |
| 160 | + return true |
| 161 | +end |
| 162 | + |
| 163 | +function init() |
| 164 | + config.MakeCommand("editorconfig", getApplyProperties, config.NoComplete) |
| 165 | + config.AddRuntimeFile("editorconfig", config.RTHelp, "help/editorconfig.md") |
| 166 | +end |
0 commit comments