This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
editorconfig.lua
161 lines (139 loc) · 5.42 KB
/
editorconfig.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
VERSION = "1.1.0"
local micro = import("micro")
local microBuffer = import("micro/buffer")
local config = import("micro/config")
local shell = import("micro/shell")
local function errlog(msg, bufpane)
microBuffer.Log(("editorconfig error: %s\n"):format(msg))
bufpane:OpenLogBuf()
end
-- 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, bufpane)
if value == nil then
log(("Ignore nil for %s"):format(key))
else
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, bufpane)
local indent_size_str = properties["indent_size"]
local tab_width_str = properties["tab_width"]
local indent_style = properties["indent_style"]
local indent_size = tonumber(indent_size_str, 10)
local tab_width = tonumber(tab_width_str, 10)
if indent_size_str == "tab" then
indent_size = tab_width
elseif tab_width == nil then
tab_width = indent_size
end
if indent_style == "space" then
setSafely("tabstospaces", true, bufpane)
setSafely("tabsize", indent_size, bufpane)
elseif indent_style == "tab" then
setSafely("tabstospaces", false, bufpane)
setSafely("tabsize", tab_width, bufpane)
elseif indent_style ~= nil then
errlog(("Unknown value for editorconfig property indent_style: %s"):format(indent_style or "nil"), bufpane)
end
end
local function setEndOfLine(properties, bufpane)
local end_of_line = properties["end_of_line"]
if end_of_line == "lf" then
setSafely("fileformat", "unix", bufpane)
elseif end_of_line == "crlf" then
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.
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
errlog(("Unknown value for editorconfig property end_of_line: %s"):format(end_of_line), bufpane)
end
end
local function setCharset(properties, bufpane)
local charset = properties["charset"]
if charset ~= "utf-8" and charset ~= nil then
-- 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, bufpane)
local val = properties["trim_trailing_whitespace"]
if val == "true" then
setSafely("rmtrailingws", true, bufpane)
elseif val == "false" then
setSafely("rmtrailingws", false, bufpane)
elseif val ~= nil then
errlog(("Unknown value for editorconfig property trim_trailing_whitespace: %s"):format(val), bufpane)
end
end
local function setInsertFinalNewline(properties, bufpane)
local val = properties["insert_final_newline"]
if val == "true" then
setSafely("eofnewline", true, bufpane)
elseif val == "false" then
setSafely("eofnewline", false, bufpane)
elseif val ~= nil then
errlog(("Unknown value for editorconfig property insert_final_newline: %s"):format(val), bufpane)
end
end
local function applyProperties(properties, bufpane)
setIndentation(properties, bufpane)
setEndOfLine(properties, bufpane)
setCharset(properties, bufpane)
setTrimTrailingWhitespace(properties, bufpane)
setInsertFinalNewline(properties, bufpane)
end
function onEditorConfigExit(output, args)
log(("editorconfig core output: \n%s"):format(output))
local properties = {}
for line in output:gmatch('([^\r?\n]+)') do
local key, value = line:match('([^=]*)=(.*)')
if key == nil or value == nil then
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
local bufpane = args[1]
applyProperties(properties, bufpane)
log("Done.")
end
function getApplyProperties(bufpane)
if (bufpane.Buf.Path or "") == "" then
-- Current buffer does not visit any file
return
end
local fullpath = bufpane.Buf.AbsPath
if fullpath == nil then
messenger:Message("editorconfig: AbsPath is empty")
return
end
log(("Running on file %s"):format(fullpath))
shell.JobSpawn("editorconfig", {fullpath}, nil, nil, onEditorConfigExit, bufpane)
end
function onBufPaneOpen(bufpane)
getApplyProperties(bufpane)
end
function onSave(bufpane)
getApplyProperties(bufpane)
return true
end
function init()
config.AddRuntimeFile("editorconfig", config.RTHelp, "help/editorconfig.md")
end
-- outside init because we want these options to take effect before
-- buffers are initialized
config.AddRuntimeFile("editorconfig", config.RTSyntax, "syntax/editorconfig.yaml")