Skip to content

Commit

Permalink
fix: typing
Browse files Browse the repository at this point in the history
  • Loading branch information
lewis6991 committed Aug 15, 2023
1 parent 4b3165b commit 16ad164
Show file tree
Hide file tree
Showing 14 changed files with 191 additions and 63 deletions.
10 changes: 9 additions & 1 deletion .luarc.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@
"diagnostics": {
"groupFileStatus": {
"strict": "Opened",
"strong": "Opened"
"strong": "Opened",
"ambiguity" : "Opened",
"duplicate" : "Opened",
"global" : "Opened",
"luadoc" : "Opened",
"redefined" : "Opened",
"type-check" : "Opened",
"unbalanced" : "Opened",
"unused" : "Opened"
},
"groupSeverity": {
"strong": "Warning",
Expand Down
8 changes: 7 additions & 1 deletion gen_help.lua
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ end
--- @param name_pad? integer
--- @return string[]
local function render_param_or_return(name, ty, desc, name_pad)
ty = ty:gsub('Gitsigns%.%w+', 'table')

name_pad = name_pad and (name_pad + 3) or 0
local name_str --- @type string

Expand Down Expand Up @@ -286,8 +288,12 @@ end
--- @param block string[]
--- @param params {[1]: string, [2]: string, [3]: string[]}[]
--- @param returns {[1]: string, [2]: string, [3]: string[]}[]
--- @return string[]
--- @return string[]?
local function render_block(header, block, params, returns)
if vim.startswith(header, '_') then
return
end

local res = { header }
list_extend(res, block)

Expand Down
2 changes: 1 addition & 1 deletion lua/gitsigns.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ local uv = vim.loop

local M = {}

local cwd_watcher ---@type uv_fs_event_t?
local cwd_watcher ---@type uv.uv_fs_event_t?

local update_cwd_head = void(function()
local paths = vim.fs.find('.git', {
Expand Down
78 changes: 50 additions & 28 deletions lua/gitsigns/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ local M = {}
--- @field vertical boolean
--- @field split 'aboveleft'|'belowright'|'topleft'|'botright'

--- @class Gitsigns.CmdArgs
--- @field vertical? boolean
--- @field split? boolean
--- @field global? boolean

--- @class Gitsigns.CmdParams
--- @field range integer
--- @field line1 integer
Expand All @@ -31,7 +36,7 @@ local M = {}
--- @field smods Gitsigns.CmdParams.Smods

-- Variations of functions from M which are used for the Gitsigns command
--- @type table<string,fun(args: table, params: Gitsigns.CmdParams)>
--- @type table<string,fun(args: Gitsigns.CmdArgs, params: Gitsigns.CmdParams)>
local C = {}

local CP = {}
Expand All @@ -46,6 +51,7 @@ local function complete_heads(arglead)
vim.fn.systemlist({ 'git', 'rev-parse', '--symbolic', '--branches', '--tags', '--remotes' })
return vim.tbl_filter(
--- @param x string
--- @return boolean
function(x)
return vim.startswith(x, arglead)
end,
Expand Down Expand Up @@ -426,24 +432,44 @@ M.reset_buffer_index = void(function()
update(bufnr)
end)

--- @class Gitsigns.NavOpts
--- @field wrap boolean
--- @field foldopen boolean
--- @field navigation_message boolean
--- @field greedy boolean
--- @field preview boolean

--- @param x string
--- @param word string
--- @return boolean
local function findword(x, word)
return string.find(x, '%f[%w_]'..word..'%f[^%w_]') ~= nil
end

--- @param opts? Gitsigns.NavOpts
--- @return Gitsigns.NavOpts
local function process_nav_opts(opts)
opts = opts or {}

-- show navigation message
if opts.navigation_message == nil then
opts.navigation_message = not vim.opt.shortmess:get().S
opts.navigation_message = vim.o.shortmess:find('S') == nil
end

-- wrap around
if opts.wrap == nil then
opts.wrap = vim.opt.wrapscan:get()
opts.wrap = vim.o.wrapscan
end

if opts.foldopen == nil then
opts.foldopen = vim.tbl_contains(vim.opt.foldopen:get(), 'search')
opts.foldopen = findword(vim.o.foldopen, 'search')
end

if opts.greedy == nil then
opts.greedy = true
end

return opts
end

-- Defer function to the next main event
Expand All @@ -462,8 +488,10 @@ local function has_preview_inline(bufnr)
return #api.nvim_buf_get_extmarks(bufnr, ns_inline, 0, -1, { limit = 1 }) > 0
end

local nav_hunk = void(function(opts)
process_nav_opts(opts)
--- @param opts? Gitsigns.NavOpts
--- @param forwards boolean
local nav_hunk = void(function(opts, forwards)
opts = process_nav_opts(opts)
local bufnr = current_buf()
local bcache = cache[bufnr]
if not bcache then
Expand All @@ -483,7 +511,7 @@ local nav_hunk = void(function(opts)
end
local line = api.nvim_win_get_cursor(0)[1]

local hunk, index = Hunks.find_nearest_hunk(line, hunks, opts.forwards, opts.wrap)
local hunk, index = Hunks.find_nearest_hunk(line, hunks, forwards, opts.wrap)

if hunk == nil then
if opts.navigation_message then
Expand All @@ -492,7 +520,7 @@ local nav_hunk = void(function(opts)
return
end

local row = opts.forwards and hunk.added.start or hunk.vend
local row = forwards and hunk.added.start or hunk.vend
if row then
-- Handle topdelete
if row == 0 then
Expand Down Expand Up @@ -544,9 +572,7 @@ end)
--- Only navigate between non-contiguous hunks. Only useful if
--- 'diff_opts' contains `linematch`. Defaults to `true`.
M.next_hunk = function(opts)
opts = opts or {}
opts.forwards = true
nav_hunk(opts)
nav_hunk(opts, true)
end

--- Jump to the previous hunk in the current buffer. If a hunk preview
Expand All @@ -556,9 +582,7 @@ end
--- Parameters: ~
--- See |gitsigns.next_hunk()|.
M.prev_hunk = function(opts)
opts = opts or {}
opts.forwards = false
nav_hunk(opts)
nav_hunk(opts, false)
end

--- @param fmt {[1]: string, [2]: string}[][]
Expand Down Expand Up @@ -629,7 +653,7 @@ local function hlmarks_for_hunk(hunk, hl)
return hls
end

--- @param fmt {[1]: string, [2]: string}[][]
--- @param fmt {[1]: string, [2]: string|Gitsigns.HlMark[]}[][]
--- @param hunk Gitsigns.Hunk.Hunk
local function insert_hunk_hlmarks(fmt, hunk)
for _, line in ipairs(fmt) do
Expand Down Expand Up @@ -1209,29 +1233,27 @@ M.get_actions = function()
end
local hunk = get_cursor_hunk()

--- @type function[]
--- @type string[]
local actions_l = {}

local function add_action(action)
actions_l[#actions_l + 1] = action
end

if hunk then
add_action('stage_hunk')
add_action('reset_hunk')
add_action('preview_hunk')
add_action('select_hunk')
vim.list_extend(actions_l, {
'stage_hunk',
'reset_hunk',
'preview_hunk',
'select_hunk'
})
else
add_action('blame_line')
actions_l[#actions_l + 1] = 'blame_line'
end

if not vim.tbl_isempty(bcache.staged_diffs) then
add_action('undo_stage_hunk')
actions_l[#actions_l + 1] = 'undo_stage_hunk'
end

local actions = {}
local actions = {} --- @type table<string,function>
for _, a in ipairs(actions_l) do
actions[a] = M[a]
actions[a] = M[a] --[[@as function]]
end

return actions
Expand Down
7 changes: 5 additions & 2 deletions lua/gitsigns/async.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ local Async_T = {}

-- Store all the async threads in a weak table so we don't prevent them from
-- being garbage collected
--- @type table<thread,uv_handle_t>
--- @type table<thread,uv.uv_handle_t>
local handles = setmetatable({}, { __mode = 'k' })

--- Returns whether the current execution context is async.
Expand Down Expand Up @@ -147,6 +147,7 @@ end
---since it is non-blocking
---@generic F: function
---@param func F
---@param argc integer
---@return F
function M.create(func, argc)
argc = argc or 0
Expand All @@ -162,7 +163,9 @@ end
---Use this to create a function which executes in an async context but
---called from a non-async context. Inherently this cannot return anything
---since it is non-blocking
---@param func async fun(...)
---@generic F: function
---@param func F
---@return async F
function M.void(func)
return function(...)
if M.running() then
Expand Down
19 changes: 11 additions & 8 deletions lua/gitsigns/attach.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ local function parse_fugitive_uri(name)
end

--- @param name string
--- @return string? buffer
--- @return string buffer
--- @return string? commit
local function parse_gitsigns_uri(name)
-- TODO(lewis6991): Support submodules
Expand Down Expand Up @@ -77,18 +77,20 @@ local function get_buf_path(bufnr)
if vim.startswith(file, 'fugitive://') then
local path, commit = parse_fugitive_uri(file)
dprintf("Fugitive buffer for file '%s' from path '%s'", path, file)
path = uv.fs_realpath(path)
if path then
return path, commit
local realpath = uv.fs_realpath(path)
if realpath then
return realpath, commit
end
end
end

if vim.startswith(file, 'gitsigns://') then
local path, commit = parse_gitsigns_uri(file)
dprintf("Gitsigns buffer for file '%s' from path '%s'", path, file)
path = uv.fs_realpath(path)
if path then
return path, commit
local realpath = uv.fs_realpath(path)
if realpath then
return realpath, commit
end
end
end
Expand Down Expand Up @@ -222,7 +224,7 @@ end
-- performed whilst another one is in progress.
--- @param cbuf integer
--- @param ctx Gitsigns.GitContext
--- @param aucmd string
--- @param aucmd? string
local attach_throttled = throttle_by_id(function(cbuf, ctx, aucmd)
local __FUNC__ = 'attach'

Expand Down Expand Up @@ -404,7 +406,7 @@ end
--- {async}
---
--- @param bufnr integer Buffer number
--- @param ctx table|nil
--- @param ctx Gitsigns.GitContext|nil
--- Git context data that may optionally be used to attach to any
--- buffer that represents a real git object.
--- • {file}: (string)
Expand All @@ -419,6 +421,7 @@ end
--- The git revision that the file belongs to.
--- • {base}: (string|nil)
--- The git revision that the file should be compared to.
--- @param _trigger? string
M.attach = void(function(bufnr, ctx, _trigger)
attach_throttled(bufnr or api.nvim_get_current_buf(), ctx, _trigger)
end)
Expand Down
9 changes: 9 additions & 0 deletions lua/gitsigns/diff.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ local config = require('gitsigns.config').config
--- @param linematch? boolean
--- @return Gitsigns.Hunk.Hunk[] hunks
return function(a, b, linematch)
-- Short circuit optimization
if not a or #a == 0 then
local Hunks = require('gitsigns.hunks')
local hunk = Hunks.create_hunk(0, 0, 1, #b)
hunk.added.lines = b
return { hunk }
end

local diff_opts = config.diff_opts
local f --- @type Gitsigns.Difffn
if diff_opts.internal then
Expand All @@ -19,5 +27,6 @@ return function(a, b, linematch)
if linematch ~= false then
linematch0 = diff_opts.linematch
end

return f(a, b, diff_opts.algorithm, diff_opts.indent_heuristic, linematch0)
end
Loading

0 comments on commit 16ad164

Please sign in to comment.