From ea40d82a399cd5384213928bb5da04fc02f08485 Mon Sep 17 00:00:00 2001 From: Mist Date: Tue, 7 May 2024 21:34:14 +0800 Subject: [PATCH 1/2] [Feat] add show_workspace config for display workspace in breadcrumbs --- lua/codesnap/config.lua | 8 +++++++- lua/codesnap/static.lua | 1 + lua/codesnap/utils/path.lua | 16 ++++++++++++++-- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lua/codesnap/config.lua b/lua/codesnap/config.lua index d2ba1b8..a91121b 100644 --- a/lua/codesnap/config.lua +++ b/lua/codesnap/config.lua @@ -25,6 +25,12 @@ local function parse_save_path(save_path) return parsed_save_path .. auto_generate_snap_filename() end +local function get_file_path(show_workspace) + local relative_path = path_utils.get_relative_path() + + return show_workspace and path_utils.get_workspace() .. "/" .. relative_path or relative_path +end + function config_module.get_config(extension) local code = visual_utils.get_selected_text() local start_line_number = visual_utils.get_start_line_number() @@ -41,7 +47,7 @@ function config_module.get_config(extension) fonts_folder = assets_folder .. "/fonts", themes_folder = assets_folder .. "/themes", theme = "base16-onedark", - file_path = static.config.has_breadcrumbs and path_utils.get_relative_path() or "", + file_path = static.config.has_breadcrumbs and get_file_path(static.config.show_workspace) or "", start_line_number = static.config.has_line_number and start_line_number or nil, }, static.config) diff --git a/lua/codesnap/static.lua b/lua/codesnap/static.lua index 670134a..4ae9869 100644 --- a/lua/codesnap/static.lua +++ b/lua/codesnap/static.lua @@ -11,6 +11,7 @@ return { breadcrumbs_separator = "/", has_breadcrumbs = false, has_line_number = false, + show_workspace = false, min_width = 0, }, cwd = path_utils.back(path_utils.back(debug.getinfo(1, "S").source:sub(2):match("(.*[/\\])"))), diff --git a/lua/codesnap/utils/path.lua b/lua/codesnap/utils/path.lua index ebf4a54..6318c3e 100644 --- a/lua/codesnap/utils/path.lua +++ b/lua/codesnap/utils/path.lua @@ -1,17 +1,29 @@ local string_utils = require("codesnap.utils.string") local path_utils = {} +function path_utils.get_escaped_cwd() + local cwd = vim.fn.getcwd() + + return string_utils.escape(cwd) +end + function path_utils.back(path) local parsed_path, _ = path:gsub("/[^\\/]+/?$", "") return parsed_path end +function path_utils.get_workspace() + local cwd = vim.fn.getcwd() + local _, _, workspace = string.find(cwd, "/([^/]+)$") + + return workspace == nil and "" or workspace +end + function path_utils.get_relative_path() local full_file_path = vim.fn.expand("%:p") - local cwd = vim.fn.getcwd() - return full_file_path:gsub(string_utils.escape(cwd), ""):sub(2) + return full_file_path:gsub(path_utils.get_escaped_cwd(), ""):sub(2) end return path_utils From 15faf40768fcbde3626d947c2301d023afed3c5b Mon Sep 17 00:00:00 2001 From: The Mist Date: Tue, 7 May 2024 21:40:37 +0800 Subject: [PATCH 2/2] [Update] add usage for the show_workspace config item --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index 5863afe..4185fd7 100644 --- a/README.md +++ b/README.md @@ -227,6 +227,23 @@ require("codesnap").setup({ The breadcrumbs look like: ![image](https://github.com/mistricky/codesnap.nvim/assets/22574136/23274faa-36a9-4d41-88a5-e48c44b4d5bf) +### Show workspace in breadcrumbs +Breadcrumbs hide the workspace name by default, if you want to display workspace in breadcrumbs, you can just set `show_workspace` as true. +```lua +require("codesnap").setup({ + -- ... + has_breadcrumbs = true + show_workspace = true +}) +``` + +require("codesnap").setup({ + -- ... + has_breadcrumbs = true + breadcrumbs_separator = "👉" +}) + + ### Custom path separator The CodeSnap.nvim uses `/` as the separator of the file path by default, of course, you can specify any symbol you prefer as the custom separator: ```lua