aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFrank Seifferth <frankseifferth@posteo.net>2021-05-11 09:52:54 +0200
committerFrank Seifferth <frankseifferth@posteo.net>2021-05-11 21:14:32 +0200
commit34be5306f330a25e239b1b6fb6dd44451edd0fde (patch)
tree08bc00282679c4f4d4a9c362842e73f6a7213ba9
parent1e5306b982cb6fb51799e9652dff7067475d3eb3 (diff)
downloadvis-editorconfig-34be5306f330a25e239b1b6fb6dd44451edd0fde.tar.gz
Implement additional settings as pre-save-hooks
-rw-r--r--edconf.lua103
1 files changed, 100 insertions, 3 deletions
diff --git a/edconf.lua b/edconf.lua
index e3fb921..9b5a546 100644
--- a/edconf.lua
+++ b/edconf.lua
@@ -14,6 +14,75 @@ function vis_set(option, value)
vis:command("set " .. option .. " " .. value)
end
+function set_pre_save(f, value)
+ if value == "true" then
+ vis.events.subscribe(vis.events.FILE_SAVE_PRE, f)
+ else
+ vis.events.unsubscribe(vis.events.FILE_SAVE_PRE, f)
+ end
+end
+
+function set_file_open(f, value)
+ if value == "true" then
+ vis.events.subscribe(vis.events.FILE_OPEN, f)
+ else
+ vis.events.unsubscribe(vis.events.FILE_OPEN, f)
+ end
+end
+
+-- Custom functionality
+function insert_final_newline(file, path)
+ if file:content(file.size-1, file.size) ~= '\n' then
+ file:insert(file.size, '\n')
+ end
+end
+
+function trim_trailing_whitespace(file, path)
+ for i=1, #file.lines do
+ if string.match(file.lines[i], '[ \t]$') then
+ file.lines[i] = string.gsub(file.lines[i], '[ \t]*$', '')
+ end
+ end
+end
+
+function enforce_crlf_eol(file, path)
+ for i=1, #file.lines do
+ if not string.match(file.lines[i], '\r$') then
+ file.lines[i] = string.gsub(file.lines[i], '$', '\r')
+ end
+ end
+end
+
+function enforce_lf_eol(file, path)
+ for i=1, #file.lines do
+ if string.match(file.lines[i], '\r$') then
+ file.lines[i] = string.gsub(file.lines[i], '\r$', '')
+ end
+ end
+end
+
+global_max_line_length = 80 -- This is ugly, but we do want to use
+ -- single function that we can register
+ -- or unregister as needed
+function max_line_length(file, path)
+ local overlong_lines = {}
+ for i=1, #file.lines do
+ if string.len(file.lines[i]) > global_max_line_length then
+ table.insert(overlong_lines, i)
+ end
+ end
+ if #overlong_lines > 0 then
+ local lines_are = (function(x)
+ if x>1 then return "lines are" else return "line is" end
+ end)(#overlong_lines)
+ vis:info(string.format(
+ "%d %s longer than %d characters: %s",
+ #overlong_lines, lines_are, global_max_line_length,
+ table.concat(overlong_lines, ",")
+ ))
+ end
+end
+
OPTIONS = {
indent_style = function (value)
vis_set("expandtab", (value == "space"))
@@ -29,11 +98,39 @@ OPTIONS = {
vis_set("tabwidth", value)
end,
+ insert_final_newline = function (value)
+ set_pre_save(insert_final_newline, value)
+ end,
+
+ trim_trailing_whitespace = function (value)
+ set_pre_save(trim_trailing_whitespace, value)
+ end,
+
+ -- End of line is only partially implemented. While vis does not
+ -- support customized newlines, it does work well enough with crlf
+ -- newlines. Therefore, setting end_of_line=crlf will just ensure
+ -- that there is a cr at the end of each line. Setting end_of_line=lf
+ -- will strip any cr characters at the end of lines. This hopefully
+ -- eases the pain of working with crlf files a little.
+ end_of_line = function (value)
+ set_pre_save(enforce_crlf_eol, tostring(value == "crlf"))
+ set_pre_save(enforce_lf_eol, tostring(value == "lf"))
+ end,
+
+ -- There is probably no straightforward way to enforce a maximum line
+ -- length across different programming languages. If a maximum line
+ -- length is set, we can at least issue a warning, however.
+ max_line_length = function(value)
+ if value ~= "off" then
+ global_max_line_length = tonumber(value)
+ end
+ set_pre_save(max_line_length, tostring(value ~= "off"))
+ end,
+
-- Not supported by vis
- -- end_of_line
-- charset
- -- trim_trailing_whitespace
- -- insert_final_newline
+ -- Partial support
+ -- end_of_line
-- max_line_length
}