diff options
author | Samadi van Koten <samadi@vktec.co.uk> | 2018-02-06 12:51:50 +0000 |
---|---|---|
committer | Samadi van Koten <samadi@vktec.co.uk> | 2018-02-06 12:51:50 +0000 |
commit | 9a574c35d30a974c492d07a558fbe0cb91395282 (patch) | |
tree | 6648611660ed3735bc13dc902fda34947876330b | |
download | vis-editorconfig-9a574c35d30a974c492d07a558fbe0cb91395282.tar.gz |
Create an editorconfig plugin for vis
-rw-r--r-- | .editorconfig | 5 | ||||
-rw-r--r-- | README.md | 14 | ||||
-rw-r--r-- | editorconfig.lua | 77 |
3 files changed, 96 insertions, 0 deletions
diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..d60c879 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,5 @@ +root = true + +[*.lua] +indent_style = space +indent_size = 2 diff --git a/README.md b/README.md new file mode 100644 index 0000000..80741ef --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +# vis-editorconfig + +A [vis][vis] plugin for [editorconfig][ec]. + +[vis]: https://github.com/martanne/vis +[ec]: http://editorconfig.org/ + +## Installation + +```shell +git clone https://github.com/vktec/vis-editorconfig "$HOME/.config/vis/editorconfig" +``` + +Then add `require "editorconfig/editorconfig"` to your `visrc.lua`. diff --git a/editorconfig.lua b/editorconfig.lua new file mode 100644 index 0000000..1370926 --- /dev/null +++ b/editorconfig.lua @@ -0,0 +1,77 @@ +require "vis" +ec = require "editorconfig_core" + +-- Simple wrapper +function vis_set(option, value) + if type(value) == "boolean" then + if value then + value = "yes" + else + value = "no" + end + end + + vis:command("set " .. option .. " " .. value) +end + +OPTIONS = { + indent_style = function (value) + vis_set("expandtab", (value == "space")) + end, + + indent_size = function (value) + if value ~= "tab" then -- tab_width is a synonym anyway + vis_set("tabwidth", value) + end + end, + + tab_width = function (value) + vis_set("tabwidth", value) + end, + + -- Not supported by vis + -- end_of_line + -- charset + -- trim_trailing_whitespace + -- insert_final_newline + -- max_line_length +} + +-- Uses editorconfig-core-lua's as yet unreleased iterator API +--function ec_iter(p) do +-- return ec.open(p) +--end + +-- Compatible with editorconfig-core-lua v0.1.1 +function ec_iter(p) + i = 0 + props, keys = ec.parse(p) + n = #keys + return function () + i = i + 1 + if i <= n then + return keys[i], props[keys[i]] + end + end +end + +function ec_set_values(path) + if path then + for name, value in ec_iter(path) do + if OPTIONS[name] then + OPTIONS[name](value) + end + end + end +end + +function ec_parse_cmd() ec_set_values(vis.win.file.path) end +vis:command_register("econfig_parse", ec_parse_cmd, "(Re)parse an editorconfig file") + +vis.events.subscribe(vis.events.FILE_OPEN, function (file) + ec_set_values(file.path) +end) + +vis.events.subscribe(vis.events.FILE_SAVE_POST, function (file, path) + ec_set_values(path) +end) |