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

[Feat] add show_workspace config for display workspace in breadcrumbs #101

Merged
merged 2 commits into from
May 7, 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
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion lua/codesnap/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)

Expand Down
1 change: 1 addition & 0 deletions lua/codesnap/static.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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("(.*[/\\])"))),
Expand Down
16 changes: 14 additions & 2 deletions lua/codesnap/utils/path.lua
Original file line number Diff line number Diff line change
@@ -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
Loading