Skip to content

Commit a8b7654

Browse files
committed
add telescope shortcts
1 parent e8699d5 commit a8b7654

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

README.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ This plugin did nothing but provide a simpler way to add, persist and switch to
1313
![HowItWorks](https://github.com/LintaoAmons/cd-project.nvim/assets/95092244/6fa66d86-38c0-4ea8-ad5e-a6ed14c263ef)
1414

1515
## Install and Config
16-
> [My config as ref](https://github.com/LintaoAmons/CoolStuffes/blob/main/nvim/.config/nvim/lua/plugins/editor-enhance/project.lua)
1716

1817
- Simple version
1918

@@ -59,7 +58,7 @@ return {
5958
-- })
6059
-- end,
6160
-- },
62-
}
61+
}
6362
})
6463
end,
6564
}
@@ -88,6 +87,17 @@ you can find the exported Apis at [./lua/cd-project/api.lua](./lua/cd-project/ap
8887

8988
- [ ] Remember the buffer and cursor location while switch to the project
9089

90+
### Workflow
91+
92+
1. Add a project into `cd-project.nvim`: `CdProjectAdd`
93+
2. switch to the project: `CdProject`
94+
1. `<CR>` will go to the project, change current working directory to the project
95+
2. `AFTER_CD` hook will be triggered, I use it to open a file in the project by `smart-open`
96+
3. Or in the telescope picker, you can use `<c-o>` to open a project in a new tab, or switch to that project's tab if it's already opened
97+
- Usecase: Open multiple projects can jump between them
98+
4. Or in the telescope picker, you can use `<c-e>` to trigger the hooks but without change the current working directory.
99+
- Usecase: You just want to open a file in that project in a split window, but don't want to change the current working directory.
100+
91101
## CONTRIBUTING
92102

93103
Don't hesitate to ask me anything about the codebase if you want to contribute.
@@ -101,4 +111,3 @@ By [telegram](https://t.me/+ssgpiHyY9580ZWFl) or [微信: CateFat](https://linta
101111
- [cd-project.nvim](https://github.com/LintaoAmons/cd-project.nvim)
102112
- [bookmarks.nvim](https://github.com/LintaoAmons/bookmarks.nvim)
103113
- [context-menu.nvim](https://github.com/LintaoAmons/context-menu.nvim)
104-

lua/cd-project/adapter/telescope.lua

+17-2
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,28 @@ local cd_project = function(opts)
6767

6868
pickers
6969
.new(opts, {
70-
attach_mappings = function(prompt_bufnr)
70+
attach_mappings = function(prompt_bufnr, map)
7171
actions.select_default:replace(function()
7272
actions.close(prompt_bufnr)
7373
---@type CdProject.Project
7474
local selected_project = action_state.get_selected_entry().value
7575
api.cd_project(selected_project.path)
7676
end)
77+
78+
map({ "i", "n" }, "<c-o>", function()
79+
actions.close(prompt_bufnr)
80+
---@type CdProject.Project
81+
local selected_project = action_state.get_selected_entry().value
82+
api.cd_project_in_tab(selected_project.path)
83+
end)
84+
85+
map({ "i", "n" }, "<c-e>", function()
86+
actions.close(prompt_bufnr)
87+
---@type CdProject.Project
88+
local selected_project = action_state.get_selected_entry().value
89+
api.cd_project(selected_project.path, { change_dir = false })
90+
end)
91+
7792
return true
7893
end,
7994
prompt_title = "cd to project",
@@ -106,7 +121,7 @@ local cd_project_in_tab = function(opts)
106121

107122
pickers
108123
.new(opts, {
109-
attach_mappings = function(prompt_bufnr)
124+
attach_mappings = function(prompt_bufnr, map)
110125
actions.select_default:replace(function()
111126
actions.close(prompt_bufnr)
112127
---@type CdProject.Project

lua/cd-project/api.lua

+6-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ local function cd_project_in_tab(dir)
9595
end
9696

9797
---@param dir string
98-
local function cd_project(dir)
98+
---@param opts? {change_dir: boolean}
99+
local function cd_project(dir, opts)
100+
opts = opts or { change_dir = true }
99101
vim.g.cd_project_last_project = vim.g.cd_project_current_project
100102
vim.g.cd_project_current_project = dir
101103

@@ -104,7 +106,9 @@ local function cd_project(dir)
104106
hook.callback(dir)
105107
end
106108

107-
vim.fn.execute("cd " .. vim.fn.fnameescape(dir))
109+
if opts.change_dir then
110+
vim.fn.execute("cd " .. vim.fn.fnameescape(dir))
111+
end
108112

109113
local hooks = cd_hooks.get_hooks(vim.g.cd_project_config.hooks, dir, "AFTER_CD")
110114
for _, hook in ipairs(hooks) do

plugin/cd-project.lua

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ vim.g.cd_project_current_project = api.find_project_dir()
1616

1717
vim.api.nvim_create_user_command("CdProject", adapter.cd_project, {})
1818
vim.api.nvim_create_user_command("CdProjectTab", adapter.cd_project_in_tab, {})
19-
vim.api.nvim_create_user_command("CdProjectAdd", api.add_current_project, {})
19+
vim.api.nvim_create_user_command("CdProjectAdd", function()
20+
api.add_current_project({ show_duplicate_hints = true })
21+
end, {})
2022
vim.api.nvim_create_user_command("CdProjectManualAdd", adapter.manual_cd_project, {})
2123
vim.api.nvim_create_user_command("CdProjectSearchAndAdd", adapter.telescope_search_and_add, {})
2224
vim.api.nvim_create_user_command("CdProjectDelete", adapter.delete_project, {})

0 commit comments

Comments
 (0)