From ad96b304cff48dbe9169fe370c59f256f559a5e6 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Wed, 21 May 2025 11:55:50 +0300 Subject: [PATCH] refactor: split into config/status/component modules --- .luarc.json | 9 +++--- lua/lualine-harpoon/component.lua | 32 +++++++++++++++++++ lua/lualine-harpoon/config.lua | 10 ++++++ lua/lualine-harpoon/init.lua | 9 ++++++ lua/lualine-harpoon/status.lua | 30 ++++++++++++++++++ lua/lualine-harpoon/utils.lua | 16 ++++++++++ lua/lualine/components/harpoon.lua | 50 +----------------------------- 7 files changed, 103 insertions(+), 53 deletions(-) create mode 100644 lua/lualine-harpoon/component.lua create mode 100644 lua/lualine-harpoon/config.lua create mode 100644 lua/lualine-harpoon/init.lua create mode 100644 lua/lualine-harpoon/status.lua create mode 100644 lua/lualine-harpoon/utils.lua diff --git a/.luarc.json b/.luarc.json index deda1dc..604705e 100644 --- a/.luarc.json +++ b/.luarc.json @@ -1,5 +1,6 @@ { - "diagnostics.disable": [ - "unused-local" - ] -} \ No newline at end of file + "diagnostics.disable": [ + "unused-local" + ] +} + diff --git a/lua/lualine-harpoon/component.lua b/lua/lualine-harpoon/component.lua new file mode 100644 index 0000000..b8aa8d1 --- /dev/null +++ b/lua/lualine-harpoon/component.lua @@ -0,0 +1,32 @@ +local status = require("lualine-harpoon.status") +local cfg = require("lualine-harpoon.config") + +local req = require("lualine_require") +local Component = req.require("lualine.component") + +local M = Component:extend() + +function M:init(opts) + M.super.init(self, opts) + self.options = vim.tbl_deep_extend("force", cfg, self.options or {}) +end + +function M:update_status() + local st = status.get_status() + if st.total == 0 then + return "" + end + local s = self.options.symbol + local n = st.current and tostring(st.current) or s.unknown + return string.format("%s%s%s%d%s", s.open, n, s.separator, st.total, s.close) +end + +function M:draw_status() + local text = self:update_status() + if self.color_hl and text ~= "" then + return self.color_hl(text) + end + return text +end + +return M diff --git a/lua/lualine-harpoon/config.lua b/lua/lualine-harpoon/config.lua new file mode 100644 index 0000000..3813680 --- /dev/null +++ b/lua/lualine-harpoon/config.lua @@ -0,0 +1,10 @@ +local M = { + symbol = { + open = "[", + close = "]", + separator = "/", + unknown = "?", + }, +} + +return M diff --git a/lua/lualine-harpoon/init.lua b/lua/lualine-harpoon/init.lua new file mode 100644 index 0000000..c7c3386 --- /dev/null +++ b/lua/lualine-harpoon/init.lua @@ -0,0 +1,9 @@ +local component = require("lualine-harpoon.component") + +local M = { + component = component, +} + +function M.setup(_opts) end + +return M diff --git a/lua/lualine-harpoon/status.lua b/lua/lualine-harpoon/status.lua new file mode 100644 index 0000000..a387978 --- /dev/null +++ b/lua/lualine-harpoon/status.lua @@ -0,0 +1,30 @@ +local ok, harpoon = pcall(require, "harpoon") +local utils = require("lualine-harpoon.utils") +local M = {} + +--- Get current 1-based index and total Harpoon marks +---@return { current: integer?, total: integer } +function M.get_status() + if not ok then + return { current = nil, total = 0 } + end + + local list = harpoon:list() + local total = list:length() + + if total == 0 then + return { current = nil, total = 0 } + end + + local root = list.config.get_root_dir() + local buf = vim.api.nvim_buf_get_name(0) + local rel_buf = utils.normalize(buf, root) + + local _, idx = list:get_by_value(rel_buf) + if type(idx) ~= "number" or idx < 1 or idx > total then + idx = nil + end + return { current = idx, total = total } +end + +return M diff --git a/lua/lualine-harpoon/utils.lua b/lua/lualine-harpoon/utils.lua new file mode 100644 index 0000000..b385952 --- /dev/null +++ b/lua/lualine-harpoon/utils.lua @@ -0,0 +1,16 @@ +local ok_path, Path = pcall(require, "plenary.path") +local M = {} + +--- Normalize `buf` relative to `root`, if plenary.path is available. +---@param buf string +---@param root string +---@return string +function M.normalize(buf, root) + if ok_path then + return Path:new(buf):make_relative(root) + else + return buf + end +end + +return M diff --git a/lua/lualine/components/harpoon.lua b/lua/lualine/components/harpoon.lua index 8b4e76f..6e5f464 100644 --- a/lua/lualine/components/harpoon.lua +++ b/lua/lualine/components/harpoon.lua @@ -1,49 +1 @@ -local ok, harpoon = pcall(require, "harpoon") -local Path = require("plenary.path") -local function normalize_path(buf_name, root) - return Path:new(buf_name):make_relative(root) -end - ---- [TODO:description] ----@return table {current} -local function get_status() - if not ok then - return { current = nil, total = 0 } - end - - local list = harpoon:list() - local total = list:length() - - local bufname = normalize_path(vim.api.nvim_buf_get_name(0), list.config.get_root_dir()) - local _, idx = list:get_by_value(bufname) - if type(idx) ~= "number" or idx < 1 or idx > total then - return { current = nil, total = total } - end - return { current = idx, total = total } -end - ---- Draw function called by lualine ----@return string -local function draw() - local st = get_status() - if st.total == 0 then - return "" - end - local idx = st.current and tostring(st.current) or "?" - return string.format("[%s/%d]", idx, st.total) -end - ---- Only show when there is at least one mark ----@return boolean -local function cond() - return get_status().total > 0 -end - -return function(opts) - return draw() - -- return { - -- draw = draw, - -- cond = opts.cond or cond, - -- color = opts.color or { fg = "#89b4fa", gui = "bold" }, - -- } -end +return require("lualine-harpoon.component")