Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
feat: port to micro 2.0.1+ (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
josh authored Feb 27, 2020
1 parent 0d5d605 commit 97247c0
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 102 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -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: <kbd>CtrlE</kbd>):
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.

Expand Down
3 changes: 0 additions & 3 deletions channel.json

This file was deleted.

137 changes: 67 additions & 70 deletions editorconfig.lua
Original file line number Diff line number Diff line change
@@ -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"]
Expand All @@ -39,122 +45,113 @@ 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')
value = value:gsub('^%s(.-)%s*$', '%1')
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")
33 changes: 11 additions & 22 deletions help/editorconfig.md
Original file line number Diff line number Diff line change
@@ -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`.
10 changes: 9 additions & 1 deletion repo.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
]
Expand Down

0 comments on commit 97247c0

Please sign in to comment.