chore: add type hints for git.yazi (#111)

Co-authored-by: sxyazi <sxyazi@gmail.com>
This commit is contained in:
PFiS
2025-07-05 19:01:07 +08:00
committed by GitHub
parent e5f00e2716
commit cbc4450a6c
2 changed files with 37 additions and 2 deletions

View File

@@ -5,6 +5,7 @@ local WINDOWS = ya.target_family() == "windows"
-- The code of supported git status,
-- also used to determine which status to show for directories when they contain different statuses
-- see `bubble_up`
---@enum CODES
local CODES = {
excluded = 100, -- ignored directory
ignored = 6, -- ignored file
@@ -26,6 +27,8 @@ local PATTERNS = {
{ "[AD][AD]", CODES.updated },
}
---@param line string
---@return CODES, string
local function match(line)
local signs = line:sub(1, 2)
for _, p in ipairs(PATTERNS) do
@@ -41,9 +44,12 @@ local function match(line)
else
return code, path
end
---@diagnostic disable-next-line: missing-return
end
end
---@param cwd Url
---@return string?
local function root(cwd)
local is_worktree = function(url)
local file, head = io.open(tostring(url)), nil
@@ -64,6 +70,8 @@ local function root(cwd)
until not cwd
end
---@param changed Changes
---@return Changes
local function bubble_up(changed)
local new, empty = {}, Url("")
for path, code in pairs(changed) do
@@ -79,6 +87,10 @@ local function bubble_up(changed)
return new
end
---@param excluded string[]
---@param cwd Url
---@param repo Url
---@return Changes
local function propagate_down(excluded, cwd, repo)
local new, rel = {}, cwd:strip_prefix(repo)
for _, path in ipairs(excluded) do
@@ -95,7 +107,12 @@ local function propagate_down(excluded, cwd, repo)
return new
end
---@param cwd string
---@param repo string
---@param changed Changes
local add = ya.sync(function(st, cwd, repo, changed)
---@cast st State
st.dirs[cwd] = repo
st.repos[repo] = st.repos[repo] or {}
for path, code in pairs(changed) do
@@ -116,7 +133,10 @@ local add = ya.sync(function(st, cwd, repo, changed)
end
end)
---@param cwd string
local remove = ya.sync(function(st, cwd)
---@cast st State
local repo = st.dirs[cwd]
if not repo then
return
@@ -141,9 +161,11 @@ local remove = ya.sync(function(st, cwd)
st.repos[repo] = nil
end)
---@param st State
---@param opts Options
local function setup(st, opts)
st.dirs = {} -- Mapping between a directory and its corresponding repository
st.repos = {} -- Mapping between a repository and the status of each of its files
st.dirs = {}
st.repos = {}
opts = opts or {}
opts.order = opts.order or 1500
@@ -184,6 +206,7 @@ local function setup(st, opts)
end, opts.order)
end
---@type UnstableFetcher
local function fetch(_, job)
local cwd = job.files[1].url.base
local repo = root(cwd)

12
git.yazi/types.lua Normal file
View 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>