Improve readability
This commit is contained in:
114
lua/hardline.lua
114
lua/hardline.lua
@@ -5,6 +5,7 @@
|
||||
-------------------- VARIABLES -----------------------------
|
||||
local fn, cmd, vim = vim.fn, vim.cmd, vim
|
||||
local o, wo = vim.o, vim.wo
|
||||
local fmt = string.format
|
||||
local common = require('hardline.common')
|
||||
local bufferline = require('hardline.bufferline')
|
||||
local M = {}
|
||||
@@ -40,7 +41,7 @@ local function aggregate_sections(sections)
|
||||
if stop then
|
||||
table.insert(aggregated, {
|
||||
class = sections[pivot].class,
|
||||
item = string.format(' %s ', table.concat(items, ' ')),
|
||||
item = fmt(' %s ', table.concat(items, ' ')),
|
||||
})
|
||||
pivot = j
|
||||
break
|
||||
@@ -56,57 +57,82 @@ local function aggregate_sections(sections)
|
||||
end
|
||||
|
||||
local function remove_empty_sections(sections)
|
||||
local filter = function(s)
|
||||
if type(s) == 'table' then return s.item ~= '' else return s ~= '' end
|
||||
local filter = function(section)
|
||||
if type(section) == 'table' then
|
||||
return section.item ~= ''
|
||||
end
|
||||
return section ~= ''
|
||||
end
|
||||
return vim.tbl_filter(filter, sections)
|
||||
end
|
||||
|
||||
local function reload_sections(sections)
|
||||
local function map(section)
|
||||
if type(section) == 'string' then
|
||||
return section
|
||||
elseif type(section) == 'function' then
|
||||
return section()
|
||||
elseif type(section) == 'table' then
|
||||
return {
|
||||
class = section.class or 'none',
|
||||
item = map(section.item),
|
||||
}
|
||||
end
|
||||
common.echo('WarningMsg', 'Invalid section.')
|
||||
return ''
|
||||
local function load_section(section)
|
||||
if type(section) == 'string' then
|
||||
return section
|
||||
end
|
||||
return vim.tbl_map(map, sections)
|
||||
if type(section) == 'function' then
|
||||
return section()
|
||||
end
|
||||
if type(section) == 'table' then
|
||||
return {
|
||||
class = section.class or 'none',
|
||||
item = load_section(section.item),
|
||||
}
|
||||
end
|
||||
common.echo('WarningMsg', 'Invalid section.')
|
||||
return ''
|
||||
end
|
||||
|
||||
local function load_sections(sections)
|
||||
return vim.tbl_map(load_section, sections)
|
||||
end
|
||||
|
||||
local function remove_hidden_sections(sections)
|
||||
local filter = function(s) return not s.hide or s.hide <= fn.winwidth(0) end
|
||||
local filter = function(s)
|
||||
return not s.hide or s.hide <= fn.winwidth(0)
|
||||
end
|
||||
return vim.tbl_filter(filter, sections)
|
||||
end
|
||||
|
||||
-------------------- SECTION HIGHLIGHTING ------------------
|
||||
local function get_section_state(section)
|
||||
if section.class == 'mode' and common.is_active() then
|
||||
local mode = common.modes[fn.mode()] or common.modes['?']
|
||||
return mode.state
|
||||
if section.class == 'mode' then
|
||||
if common.is_active() then
|
||||
local mode = common.modes[fn.mode()] or common.modes['?']
|
||||
return mode.state
|
||||
end
|
||||
return common.is_active() and 'active' or 'inactive'
|
||||
end
|
||||
if section.class == 'bufferline' then
|
||||
if section.separator then return 'separator' end
|
||||
if section.separator then
|
||||
return 'separator'
|
||||
end
|
||||
local state = section.current and 'current' or 'background'
|
||||
if section.modified then state = string.format('%s_modified', state) end
|
||||
if section.modified then
|
||||
state = fmt('%s_modified', state)
|
||||
end
|
||||
return state
|
||||
end
|
||||
return common.is_active() and 'active' or 'inactive'
|
||||
return ''
|
||||
end
|
||||
|
||||
local function color_section(section)
|
||||
if type(section) ~= 'table' then return section end
|
||||
if section.class == 'none' then return section.item end
|
||||
local function highlight_section(section)
|
||||
if type(section) ~= 'table' then
|
||||
return section
|
||||
end
|
||||
if section.class == 'none' then
|
||||
return section.item
|
||||
end
|
||||
local state = get_section_state(section)
|
||||
local hlgroup = string.format('Hardline_%s_%s', section.class, state)
|
||||
if fn.hlexists(hlgroup) == 0 then return section.item end
|
||||
return string.format('%%#%s#%s%%*', hlgroup, section.item)
|
||||
local hlgroup = fmt('Hardline_%s_%s', section.class, state)
|
||||
if fn.hlexists(hlgroup) == 0 then
|
||||
return section.item
|
||||
end
|
||||
return fmt('%%#%s#%s%%*', hlgroup, section.item)
|
||||
end
|
||||
|
||||
local function highlight_sections(sections)
|
||||
return vim.tbl_map(highlight_section, sections)
|
||||
end
|
||||
|
||||
-------------------- STATUSLINE ----------------------------
|
||||
@@ -115,12 +141,12 @@ function M.update_statusline()
|
||||
if common.is_active() or not sections then
|
||||
sections = M.options.sections
|
||||
sections = remove_hidden_sections(sections)
|
||||
sections = reload_sections(sections)
|
||||
sections = load_sections(sections)
|
||||
sections = remove_empty_sections(sections)
|
||||
sections = aggregate_sections(sections)
|
||||
cache.previous, cache.current = cache.current, sections
|
||||
end
|
||||
return table.concat(vim.tbl_map(color_section, sections))
|
||||
return table.concat(highlight_sections(sections))
|
||||
end
|
||||
|
||||
-------------------- BUFFERLINE ----------------------------
|
||||
@@ -130,28 +156,32 @@ function M.update_bufferline()
|
||||
local separator = '|'
|
||||
for i, buffer in ipairs(buffers) do
|
||||
table.insert(sections, bufferline.to_section(buffer))
|
||||
if i < #buffers then table.insert(sections, separator) end
|
||||
if i < #buffers then
|
||||
table.insert(sections, separator)
|
||||
end
|
||||
end
|
||||
return table.concat(vim.tbl_map(color_section, sections))
|
||||
return table.concat(highlight_sections(sections))
|
||||
end
|
||||
|
||||
-------------------- SETUP -----------------------------
|
||||
local function set_theme()
|
||||
if type(M.options.theme) ~= 'string' then return end
|
||||
local theme = string.format('hardline.themes.%s', M.options.theme)
|
||||
if type(M.options.theme) ~= 'string' then
|
||||
return
|
||||
end
|
||||
local theme = fmt('hardline.themes.%s', M.options.theme)
|
||||
M.options.theme = require(theme)
|
||||
end
|
||||
|
||||
local function set_hlgroups()
|
||||
for class, attr in pairs(M.options.theme) do
|
||||
for state, args in pairs(attr) do
|
||||
local hlgroup = string.format('Hardline_%s_%s', class, state)
|
||||
local hlgroup = fmt('Hardline_%s_%s', class, state)
|
||||
local a = {}
|
||||
for k, v in pairs(args) do
|
||||
table.insert(a, string.format('%s=%s', k, v))
|
||||
table.insert(a, fmt('%s=%s', k, v))
|
||||
end
|
||||
a = table.concat(a, ' ')
|
||||
cmd(string.format('autocmd VimEnter,ColorScheme * hi %s %s', hlgroup, a))
|
||||
cmd(fmt('autocmd VimEnter,ColorScheme * hi %s %s', hlgroup, a))
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -172,7 +202,9 @@ function M.setup(user_options)
|
||||
set_theme()
|
||||
set_hlgroups()
|
||||
set_statusline()
|
||||
if M.options.bufferline then set_bufferline() end
|
||||
if M.options.bufferline then
|
||||
set_bufferline()
|
||||
end
|
||||
end
|
||||
|
||||
------------------------------------------------------------
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
local fn, vim = vim.fn, vim
|
||||
local fmt = string.format
|
||||
|
||||
local function exclude(bufnr)
|
||||
return (
|
||||
fn.buflisted(bufnr) == 0 or
|
||||
fn.getbufvar(bufnr, '&filetype') == 'qf'
|
||||
)
|
||||
return fn.buflisted(bufnr) == 0 or fn.getbufvar(bufnr, '&filetype') == 'qf'
|
||||
end
|
||||
|
||||
local function get_head(path, tail)
|
||||
@@ -26,11 +24,13 @@ local function unique_tail(buffers)
|
||||
end
|
||||
duplicate = duplicate or hist[buffer.name] > 1
|
||||
end
|
||||
if not duplicate then return end
|
||||
if not duplicate then
|
||||
return
|
||||
end
|
||||
for _, buffer in ipairs(buffers) do
|
||||
if hist[buffer.name] > 1 then
|
||||
local parent = get_head(fn.bufname(buffer.bufnr), buffer.name)
|
||||
buffer.name = string.format('%s/%s', parent, buffer.name)
|
||||
buffer.name = fmt('%s/%s', parent, buffer.name)
|
||||
end
|
||||
end
|
||||
unique_tail(buffers)
|
||||
@@ -39,14 +39,20 @@ end
|
||||
local function to_section(buffer)
|
||||
local flags = {}
|
||||
local item = buffer.name == '' and '[No Name]' or buffer.name
|
||||
if buffer.flags.modified then table.insert(flags, '[+]') end
|
||||
if not buffer.flags.modifiable then table.insert(flags, '[-]') end
|
||||
if buffer.flags.readonly then table.insert(flags, '[RO]') end
|
||||
if buffer.flags.modified then
|
||||
table.insert(flags, '[+]')
|
||||
end
|
||||
if not buffer.flags.modifiable then
|
||||
table.insert(flags, '[-]')
|
||||
end
|
||||
if buffer.flags.readonly then
|
||||
table.insert(flags, '[RO]')
|
||||
end
|
||||
flags = table.concat(flags)
|
||||
item = flags == '' and item or string.format('%s %s', item, flags)
|
||||
item = flags == '' and item or fmt('%s %s', item, flags)
|
||||
return {
|
||||
class = 'bufferline',
|
||||
item = string.format(' %s ', item),
|
||||
item = fmt(' %s ', item),
|
||||
modified = buffer.flags.modified,
|
||||
current = buffer.current,
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
local cmd, fn = vim.cmd, vim.fn
|
||||
local g = vim.g
|
||||
local fmt = string.format
|
||||
local M = {}
|
||||
|
||||
M.modes = {
|
||||
@@ -18,8 +19,8 @@ M.modes = {
|
||||
}
|
||||
|
||||
function M.echo(hlgroup, msg)
|
||||
cmd(string.format('echohl %s', hlgroup))
|
||||
cmd(string.format('echo "[hardline] %s"', msg))
|
||||
cmd(fmt('echohl %s', hlgroup))
|
||||
cmd(fmt('echo "[hardline] %s"', msg))
|
||||
cmd('echohl None')
|
||||
end
|
||||
|
||||
@@ -28,9 +29,9 @@ function M.is_active()
|
||||
end
|
||||
|
||||
function M.set_cache_autocmds(augroup)
|
||||
cmd(string.format('augroup %s', augroup))
|
||||
cmd(fmt('augroup %s', augroup))
|
||||
cmd('autocmd!')
|
||||
cmd(string.format('autocmd CursorHold,BufWritePost * unlet! b:%s', augroup))
|
||||
cmd(fmt('autocmd CursorHold,BufWritePost * unlet! b:%s', augroup))
|
||||
cmd('augroup END')
|
||||
end
|
||||
|
||||
|
||||
@@ -6,20 +6,28 @@ local function get_name()
|
||||
end
|
||||
|
||||
local function get_readonly()
|
||||
if bo.readonly then return '[RO]' end
|
||||
if bo.readonly then
|
||||
return '[RO]'
|
||||
end
|
||||
return ''
|
||||
end
|
||||
|
||||
local function get_modified()
|
||||
if bo.modified then return '[+]' end
|
||||
if not bo.modifiable then return '[-]' end
|
||||
if bo.modified then
|
||||
return '[+]'
|
||||
end
|
||||
if not bo.modifiable then
|
||||
return '[-]'
|
||||
end
|
||||
return ''
|
||||
end
|
||||
|
||||
local function get_item()
|
||||
local name = get_name()
|
||||
local flags = table.concat({get_readonly(), get_modified()})
|
||||
if flags ~= '' then flags = ' ' .. flags end
|
||||
if flags ~= '' then
|
||||
flags = ' ' .. flags
|
||||
end
|
||||
return table.concat({name, flags})
|
||||
end
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
local fn = vim.fn
|
||||
local g = vim.g
|
||||
local fmt = string.format
|
||||
|
||||
local function get_hunks()
|
||||
local summary
|
||||
@@ -7,20 +8,16 @@ local function get_hunks()
|
||||
summary = fn.GitGutterGetHunkSummary()
|
||||
elseif g.loaded_signify then
|
||||
summary = fn['sy#repo#get_stats']()
|
||||
-- signify return [-1,-1,-1] in empty buffer
|
||||
-- that is why we check if one of value is -1
|
||||
-- then stop the operation by return empty string
|
||||
if (summary[1] == -1) then
|
||||
if (summary[1] == -1) then -- signify returns {-1, -1, -1} in empty buffer
|
||||
return ''
|
||||
end
|
||||
else
|
||||
-- return empty string if none of them is loaded
|
||||
return ''
|
||||
end
|
||||
return table.concat({
|
||||
string.format('+%d', summary[1]),
|
||||
string.format('~%d', summary[2]),
|
||||
string.format('-%d', summary[3]),
|
||||
fmt('+%d', summary[1]),
|
||||
fmt('~%d', summary[2]),
|
||||
fmt('-%d', summary[3]),
|
||||
}, ' ')
|
||||
end
|
||||
|
||||
@@ -31,12 +28,14 @@ local function get_branch()
|
||||
elseif g.loaded_gina then
|
||||
branch = fn['gina#component#repo#branch']()
|
||||
end
|
||||
return branch ~= '' and string.format('(%s)', branch) or ''
|
||||
return branch ~= '' and fmt('(%s)', branch) or ''
|
||||
end
|
||||
|
||||
local function get_item()
|
||||
local hunks, branch = get_hunks(), get_branch()
|
||||
if branch == '' then return '' end
|
||||
if branch == '' then
|
||||
return ''
|
||||
end
|
||||
branch = not branch and '' or ' ' .. branch
|
||||
return table.concat({hunks, branch})
|
||||
end
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
local fn = vim.fn
|
||||
local fmt = string.format
|
||||
|
||||
local function pad(c, m)
|
||||
local padch = '·'
|
||||
@@ -8,20 +9,20 @@ end
|
||||
local function get_line()
|
||||
local nbline = fn.line('$')
|
||||
local line = fn.line('.')
|
||||
return string.format('%s%d/%d', pad(line, nbline), line, nbline)
|
||||
return fmt('%s%d/%d', pad(line, nbline), line, nbline)
|
||||
end
|
||||
|
||||
local function get_column()
|
||||
local nbcol = fn.col('$') - 1
|
||||
local col = fn.col('.')
|
||||
return string.format('%s%d/%s%d', pad(col, 100), col, pad(nbcol, 100), nbcol)
|
||||
return fmt('%s%d/%s%d', pad(col, 100), col, pad(nbcol, 100), nbcol)
|
||||
end
|
||||
|
||||
local function get_percent()
|
||||
local nb_lines = fn.line('$')
|
||||
local line = fn.line('.')
|
||||
local percent = math.floor(line * 100 / nb_lines)
|
||||
return string.format('%s%d%%%%', pad(percent, 100), percent)
|
||||
return fmt('%s%d%%%%', pad(percent, 100), percent)
|
||||
end
|
||||
|
||||
local function get_item()
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
local lsp = vim.lsp
|
||||
local fmt = string.format
|
||||
|
||||
local function get_diagnostic(prefix, severity)
|
||||
local count = lsp.diagnostic.get_count(0, severity)
|
||||
if count < 1 then return '' end
|
||||
return string.format('%s:%d', prefix, count)
|
||||
if count < 1 then
|
||||
return ''
|
||||
end
|
||||
return fmt('%s:%d', prefix, count)
|
||||
end
|
||||
|
||||
local function get_error()
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
local fn = vim.fn
|
||||
local o, bo, wo = vim.o, vim.bo, vim.wo
|
||||
local fmt = string.format
|
||||
local common = require('hardline.common')
|
||||
|
||||
local function get_mode()
|
||||
@@ -8,19 +9,27 @@ local function get_mode()
|
||||
end
|
||||
|
||||
local function get_paste()
|
||||
if not o.paste then return '' end
|
||||
if not o.paste then
|
||||
return ''
|
||||
end
|
||||
return 'PASTE'
|
||||
end
|
||||
|
||||
local function get_spell()
|
||||
if not wo.spell then return '' end
|
||||
return string.format('SPELL[%s]', string.upper(bo.spelllang))
|
||||
if not wo.spell then
|
||||
return ''
|
||||
end
|
||||
return fmt('SPELL[%s]', string.upper(bo.spelllang))
|
||||
end
|
||||
|
||||
local function get_item()
|
||||
local mode, paste, spell = get_mode(), get_paste(), get_spell()
|
||||
if paste ~= '' then paste = ' ' .. paste end
|
||||
if spell ~= '' then spell = ' ' .. spell end
|
||||
if paste ~= '' then
|
||||
paste = ' ' .. paste
|
||||
end
|
||||
if spell ~= '' then
|
||||
spell = ' ' .. spell
|
||||
end
|
||||
return table.concat({mode, paste, spell})
|
||||
end
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
local fn, vim = vim.fn, vim
|
||||
local b, bo = vim.b, vim.bo
|
||||
local fmt = string.format
|
||||
local common = require('hardline.common')
|
||||
|
||||
local enabled = false
|
||||
@@ -11,8 +12,10 @@ local options = {
|
||||
|
||||
local function search(prefix, pattern)
|
||||
local line = fn.search(pattern, 'nw')
|
||||
if line == 0 then return '' end
|
||||
return string.format('[%s:%d]', prefix, line)
|
||||
if line == 0 then
|
||||
return ''
|
||||
end
|
||||
return fmt('[%s:%d]', prefix, line)
|
||||
end
|
||||
|
||||
local function check_trailing()
|
||||
@@ -21,8 +24,8 @@ end
|
||||
|
||||
local function check_mix_indent()
|
||||
local tst = [[(^\t* +\t\s*\S)]]
|
||||
local tls = string.format([[(^\t+ {%d,}\S)]], bo.tabstop)
|
||||
local pattern = string.format([[\v%s|%s]], tst, tls)
|
||||
local tls = fmt([[(^\t+ {%d,}\S)]], bo.tabstop)
|
||||
local pattern = fmt([[\v%s|%s]], tst, tls)
|
||||
return search('mix-indent', pattern)
|
||||
end
|
||||
|
||||
@@ -33,8 +36,10 @@ local function check_mix_indent_file()
|
||||
end
|
||||
local indent_tabs = fn.search([[\v(^\t+)]], 'nw')
|
||||
local indent_spc = fn.search(head_spc, 'nw')
|
||||
if indent_tabs == 0 or indent_spc == 0 then return '' end
|
||||
return string.format('[mix-indent-file:%d,%d]', indent_spc, indent_tabs)
|
||||
if indent_tabs == 0 or indent_spc == 0 then
|
||||
return ''
|
||||
end
|
||||
return fmt('[mix-indent-file:%d,%d]', indent_spc, indent_tabs)
|
||||
end
|
||||
|
||||
local function check_conflict()
|
||||
@@ -43,7 +48,7 @@ local function check_conflict()
|
||||
if bo.filetype == 'rst' then
|
||||
raw_pattern = [[^\%%(\%%(<\{7} %s\)\|\%%(>\{7\} %s\)\)$]]
|
||||
end
|
||||
local pattern = string.format(raw_pattern, annotation, annotation)
|
||||
local pattern = fmt(raw_pattern, annotation, annotation)
|
||||
return search('conflict', pattern)
|
||||
end
|
||||
|
||||
@@ -52,9 +57,15 @@ local function get_item()
|
||||
common.set_cache_autocmds('hardline_whitespace')
|
||||
enabled = true
|
||||
end
|
||||
if bo.readonly or not bo.modifiable then return '' end
|
||||
if fn.line('$') > options.max_lines then return '' end
|
||||
if b.hardline_whitespace then return cache end
|
||||
if bo.readonly or not bo.modifiable then
|
||||
return ''
|
||||
end
|
||||
if fn.line('$') > options.max_lines then
|
||||
return ''
|
||||
end
|
||||
if b.hardline_whitespace then
|
||||
return cache
|
||||
end
|
||||
b.hardline_whitespace = true
|
||||
cache = table.concat({
|
||||
check_trailing(),
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
local fn, vim = vim.fn, vim
|
||||
local b, bo = vim.b, vim.bo
|
||||
local fmt = string.format
|
||||
local common = require('hardline.common')
|
||||
|
||||
local enabled = false
|
||||
@@ -28,7 +29,7 @@ end
|
||||
local function get_wordcount()
|
||||
local query = in_visual() and 'visual_words' or 'words'
|
||||
local wordcount = fn.wordcount()[query]
|
||||
return string.format('%d words', wordcount)
|
||||
return fmt('%d words', wordcount)
|
||||
end
|
||||
|
||||
local function get_item()
|
||||
@@ -36,9 +37,15 @@ local function get_item()
|
||||
common.set_cache_autocmds('hardline_wordcount')
|
||||
enabled = true
|
||||
end
|
||||
if not vim.tbl_contains(options.filetypes, bo.filetype) then return '' end
|
||||
if fn.line('$') > options.max_lines then return '' end
|
||||
if b.hardline_wordcount and not in_visual() then return cache end
|
||||
if not vim.tbl_contains(options.filetypes, bo.filetype) then
|
||||
return ''
|
||||
end
|
||||
if fn.line('$') > options.max_lines then
|
||||
return ''
|
||||
end
|
||||
if b.hardline_wordcount and not in_visual() then
|
||||
return cache
|
||||
end
|
||||
b.hardline_wordcount = true
|
||||
cache = get_wordcount()
|
||||
return cache
|
||||
|
||||
Reference in New Issue
Block a user