refactor: clean code

This commit is contained in:
Olivier Roques
2022-01-27 18:53:47 +01:00
parent 642d6f3314
commit d21bdb488a
10 changed files with 70 additions and 89 deletions

View File

@@ -1,22 +1,21 @@
local fn, vim = vim.fn, vim
local fmt = string.format
local function is_excluded(bufnr, settings)
local excluded = true
excluded = excluded and fn.buflisted(bufnr) == 0
excluded = excluded or fn.getbufvar(bufnr, '&filetype') == 'qf'
excluded = excluded and vim.fn.buflisted(bufnr) == 0
excluded = excluded or vim.fn.getbufvar(bufnr, '&filetype') == 'qf'
if settings.exclude_terminal then
excluded = excluded or fn.getbufvar(bufnr, '&buftype') == 'terminal'
excluded = excluded or vim.fn.getbufvar(bufnr, '&buftype') == 'terminal'
end
return excluded
end
local function get_head(path, tail)
local result = fn.fnamemodify(path, ':p')
local result = vim.fn.fnamemodify(path, ':p')
for i = 1, #vim.split(tail, '/') do
result = fn.fnamemodify(result, ':h')
result = vim.fn.fnamemodify(result, ':h')
end
return fn.fnamemodify(result, ':t')
return vim.fn.fnamemodify(result, ':t')
end
local function unique_tail(buffers)
@@ -35,7 +34,7 @@ local function unique_tail(buffers)
end
for _, buffer in ipairs(buffers) do
if hist[buffer.name] > 1 then
local parent = get_head(fn.bufname(buffer.bufnr), buffer.name)
local parent = get_head(vim.fn.bufname(buffer.bufnr), buffer.name)
buffer.name = fmt('%s/%s', parent, buffer.name)
end
end
@@ -71,16 +70,16 @@ end
local function get_buffers(settings)
local buffers = {}
for nr = 1, fn.bufnr('$') do
for nr = 1, vim.fn.bufnr('$') do
if not is_excluded(nr, settings) then
table.insert(buffers, {
bufnr = nr,
name = fn.fnamemodify(fn.bufname(nr), ':t'),
current = nr == fn.bufnr('%'),
name = vim.fn.fnamemodify(vim.fn.bufname(nr), ':t'),
current = nr == vim.fn.bufnr('%'),
flags = {
modified = fn.getbufvar(nr, '&modified') == 1,
modifiable = fn.getbufvar(nr, '&modifiable') == 1,
readonly = fn.getbufvar(nr, '&readonly') == 1,
modified = vim.fn.getbufvar(nr, '&modified') == 1,
modifiable = vim.fn.getbufvar(nr, '&modifiable') == 1,
readonly = vim.fn.getbufvar(nr, '&readonly') == 1,
},
})
end

View File

@@ -1,7 +1,5 @@
local fn = vim.fn
local function get_item()
return fn.fnamemodify(fn.getcwd(), ':~')
return vim.fn.fnamemodify(vim.fn.getcwd(), ':~')
end
return {

View File

@@ -1,22 +1,19 @@
local fn = vim.fn
local bo = vim.bo
local function get_name()
return fn.expand('%:~:.')
return vim.fn.expand('%:~:.')
end
local function get_readonly()
if bo.readonly then
if vim.bo.readonly then
return '[RO]'
end
return ''
end
local function get_modified()
if bo.modified then
if vim.bo.modified then
return '[+]'
end
if not bo.modifiable then
if not vim.bo.modifiable then
return '[-]'
end
return ''

View File

@@ -1,7 +1,5 @@
local bo = vim.bo
local function get_item()
return bo.filetype
return vim.bo.filetype
end
return {

View File

@@ -1,6 +1,3 @@
local fn = vim.fn
local g = vim.g
local b = vim.b
local fmt = string.format
local function concat_hunks(hunks)
@@ -13,16 +10,16 @@ end
local function get_hunks()
local hunks = {}
if g.loaded_gitgutter then
hunks = fn.GitGutterGetHunkSummary()
elseif g.loaded_signify then
hunks = fn['sy#repo#get_stats']()
if vim.g.loaded_gitgutter then
hunks = vim.fn.GitGutterGetHunkSummary()
elseif vim.g.loaded_signify then
hunks = vim.fn['sy#repo#get_stats']()
hunks = hunks[1] == -1 and {} or hunks
elseif b.gitsigns_status_dict then
elseif vim.b.gitsigns_status_dict then
hunks = {
b.gitsigns_status_dict.added,
b.gitsigns_status_dict.changed,
b.gitsigns_status_dict.removed,
vim.b.gitsigns_status_dict.added,
vim.b.gitsigns_status_dict.changed,
vim.b.gitsigns_status_dict.removed,
}
end
return concat_hunks(hunks)
@@ -30,14 +27,14 @@ end
local function get_branch()
local branch = ''
if g.loaded_fugitive then
branch = fn.FugitiveHead()
elseif g.loaded_gina then
branch = fn['gina#component#repo#branch']()
elseif g.loaded_gitbranch then
branch = fn['gitbranch#name']()
elseif b.gitsigns_head ~= nil then
branch = b.gitsigns_head
if vim.g.loaded_fugitive then
branch = vim.fn.FugitiveHead()
elseif vim.g.loaded_gina then
branch = vim.fn['gina#component#repo#branch']()
elseif vim.g.loaded_gitbranch then
branch = vim.fn['gitbranch#name']()
elseif vim.b.gitsigns_head ~= nil then
branch = vim.b.gitsigns_head
end
return branch ~= '' and fmt('(%s)', branch) or ''
end

View File

@@ -1,4 +1,3 @@
local fn = vim.fn
local fmt = string.format
local function pad(c, m)
@@ -7,20 +6,20 @@ local function pad(c, m)
end
local function get_line()
local nbline = fn.line('$')
local line = fn.line('.')
local nbline = vim.fn.line('$')
local line = vim.fn.line('.')
return fmt('%s%d/%d', pad(line, nbline), line, nbline)
end
local function get_column()
local nbcol = fn.col('$') - 1
local col = fn.col('.')
local nbcol = vim.fn.col('$') - 1
local col = vim.fn.col('.')
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 nb_lines = vim.fn.line('$')
local line = vim.fn.line('.')
local percent = math.floor(line * 100 / nb_lines)
return fmt('%s%d%%%%', pad(percent, 100), percent)
end

View File

@@ -1,16 +1,15 @@
local diagnostic, fn, lsp = vim.diagnostic, vim.fn, vim.lsp
local fmt = string.format
local function get_diagnostic(prefix, severity)
local count
if fn.has('nvim-0.6') == 0 then
count = lsp.diagnostic.get_count(0, severity)
if vim.fn.has('nvim-0.6') == 0 then
count = vim.lsp.diagnostic.get_count(0, severity)
else
local severities = {
['Warning'] = diagnostic.severity.WARN,
['Error'] = diagnostic.severity.ERROR,
['Warning'] = vim.diagnostic.severity.WARN,
['Error'] = vim.diagnostic.severity.ERROR,
}
count = #diagnostic.get(0, {severity=severities[severity]})
count = #vim.diagnostic.get(0, {severity=severities[severity]})
end
if count < 1 then
return ''

View File

@@ -1,25 +1,23 @@
local fn = vim.fn
local o, bo, wo = vim.o, vim.bo, vim.wo
local fmt = string.format
local common = require('hardline.common')
local fmt = string.format
local function get_mode()
local mode = common.modes[fn.mode()] or common.modes['?']
local mode = common.modes[vim.fn.mode()] or common.modes['?']
return mode.text
end
local function get_paste()
if not o.paste then
if not vim.o.paste then
return ''
end
return 'PASTE'
end
local function get_spell()
if not wo.spell then
if not vim.wo.spell then
return ''
end
return fmt('SPELL[%s]', string.upper(bo.spelllang))
return fmt('SPELL[%s]', string.upper(vim.bo.spelllang))
end
local function get_item()

View File

@@ -1,7 +1,5 @@
local fn, vim = vim.fn, vim
local b, bo = vim.b, vim.bo
local fmt = string.format
local common = require('hardline.common')
local fmt = string.format
local enabled = false
local cache = ''
@@ -11,7 +9,7 @@ local options = {
}
local function search(prefix, pattern)
local line = fn.search(pattern, 'nw')
local line = vim.fn.search(pattern, 'nw')
if line == 0 then
return ''
end
@@ -19,7 +17,7 @@ local function search(prefix, pattern)
end
local function check_trailing()
if vim.tbl_contains({'markdown'}, bo.filetype) then
if vim.tbl_contains({'markdown'}, vim.bo.filetype) then
return ''
end
return search('trailing', [[\s$]])
@@ -27,18 +25,18 @@ end
local function check_mix_indent()
local tst = [[(^\t* +\t\s*\S)]]
local tls = fmt([[(^\t+ {%d,}\S)]], bo.tabstop)
local tls = fmt([[(^\t+ {%d,}\S)]], vim.bo.tabstop)
local pattern = fmt([[\v%s|%s]], tst, tls)
return search('mix-indent', pattern)
end
local function check_mix_indent_file()
local head_spc = [[\v(^ +)]]
if vim.tbl_contains(options.c_langs, bo.filetype) then
if vim.tbl_contains(options.c_langs, vim.bo.filetype) then
head_spc = [[\v(^ +\*@!)]]
end
local indent_tabs = fn.search([[\v(^\t+)]], 'nw')
local indent_spc = fn.search(head_spc, 'nw')
local indent_tabs = vim.fn.search([[\v(^\t+)]], 'nw')
local indent_spc = vim.fn.search(head_spc, 'nw')
if indent_tabs == 0 or indent_spc == 0 then
return ''
end
@@ -48,7 +46,7 @@ end
local function check_conflict()
local annotation = [[\%([0-9A-Za-z_.:]\+\)\?]]
local raw_pattern = [[^\%%(\%%(<\{7} %s\)\|\%%(=\{7\}\)\|\%%(>\{7\} %s\)\)$]]
if bo.filetype == 'rst' then
if vim.bo.filetype == 'rst' then
raw_pattern = [[^\%%(\%%(<\{7} %s\)\|\%%(>\{7\} %s\)\)$]]
end
local pattern = fmt(raw_pattern, annotation, annotation)
@@ -60,16 +58,16 @@ local function get_item()
common.set_cache_autocmds('hardline_whitespace')
enabled = true
end
if bo.readonly or not bo.modifiable then
if vim.bo.readonly or not vim.bo.modifiable then
return ''
end
if fn.line('$') > options.max_lines then
if vim.fn.line('$') > options.max_lines then
return ''
end
if b.hardline_whitespace then
if vim.b.hardline_whitespace then
return cache
end
b.hardline_whitespace = true
vim.b.hardline_whitespace = true
cache = table.concat({
check_trailing(),
check_mix_indent(),

View File

@@ -1,7 +1,5 @@
local fn, vim = vim.fn, vim
local b, bo = vim.b, vim.bo
local fmt = string.format
local common = require('hardline.common')
local fmt = string.format
local enabled = false
local cache = ''
@@ -22,13 +20,13 @@ local options = {
}
local function in_visual()
local mode = common.modes[fn.mode()] or common.modes['?']
local mode = common.modes[vim.fn.mode()] or common.modes['?']
return mode.state == 'visual'
end
local function get_wordcount()
local query = in_visual() and 'visual_words' or 'words'
local wordcount = fn.wordcount()[query]
local wordcount = vim.fn.wordcount()[query]
return fmt('%d words', wordcount)
end
@@ -37,16 +35,16 @@ local function get_item()
common.set_cache_autocmds('hardline_wordcount')
enabled = true
end
if not vim.tbl_contains(options.filetypes, bo.filetype) then
if not vim.tbl_contains(options.filetypes, vim.bo.filetype) then
return ''
end
if fn.line('$') > options.max_lines then
if vim.fn.line('$') > options.max_lines then
return ''
end
if b.hardline_wordcount and not in_visual() then
if vim.b.hardline_wordcount and not in_visual() then
return cache
end
b.hardline_wordcount = true
vim.b.hardline_wordcount = true
cache = get_wordcount()
return cache
end