| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
|
|
| |
| local properties = {} |
|
|
| |
| |
| |
| |
| |
| |
| local function assert(v, message) |
| return v or error(message, 0) |
| end |
|
|
| |
| |
| |
| local function warn(msg, ...) |
| vim.notify_once(msg:format(...), vim.log.levels.WARN, { |
| title = 'editorconfig', |
| }) |
| end |
|
|
| |
| |
| |
| function properties.root() |
| |
| end |
|
|
| |
| |
| function properties.charset(bufnr, val) |
| assert( |
| 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 |
| vim.bo[bufnr].fileencoding = 'utf-8' |
| vim.bo[bufnr].bomb = val == 'utf-8-bom' |
| elseif val == 'utf-16be' then |
| vim.bo[bufnr].fileencoding = 'utf-16' |
| else |
| vim.bo[bufnr].fileencoding = val |
| end |
| end |
|
|
| |
| |
| |
| function properties.end_of_line(bufnr, val) |
| vim.bo[bufnr].fileformat = assert( |
| ({ lf = 'unix', crlf = 'dos', cr = 'mac' })[val], |
| 'end_of_line must be one of "lf", "crlf", or "cr"' |
| ) |
| end |
|
|
| |
| function properties.indent_style(bufnr, val, opts) |
| assert(val == 'tab' or val == 'space', 'indent_style must be either "tab" or "space"') |
| vim.bo[bufnr].expandtab = val == 'space' |
| if val == 'tab' and not opts.indent_size then |
| vim.bo[bufnr].shiftwidth = 0 |
| vim.bo[bufnr].softtabstop = 0 |
| end |
| end |
|
|
| |
| |
| |
| |
| function properties.indent_size(bufnr, val, opts) |
| if val == 'tab' then |
| vim.bo[bufnr].shiftwidth = 0 |
| vim.bo[bufnr].softtabstop = 0 |
| else |
| local n = assert(tonumber(val), 'indent_size must be a number') |
| vim.bo[bufnr].shiftwidth = n |
| vim.bo[bufnr].softtabstop = -1 |
| if not opts.tab_width then |
| vim.bo[bufnr].tabstop = n |
| end |
| end |
| end |
|
|
| |
| function properties.tab_width(bufnr, val) |
| vim.bo[bufnr].tabstop = assert(tonumber(val), 'tab_width must be a number') |
| end |
|
|
| |
| |
| function properties.max_line_length(bufnr, val) |
| local n = tonumber(val) |
| if n then |
| vim.bo[bufnr].textwidth = n |
| else |
| assert(val == 'off', 'max_line_length must be a number or "off"') |
| vim.bo[bufnr].textwidth = 0 |
| end |
| end |
|
|
| |
| function properties.trim_trailing_whitespace(bufnr, val) |
| assert( |
| val == 'true' or val == 'false', |
| 'trim_trailing_whitespace must be either "true" or "false"' |
| ) |
| if val == 'true' then |
| vim.api.nvim_create_autocmd('BufWritePre', { |
| group = 'nvim.editorconfig', |
| buffer = bufnr, |
| callback = function() |
| local view = vim.fn.winsaveview() |
| vim.api.nvim_command('silent! undojoin') |
| vim.api.nvim_command('silent keepjumps keeppatterns %s/\\s\\+$//e') |
| vim.fn.winrestview(view) |
| end, |
| }) |
| else |
| vim.api.nvim_clear_autocmds({ |
| event = 'BufWritePre', |
| group = 'nvim.editorconfig', |
| buffer = bufnr, |
| }) |
| end |
| end |
|
|
| |
| |
| function 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' |
|
|
| |
| |
| local endofline = val == 'true' |
| if vim.bo[bufnr].endofline ~= endofline then |
| vim.api.nvim_create_autocmd('BufWritePre', { |
| group = 'nvim.editorconfig', |
| buffer = bufnr, |
| once = true, |
| callback = function() |
| vim.bo[bufnr].endofline = endofline |
| end, |
| }) |
| end |
| end |
|
|
| |
| |
| function properties.spelling_language(bufnr, val) |
| local error_msg = |
| 'spelling_language must be of the format ss or ss-TT, where ss is an ISO 639 language code and TT is an ISO 3166 territory identifier.' |
|
|
| assert(val:len() == 2 or val:len() == 5, error_msg) |
|
|
| local language_code = val:sub(1, 2):lower() |
| assert(language_code:match('%l%l'), error_msg) |
| if val:len() == 2 then |
| vim.bo[bufnr].spelllang = language_code |
| else |
| assert(val:sub(3, 3) == '-', error_msg) |
|
|
| local territory_code = val:sub(4, 5):lower() |
| assert(territory_code:match('%l%l'), error_msg) |
| vim.bo[bufnr].spelllang = language_code .. '_' .. territory_code |
| end |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| local function glob2regpat(glob) |
| local placeholder = '@@PLACEHOLDER@@' |
| local glob1 = vim.fn.substitute( |
| glob:gsub('{(%d+)%.%.(%d+)}', '[%1-%2]'), |
| '\\*\\@<!\\*\\*\\@!', |
| placeholder, |
| 'g' |
| ) |
| local regpat = vim.fn.glob2regpat(glob1) |
| return (regpat:gsub(placeholder, '[^/]*')) |
| end |
|
|
| |
| |
| |
| |
| |
| |
| local function parse_line(line) |
| if not line:find('^%s*[^ #;]') then |
| return |
| end |
|
|
| |
| local glob = line:match('^%s*%[(.*)%]%s*$') |
| if glob then |
| return glob |
| end |
|
|
| local key, val = line:match('^%s*([^:= ][^:=]-)%s*[:=]%s*(.-)%s*$') |
| if key ~= nil and val ~= nil then |
| return nil, key:lower(), val:lower() |
| end |
| end |
|
|
| |
| |
| |
| |
| |
| local function parse(filepath, dir) |
| local pat |
| local opts = {} |
| local f = io.open(dir .. '/.editorconfig') |
| if f then |
| for line in f:lines() do |
| local glob, key, val = parse_line(line) |
| if glob then |
| glob = glob:find('/') and (dir .. '/' .. glob:gsub('^/', '')) or ('**/' .. glob) |
| local ok, regpat = pcall(glob2regpat, glob) |
| if ok then |
| pat = vim.regex(regpat) |
| else |
| pat = nil |
| warn('editorconfig: Error occurred while parsing glob pattern "%s": %s', glob, regpat) |
| 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 |
| end |
| end |
| end |
| f:close() |
| end |
| return opts |
| end |
|
|
| local M = {} |
|
|
| |
| M.properties = properties |
|
|
| |
| |
| |
| 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 = {} |
| for parent in vim.fs.parents(path) do |
| for k, v in pairs(parse(path, parent)) do |
| if opts[k] == nil then |
| opts[k] = v |
| end |
| end |
|
|
| if opts.root then |
| break |
| end |
| end |
|
|
| local applied = {} |
| for opt, val in pairs(opts) do |
| if val ~= 'unset' then |
| local func = M.properties[opt] |
| if func then |
| |
| local ok, err = pcall(func, bufnr, val, opts) |
| if ok then |
| applied[opt] = val |
| else |
| warn('editorconfig: invalid value for option %s: %s. %s', opt, val, err) |
| end |
| end |
| end |
| end |
|
|
| vim.b[bufnr].editorconfig = applied |
| end |
|
|
| return M |
|
|