chore: add stylua to format the codebase, and run on CI (#1055)

This commit is contained in:
Kiyan
2022-03-06 17:33:30 +01:00
committed by GitHub
parent 76d181d480
commit 0816064a8b
43 changed files with 871 additions and 732 deletions

View File

@@ -1,36 +1,34 @@
local M = {}
function M.compute_lines()
local help_lines = {'HELP'}
local help_hl = {{'NvimTreeRootFolder', 0, 0, #help_lines[1]}}
local help_lines = { "HELP" }
local help_hl = { { "NvimTreeRootFolder", 0, 0, #help_lines[1] } }
local mappings = vim.tbl_filter(function(v)
return (v.cb ~= nil and v.cb ~= "") or (v.action ~= nil and v.action ~= "")
end, require'nvim-tree.actions'.mappings)
end, require("nvim-tree.actions").mappings)
local processed = {}
for _, b in pairs(mappings) do
local cb = b.cb
local key = b.key
local name
if cb and cb:sub(1,35) == require'nvim-tree.config'.nvim_tree_callback('test'):sub(1,35) then
name = cb:match("'[^']+'[^']*$")
name = name:match("'[^']+'")
if cb and cb:sub(1, 35) == require("nvim-tree.config").nvim_tree_callback("test"):sub(1, 35) then
name = cb:match "'[^']+'[^']*$"
name = name:match "'[^']+'"
elseif b.action then
name = b.action
else
name = (b.name ~= nil) and b.name or cb
name = '"' .. name .. '"'
end
table.insert(processed, {key, name, true})
table.insert(processed, { key, name, true })
end
table.sort(processed, function(a,b)
return (a[3] == b[3]
and (a[2] < b[2] or (a[2] == b[2] and #a[1] < #b[1])))
or (a[3] and not b[3])
table.sort(processed, function(a, b)
return (a[3] == b[3] and (a[2] < b[2] or (a[2] == b[2] and #a[1] < #b[1]))) or (a[3] and not b[3])
end)
local num = 0
for _, val in pairs(processed) do
local keys = type(val[1]) == "string" and {val[1]} or val[1]
local keys = type(val[1]) == "string" and { val[1] } or val[1]
local map_name = val[2]
local builtin = val[3]
for _, key in pairs(keys) do
@@ -39,10 +37,10 @@ function M.compute_lines()
table.insert(help_lines, bind_string)
local hl_len = math.max(6, string.len(key)) + 2
table.insert(help_hl, {'NvimTreeFolderName', num, 0, hl_len})
table.insert(help_hl, { "NvimTreeFolderName", num, 0, hl_len })
if not builtin then
table.insert(help_hl, {'NvimTreeFileRenamed', num, hl_len, -1})
table.insert(help_hl, { "NvimTreeFileRenamed", num, hl_len, -1 })
end
end
end

View File

@@ -12,7 +12,7 @@ function M.get_config()
renamed = "",
untracked = "",
deleted = "",
ignored = ""
ignored = "",
},
folder_icons = {
arrow_closed = "",
@@ -47,14 +47,14 @@ function M.get_config()
end
end
local has_devicons = pcall(require, 'nvim-web-devicons')
local has_devicons = pcall(require, "nvim-web-devicons")
return {
show_file_icon = show_icons.files == 1,
show_folder_icon = show_icons.folders == 1,
show_git_icon = show_icons.git == 1,
show_folder_arrows = show_icons.folder_arrows == 1,
has_devicons = has_devicons,
icons = icons
icons = icons,
}
end

View File

@@ -1,27 +1,29 @@
local utils = require'nvim-tree.utils'
local view = require'nvim-tree.view'
local _padding = require'nvim-tree.renderer.padding'
local _help = require'nvim-tree.renderer.help'
local _icons = require'nvim-tree.renderer.icons'
local utils = require "nvim-tree.utils"
local view = require "nvim-tree.view"
local _padding = require "nvim-tree.renderer.padding"
local _help = require "nvim-tree.renderer.help"
local _icons = require "nvim-tree.renderer.icons"
local api = vim.api
local lines = {}
local hl = {}
local index = 0
local namespace_id = api.nvim_create_namespace('NvimTreeHighlights')
local namespace_id = api.nvim_create_namespace "NvimTreeHighlights"
local icon_state = _icons.get_config()
local should_hl_opened_files = (vim.g.nvim_tree_highlight_opened_files or 0) ~= 0
local get_folder_icon = function() return "" end
local get_folder_icon = function()
return ""
end
local function get_trailing_length()
return vim.g.nvim_tree_add_trailing and 1 or 0
end
local set_folder_hl = function(line, depth, git_icon_len, _, hl_group)
table.insert(hl, {hl_group, line, depth+git_icon_len, -1})
table.insert(hl, { hl_group, line, depth + git_icon_len, -1 })
end
local icon_padding = vim.g.nvim_tree_icon_padding or " "
@@ -46,19 +48,21 @@ if icon_state.show_folder_icon then
n = icon_state.icons.folder_icons.empty
end
end
return n..icon_padding
return n .. icon_padding
end
set_folder_hl = function(line, depth, icon_len, name_len, hl_group)
table.insert(hl, {hl_group, line, depth+icon_len, depth+icon_len+name_len+get_trailing_length()})
local hl_icon = (vim.g.nvim_tree_highlight_opened_files or 0) ~= 0 and hl_group or 'NvimTreeFolderIcon'
table.insert(hl, {hl_icon, line, depth, depth+icon_len})
table.insert(hl, { hl_group, line, depth + icon_len, depth + icon_len + name_len + get_trailing_length() })
local hl_icon = (vim.g.nvim_tree_highlight_opened_files or 0) ~= 0 and hl_group or "NvimTreeFolderIcon"
table.insert(hl, { hl_icon, line, depth, depth + icon_len })
end
end
local get_file_icon = function() return "" end
local get_file_icon = function()
return ""
end
if icon_state.show_file_icon then
if icon_state.has_devicons then
local web_devicons = require'nvim-web-devicons'
local web_devicons = require "nvim-web-devicons"
get_file_icon = function(fname, extension, line, depth)
local icon, hl_group = web_devicons.get_icon(fname, extension)
@@ -67,36 +71,42 @@ if icon_state.show_file_icon then
if hl_group then
table.insert(hl, { hl_group, line, depth, depth + #icon + 1 })
end
return icon..icon_padding
return icon .. icon_padding
elseif string.match(extension, "%.(.*)") then
-- If there are more extensions to the file, try to grab the icon for them recursively
return get_file_icon(fname, string.match(extension, "%.(.*)"), line, depth)
else
return #icon_state.icons.default > 0 and icon_state.icons.default..icon_padding or ""
return #icon_state.icons.default > 0 and icon_state.icons.default .. icon_padding or ""
end
end
else
get_file_icon = function()
return #icon_state.icons.default > 0 and icon_state.icons.default..icon_padding or ""
return #icon_state.icons.default > 0 and icon_state.icons.default .. icon_padding or ""
end
end
end
local get_symlink_icon = function() return icon_state.icons.symlink end
local get_symlink_icon = function()
return icon_state.icons.symlink
end
if icon_state.show_file_icon then
get_symlink_icon = function()
return #icon_state.icons.symlink > 0 and icon_state.icons.symlink..icon_padding or ""
return #icon_state.icons.symlink > 0 and icon_state.icons.symlink .. icon_padding or ""
end
end
local get_special_icon = function() return "" end
local get_special_icon = function()
return ""
end
if icon_state.show_file_icon then
get_special_icon = function()
return #icon_state.icons.default > 0 and icon_state.icons.default..icon_padding or ""
return #icon_state.icons.default > 0 and icon_state.icons.default .. icon_padding or ""
end
end
local get_git_icons = function() return "" end
local get_git_icons = function()
return ""
end
local get_git_hl = function() end
if vim.g.nvim_tree_git_hl == 1 then
@@ -109,11 +119,11 @@ if vim.g.nvim_tree_git_hl == 1 then
[" T"] = { { hl = "NvimTreeFileDirty" } },
["MM"] = {
{ hl = "NvimTreeFileStaged" },
{ hl = "NvimTreeFileDirty" }
{ hl = "NvimTreeFileDirty" },
},
["A "] = {
{ hl = "NvimTreeFileStaged" },
{ hl = "NvimTreeFileNew" }
{ hl = "NvimTreeFileNew" },
},
["AU"] = {
{ hl = "NvimTreeFileMerge" },
@@ -122,7 +132,7 @@ if vim.g.nvim_tree_git_hl == 1 then
-- not sure about this one
["AA"] = {
{ hl = "NvimTreeFileMerge" },
{ hl = "NvimTreeFileStaged" }
{ hl = "NvimTreeFileStaged" },
},
["AD"] = {
{ hl = "NvimTreeFileStaged" },
@@ -133,7 +143,7 @@ if vim.g.nvim_tree_git_hl == 1 then
["AM"] = {
{ hl = "NvimTreeFileStaged" },
{ hl = "NvimTreeFileNew" },
{ hl = "NvimTreeFileDirty" }
{ hl = "NvimTreeFileDirty" },
},
["??"] = { { hl = "NvimTreeFileNew" } },
["R "] = { { hl = "NvimTreeFileRenamed" } },
@@ -145,11 +155,11 @@ if vim.g.nvim_tree_git_hl == 1 then
["RD"] = { { hl = "NvimTreeFileDeleted" } },
["D "] = {
{ hl = "NvimTreeFileDeleted" },
{ hl = "NvimTreeFileStaged" }
{ hl = "NvimTreeFileStaged" },
},
["DU"] = {
{ hl = "NvimTreeFileDeleted" },
{ hl = "NvimTreeFileMerge" }
{ hl = "NvimTreeFileMerge" },
},
[" A"] = { { hl = "none" } },
["RM"] = { { hl = "NvimTreeFileRenamed" } },
@@ -159,12 +169,18 @@ if vim.g.nvim_tree_git_hl == 1 then
}
get_git_hl = function(node)
local git_status = node.git_status
if not git_status then return end
if not git_status then
return
end
local icons = git_hl[git_status]
if icons == nil then
utils.warn('Unrecognized git state "'..git_status..'". Please open up an issue on https://github.com/kyazdani42/nvim-tree.lua/issues with this message.')
utils.warn(
'Unrecognized git state "'
.. git_status
.. '". Please open up an issue on https://github.com/kyazdani42/nvim-tree.lua/issues with this message.'
)
icons = git_hl.dirty
end
@@ -184,7 +200,7 @@ if icon_state.show_git_icon then
[" T"] = { { icon = icon_state.icons.git_icons.unstaged, hl = "NvimTreeGitDirty" } },
["MM"] = {
{ icon = icon_state.icons.git_icons.staged, hl = "NvimTreeGitStaged" },
{ icon = icon_state.icons.git_icons.unstaged, hl = "NvimTreeGitDirty" }
{ icon = icon_state.icons.git_icons.unstaged, hl = "NvimTreeGitDirty" },
},
["MD"] = {
{ icon = icon_state.icons.git_icons.staged, hl = "NvimTreeGitStaged" },
@@ -209,7 +225,7 @@ if icon_state.show_git_icon then
},
["AM"] = {
{ icon = icon_state.icons.git_icons.staged, hl = "NvimTreeGitStaged" },
{ icon = icon_state.icons.git_icons.unstaged, hl = "NvimTreeGitDirty" }
{ icon = icon_state.icons.git_icons.unstaged, hl = "NvimTreeGitDirty" },
},
["??"] = { { icon = icon_state.icons.git_icons.untracked, hl = "NvimTreeGitNew" } },
["R "] = { { icon = icon_state.icons.git_icons.renamed, hl = "NvimTreeGitRenamed" } },
@@ -235,20 +251,26 @@ if icon_state.show_git_icon then
get_git_icons = function(node, line, depth, icon_len)
local git_status = node.git_status
if not git_status then return "" end
if not git_status then
return ""
end
local icon = ""
local icons = git_icon_state[git_status]
if not icons then
if vim.g.nvim_tree_git_hl ~= 1 then
utils.warn('Unrecognized git state "'..git_status..'". Please open up an issue on https://github.com/kyazdani42/nvim-tree.lua/issues with this message.')
utils.warn(
'Unrecognized git state "'
.. git_status
.. '". Please open up an issue on https://github.com/kyazdani42/nvim-tree.lua/issues with this message.'
)
end
icons = git_icon_state.dirty
end
for _, v in ipairs(icons) do
if #v.icon > 0 then
table.insert(hl, { v.hl, line, depth+icon_len+#icon, depth+icon_len+#icon+#v.icon })
icon = icon..v.icon..icon_padding
table.insert(hl, { v.hl, line, depth + icon_len + #icon, depth + icon_len + #icon + #v.icon })
icon = icon .. v.icon .. icon_padding
end
end
@@ -264,23 +286,24 @@ local picture = {
}
local function update_draw_data(tree, depth, markers)
local root_folder_modifier = vim.g.nvim_tree_root_folder_modifier or ':~'
local special = vim.g.nvim_tree_special_files or {
["Cargo.toml"] = true,
Makefile = true,
["README.md"] = true,
["readme.md"] = true,
}
local root_folder_modifier = vim.g.nvim_tree_root_folder_modifier or ":~"
local special = vim.g.nvim_tree_special_files
or {
["Cargo.toml"] = true,
Makefile = true,
["README.md"] = true,
["readme.md"] = true,
}
local hide_root_folder = view.View.hide_root_folder
if tree.cwd and tree.cwd ~= '/' and not hide_root_folder then
local root_name = utils.path_join({
if tree.cwd and tree.cwd ~= "/" and not hide_root_folder then
local root_name = utils.path_join {
utils.path_remove_trailing(vim.fn.fnamemodify(tree.cwd, root_folder_modifier)),
".."
})
"..",
}
table.insert(lines, root_name)
table.insert(hl, {'NvimTreeRootFolder', index, 0, string.len(root_name)})
table.insert(hl, { "NvimTreeRootFolder", index, 0, string.len(root_name) })
index = 1
end
@@ -288,7 +311,7 @@ local function update_draw_data(tree, depth, markers)
local padding = _padding.get_padding(depth, idx, tree, node, markers)
local offset = string.len(padding)
if depth > 0 then
table.insert(hl, { 'NvimTreeIndentMarker', index, 0, offset })
table.insert(hl, { "NvimTreeIndentMarker", index, 0, offset })
end
local git_hl = get_git_hl(node)
@@ -305,63 +328,71 @@ local function update_draw_data(tree, depth, markers)
name = name .. "/" .. next.name
next = next.group_next
end
if not has_children then folder_hl = "NvimTreeEmptyFolderName" end
if node.open then folder_hl = "NvimTreeOpenedFolderName" end
if not has_children then
folder_hl = "NvimTreeEmptyFolderName"
end
if node.open then
folder_hl = "NvimTreeOpenedFolderName"
end
if special[node.absolute_path] then
folder_hl = "NvimTreeSpecialFolderName"
end
set_folder_hl(index, offset, #icon+#git_icon, #name, folder_hl)
set_folder_hl(index, offset, #icon + #git_icon, #name, folder_hl)
if git_hl then
set_folder_hl(index, offset, #icon+#git_icon, #name, git_hl)
set_folder_hl(index, offset, #icon + #git_icon, #name, git_hl)
end
index = index + 1
if node.open then
table.insert(lines, padding..icon..git_icon..name..(vim.g.nvim_tree_add_trailing == 1 and '/' or ''))
table.insert(lines, padding .. icon .. git_icon .. name .. (vim.g.nvim_tree_add_trailing == 1 and "/" or ""))
update_draw_data(node, depth + 2, markers)
else
table.insert(lines, padding..icon..git_icon..name..(vim.g.nvim_tree_add_trailing == 1 and '/' or ''))
table.insert(lines, padding .. icon .. git_icon .. name .. (vim.g.nvim_tree_add_trailing == 1 and "/" or ""))
end
elseif node.link_to then
local icon = get_symlink_icon()
local link_hl = git_hl or 'NvimTreeSymlink'
local arrow = vim.g.nvim_tree_symlink_arrow or ''
local link_hl = git_hl or "NvimTreeSymlink"
local arrow = vim.g.nvim_tree_symlink_arrow or ""
table.insert(hl, { link_hl, index, offset, -1 })
table.insert(lines, padding..icon..node.name..arrow..node.link_to)
table.insert(lines, padding .. icon .. node.name .. arrow .. node.link_to)
index = index + 1
else
local icon
local git_icons
if special[node.absolute_path] or special[node.name] then
icon = get_special_icon()
git_icons = get_git_icons(node, index, offset, 0)
table.insert(hl, {'NvimTreeSpecialFile', index, offset+#git_icons, -1})
table.insert(hl, { "NvimTreeSpecialFile", index, offset + #git_icons, -1 })
else
icon = get_file_icon(node.name, node.extension, index, offset)
git_icons = get_git_icons(node, index, offset, #icon)
end
table.insert(lines, padding..icon..git_icons..node.name)
table.insert(lines, padding .. icon .. git_icons .. node.name)
if node.executable then
table.insert(hl, {'NvimTreeExecFile', index, offset+#icon+#git_icons, -1 })
table.insert(hl, { "NvimTreeExecFile", index, offset + #icon + #git_icons, -1 })
elseif picture[node.extension] then
table.insert(hl, {'NvimTreeImageFile', index, offset+#icon+#git_icons, -1 })
table.insert(hl, { "NvimTreeImageFile", index, offset + #icon + #git_icons, -1 })
end
if should_hl_opened_files then
if vim.fn.bufloaded(node.absolute_path) > 0 then
if vim.g.nvim_tree_highlight_opened_files == 1 then
table.insert(hl, {'NvimTreeOpenedFile', index, offset, offset+#icon }) -- highlight icon only
table.insert(hl, { "NvimTreeOpenedFile", index, offset, offset + #icon }) -- highlight icon only
elseif vim.g.nvim_tree_highlight_opened_files == 2 then
table.insert(hl, {'NvimTreeOpenedFile', index, offset+#icon+#git_icons, offset+#icon+#git_icons+#node.name }) -- highlight name only
table.insert(hl, {
"NvimTreeOpenedFile",
index,
offset + #icon + #git_icons,
offset + #icon + #git_icons + #node.name,
}) -- highlight name only
elseif vim.g.nvim_tree_highlight_opened_files == 3 then
table.insert(hl, {'NvimTreeOpenedFile', index, offset, -1 }) -- highlight whole line
table.insert(hl, { "NvimTreeOpenedFile", index, offset, -1 }) -- highlight whole line
end
end
end
if git_hl then
table.insert(hl, {git_hl, index, offset+#icon+#git_icons, -1 })
table.insert(hl, { git_hl, index, offset + #icon + #git_icons, -1 })
end
index = index + 1
end
@@ -384,8 +415,7 @@ function M.draw()
hl = {}
icon_state = _icons.get_config()
local show_arrows =
vim.g.nvim_tree_indent_markers ~= 1
local show_arrows = vim.g.nvim_tree_indent_markers ~= 1
and icon_state.show_folder_icon
and icon_state.show_folder_arrows
_padding.reload_padding_function()
@@ -394,10 +424,10 @@ function M.draw()
if view.is_help_ui() then
lines, hl = _help.compute_lines()
end
api.nvim_buf_set_option(bufnr, 'modifiable', true)
api.nvim_buf_set_option(bufnr, "modifiable", true)
api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
M.render_hl(bufnr)
api.nvim_buf_set_option(bufnr, 'modifiable', false)
api.nvim_buf_set_option(bufnr, "modifiable", false)
if cursor and #lines >= cursor[1] then
api.nvim_win_set_cursor(view.get_winnr(), cursor)
@@ -405,7 +435,9 @@ function M.draw()
end
function M.render_hl(bufnr)
if not bufnr or not api.nvim_buf_is_loaded(bufnr) then return end
if not bufnr or not api.nvim_buf_is_loaded(bufnr) then
return
end
api.nvim_buf_clear_namespace(bufnr, namespace_id, 0, -1)
for _, data in ipairs(hl) do
api.nvim_buf_add_highlight(bufnr, namespace_id, data[1], data[2], data[3], data[4])

View File

@@ -1,31 +1,31 @@
local M = {}
function M.get_padding(depth)
return string.rep(' ', depth)
return string.rep(" ", depth)
end
local function get_padding_arrows(icon_state)
return function(depth, _, _, node)
if node.nodes then
local icon = icon_state.icons.folder_icons[node.open and 'arrow_open' or 'arrow_closed']
return string.rep(' ', depth - 2)..icon..' '
local icon = icon_state.icons.folder_icons[node.open and "arrow_open" or "arrow_closed"]
return string.rep(" ", depth - 2) .. icon .. " "
end
return string.rep(' ', depth)
return string.rep(" ", depth)
end
end
local function get_padding_indent_markers(depth, idx, tree, _, markers)
local padding = ""
if depth ~= 0 then
local rdepth = depth/2
local rdepth = depth / 2
markers[rdepth] = idx ~= #tree.nodes
for i=1,rdepth do
for i = 1, rdepth do
if idx == #tree.nodes and i == rdepth then
padding = padding..''
padding = padding .. ""
elseif markers[i] then
padding = padding..''
padding = padding .. ""
else
padding = padding..' '
padding = padding .. " "
end
end
end
@@ -33,7 +33,7 @@ local function get_padding_indent_markers(depth, idx, tree, _, markers)
end
function M.reload_padding_function()
local icon_state = require'nvim-tree.renderer.icons'.get_config()
local icon_state = require("nvim-tree.renderer.icons").get_config()
if icon_state.show_folder_icon and icon_state.show_folder_arrows then
M.get_padding = get_padding_arrows(icon_state)