├── .editorconfig ├── LICENSE ├── README.md ├── assets └── syntax_highlight.png ├── editorconfig.lua ├── help └── editorconfig.md ├── repo.json └── syntax └── editorconfig.yaml /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | 5 | indent_style = space 6 | indent_size = 4 7 | end_of_line = lf 8 | charset = utf-8 9 | 10 | trim_trailing_whitespace = true 11 | insert_final_newline = true 12 | max_line_length = 80 13 | 14 | [*.json] 15 | indent_size = 2 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 github.com/10sr + contributors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Maintainer Needed 2 | 3 | I usually do not use the Micro editor now, and I don't have the time to maintain this project any more. 4 | Forking and replacing this repository is really appreciated. Thanks! 5 | 6 | 7 | 8 | # editorconfig-micro 9 | 10 | [EditorConfig] plugin for the [`micro`] editor. Works with `micro` v2. 11 | 12 | ![Syntax Highlight](https://raw.githubusercontent.com/10sr/editorconfig-micro/master/assets/syntax_highlight.png) 13 | 14 | ### Prerequisites 15 | 16 | You'll need an `editorconfig` core executable, like [EditorConfig C Core], installed and on your PATH. 17 | 18 | 19 | ### Installation 20 | 21 | From the command line, type `micro -plugin install editorconfig`. Or, in micro's command mode, type `plugin install editorconfig`. 22 | 23 | Alternatively, you may directly clone this repository: 24 | 25 | git clone https://github.com/10sr/editorconfig-micro "${XDG_CONFIG_HOME:-~/.config}/micro/plug/editorconfig" 26 | 27 | That's all! This plugin will be automatically enabled after you restart [`micro`]. It will automatically apply the appropriate editorconfig properties on files when they are opened and saved. 28 | 29 | It's also recommended to disable micro's `ftoptions` builtin plugin (set it to false in micro's settings.json) as it's a very limited subset of duplicate functionality than what editorconfig provides. 30 | 31 | For more information, use `help editorconfig` in command mode or view `help/editorconfig.md` in this repo. 32 | 33 | 34 | ### Supported Properties 35 | 36 | * `root` (only used by EditorConfig Core) 37 | * `indent_style` 38 | * `indent_size` 39 | * `tab_width` 40 | * `charset` 41 | * Currently, [`micro`] only [supports][EditorConfig Options] the `utf-8` charset. 42 | * `end_of_line` 43 | * Currently, [`micro`] only [supports][EditorConfig Options] `lf` and `crlf`. 44 | * `insert_final_newline` 45 | * `trim_trailing_whitespace` 46 | 47 | 48 | ### Unsupported Properties 49 | 50 | * `max_line_length` 51 | 52 | 53 | ### License 54 | 55 | This software is licensed under MIT License. 56 | See [LICENSE](LICENSE) for details. 57 | 58 | [`micro`]: https://micro-editor.github.io 59 | [EditorConfig]: http://editorconfig.org 60 | [EditorConfig Options]: https://github.com/zyedidia/micro/blob/master/runtime/help/options.md 61 | [EditorConfig C Core]: https://github.com/editorconfig/editorconfig-core-c 62 | -------------------------------------------------------------------------------- /assets/syntax_highlight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/10sr/editorconfig-micro/892aa42b50e6f57a5065b6f4df8c4e0a3913c9ee/assets/syntax_highlight.png -------------------------------------------------------------------------------- /editorconfig.lua: -------------------------------------------------------------------------------- 1 | VERSION = "1.1.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 function errlog(msg, bufpane) 9 | microBuffer.Log(("editorconfig error: %s\n"):format(msg)) 10 | bufpane:OpenLogBuf() 11 | end 12 | 13 | -- for debugging; use micro -debug, and then inspect log.txt 14 | local function log(msg) 15 | micro.Log(("editorconfig log: %s"):format(msg)) 16 | end 17 | 18 | local function setSafely(key, value, bufpane) 19 | if value == nil then 20 | log(("Ignore nil for %s"):format(key)) 21 | else 22 | buffer = bufpane.Buf 23 | local oldValue = config.GetGlobalOption(key) 24 | if oldValue ~= value then 25 | log(("Set %s = %s (was %s)"):format(key, value, oldValue)) 26 | buffer:SetOptionNative(key, value) 27 | else 28 | log(("Unchanged %s = %s"):format(key, oldValue)) 29 | end 30 | end 31 | end 32 | 33 | local function setIndentation(properties, bufpane) 34 | local indent_size_str = properties["indent_size"] 35 | local tab_width_str = properties["tab_width"] 36 | local indent_style = properties["indent_style"] 37 | 38 | local indent_size = tonumber(indent_size_str, 10) 39 | local tab_width = tonumber(tab_width_str, 10) 40 | 41 | if indent_size_str == "tab" then 42 | indent_size = tab_width 43 | elseif tab_width == nil then 44 | tab_width = indent_size 45 | end 46 | 47 | if indent_style == "space" then 48 | setSafely("tabstospaces", true, bufpane) 49 | setSafely("tabsize", indent_size, bufpane) 50 | elseif indent_style == "tab" then 51 | setSafely("tabstospaces", false, bufpane) 52 | setSafely("tabsize", tab_width, bufpane) 53 | elseif indent_style ~= nil then 54 | errlog(("Unknown value for editorconfig property indent_style: %s"):format(indent_style or "nil"), bufpane) 55 | end 56 | end 57 | 58 | local function setEndOfLine(properties, bufpane) 59 | local end_of_line = properties["end_of_line"] 60 | if end_of_line == "lf" then 61 | setSafely("fileformat", "unix", bufpane) 62 | elseif end_of_line == "crlf" then 63 | setSafely("fileformat", "dos", bufpane) 64 | elseif end_of_line == "cr" then 65 | -- See https://github.com/zyedidia/micro/blob/master/runtime/help/options.md for supported runtime options. 66 | errlog(("Value %s for editorconfig property end_of_line is not currently supported by micro."):format(end_of_line), bufpane) 67 | elseif end_of_line ~= nil then 68 | errlog(("Unknown value for editorconfig property end_of_line: %s"):format(end_of_line), bufpane) 69 | end 70 | end 71 | 72 | local function setCharset(properties, bufpane) 73 | local charset = properties["charset"] 74 | if charset ~= "utf-8" and charset ~= nil then 75 | -- TODO: I believe micro 2.0 added support for more charsets, so this is gonna have to be updated accordingly. 76 | -- Also now we need to actually set the charset since it isn't just utf-8. 77 | errlog(("Value %s for editorconfig property charset is not currently supported by micro."):format(charset), bufpane) 78 | end 79 | end 80 | 81 | local function setTrimTrailingWhitespace(properties, bufpane) 82 | local val = properties["trim_trailing_whitespace"] 83 | if val == "true" then 84 | setSafely("rmtrailingws", true, bufpane) 85 | elseif val == "false" then 86 | setSafely("rmtrailingws", false, bufpane) 87 | elseif val ~= nil then 88 | errlog(("Unknown value for editorconfig property trim_trailing_whitespace: %s"):format(val), bufpane) 89 | end 90 | end 91 | 92 | local function setInsertFinalNewline(properties, bufpane) 93 | local val = properties["insert_final_newline"] 94 | if val == "true" then 95 | setSafely("eofnewline", true, bufpane) 96 | elseif val == "false" then 97 | setSafely("eofnewline", false, bufpane) 98 | elseif val ~= nil then 99 | errlog(("Unknown value for editorconfig property insert_final_newline: %s"):format(val), bufpane) 100 | end 101 | end 102 | 103 | local function applyProperties(properties, bufpane) 104 | setIndentation(properties, bufpane) 105 | setEndOfLine(properties, bufpane) 106 | setCharset(properties, bufpane) 107 | setTrimTrailingWhitespace(properties, bufpane) 108 | setInsertFinalNewline(properties, bufpane) 109 | end 110 | 111 | function onEditorConfigExit(output, args) 112 | log(("editorconfig core output: \n%s"):format(output)) 113 | local properties = {} 114 | for line in output:gmatch('([^\r?\n]+)') do 115 | local key, value = line:match('([^=]*)=(.*)') 116 | if key == nil or value == nil then 117 | errlog(("Failed to parse editorconfig output: %s"):format(line)) 118 | return 119 | end 120 | key = key:gsub('^%s(.-)%s*$', '%1') 121 | value = value:gsub('^%s(.-)%s*$', '%1') 122 | properties[key] = value 123 | end 124 | 125 | local bufpane = args[1] 126 | applyProperties(properties, bufpane) 127 | log("Done.") 128 | end 129 | 130 | function getApplyProperties(bufpane) 131 | if (bufpane.Buf.Path or "") == "" then 132 | -- Current buffer does not visit any file 133 | return 134 | end 135 | 136 | local fullpath = bufpane.Buf.AbsPath 137 | if fullpath == nil then 138 | messenger:Message("editorconfig: AbsPath is empty") 139 | return 140 | end 141 | 142 | log(("Running on file %s"):format(fullpath)) 143 | shell.JobSpawn("editorconfig", {fullpath}, nil, nil, onEditorConfigExit, bufpane) 144 | end 145 | 146 | function onBufPaneOpen(bufpane) 147 | getApplyProperties(bufpane) 148 | end 149 | 150 | function onSave(bufpane) 151 | getApplyProperties(bufpane) 152 | return true 153 | end 154 | 155 | function init() 156 | config.AddRuntimeFile("editorconfig", config.RTHelp, "help/editorconfig.md") 157 | end 158 | 159 | -- outside init because we want these options to take effect before 160 | -- buffers are initialized 161 | config.AddRuntimeFile("editorconfig", config.RTSyntax, "syntax/editorconfig.yaml") 162 | -------------------------------------------------------------------------------- /help/editorconfig.md: -------------------------------------------------------------------------------- 1 | editorconfig 2 | ============ 3 | 4 | http://editorconfig.org helps developers define and maintain 5 | consistent coding styles between different editors and IDEs. 6 | This is the EditorConfig plugin for the `micro` editor. 7 | 8 | This plugin requires an editorconfig core executable to be installed. 9 | https://github.com/editorconfig/editorconfig-core-c 10 | 11 | 12 | Usage 13 | ----- 14 | 15 | Once installed, this plugin will automatically use the editorconfig core to 16 | obtain the desired editorconfig properties based on the file, and apply the 17 | corresponding micro options on both open (necessary for things like tabstospaces) 18 | and save (necessary for things like rmtrailingws). 19 | 20 | If any micro options have been changed as a result of editorconfig configuration, 21 | they will be logged to micro's debug log. You may view them by running micro in 22 | debug mode (`micro -debug`) and then subsequently inspecting `log.txt`. 23 | -------------------------------------------------------------------------------- /repo.json: -------------------------------------------------------------------------------- 1 | [{ 2 | "Name": "editorconfig", 3 | "Description": "EditorConfig plugin for micro", 4 | "Tags": ["editorconfig", "utility", "format"], 5 | "Website": "https://github.com/10sr/editorconfig-micro", 6 | "Versions": [ 7 | { 8 | "Version": "1.1.0", 9 | "Url": "https://github.com/10sr/editorconfig-micro/archive/v1.1.0.zip", 10 | "Require": { 11 | "micro": ">=2.0.1" 12 | } 13 | }, 14 | { 15 | "Version": "1.0.0", 16 | "Url": "https://github.com/10sr/editorconfig-micro/archive/v1.0.0.zip", 17 | "Require": { 18 | "micro": ">=2.0.1" 19 | } 20 | }, 21 | { 22 | "Version": "0.2.3", 23 | "Url": "https://github.com/10sr/editorconfig-micro/archive/v0.2.3.zip", 24 | "Require": { 25 | "micro": ">=1.3.2 <2.0.0" 26 | } 27 | } 28 | ] 29 | }] 30 | -------------------------------------------------------------------------------- /syntax/editorconfig.yaml: -------------------------------------------------------------------------------- 1 | filetype: editorconfig 2 | 3 | detect: 4 | filename: ".?editor-?config(?:config|-conf)?$" 5 | 6 | rules: 7 | # Supported Properties 8 | - statement: "\\b(indent_style|indent_size|tab_width|end_of_line|charset|trim_trailing_whitespace|insert_final_newline|root)\\b" 9 | # Values 10 | - special: "=\\s?.+" 11 | # Files 12 | - identifier: "^[[:space:]]*\\[.*\\]$" 13 | # Brackets 14 | - brightwhite: "\\[|]|{|}|,|=" 15 | # Wildcard Patterns 16 | - statement: "\\*|!|\\?" 17 | 18 | - comment: 19 | start: "#" 20 | end: "$" 21 | rules: [] 22 | --------------------------------------------------------------------------------