Skip to content

Commit 391aa45

Browse files
Add builtin pickers to show_custom_functions picker (#40)
* Add builtin pickers to git_function picker Signed-off-by: Aaron Hallaert <[email protected]> * Update README for diff_commit_line Signed-off-by: Aaron Hallaert <[email protected]> --------- Signed-off-by: Aaron Hallaert <[email protected]>
1 parent 29762b0 commit 391aa45

File tree

3 files changed

+150
-63
lines changed

3 files changed

+150
-63
lines changed

README.md

Lines changed: 70 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@
1010
:Telescope advanced_git_search {function_name}
1111
```
1212

13-
or in lua
13+
#### or in lua
1414

1515
```lua
1616
require('telescope').extensions.advanced_git_search.{function_name}()
1717
```
1818

19+
#### or through another Telescope picker
20+
21+
execute `:AdvancedGitSearch`, choose your picker and press `<CR>`
22+
1923
### 🔎 Enter a query
2024

2125
Your usual telescope experience. See the individual commands for the grep behaviour.
@@ -27,26 +31,66 @@ the author name.
2731

2832
## ⚡️Commands
2933

30-
### 1. diff_branch_file
34+
### 1. search_log_content -- Search in repo log content
3135

32-
Opens a Telescope window with a list of local branches
36+
Opens a Telescope window with a list of all previous commit.
3337

34-
_Grep behaviour_: filter on branch name.
38+
_Grep behaviour_: filter on added, updated or removed code (log content: `-G` option in git).
3539

3640
#### _Keymaps_
3741

38-
- `<CR>` opens a diff for the current file with the selected branch
42+
- `<CR>` Opens a diff of the current file with the selected commit
43+
- `<C-e>` show the entire commit for all files in neovim with diff plugin
44+
- `<C-o>` Open the selected commit in the browser
45+
- `<C-y>` copy the commit hash to clipboard
46+
47+
### 2. search_log_content_file -- Search in file log content
48+
49+
Opens a Telescope window with a list of git commits that changed the
50+
current file (renames included).
51+
52+
_Grep behaviour_: filter on added, updated or removed code (log content: `-G` option in git).
53+
54+
#### _Keymaps_
55+
56+
- `<CR>` Opens a diff of the current file with the selected commit
57+
- `<C-e>` show the entire commit for all files in neovim with diff plugin
58+
- `<C-o>` Open the selected commit in the browser
59+
- `<C-y>` copy the commit hash to clipboard
60+
61+
### 3. diff_commit_file -- Diff current file with commit
62+
63+
Opens a Telescope window with a list of git commits that changed the
64+
current file (renames included).
65+
66+
_Grep behaviour_: filter on commit message.
3967

40-
### 2. diff_commit_line
68+
#### _Keymaps_
69+
70+
- `<CR>` Opens a diff of the current file with the selected commit
71+
- `<C-e>` show the entire commit for all files in neovim with diff plugin
72+
- `<C-o>` Open the selected commit in the browser
73+
- `<C-y>` copy the commit hash to clipboard
74+
75+
### 4. diff_commit_line -- Diff current file with selected line history
4176

4277
Opens a Telescope window with a list of previous commit logs with respect to
4378
selected lines
4479

4580
_Grep behaviour_: filter on commit message.
4681

47-
Note: First you have to select the lines in visual mode, then go back to normal
82+
#### How to use
83+
84+
_The following only applies when you use one of the commands below._
85+
86+
```vim
87+
:Telescope advanced_git_search diff_commit_line
88+
:lua require('telescope').extensions.advanced_git_search.diff_commit_line()
89+
```
90+
91+
First you have to select the lines in visual mode, then go back to normal
4892
mode and execute this command.
49-
To make this a bit easier, you can wrap it in a user command and define a keybind:
93+
To make a bit easier, you can wrap it in a user command and define a keybind:
5094

5195
```lua
5296
vim.api.nvim_create_user_command(
@@ -63,55 +107,26 @@ vim.api.nvim_set_keymap(
63107
)
64108
```
65109

110+
No extra setup is needed when you use `:AdvancedGitSearch`.
111+
66112
#### _Keymaps_
67113

68114
- `<CR>` opens a diff for the current file with the corresponding file on the selected commit
69115
- `<C-e>` show the entire commit for all files in neovim with diff plugin
70116
- `<C-o>` opens a the selected commit in the browser
71117
- `<C-y>` copy the commit hash to clipboard
72118

73-
### 3. diff_commit_file
74-
75-
Opens a Telescope window with a list of git commits that changed the
76-
current file (renames included).
77-
78-
_Grep behaviour_: filter on commit message.
79-
80-
#### _Keymaps_
81-
82-
- `<CR>` Opens a diff of the current file with the selected commit
83-
- `<C-e>` show the entire commit for all files in neovim with diff plugin
84-
- `<C-o>` Open the selected commit in the browser
85-
- `<C-y>` copy the commit hash to clipboard
86-
87-
### 4. search_log_content
88-
89-
Opens a Telescope window with a list of all previous commit.
90-
91-
_Grep behaviour_: filter on added, updated or removed code (log content: `-G` option in git).
119+
### 5. diff_branch_file -- Diff file with branch
92120

93-
#### _Keymaps_
94-
95-
- `<CR>` Opens a diff of the current file with the selected commit
96-
- `<C-e>` show the entire commit for all files in neovim with diff plugin
97-
- `<C-o>` Open the selected commit in the browser
98-
- `<C-y>` copy the commit hash to clipboard
99-
100-
### 5. search_log_content_file
101-
102-
Opens a Telescope window with a list of git commits that changed the
103-
current file (renames included).
121+
Opens a Telescope window with a list of local branches
104122

105-
_Grep behaviour_: filter on added, updated or removed code (log content: `-G` option in git).
123+
_Grep behaviour_: filter on branch name.
106124

107125
#### _Keymaps_
108126

109-
- `<CR>` Opens a diff of the current file with the selected commit
110-
- `<C-e>` show the entire commit for all files in neovim with diff plugin
111-
- `<C-o>` Open the selected commit in the browser
112-
- `<C-y>` copy the commit hash to clipboard
127+
- `<CR>` opens a diff for the current file with the selected branch
113128

114-
### 6. changed_on_branch
129+
### 6. changed_on_branch -- Changed on current branch (experimental)
115130

116131
Opens a Telescope window with a list of changed files on the current branch (including staged files).
117132
The fork point of the current branch is determined with the following command:
@@ -133,7 +148,7 @@ _Grep behaviour_: filter on filename.
133148

134149
- `<CR>` opens the selected file.
135150

136-
### 7. checkout_reflog
151+
### 7. checkout_reflog -- Checkout from reflog
137152

138153
Opens a Telescope window with all reflog entries
139154

@@ -144,6 +159,7 @@ Opens a Telescope window with all reflog entries
144159
### 8. show_custom_functions
145160

146161
A telescope picker for all functions above.
162+
Enable `show_builtin_git_pickers` to additionally show Telescopes builtin git pickers.
147163

148164
## ⚙️ Installation
149165

@@ -160,12 +176,14 @@ With Lazy
160176
advanced_git_search = {
161177
-- fugitive or diffview
162178
diff_plugin = "fugitive",
163-
-- customize git in previewer
179+
-- customize git in previewer
164180
-- e.g. flags such as { "--no-pager" }, or { "-c", "delta.side-by-side=false" }
165181
git_flags = {},
166-
-- customize git diff in previewer
182+
-- customize git diff in previewer
167183
-- e.g. flags such as { "--raw" }
168184
git_diff_flags = {},
185+
-- Show builtin git pickers when executing "show_custom_functions" or :AdvancedGitSearch
186+
show_builtin_git_pickers = false,
169187
}
170188
}
171189
}
@@ -196,14 +214,16 @@ With Packer
196214
-- move this to the place where you call the telescope setup function
197215
extensions = {
198216
advanced_git_search = {
199-
-- fugitive or diffview
217+
-- Fugitive or diffview
200218
diff_plugin = "fugitive",
201-
-- customize git in previewer
219+
-- Customize git in previewer
202220
-- e.g. flags such as { "--no-pager" }, or { "-c", "delta.side-by-side=false" }
203221
git_flags = {},
204-
-- customize git diff in previewer
222+
-- Customize git diff in previewer
205223
-- e.g. flags such as { "--raw" }
206224
git_diff_flags = {},
225+
-- Show builtin git pickers when executing "show_custom_functions" or :AdvancedGitSearch
226+
show_builtin_git_pickers = false,
207227
}
208228
}
209229
}

lua/advanced_git_search/init.lua

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local actions = require("telescope.actions")
22
local action_state = require("telescope.actions.state")
33
local git_utils = require("advanced_git_search.utils.git")
4+
local config = require("advanced_git_search.utils.config")
45

56
local pickers = require("telescope.pickers")
67
local sorters = require("telescope.sorters")
@@ -163,17 +164,54 @@ M.checkout_reflog = function()
163164
:find()
164165
end
165166

166-
local git_functions = {
167-
{ value = "Changed on current branch", func = M.changed_on_branch },
168-
{ value = "Find in repo log content", func = M.search_log_content },
169-
{ value = "Find in file log content", func = M.search_log_content_file },
170-
{ value = "Diff file with branch", func = M.diff_branch_file },
171-
{ value = "Diff file with previous commit", func = M.diff_commit_file },
167+
local custom_git_functions = {
172168
{
173-
value = "Diff file with selected line history",
169+
value = "Search in repo log content",
170+
func = M.search_log_content,
171+
},
172+
{
173+
value = "Search in file log content",
174+
func = M.search_log_content_file,
175+
},
176+
{
177+
value = "Diff current file with commit",
178+
func = M.diff_commit_file,
179+
},
180+
{
181+
value = "Diff current file with selected line history",
174182
func = M.diff_commit_line,
175183
},
176-
{ value = "Checkout from reflog", func = M.checkout_reflog },
184+
{
185+
value = "Diff file with branch",
186+
func = M.diff_branch_file,
187+
},
188+
{
189+
value = "Changed on current branch (experimental)",
190+
func = M.changed_on_branch,
191+
},
192+
{
193+
value = "Checkout from reflog",
194+
func = M.checkout_reflog,
195+
},
196+
}
197+
198+
local builtin_git_functions = {
199+
{
200+
value = "Git commits [telescope.builtin]",
201+
func = require("telescope.builtin").git_commits,
202+
},
203+
{
204+
value = "Git branches [telescope.builtin]",
205+
func = require("telescope.builtin").git_branches,
206+
},
207+
{
208+
value = "Git status [telescope.builtin]",
209+
func = require("telescope.builtin").git_status,
210+
},
211+
{
212+
value = "Git stash [telescope.builtin]",
213+
func = require("telescope.builtin").git_stash,
214+
},
177215
}
178216

179217
local function map_item(git_functions_table, f)
@@ -184,8 +222,23 @@ local function map_item(git_functions_table, f)
184222
return t
185223
end
186224

225+
local git_functions_table = function()
226+
local t = {}
227+
for _, v in pairs(custom_git_functions) do
228+
t[#t + 1] = v
229+
end
230+
231+
if config.show_builtin_git_pickers() then
232+
for _, v in pairs(builtin_git_functions) do
233+
t[#t + 1] = v
234+
end
235+
end
236+
237+
return t
238+
end
239+
187240
local function execute_git_function(value)
188-
for _, v in pairs(git_functions) do
241+
for _, v in pairs(git_functions_table()) do
189242
if v["value"] == value then
190243
v["func"]()
191244
return
@@ -195,12 +248,14 @@ end
195248

196249
--- Opens all a selector for all advanced git search functions
197250
M.show_custom_functions = function()
251+
local keys = map_item(git_functions_table(), function(item)
252+
return item["value"]
253+
end)
254+
198255
pickers
199256
.new({
200-
results_title = "Git action",
201-
finder = finders.new_table(map_item(git_functions, function(item)
202-
return item["value"]
203-
end)),
257+
prompt_title = "Git actions",
258+
finder = finders.new_table(keys),
204259
sorter = sorters.get_fuzzy_file(),
205260
attach_mappings = function(_, map)
206261
ags_mappings.omnimap(map, "<CR>", function(prompt_bufnr)
@@ -215,4 +270,10 @@ M.show_custom_functions = function()
215270
:find()
216271
end
217272

273+
vim.api.nvim_create_user_command(
274+
"AdvancedGitSearch",
275+
"lua require('telescope').extensions.advanced_git_search.show_custom_functions()",
276+
{ range = true }
277+
)
278+
218279
return M

lua/advanced_git_search/utils/config.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ M.setup = function(ext_config)
77

88
ext_config.diff_plugin = ext_config.diff_plugin or "fugitive"
99
ext_config.git_diff_flags = ext_config.git_diff_flags or {}
10+
ext_config.show_builtin_git_pickers = ext_config.show_builtin_git_pickers
11+
or false
1012

1113
config = ext_config
1214
end
@@ -76,4 +78,8 @@ M.diff_plugin = function()
7678
return diff_plugin
7779
end
7880

81+
M.show_builtin_git_pickers = function()
82+
return config["show_builtin_git_pickers"]
83+
end
84+
7985
return M

0 commit comments

Comments
 (0)