chore: add type hints for git.yazi (#111)
Co-authored-by: sxyazi <sxyazi@gmail.com>
This commit is contained in:
@@ -5,6 +5,7 @@ local WINDOWS = ya.target_family() == "windows"
|
|||||||
-- The code of supported git status,
|
-- The code of supported git status,
|
||||||
-- also used to determine which status to show for directories when they contain different statuses
|
-- also used to determine which status to show for directories when they contain different statuses
|
||||||
-- see `bubble_up`
|
-- see `bubble_up`
|
||||||
|
---@enum CODES
|
||||||
local CODES = {
|
local CODES = {
|
||||||
excluded = 100, -- ignored directory
|
excluded = 100, -- ignored directory
|
||||||
ignored = 6, -- ignored file
|
ignored = 6, -- ignored file
|
||||||
@@ -26,6 +27,8 @@ local PATTERNS = {
|
|||||||
{ "[AD][AD]", CODES.updated },
|
{ "[AD][AD]", CODES.updated },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
---@param line string
|
||||||
|
---@return CODES, string
|
||||||
local function match(line)
|
local function match(line)
|
||||||
local signs = line:sub(1, 2)
|
local signs = line:sub(1, 2)
|
||||||
for _, p in ipairs(PATTERNS) do
|
for _, p in ipairs(PATTERNS) do
|
||||||
@@ -41,9 +44,12 @@ local function match(line)
|
|||||||
else
|
else
|
||||||
return code, path
|
return code, path
|
||||||
end
|
end
|
||||||
|
---@diagnostic disable-next-line: missing-return
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param cwd Url
|
||||||
|
---@return string?
|
||||||
local function root(cwd)
|
local function root(cwd)
|
||||||
local is_worktree = function(url)
|
local is_worktree = function(url)
|
||||||
local file, head = io.open(tostring(url)), nil
|
local file, head = io.open(tostring(url)), nil
|
||||||
@@ -64,6 +70,8 @@ local function root(cwd)
|
|||||||
until not cwd
|
until not cwd
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param changed Changes
|
||||||
|
---@return Changes
|
||||||
local function bubble_up(changed)
|
local function bubble_up(changed)
|
||||||
local new, empty = {}, Url("")
|
local new, empty = {}, Url("")
|
||||||
for path, code in pairs(changed) do
|
for path, code in pairs(changed) do
|
||||||
@@ -79,6 +87,10 @@ local function bubble_up(changed)
|
|||||||
return new
|
return new
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param excluded string[]
|
||||||
|
---@param cwd Url
|
||||||
|
---@param repo Url
|
||||||
|
---@return Changes
|
||||||
local function propagate_down(excluded, cwd, repo)
|
local function propagate_down(excluded, cwd, repo)
|
||||||
local new, rel = {}, cwd:strip_prefix(repo)
|
local new, rel = {}, cwd:strip_prefix(repo)
|
||||||
for _, path in ipairs(excluded) do
|
for _, path in ipairs(excluded) do
|
||||||
@@ -95,7 +107,12 @@ local function propagate_down(excluded, cwd, repo)
|
|||||||
return new
|
return new
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param cwd string
|
||||||
|
---@param repo string
|
||||||
|
---@param changed Changes
|
||||||
local add = ya.sync(function(st, cwd, repo, changed)
|
local add = ya.sync(function(st, cwd, repo, changed)
|
||||||
|
---@cast st State
|
||||||
|
|
||||||
st.dirs[cwd] = repo
|
st.dirs[cwd] = repo
|
||||||
st.repos[repo] = st.repos[repo] or {}
|
st.repos[repo] = st.repos[repo] or {}
|
||||||
for path, code in pairs(changed) do
|
for path, code in pairs(changed) do
|
||||||
@@ -116,7 +133,10 @@ local add = ya.sync(function(st, cwd, repo, changed)
|
|||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
---@param cwd string
|
||||||
local remove = ya.sync(function(st, cwd)
|
local remove = ya.sync(function(st, cwd)
|
||||||
|
---@cast st State
|
||||||
|
|
||||||
local repo = st.dirs[cwd]
|
local repo = st.dirs[cwd]
|
||||||
if not repo then
|
if not repo then
|
||||||
return
|
return
|
||||||
@@ -141,9 +161,11 @@ local remove = ya.sync(function(st, cwd)
|
|||||||
st.repos[repo] = nil
|
st.repos[repo] = nil
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
---@param st State
|
||||||
|
---@param opts Options
|
||||||
local function setup(st, opts)
|
local function setup(st, opts)
|
||||||
st.dirs = {} -- Mapping between a directory and its corresponding repository
|
st.dirs = {}
|
||||||
st.repos = {} -- Mapping between a repository and the status of each of its files
|
st.repos = {}
|
||||||
|
|
||||||
opts = opts or {}
|
opts = opts or {}
|
||||||
opts.order = opts.order or 1500
|
opts.order = opts.order or 1500
|
||||||
@@ -184,6 +206,7 @@ local function setup(st, opts)
|
|||||||
end, opts.order)
|
end, opts.order)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@type UnstableFetcher
|
||||||
local function fetch(_, job)
|
local function fetch(_, job)
|
||||||
local cwd = job.files[1].url.base
|
local cwd = job.files[1].url.base
|
||||||
local repo = root(cwd)
|
local repo = root(cwd)
|
||||||
|
|||||||
12
git.yazi/types.lua
Normal file
12
git.yazi/types.lua
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
---@class State
|
||||||
|
---@field dirs table<string, string|CODES> Mapping between a directory and its corresponding repository
|
||||||
|
---@field repos table<string, Changes> Mapping between a repository and the status of each of its files
|
||||||
|
|
||||||
|
---@class Options
|
||||||
|
---@field order number The order in which the status is displayed
|
||||||
|
---@field renamed boolean Whether to include renamed files in the status (or treat them as modified)
|
||||||
|
|
||||||
|
-- TODO: move this to `types.yazi` once it's get stable
|
||||||
|
---@alias UnstableFetcher fun(self: unknown, job: { files: File[] })
|
||||||
|
|
||||||
|
---@alias Changes table<string, CODES>
|
||||||
Reference in New Issue
Block a user