diff options
author | Josh Rahm <joshuarahm@gmail.com> | 2023-11-30 20:35:25 +0000 |
---|---|---|
committer | Josh Rahm <joshuarahm@gmail.com> | 2023-11-30 20:35:25 +0000 |
commit | 1b7b916b7631ddf73c38e3a0070d64e4636cb2f3 (patch) | |
tree | cd08258054db80bb9a11b1061bb091c70b76926a /runtime/lua/editorconfig.lua | |
parent | eaa89c11d0f8aefbb512de769c6c82f61a8baca3 (diff) | |
parent | 4a8bf24ac690004aedf5540fa440e788459e5e34 (diff) | |
download | rneovim-1b7b916b7631ddf73c38e3a0070d64e4636cb2f3.tar.gz rneovim-1b7b916b7631ddf73c38e3a0070d64e4636cb2f3.tar.bz2 rneovim-1b7b916b7631ddf73c38e3a0070d64e4636cb2f3.zip |
Merge remote-tracking branch 'upstream/master' into aucmd_textputpostaucmd_textputpost
Diffstat (limited to 'runtime/lua/editorconfig.lua')
-rw-r--r-- | runtime/lua/editorconfig.lua | 37 |
1 files changed, 28 insertions, 9 deletions
diff --git a/runtime/lua/editorconfig.lua b/runtime/lua/editorconfig.lua index 2079006234..49d63807a6 100644 --- a/runtime/lua/editorconfig.lua +++ b/runtime/lua/editorconfig.lua @@ -1,5 +1,6 @@ local M = {} +--- @type table<string,fun(bufnr: integer, val: string, opts?: table)> M.properties = {} --- Modified version of the builtin assert that does not include error position information @@ -19,14 +20,14 @@ end --- ---@private local function warn(msg, ...) - vim.notify(string.format(msg, ...), vim.log.levels.WARN, { + vim.notify_once(string.format(msg, ...), vim.log.levels.WARN, { title = 'editorconfig', }) end function M.properties.charset(bufnr, val) assert( - vim.tbl_contains({ 'utf-8', 'utf-8-bom', 'latin1', 'utf-16be', 'utf-16le' }, val), + vim.list_contains({ 'utf-8', 'utf-8-bom', 'latin1', 'utf-16be', 'utf-16le' }, val), 'charset must be one of "utf-8", "utf-8-bom", "latin1", "utf-16be", or "utf-16le"' ) if val == 'utf-8' or val == 'utf-8-bom' then @@ -111,7 +112,20 @@ end function M.properties.insert_final_newline(bufnr, val) assert(val == 'true' or val == 'false', 'insert_final_newline must be either "true" or "false"') vim.bo[bufnr].fixendofline = val == 'true' - vim.bo[bufnr].endofline = val == 'true' + + -- 'endofline' can be read to detect if the file contains a final newline, + -- so only change 'endofline' right before writing the file + local endofline = val == 'true' + if vim.bo[bufnr].endofline ~= endofline then + vim.api.nvim_create_autocmd('BufWritePre', { + group = 'editorconfig', + buffer = bufnr, + once = true, + callback = function() + vim.bo[bufnr].endofline = endofline + end, + }) + end end --- Modified version of |glob2regpat()| that does not match path separators on *. @@ -168,12 +182,12 @@ end --- ---@param filepath string File path of the file to apply EditorConfig settings to ---@param dir string Current directory ----@return table Table of options to apply to the given file +---@return table<string,string|boolean> Table of options to apply to the given file --- ---@private local function parse(filepath, dir) - local pat = nil - local opts = {} + local pat --- @type vim.regex? + local opts = {} --- @type table<string,string|boolean> local f = io.open(dir .. '/.editorconfig') if f then for line in f:lines() do @@ -189,6 +203,7 @@ local function parse(filepath, dir) end elseif key ~= nil and val ~= nil then if key == 'root' then + assert(val == 'true' or val == 'false', 'root must be either "true" or "false"') opts.root = val == 'true' elseif pat and pat:match_str(filepath) then opts[key] = val @@ -202,17 +217,21 @@ end --- Configure the given buffer with options from an .editorconfig file --- ----@param bufnr number Buffer number to configure +---@param bufnr integer Buffer number to configure --- ---@private function M.config(bufnr) bufnr = bufnr or vim.api.nvim_get_current_buf() + if not vim.api.nvim_buf_is_valid(bufnr) then + return + end + local path = vim.fs.normalize(vim.api.nvim_buf_get_name(bufnr)) if vim.bo[bufnr].buftype ~= '' or not vim.bo[bufnr].modifiable or path == '' then return end - local opts = {} + local opts = {} --- @type table<string,string|boolean> for parent in vim.fs.parents(path) do for k, v in pairs(parse(path, parent)) do if opts[k] == nil then @@ -225,7 +244,7 @@ function M.config(bufnr) end end - local applied = {} + local applied = {} --- @type table<string,string|boolean> for opt, val in pairs(opts) do if val ~= 'unset' then local func = M.properties[opt] |