Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typings for hlchunk.setup() #135

Merged
merged 2 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lua/hlchunk/init.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
local hlchunk = {}

---@class HlChunk.UserConf
---@field blank? HlChunk.UserBlankConf
---@field chunk? HlChunk.UserChunkConf
---@field indent? HlChunk.UserIndentConf
---@field line_num? HlChunk.UserLineNumConf

---@param userConf HlChunk.UserConf
hlchunk.setup = function(userConf)
for mod_name, mod_conf in pairs(userConf) do
if mod_conf.enable then
local mod_path = "hlchunk.mods." .. mod_name
local Mod = require(mod_path) --[[@as BaseMod]]
local Mod = require(mod_path) --[[@as HlChunk.BaseMod]]
local mod = Mod(mod_conf)
mod:enable()
end
Expand Down
18 changes: 9 additions & 9 deletions lua/hlchunk/mods/base_mod/base_conf.lua
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
local class = require("hlchunk.utils.class")
local ft = require("hlchunk.utils.filetype")

---@alias StyleType string | table<string, string> | table<string, table<string, string>>
---@alias HlChunk.StyleType string | table<string, string> | table<string, table<string, string>>

---@class UserBaseConf
---@class HlChunk.UserBaseConf
---@field enable? boolean
---@field style? StyleType
---@field style? HlChunk.StyleType
---@field exclude_filetypes? table<string, boolean>
---@field notify? boolean
---@field priority? number

---@class BaseConf
---@class HlChunk.BaseConf
---@field enable boolean
---@field style StyleType
---@field style HlChunk.StyleType
---@field exclude_filetypes table<string, boolean>
---@field notify boolean
---@field priority number
---@field init fun(self: UserBaseConf, conf: table)
---@overload fun(conf?: UserBaseConf): BaseConf
---@overload fun(conf?: BaseConf): BaseConf
---@field init fun(self: HlChunk.UserBaseConf, conf: table)
---@overload fun(conf?: HlChunk.UserBaseConf): HlChunk.BaseConf
---@overload fun(conf?: HlChunk.BaseConf): HlChunk.BaseConf
local BaseConf = class(function(self, conf)
local default_conf = {
enable = false,
Expand All @@ -27,7 +27,7 @@ local BaseConf = class(function(self, conf)
priority = 0,
exclude_filetypes = ft.exclude_filetypes,
}
conf = vim.tbl_deep_extend("force", default_conf, conf or {}) --[[@as BaseConf]]
conf = vim.tbl_deep_extend("force", default_conf, conf or {}) --[[@as HlChunk.BaseConf]]
self.enable = conf.enable
self.style = conf.style
self.exclude_filetypes = conf.exclude_filetypes
Expand Down
40 changes: 20 additions & 20 deletions lua/hlchunk/mods/base_mod/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ local cFunc = require("hlchunk.utils.cFunc")
local api = vim.api
local fn = vim.fn

---@param self BaseMod
---@param conf BaseConf
---@param meta MetaInfo
---@param self HlChunk.BaseMod
---@param conf HlChunk.BaseConf
---@param meta HlChunk.MetaInfo
local constrctor = function(self, conf, meta)
local default_meta = {
name = "",
Expand All @@ -21,22 +21,22 @@ local constrctor = function(self, conf, meta)
self.conf = BaseConf(conf)
end

---@class BaseMod
---@field meta MetaInfo include info just used in mod inside, user can't access it
---@field conf BaseConf user config
---@field init fun(self: BaseMod, conf: BaseConf, meta: MetaInfo) not used for init mod, but as super keyword when inherit
---@field enable fun(self: BaseMod) enable the mod, the main entry of the mod
---@field disable fun(self: BaseMod) disable the mod
---@field protected shouldRender fun(self: BaseMod, bufnr: number): boolean just a tool function
---@field protected render fun(self: BaseMod, range: Scope)
---@field protected clear fun(self: BaseMod, range: Scope)
---@field protected createUsercmd fun(self: BaseMod)
---@field protected createAutocmd fun(self: BaseMod)
---@field protected clearAutocmd fun(self: BaseMod)
---@field protected setHl fun(self: BaseMod)
---@field protected clearHl fun(self: BaseMod)
---@field protected notify fun(self: BaseMod, msg: string, level?: string, opts?: table)
---@overload fun(conf?: UserBaseConf, meta?: MetaInfo): BaseMod
---@class HlChunk.BaseMod
---@field meta HlChunk.MetaInfo include info just used in mod inside, user can't access it
---@field conf HlChunk.BaseConf user config
---@field init fun(self: HlChunk.BaseMod, conf: HlChunk.BaseConf, meta: HlChunk.MetaInfo) not used for init mod, but as super keyword when inherit
---@field enable fun(self: HlChunk.BaseMod) enable the mod, the main entry of the mod
---@field disable fun(self: HlChunk.BaseMod) disable the mod
---@field protected shouldRender fun(self: HlChunk.BaseMod, bufnr: number): boolean just a tool function
---@field protected render fun(self: HlChunk.BaseMod, range: HlChunk.Scope)
---@field protected clear fun(self: HlChunk.BaseMod, range: HlChunk.Scope)
---@field protected createUsercmd fun(self: HlChunk.BaseMod)
---@field protected createAutocmd fun(self: HlChunk.BaseMod)
---@field protected clearAutocmd fun(self: HlChunk.BaseMod)
---@field protected setHl fun(self: HlChunk.BaseMod)
---@field protected clearHl fun(self: HlChunk.BaseMod)
---@field protected notify fun(self: HlChunk.BaseMod, msg: string, level?: string, opts?: table)
---@overload fun(conf?: HlChunk.UserBaseConf, meta?: HlChunk.MetaInfo): HlChunk.BaseMod
local BaseMod = class(constrctor)

function BaseMod:enable()
Expand Down Expand Up @@ -84,7 +84,7 @@ function BaseMod:render(range)
end

-- TODO: API-indexing
---@param range Scope the range to clear, start line and end line all include, 0-index
---@param range HlChunk.Scope the range to clear, start line and end line all include, 0-index
function BaseMod:clear(range)
local start = range.start
local finish = range.finish + 1
Expand Down
2 changes: 1 addition & 1 deletion lua/hlchunk/mods/base_mod/type.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---@class MetaInfo
---@class HlChunk.MetaInfo
---@field name string
---@field augroup_name string
---@field hl_base_name string
Expand Down
8 changes: 4 additions & 4 deletions lua/hlchunk/mods/blank/blank_conf.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
local class = require("hlchunk.utils.class")
local IndentConf = require("hlchunk.mods.indent.indent_conf")

---@class UserBlankConf : UserIndentConf
---@class HlChunk.UserBlankConf : HlChunk.UserIndentConf

---@class BlankConf : BaseConf
---@overload fun(conf?: UserIndentConf): IndentConf
---@class HlChunk.BlankConf : HlChunk.BaseConf
---@overload fun(conf?: HlChunk.UserIndentConf): HlChunk.IndentConf
local BlankConf = class(IndentConf, function(self, conf)
local default_conf = {
priority = 9,
chars = { "․" },
}
conf = vim.tbl_deep_extend("force", default_conf, conf or {}) --[[@as IndentConf]]
conf = vim.tbl_deep_extend("force", default_conf, conf or {}) --[[@as HlChunk.IndentConf]]
IndentConf.init(self, conf)

self.priority = conf.priority
Expand Down
4 changes: 2 additions & 2 deletions lua/hlchunk/mods/blank/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ local constuctor = function(self, conf, meta)
self.conf = BlankConf(conf)
end

---@class BlankMod: IndentMod
---@overload fun(conf?: UserIndentConf, meta?: MetaInfo): IndentMod
---@class HlChunk.BlankMod: HlChunk.IndentMod
---@overload fun(conf?: HlChunk.UserIndentConf, meta?: HlChunk.MetaInfo): HlChunk.IndentMod
local BlankMod = class(IndentMod, constuctor)

function BlankMod:renderLeader(bufnr, index, offset, shadow_char_num, row_opts)
Expand Down
8 changes: 4 additions & 4 deletions lua/hlchunk/mods/chunk/chunk_conf.lua
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
local class = require("hlchunk.utils.class")
local BaseConf = require("hlchunk.mods.base_mod.base_conf")

---@class UserChunkConf : UserBaseConf
---@class HlChunk.UserChunkConf : HlChunk.UserBaseConf
---@field use_treesitter? boolean
---@field chars? table<string, string>
---@field textobject? string
---@field max_file_size? number
---@field error_sign? boolean

---@class ChunkConf : BaseConf
---@class HlChunk.ChunkConf : HlChunk.BaseConf
---@field use_treesitter boolean
---@field chars table<string, string>
---@field textobject string
---@field max_file_size number
---@field error_sign boolean
---@field duration number
---@field delay number
---@overload fun(conf?: table): ChunkConf
---@overload fun(conf?: table): HlChunk.ChunkConf
local ChunkConf = class(BaseConf, function(self, conf)
local default_conf = {
priority = 15,
Expand All @@ -39,7 +39,7 @@ local ChunkConf = class(BaseConf, function(self, conf)
duration = 200,
delay = 300,
}
conf = vim.tbl_deep_extend("force", default_conf, conf or {}) --[[@as ChunkConf]]
conf = vim.tbl_deep_extend("force", default_conf, conf or {}) --[[@as HlChunk.ChunkConf]]
BaseConf.init(self, conf)

self.priority = conf.priority
Expand Down
14 changes: 7 additions & 7 deletions lua/hlchunk/mods/chunk/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ local rangeFromTo = chunkHelper.rangeFromTo
local utf8Split = chunkHelper.utf8Split
local shallowCmp = chunkHelper.shallowCmp

---@class ChunkMetaInfo : MetaInfo
---@field task LoopTask | nil
---@class HlChunk.ChunkMetaInfo : HlChunk.MetaInfo
---@field task HlChunk.LoopTask | nil
---@field pre_virt_text_list string[]
---@field pre_row_list number[]
---@field pre_virt_text_win_col_list number[]
Expand All @@ -44,11 +44,11 @@ local constructor = function(self, conf, meta)
self.conf = ChunkConf(conf)
end

---@class ChunkMod : BaseMod
---@field conf ChunkConf
---@field meta ChunkMetaInfo
---@field render fun(self: ChunkMod, range: Scope, opts?: {error: boolean, lazy: boolean})
---@overload fun(conf?: UserChunkConf, meta?: MetaInfo): ChunkMod
---@class HlChunk.ChunkMod : HlChunk.BaseMod
---@field conf HlChunk.ChunkConf
---@field meta HlChunk.ChunkMetaInfo
---@field render fun(self: HlChunk.ChunkMod, range: HlChunk.Scope, opts?: {error: boolean, lazy: boolean})
---@overload fun(conf?: HlChunk.UserChunkConf, meta?: HlChunk.MetaInfo): HlChunk.ChunkMod
local ChunkMod = class(BaseMod, constructor)

-- chunk_mod can use text object, so add a new function extra to handle it
Expand Down
8 changes: 4 additions & 4 deletions lua/hlchunk/mods/indent/indent_conf.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
local class = require("hlchunk.utils.class")
local BaseConf = require("hlchunk.mods.base_mod.base_conf")

---@class UserIndentConf : UserBaseConf
---@class HlChunk.UserIndentConf : HlChunk.UserBaseConf
---@field chars? string[]
---@field use_treesitter? boolean
---@field ahead_lines? number
---@field delay? number

---@class IndentConf : BaseConf
---@class HlChunk.IndentConf : HlChunk.BaseConf
---@field use_treesitter boolean
---@field chars table<string, string>
---@field ahead_lines number
---@field delay number default 50ms
---@field filter_list table<number, function>
---@overload fun(conf?: UserIndentConf): IndentConf
---@overload fun(conf?: HlChunk.UserIndentConf): HlChunk.IndentConf
local IndentConf = class(BaseConf, function(self, conf)
local default_conf = {
style = { vim.api.nvim_get_hl(0, { name = "Whitespace" }) },
Expand All @@ -24,7 +24,7 @@ local IndentConf = class(BaseConf, function(self, conf)
delay = 100,
filter_list = {},
}
conf = vim.tbl_deep_extend("force", default_conf, conf or {}) --[[@as IndentConf]]
conf = vim.tbl_deep_extend("force", default_conf, conf or {}) --[[@as HlChunk.IndentConf]]
BaseConf.init(self, conf)

self.style = conf.style
Expand Down
16 changes: 8 additions & 8 deletions lua/hlchunk/mods/indent/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ local api = vim.api
local fn = vim.fn
local ROWS_INDENT_RETCODE = indentHelper.ROWS_INDENT_RETCODE

---@class IndentMetaInfo : MetaInfo
---@class HlChunk.IndentMetaInfo : HlChunk.MetaInfo
---@field pre_leftcol number

local constructor = function(self, conf, meta)
Expand All @@ -30,19 +30,19 @@ local constructor = function(self, conf, meta)
self.conf = IndentConf(conf)
end

---@class RenderInfo
---@class HlChunk.RenderInfo
---@field lnum number
---@field virt_text_win_col number
---@field virt_text table
---@field level number

---@class IndentMod : BaseMod
---@field conf IndentConf
---@field meta IndentMetaInfo
---@field render fun(self: IndentMod, range: Scope, opts: {lazy: boolean})
---@field calcRenderInfo fun(self: IndentMod, range: Scope): RenderInfo
---@class HlChunk.IndentMod : HlChunk.BaseMod
---@field conf HlChunk.IndentConf
---@field meta HlChunk.IndentMetaInfo
---@field render fun(self: HlChunk.IndentMod, range: HlChunk.Scope, opts: {lazy: boolean})
---@field calcRenderInfo fun(self: HlChunk.IndentMod, range: HlChunk.Scope): HlChunk.RenderInfo
---@field setmark function
---@overload fun(conf?: UserIndentConf, meta?: MetaInfo): IndentMod
---@overload fun(conf?: HlChunk.UserIndentConf, meta?: HlChunk.MetaInfo): HlChunk.IndentMod
local IndentMod = class(BaseMod, constructor)

local indent_cache = Cache("bufnr", "line")
Expand Down
12 changes: 6 additions & 6 deletions lua/hlchunk/mods/line_num/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local class = require("hlchunk.utils.class")
local api = vim.api
local CHUNK_RANGE_RET = chunkHelper.CHUNK_RANGE_RET

---@class LineNumMetaInfo : MetaInfo
---@class HlChunk.LineNumMetaInfo : HlChunk.MetaInfo

local constructor = function(self, conf, meta)
local default_meta = {
Expand All @@ -21,11 +21,11 @@ local constructor = function(self, conf, meta)
self.conf = LineNumConf(conf)
end

---@class LineNumMod : BaseMod
---@field conf LineNumConf
---@field meta LineNumMetaInfo
---@field render fun(self: LineNumMod, range: Scope, opts?: {error: boolean})
---@overload fun(conf?: UserLineNumConf, meta?: MetaInfo): LineNumMod
---@class HlChunk.LineNumMod : HlChunk.BaseMod
---@field conf HlChunk.LineNumConf
---@field meta HlChunk.LineNumMetaInfo
---@field render fun(self: HlChunk.LineNumMod, range: HlChunk.Scope, opts?: {error: boolean})
---@overload fun(conf?: HlChunk.UserLineNumConf, meta?: HlChunk.MetaInfo): HlChunk.LineNumMod
local LineNumMod = class(BaseMod, constructor)

function LineNumMod:render(range)
Expand Down
8 changes: 4 additions & 4 deletions lua/hlchunk/mods/line_num/line_num_conf.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
local class = require("hlchunk.utils.class")
local BaseConf = require("hlchunk.mods.base_mod.base_conf")

---@class UserLineNumConf : UserBaseConf
---@class HlChunk.UserLineNumConf : HlChunk.UserBaseConf
---@field use_treesitter? boolean

---@class LineNumConf : BaseConf
---@class HlChunk.LineNumConf : HlChunk.BaseConf
---@field use_treesitter boolean
---@overload fun(conf?: table): ChunkConf
---@overload fun(conf?: table): HlChunk.ChunkConf
local LineNumConf = class(BaseConf, function(self, conf)
local default_conf = {
style = "#806d9c",
priority = 10,
use_treesitter = false,
}
conf = vim.tbl_deep_extend("force", default_conf, conf or {}) --[[@as LineNumConf]]
conf = vim.tbl_deep_extend("force", default_conf, conf or {}) --[[@as HlChunk.LineNumConf]]
BaseConf.init(self, conf)

self.style = conf.style
Expand Down
4 changes: 2 additions & 2 deletions lua/hlchunk/utils/cache.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
local class = require("hlchunk.utils.class")

---@class Cache
---@class HlChunk.Cache
---@field private cache table
---@field private keys table
---@overload fun(...):Cache
---@overload fun(...):HlChunk.Cache
local Cache = class(function(self, ...)
self.keys = { ... } -- 在构造函数中保存键名
self.cache = {}
Expand Down
8 changes: 4 additions & 4 deletions lua/hlchunk/utils/chunkHelper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ chunkHelper.CHUNK_RANGE_RET = {
NO_TS = 3,
}

---@param pos Pos 0-index (row, col)
---@param pos HlChunk.Pos 0-index (row, col)
local function get_chunk_range_by_context(pos)
local base_flag = "nWz"
local cur_row_val = vim.api.nvim_buf_get_lines(pos.bufnr, pos.row, pos.row + 1, false)[1]
Expand All @@ -57,7 +57,7 @@ local function get_chunk_range_by_context(pos)
return chunkHelper.CHUNK_RANGE_RET.OK, Scope(pos.bufnr, beg_row - 1, end_row - 1)
end

---@param pos Pos 0-index for row, 0-index for col, API-indexing
---@param pos HlChunk.Pos 0-index for row, 0-index for col, API-indexing
local function get_chunk_range_by_treesitter(pos)
if not has_treesitter(pos.bufnr) then
return chunkHelper.CHUNK_RANGE_RET.NO_TS, Scope(pos.bufnr, -1, -1)
Expand Down Expand Up @@ -87,9 +87,9 @@ local function get_chunk_range_by_treesitter(pos)
return chunkHelper.CHUNK_RANGE_RET.NO_CHUNK, Scope(pos.bufnr, -1, -1)
end

---@param opts? {pos: Pos, use_treesitter: boolean}
---@param opts? {pos: HlChunk.Pos, use_treesitter: boolean}
---@return CHUNK_RANGE_RETCODE enum
---@return Scope
---@return HlChunk.Scope
function chunkHelper.get_chunk_range(opts)
opts = opts or { use_treesitter = false }

Expand Down
Loading
Loading