From 97247c0f26d5186e5cd607186d98538ba0e45f16 Mon Sep 17 00:00:00 2001 From: josh Date: Thu, 27 Feb 2020 17:35:55 +0000 Subject: [PATCH] feat: port to micro 2.0.1+ (#10) --- README.md | 15 +++-- channel.json | 3 - editorconfig.lua | 137 +++++++++++++++++++++---------------------- help/editorconfig.md | 33 ++++------- repo.json | 10 +++- 5 files changed, 96 insertions(+), 102 deletions(-) delete mode 100644 channel.json diff --git a/README.md b/README.md index 1d0448f..11e18a4 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,24 @@ # editorconfig-micro -[EditorConfig] plugin for the [`micro`] editor. +[EditorConfig] plugin for the [`micro`] editor. Works with `micro` v2. ### Prerequisites -* 1.3.2 <= [`micro`] editor < 2.0.0 -* An `editorconfig` core executable (e.g. [EditorConfig C Core]) +You'll need an `editorconfig` core executable, like [EditorConfig C Core], installed and on your PATH. ### Installation -While in micro's command mode (default keybinding: CtrlE): +From the command line, type `micro -plugin install editorconfig`. Or, in micro's command mode, type `plugin install editorconfig`. -`plugin install editorconfig` +Alternatively, you may directly clone this repository: -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 or saved. + git clone https://github.com/10sr/editorconfig-micro "${XDG_CONFIG_HOME:-~/.config}/micro/plug/editorconfig" + +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. + +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. For more information, use `help editorconfig` in command mode or view `help/editorconfig.md` in this repo. diff --git a/channel.json b/channel.json deleted file mode 100644 index 3f4269d..0000000 --- a/channel.json +++ /dev/null @@ -1,3 +0,0 @@ -[ - "https://raw.githubusercontent.com/10sr/editorconfig-micro/848fc839cad565c61d96b99f38121dda0f485607/repo.json" -] diff --git a/editorconfig.lua b/editorconfig.lua index 92f0028..116e359 100644 --- a/editorconfig.lua +++ b/editorconfig.lua @@ -1,30 +1,36 @@ -VERSION = "0.2.3" +VERSION = "1.0.0" -local verbose = GetOption("editorconfigverbose") or false +local micro = import("micro") +local microBuffer = import("micro/buffer") +local config = import("micro/config") +local shell = import("micro/shell") -local function logger(msg, view) - messenger:AddLog(("EditorConfig <%s>: %s"): - format(view.Buf.GetName(view.Buf), msg)) +local function errlog(msg, bufpane) + microBuffer.Log(("editorconfig error: %s\n"):format(msg)) + bufpane:OpenLogBuf() end -local function msg(msg, view) - messenger:Message(("EditorConfig <%s>: %s"): - format(view.Buf.GetName(view.Buf), msg)) +-- for debugging; use micro -debug, and then inspect log.txt +local function log(msg) + micro.Log(("editorconfig log: %s"):format(msg)) end -local function setSafely(key, value, view) +local function setSafely(key, value, bufpane) if value == nil then - -- logger(("Ignore nil for %s"):format(key), view) + log(("Ignore nil for %s"):format(key)) else - if GetOption(key) ~= value then - logger(("Set %s = %s"):format(key, value), view) - SetLocalOption(key, value, view) + buffer = bufpane.Buf + local oldValue = config.GetGlobalOption(key) + if oldValue ~= value then + log(("Set %s = %s (was %s)"):format(key, value, oldValue)) + buffer:SetOptionNative(key, value) + else + log(("Unchanged %s = %s"):format(key, oldValue)) end end end - -local function setIndentation(properties, view) +local function setIndentation(properties, bufpane) local indent_size_str = properties["indent_size"] local tab_width_str = properties["tab_width"] local indent_style = properties["indent_style"] @@ -39,78 +45,76 @@ local function setIndentation(properties, view) end if indent_style == "space" then - setSafely("tabstospaces", true, view) - setSafely("tabsize", indent_size, view) + setSafely("tabstospaces", true, bufpane) + setSafely("tabsize", indent_size, bufpane) elseif indent_style == "tab" then - setSafely("tabstospaces", false, view) - setSafely("tabsize", tab_width, view) + setSafely("tabstospaces", false, bufpane) + setSafely("tabsize", tab_width, bufpane) elseif indent_style ~= nil then - logger(("Unknown value for editorconfig property indent_style: %s"):format(indent_style or "nil"), view) + errlog(("Unknown value for editorconfig property indent_style: %s"):format(indent_style or "nil"), bufpane) end end -local function setEndOfLine(properties, view) +local function setEndOfLine(properties, bufpane) local end_of_line = properties["end_of_line"] if end_of_line == "lf" then - setSafely("fileformat", "unix", view) + setSafely("fileformat", "unix", bufpane) elseif end_of_line == "crlf" then - setSafely("fileformat", "dos", view) + setSafely("fileformat", "dos", bufpane) elseif end_of_line == "cr" then -- See https://github.com/zyedidia/micro/blob/master/runtime/help/options.md for supported runtime options. - msg(("Value %s for editorconfig property end_of_line is not currently supported by micro."):format(end_of_line), view) + errlog(("Value %s for editorconfig property end_of_line is not currently supported by micro."):format(end_of_line), bufpane) elseif end_of_line ~= nil then - msg(("Unknown value for editorconfig property end_of_line: %s"):format(end_of_line), view) + errlog(("Unknown value for editorconfig property end_of_line: %s"):format(end_of_line), bufpane) end end -local function setCharset(properties, view) +local function setCharset(properties, bufpane) local charset = properties["charset"] if charset ~= "utf-8" and charset ~= nil then - msg(("Value %s for editorconfig property charset is not currently supported by micro."):format(charset), view) + -- TODO: I believe micro 2.0 added support for more charsets, so this is gonna have to be updated accordingly. + -- Also now we need to actually set the charset since it isn't just utf-8. + errlog(("Value %s for editorconfig property charset is not currently supported by micro."):format(charset), bufpane) end end -local function setTrimTrailingWhitespace(properties, view) +local function setTrimTrailingWhitespace(properties, bufpane) local val = properties["trim_trailing_whitespace"] if val == "true" then - setSafely("rmtrailingws", true, view) + setSafely("rmtrailingws", true, bufpane) elseif val == "false" then - setSafely("rmtrailingws", false, view) + setSafely("rmtrailingws", false, bufpane) elseif val ~= nil then - logger(("Unknown value for editorconfig property trim_trailing_whitespace: %s"):format(val), view) + errlog(("Unknown value for editorconfig property trim_trailing_whitespace: %s"):format(val), bufpane) end end -local function setInsertFinalNewline(properties, view) +local function setInsertFinalNewline(properties, bufpane) local val = properties["insert_final_newline"] if val == "true" then - setSafely("eofnewline", true, view) + setSafely("eofnewline", true, bufpane) elseif val == "false" then - setSafely("eofnewline", false, view) + setSafely("eofnewline", false, bufpane) elseif val ~= nil then - logger(("Unknown value for editorconfig property insert_final_newline: %s"):format(val), view) + errlog(("Unknown value for editorconfig property insert_final_newline: %s"):format(val), bufpane) end end -local function applyProperties(properties, view) - setIndentation(properties, view) - setEndOfLine(properties, view) - setCharset(properties, view) - setTrimTrailingWhitespace(properties, view) - setInsertFinalNewline(properties, view) +local function applyProperties(properties, bufpane) + setIndentation(properties, bufpane) + setEndOfLine(properties, bufpane) + setCharset(properties, bufpane) + setTrimTrailingWhitespace(properties, bufpane) + setInsertFinalNewline(properties, bufpane) end -function onEditorConfigExit(output) - local view = CurView() - if verbose then - logger(("Output: \n%s"):format(output), view) - end +function onEditorConfigExit(output, args) + log(("editorconfig core output: \n%s"):format(output)) local properties = {} for line in output:gmatch('([^\n]+)') do local key, value = line:match('([^=]*)=(.*)') if key == nil or value == nil then - msg(("Failed to parse editorconfig output: %s"): - format(line), view) + errlog(("Failed to parse editorconfig output: %s"):format(line)) return end key = key:gsub('^%s(.-)%s*$', '%1') @@ -118,43 +122,36 @@ function onEditorConfigExit(output) properties[key] = value end - applyProperties(properties, view) - if verbose then - logger("Running editorconfig done", view) - end + local bufpane = args[1] + applyProperties(properties, bufpane) + log("Done.") end -function getApplyProperties(view) - view = view or CurView() - - if (view.Buf.Path or "") == "" then +function getApplyProperties(bufpane) + if (bufpane.Buf.Path or "") == "" then -- Current buffer does not visit any file return end - local fullpath = view.Buf.AbsPath + local fullpath = bufpane.Buf.AbsPath if fullpath == nil then messenger:Message("editorconfig: AbsPath is empty") return end - if verbose then - logger(("Running editorconfig %s"):format(fullpath), view) - end - JobSpawn("editorconfig", {fullpath}, "", "", "editorconfig.onEditorConfigExit") + log(("Running on file %s"):format(fullpath)) + shell.JobSpawn("editorconfig", {fullpath}, nil, nil, onEditorConfigExit, bufpane) end -function onOpenFile(view) - getApplyProperties(view) +function onBufPaneOpen(bufpane) + getApplyProperties(bufpane) end -function onViewOpen(view) - getApplyProperties(view) +function onSave(bufpane) + getApplyProperties(bufpane) + return true end -function onSave(view) - getApplyProperties(view) +function init() + config.AddRuntimeFile("editorconfig", config.RTHelp, "help/editorconfig.md") end - -MakeCommand("editorconfig", "editorconfig.getApplyProperties") -AddRuntimeFile("editorconfig", "help", "help/editorconfig.md") diff --git a/help/editorconfig.md b/help/editorconfig.md index b45863c..995e9cb 100644 --- a/help/editorconfig.md +++ b/help/editorconfig.md @@ -1,33 +1,22 @@ -editorconfig-micro -================== +editorconfig +============ -[EditorConfig][] helps developers define and maintain +http://editorconfig.org helps developers define and maintain consistent coding styles between different editors and IDEs. This is the EditorConfig plugin for the `micro` editor. This plugin requires an editorconfig core executable to be installed. -For example, download the [EditorConfig C Core][] and follow the instructions in -the README and INSTALL files to install it. +https://github.com/editorconfig/editorconfig-core-c Usage ----- -Once installed, this plugin will automatically execute `editorconfig.getApplyProperties` -on files when they are opened or saved. +Once installed, this plugin will automatically use the editorconfig core to +obtain the desired editorconfig properties based on the file, and apply the +corresponding micro options on both open (necessary for things like tabstospaces) +and save (necessary for things like rmtrailingws). -You can also explicitly use the `editorconfig` command in command mode, or bind it to -a keystroke. For example: - -```json -{ - "Alt-e": "editorconfig.getApplyProperties" -} -``` - -If any editorconfig properties have been changed, they will be logged, which can be viewed -with `log` in command mode. If you want to see verbose logs, you must manually add `"editorconfigverbose": true,` to your user settings in `~/.config/micro/settings.json`. - - -[EditorConfig]: http://editorconfig.org -[EditorConfig C Core]: https://github.com/editorconfig/editorconfig-core-c +If any micro options have been changed as a result of editorconfig configuration, +they will be logged to micro's debug log. You may view them by running micro in +debug mode (`micro -debug`) and then subsequently inspecting `log.txt`. diff --git a/repo.json b/repo.json index e550647..93d7787 100644 --- a/repo.json +++ b/repo.json @@ -2,12 +2,20 @@ "Name": "editorconfig", "Description": "EditorConfig plugin for micro", "Tags": ["editorconfig", "utility", "format"], + "Website": "https://github.com/10sr/editorconfig-micro", "Versions": [ + { + "Version": "1.0.0", + "Url": "https://github.com/10sr/editorconfig-micro/archive/v1.0.0.zip", + "Require": { + "micro": ">=2.0.1" + } + }, { "Version": "0.2.3", "Url": "https://github.com/10sr/editorconfig-micro/archive/v0.2.3.zip", "Require": { - "micro": ">=1.3.2" + "micro": ">=1.3.2 <2.0.0" } } ]