Skip to content

Commit

Permalink
add telescope shortcts
Browse files Browse the repository at this point in the history
  • Loading branch information
LintaoAmons committed Oct 29, 2024
1 parent e8699d5 commit a8b7654
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ This plugin did nothing but provide a simpler way to add, persist and switch to
![HowItWorks](https://github.com/LintaoAmons/cd-project.nvim/assets/95092244/6fa66d86-38c0-4ea8-ad5e-a6ed14c263ef)

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

- Simple version

Expand Down Expand Up @@ -59,7 +58,7 @@ return {
-- })
-- end,
-- },
}
}
})
end,
}
Expand Down Expand Up @@ -88,6 +87,17 @@ you can find the exported Apis at [./lua/cd-project/api.lua](./lua/cd-project/ap

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

### Workflow

1. Add a project into `cd-project.nvim`: `CdProjectAdd`
2. switch to the project: `CdProject`
1. `<CR>` will go to the project, change current working directory to the project
2. `AFTER_CD` hook will be triggered, I use it to open a file in the project by `smart-open`
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
- Usecase: Open multiple projects can jump between them
4. Or in the telescope picker, you can use `<c-e>` to trigger the hooks but without change the current working directory.
- 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.

## CONTRIBUTING

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

19 changes: 17 additions & 2 deletions lua/cd-project/adapter/telescope.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,28 @@ local cd_project = function(opts)

pickers
.new(opts, {
attach_mappings = function(prompt_bufnr)
attach_mappings = function(prompt_bufnr, map)
actions.select_default:replace(function()
actions.close(prompt_bufnr)
---@type CdProject.Project
local selected_project = action_state.get_selected_entry().value
api.cd_project(selected_project.path)
end)

map({ "i", "n" }, "<c-o>", function()
actions.close(prompt_bufnr)
---@type CdProject.Project
local selected_project = action_state.get_selected_entry().value
api.cd_project_in_tab(selected_project.path)
end)

map({ "i", "n" }, "<c-e>", function()
actions.close(prompt_bufnr)
---@type CdProject.Project
local selected_project = action_state.get_selected_entry().value
api.cd_project(selected_project.path, { change_dir = false })
end)

return true
end,
prompt_title = "cd to project",
Expand Down Expand Up @@ -106,7 +121,7 @@ local cd_project_in_tab = function(opts)

pickers
.new(opts, {
attach_mappings = function(prompt_bufnr)
attach_mappings = function(prompt_bufnr, map)
actions.select_default:replace(function()
actions.close(prompt_bufnr)
---@type CdProject.Project
Expand Down
8 changes: 6 additions & 2 deletions lua/cd-project/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ local function cd_project_in_tab(dir)
end

---@param dir string
local function cd_project(dir)
---@param opts? {change_dir: boolean}
local function cd_project(dir, opts)
opts = opts or { change_dir = true }
vim.g.cd_project_last_project = vim.g.cd_project_current_project
vim.g.cd_project_current_project = dir

Expand All @@ -104,7 +106,9 @@ local function cd_project(dir)
hook.callback(dir)
end

vim.fn.execute("cd " .. vim.fn.fnameescape(dir))
if opts.change_dir then
vim.fn.execute("cd " .. vim.fn.fnameescape(dir))
end

local hooks = cd_hooks.get_hooks(vim.g.cd_project_config.hooks, dir, "AFTER_CD")
for _, hook in ipairs(hooks) do
Expand Down
4 changes: 3 additions & 1 deletion plugin/cd-project.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ vim.g.cd_project_current_project = api.find_project_dir()

vim.api.nvim_create_user_command("CdProject", adapter.cd_project, {})
vim.api.nvim_create_user_command("CdProjectTab", adapter.cd_project_in_tab, {})
vim.api.nvim_create_user_command("CdProjectAdd", api.add_current_project, {})
vim.api.nvim_create_user_command("CdProjectAdd", function()
api.add_current_project({ show_duplicate_hints = true })
end, {})
vim.api.nvim_create_user_command("CdProjectManualAdd", adapter.manual_cd_project, {})
vim.api.nvim_create_user_command("CdProjectSearchAndAdd", adapter.telescope_search_and_add, {})
vim.api.nvim_create_user_command("CdProjectDelete", adapter.delete_project, {})
Expand Down

0 comments on commit a8b7654

Please sign in to comment.