chore: add stylua to format the codebase, and run on CI (#1055)
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
local git_utils = require'nvim-tree.git.utils'
|
||||
local Runner = require'nvim-tree.git.runner'
|
||||
local git_utils = require "nvim-tree.git.utils"
|
||||
local Runner = require "nvim-tree.git.runner"
|
||||
|
||||
local M = {
|
||||
config = nil,
|
||||
projects = {},
|
||||
cwd_to_project_root = {}
|
||||
cwd_to_project_root = {},
|
||||
}
|
||||
|
||||
function M.reload()
|
||||
@@ -22,7 +22,7 @@ function M.reload()
|
||||
}
|
||||
M.projects[project_root] = {
|
||||
files = git_status,
|
||||
dirs = git_utils.file_status_to_dir_status(git_status, project_root)
|
||||
dirs = git_utils.file_status_to_dir_status(git_status, project_root),
|
||||
}
|
||||
end
|
||||
|
||||
@@ -62,11 +62,11 @@ function M.load_project_status(cwd)
|
||||
project_root = project_root,
|
||||
list_untracked = git_utils.should_show_untracked(project_root),
|
||||
list_ignored = true,
|
||||
timeout = M.config.timeout
|
||||
timeout = M.config.timeout,
|
||||
}
|
||||
M.projects[project_root] = {
|
||||
files = git_status,
|
||||
dirs = git_utils.file_status_to_dir_status(git_status, project_root)
|
||||
dirs = git_utils.file_status_to_dir_status(git_status, project_root),
|
||||
}
|
||||
return M.projects[project_root]
|
||||
end
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
local uv = vim.loop
|
||||
local utils = require'nvim-tree.utils'
|
||||
local utils = require "nvim-tree.utils"
|
||||
|
||||
local Runner = {}
|
||||
Runner.__index = Runner
|
||||
@@ -7,22 +7,22 @@ Runner.__index = Runner
|
||||
function Runner:_parse_status_output(line)
|
||||
local status = line:sub(1, 2)
|
||||
-- removing `"` when git is returning special file status containing spaces
|
||||
local path = line:sub(4, -2):gsub('^"', ''):gsub('"$', '')
|
||||
local path = line:sub(4, -2):gsub('^"', ""):gsub('"$', "")
|
||||
-- replacing slashes if on windows
|
||||
if vim.fn.has('win32') == 1 then
|
||||
path = path:gsub('/', '\\')
|
||||
if vim.fn.has "win32" == 1 then
|
||||
path = path:gsub("/", "\\")
|
||||
end
|
||||
if #status > 0 and #path > 0 then
|
||||
self.output[utils.path_remove_trailing(utils.path_join({self.project_root,path}))] = status
|
||||
self.output[utils.path_remove_trailing(utils.path_join { self.project_root, path })] = status
|
||||
end
|
||||
return #line
|
||||
end
|
||||
|
||||
function Runner:_handle_incoming_data(prev_output, incoming)
|
||||
if incoming and utils.str_find(incoming, '\n') then
|
||||
local prev = prev_output..incoming
|
||||
if incoming and utils.str_find(incoming, "\n") then
|
||||
local prev = prev_output .. incoming
|
||||
local i = 1
|
||||
for line in prev:gmatch('[^\n]*\n') do
|
||||
for line in prev:gmatch "[^\n]*\n" do
|
||||
i = i + self:_parse_status_output(line)
|
||||
end
|
||||
|
||||
@@ -30,10 +30,10 @@ function Runner:_handle_incoming_data(prev_output, incoming)
|
||||
end
|
||||
|
||||
if incoming then
|
||||
return prev_output..incoming
|
||||
return prev_output .. incoming
|
||||
end
|
||||
|
||||
for line in prev_output:gmatch('[^\n]*\n') do
|
||||
for line in prev_output:gmatch "[^\n]*\n" do
|
||||
self._parse_status_output(line)
|
||||
end
|
||||
|
||||
@@ -41,10 +41,10 @@ function Runner:_handle_incoming_data(prev_output, incoming)
|
||||
end
|
||||
|
||||
function Runner:_getopts(stdout_handle)
|
||||
local untracked = self.list_untracked and '-u' or nil
|
||||
local ignored = (self.list_untracked and self.list_ignored) and '--ignored=matching' or '--ignored=no'
|
||||
local untracked = self.list_untracked and "-u" or nil
|
||||
local ignored = (self.list_untracked and self.list_ignored) and "--ignored=matching" or "--ignored=no"
|
||||
return {
|
||||
args = {"--no-optional-locks", "status", "--porcelain=v1", ignored, untracked},
|
||||
args = { "--no-optional-locks", "status", "--porcelain=v1", ignored, untracked },
|
||||
cwd = self.project_root,
|
||||
stdio = { nil, stdout_handle, nil },
|
||||
}
|
||||
@@ -74,14 +74,24 @@ function Runner:_run_git_job()
|
||||
handle, pid = uv.spawn(
|
||||
"git",
|
||||
self:_getopts(stdout),
|
||||
vim.schedule_wrap(function() on_finish() end)
|
||||
vim.schedule_wrap(function()
|
||||
on_finish()
|
||||
end)
|
||||
)
|
||||
|
||||
timer:start(self.timeout, 0, vim.schedule_wrap(function() on_finish() end))
|
||||
timer:start(
|
||||
self.timeout,
|
||||
0,
|
||||
vim.schedule_wrap(function()
|
||||
on_finish()
|
||||
end)
|
||||
)
|
||||
|
||||
local output_leftover = ''
|
||||
local output_leftover = ""
|
||||
local function manage_output(err, data)
|
||||
if err then return end
|
||||
if err then
|
||||
return
|
||||
end
|
||||
output_leftover = self:_handle_incoming_data(output_leftover, data)
|
||||
end
|
||||
|
||||
@@ -89,7 +99,10 @@ function Runner:_run_git_job()
|
||||
end
|
||||
|
||||
function Runner:_wait()
|
||||
while not vim.wait(30, function() return self._done end, 30) do end
|
||||
while not vim.wait(30, function()
|
||||
return self._done
|
||||
end, 30) do
|
||||
end
|
||||
end
|
||||
|
||||
-- This module runs a git process, which will be killed if it takes more than timeout which defaults to 400ms
|
||||
@@ -100,7 +113,7 @@ function Runner.run(opts)
|
||||
list_ignored = opts.list_ignored,
|
||||
timeout = opts.timeout or 400,
|
||||
output = {},
|
||||
_done = false
|
||||
_done = false,
|
||||
}, Runner)
|
||||
|
||||
self:_run_git_job()
|
||||
|
||||
@@ -4,12 +4,12 @@ function M.get_toplevel(cwd)
|
||||
local cmd = "git -C " .. vim.fn.shellescape(cwd) .. " rev-parse --show-toplevel"
|
||||
local toplevel = vim.fn.system(cmd)
|
||||
|
||||
if not toplevel or #toplevel == 0 or toplevel:match('fatal') then
|
||||
if not toplevel or #toplevel == 0 or toplevel:match "fatal" then
|
||||
return nil
|
||||
end
|
||||
|
||||
-- git always returns path with forward slashes
|
||||
if vim.fn.has('win32') == 1 then
|
||||
if vim.fn.has "win32" == 1 then
|
||||
toplevel = toplevel:gsub("/", "\\")
|
||||
end
|
||||
|
||||
@@ -24,25 +24,25 @@ function M.should_show_untracked(cwd)
|
||||
return untracked[cwd]
|
||||
end
|
||||
|
||||
local cmd = "git -C "..cwd.." config --type=bool status.showUntrackedFiles"
|
||||
local cmd = "git -C " .. cwd .. " config --type=bool status.showUntrackedFiles"
|
||||
local has_untracked = vim.fn.system(cmd)
|
||||
untracked[cwd] = vim.trim(has_untracked) ~= 'false'
|
||||
untracked[cwd] = vim.trim(has_untracked) ~= "false"
|
||||
return untracked[cwd]
|
||||
end
|
||||
|
||||
function M.file_status_to_dir_status(status, cwd)
|
||||
local dirs = {}
|
||||
for p, s in pairs(status) do
|
||||
if s ~= '!!' then
|
||||
local modified = vim.fn.fnamemodify(p, ':h')
|
||||
if s ~= "!!" then
|
||||
local modified = vim.fn.fnamemodify(p, ":h")
|
||||
dirs[modified] = s
|
||||
end
|
||||
end
|
||||
|
||||
for dirname, s in pairs(dirs) do
|
||||
local modified = dirname
|
||||
while modified ~= cwd and modified ~= '/' do
|
||||
modified = vim.fn.fnamemodify(modified, ':h')
|
||||
while modified ~= cwd and modified ~= "/" do
|
||||
modified = vim.fn.fnamemodify(modified, ":h")
|
||||
dirs[modified] = s
|
||||
end
|
||||
end
|
||||
@@ -51,4 +51,3 @@ function M.file_status_to_dir_status(status, cwd)
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
|
||||
Reference in New Issue
Block a user