Skip to content

Commit 22f0879

Browse files
committed
Merge remote-tracking branch 'upstream/master' into fix-multiple-clients
* upstream/master: picker(live_grep): add option to grep only over open files (nvim-telescope#666) git(action): create and checkout branch (nvim-telescope#755) readme: fix broken links and spelling errors (nvim-telescope#753) added a new DynamicFinder (which can be used with rust_analyzer) (nvim-telescope#705) feat: add icons to git_status finder (nvim-telescope#401) fix: update to newer code (nvim-telescope#744) pickers(buffers): added only_cwd opt (nvim-telescope#739) feat: asyncify pickers - except for live_grep (nvim-telescope#709) fix: Use standardized names for current buffer fuzzy find (nvim-telescope#737) fix(git_branches): use the quoted fields instead of json-formatting and fix regressions with nvim-telescope#695 (nvim-telescope#704) feat: buf highlights for current buffer fuzzy find (nvim-telescope#732)
2 parents b8413d2 + 07d5181 commit 22f0879

26 files changed

Lines changed: 788 additions & 438 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
build/
22
doc/tags
3+
4+
.luacheckcache

.luacheckrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ ignore = {
1313
}
1414

1515
globals = {
16+
"_",
1617
"TelescopeGlobalState",
1718
"TelescopeCachedUppers",
1819
"TelescopeCachedTails",

README.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Community driven built-in [pickers](#pickers), [sorters](#sorters) and [previewe
4040
- [Gallery](https://github.com/nvim-telescope/telescope.nvim/wiki/Gallery)
4141
- [FAQ](#faq)
4242
- [Configuration recipes](https://github.com/nvim-telescope/telescope.nvim/wiki/Configuration-Recipes)
43-
- [Contributing](#contribution)
43+
- [Contributing](#contributing)
4444

4545
## Getting Started
4646

@@ -113,7 +113,7 @@ This section should help you explore available options to configure and
113113
customize your `telescope.nvim`.
114114

115115
Unlike most vim plugins, `telescope.nvim` can be customized either by applying
116-
customizations globally or individual pre picker.
116+
customizations globally or individual per picker.
117117

118118
- **Global Customization** affecting all pickers can be done through the
119119
main `setup()` method (see defaults below)
@@ -190,7 +190,7 @@ require('telescope').setup{
190190
EOF
191191
```
192192

193-
<!-- TODO: move some options to Options affecting Behaviour -->
193+
<!-- TODO: move some options to Options affecting behavior -->
194194

195195
### Options affecting Presentation
196196

@@ -374,6 +374,14 @@ Built-in functions. Ready to be bound to any key you like. :smile:
374374
| `builtin.live_grep` | Searches in current directory files. (respecting .gitignore) |
375375
| `builtin.file_browser` | Ivy-like file explorer. Creates files by typing in filename and pressing `<C-e>`. Press `<C-e>` without prompt for more info |
376376

377+
#### Options for builtin.live_grep
378+
379+
| Keys | Description | Options |
380+
|------------------------|-------------------------------------------------------|--------------|
381+
| `grep_open_files` | Restrict live_grep to currently open files. | boolean |
382+
| `search_dirs` | List of directories to search in. | list |
383+
384+
377385
### Vim Pickers
378386

379387
| Functions | Description |
@@ -419,7 +427,7 @@ Built-in functions. Ready to be bound to any key you like. :smile:
419427
| `builtin.git_commits` | Lists git commits with diff preview and on enter checkout the commit. |
420428
| `builtin.git_bcommits` | Lists buffer's git commits with diff preview and checkouts it out on enter. |
421429
| `builtin.git_branches` | Lists all branches with log preview, checkout action (<cr>), track action (<c-t>) and rebase action(<c-r>). |
422-
| `builtin.git_status` | Lists current changes per file with diff preview and add action. (Multiselection still WIP) |
430+
| `builtin.git_status` | Lists current changes per file with diff preview and add action. (Multi-selection still WIP) |
423431

424432
### Treesitter Picker
425433

@@ -472,7 +480,7 @@ autocmd User TelescopePreviewerLoaded setlocal wrap
472480
| `sorters.fuzzy_with_index_bias` | Used to list stuff with consideration to when the item is added |
473481

474482
A `Sorter` is called by the `Picker` on each item returned by the `Finder`. It
475-
return a number, which is equivalent to the "distance" between the current
483+
returns a number, which is equivalent to the "distance" between the current
476484
`prompt` and the `entry` returned by a `finder`.
477485

478486
<!-- TODO review -->
@@ -511,15 +519,15 @@ make a theme, check out `lua/telescope/themes.lua`.
511519

512520
Telescope user autocmds:
513521

514-
| Event | Description |
515-
|---------------------------------|----------------------------------------------------|
516-
| `User TelescopeFindPre` | Do it before create Telescope all the float window |
517-
| `User TelescopePreviewerLoaded` | Do it after Telescope previewer window create |
522+
| Event | Description |
523+
|---------------------------------|---------------------------------------------------------|
524+
| `User TelescopeFindPre` | Do it before Telescope creates all the floating windows |
525+
| `User TelescopePreviewerLoaded` | Do it after Telescope previewer window is created |
518526

519527

520528
## Extensions
521529

522-
Telescope provides the capabilties to create & register extensions, which improve telescope in a variety of ways.
530+
Telescope provides the capabilities to create & register extensions, which improve telescope in a variety of ways.
523531

524532
Some extensions provide integration with external tools, outside of the scope of `builtins`. Others provide performance
525533
enhancements by using compiled C and interfacing directly with Lua.
@@ -669,9 +677,8 @@ supports tab completions and settings options.
669677

670678
### How to change some defaults in built-in functions?
671679

672-
All options available from the setup function (see [Configuration options]()) and
673-
some other functions can be easily changed in custom pickers or built-in
674-
functions.
680+
All options available from the setup function (see [Configuration options](#customization)
681+
and some other functions can be easily changed in custom pickers or built-in functions.
675682
<!-- TODO: insert a list of available options like previewer and prompt prefix -->
676683

677684
```lua
@@ -685,7 +692,7 @@ nnoremap <leader>fg :Telescope live_grep prompt_prefix=🔍<CR>
685692

686693
### How to change Telescope Highlights group?
687694

688-
There are 10 highlights group you can play around with in order to meet your needs:
695+
There are 10 highlight groups you can play around with in order to meet your needs:
689696

690697
```viml
691698
highlight TelescopeSelection guifg=#D79921 gui=bold " selected item

lua/telescope/actions/init.lua

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,38 @@ actions.insert_value = function(prompt_bufnr)
297297
return entry.value
298298
end
299299

300+
--- Create and checkout a new git branch if it doesn't already exist
301+
---@param prompt_bufnr number: The prompt bufnr
302+
actions.git_create_branch = function(prompt_bufnr)
303+
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
304+
local new_branch = action_state.get_current_line()
305+
306+
if new_branch == "" then
307+
print('Please enter the name of the new branch to create')
308+
else
309+
local confirmation = vim.fn.input(string.format('Create new branch "%s"? [y/n]: ', new_branch))
310+
if string.len(confirmation) == 0 or string.sub(string.lower(confirmation), 0, 1) ~= 'y' then
311+
print(string.format('Didn\'t create branch "%s"', new_branch))
312+
return
313+
end
314+
315+
actions.close(prompt_bufnr)
316+
317+
local _, ret, stderr = utils.get_os_command_output({ 'git', 'checkout', '-b', new_branch }, cwd)
318+
if ret == 0 then
319+
print(string.format('Switched to a new branch: %s', new_branch))
320+
else
321+
print(string.format(
322+
'Error when creating new branch: %s Git returned "%s"',
323+
new_branch,
324+
table.concat(stderr, ' ')
325+
))
326+
end
327+
end
328+
end
329+
330+
--- Checkout an existing git branch
331+
---@param prompt_bufnr number: The prompt bufnr
300332
actions.git_checkout = function(prompt_bufnr)
301333
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
302334
local selection = action_state.get_selected_entry()
@@ -313,6 +345,8 @@ actions.git_checkout = function(prompt_bufnr)
313345
end
314346
end
315347

348+
--- Tell git to track the currently selected remote branch in Telescope
349+
---@param prompt_bufnr number: The prompt bufnr
316350
actions.git_track_branch = function(prompt_bufnr)
317351
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
318352
local selection = action_state.get_selected_entry()
@@ -329,6 +363,8 @@ actions.git_track_branch = function(prompt_bufnr)
329363
end
330364
end
331365

366+
--- Delete the currently selected branch
367+
---@param prompt_bufnr number: The prompt bufnr
332368
actions.git_delete_branch = function(prompt_bufnr)
333369
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
334370
local selection = action_state.get_selected_entry()
@@ -349,6 +385,8 @@ actions.git_delete_branch = function(prompt_bufnr)
349385
end
350386
end
351387

388+
--- Rebase to selected git branch
389+
---@param prompt_bufnr number: The prompt bufnr
352390
actions.git_rebase_branch = function(prompt_bufnr)
353391
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
354392
local selection = action_state.get_selected_entry()
@@ -369,6 +407,8 @@ actions.git_rebase_branch = function(prompt_bufnr)
369407
end
370408
end
371409

410+
--- Stage/unstage selected file
411+
---@param prompt_bufnr number: The prompt bufnr
372412
actions.git_staging_toggle = function(prompt_bufnr)
373413
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
374414
local selection = action_state.get_selected_entry()

lua/telescope/builtin/files.lua

Lines changed: 85 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ local Path = require('plenary.path')
1313
local os_sep = Path.path.sep
1414

1515
local flatten = vim.tbl_flatten
16+
local filter = vim.tbl_filter
1617

1718
local files = {}
1819

@@ -29,10 +30,28 @@ end
2930

3031
-- Special keys:
3132
-- opts.search_dirs -- list of directory to search in
33+
-- opts.grep_open_files -- boolean to restrict search to open files
3234
files.live_grep = function(opts)
3335
local vimgrep_arguments = opts.vimgrep_arguments or conf.vimgrep_arguments
3436
local search_dirs = opts.search_dirs
35-
opts.cwd = opts.cwd and vim.fn.expand(opts.cwd)
37+
local grep_open_files = opts.grep_open_files
38+
opts.cwd = opts.cwd and vim.fn.expand(opts.cwd) or vim.loop.cwd()
39+
40+
local filelist = {}
41+
42+
if grep_open_files then
43+
local bufnrs = filter(function(b)
44+
if 1 ~= vim.fn.buflisted(b) then return false end
45+
return true
46+
end, vim.api.nvim_list_bufs())
47+
if not next(bufnrs) then return end
48+
49+
local tele_path = require'telescope.path'
50+
for _, bufnr in ipairs(bufnrs) do
51+
local file = vim.api.nvim_buf_get_name(bufnr)
52+
table.insert(filelist, tele_path.make_relative(file, opts.cwd))
53+
end
54+
end
3655

3756
if search_dirs then
3857
for i, path in ipairs(search_dirs) do
@@ -49,15 +68,19 @@ files.live_grep = function(opts)
4968

5069
prompt = escape_chars(prompt)
5170

52-
local args = flatten { vimgrep_arguments, prompt }
71+
local search_list = {}
5372

5473
if search_dirs then
55-
table.insert(args, search_dirs)
74+
table.insert(search_list, search_dirs)
5675
elseif os_sep == '\\' then
57-
table.insert(args, '.')
76+
table.insert(search_list, '.')
5877
end
5978

60-
return args
79+
if grep_open_files then
80+
search_list = filelist
81+
end
82+
83+
return flatten { vimgrep_arguments, prompt, search_list }
6184
end,
6285
opts.entry_maker or make_entry.gen_from_vimgrep(opts),
6386
opts.max_results,
@@ -72,6 +95,7 @@ files.live_grep = function(opts)
7295
}):find()
7396
end
7497

98+
7599
-- Special keys:
76100
-- opts.search -- the string to search.
77101
-- opts.search_dirs -- list of directory to search in
@@ -211,16 +235,16 @@ end
211235
files.file_browser = function(opts)
212236
opts = opts or {}
213237

238+
opts.depth = opts.depth or 1
214239
opts.cwd = opts.cwd and vim.fn.expand(opts.cwd) or vim.loop.cwd()
215-
216-
local gen_new_finder = function(path)
240+
opts.new_finder = opts.new_finder or function(path)
217241
opts.cwd = path
218242
local data = {}
219243

220244
scan.scan_dir(path, {
221245
hidden = opts.hidden or false,
222246
add_dirs = true,
223-
depth = 1,
247+
depth = opts.depth,
224248
on_insert = function(entry, typ)
225249
table.insert(data, typ == 'directory' and (entry .. os_sep) or entry)
226250
end
@@ -242,8 +266,8 @@ files.file_browser = function(opts)
242266
end
243267

244268
pickers.new(opts, {
245-
prompt_title = 'Find Files',
246-
finder = gen_new_finder(opts.cwd),
269+
prompt_title = 'File Browser',
270+
finder = opts.new_finder(opts.cwd),
247271
previewer = conf.file_previewer(opts),
248272
sorter = conf.file_sorter(opts),
249273
attach_mappings = function(prompt_bufnr, map)
@@ -253,7 +277,7 @@ files.file_browser = function(opts)
253277
local new_cwd = vim.fn.expand(action_state.get_selected_entry().path:sub(1, -2))
254278
local current_picker = action_state.get_current_picker(prompt_bufnr)
255279
current_picker.cwd = new_cwd
256-
current_picker:refresh(gen_new_finder(new_cwd), { reset_prompt = true })
280+
current_picker:refresh(opts.new_finder(new_cwd), { reset_prompt = true })
257281
end)
258282

259283
local create_new_file = function()
@@ -276,7 +300,7 @@ files.file_browser = function(opts)
276300
Path:new(fpath:sub(1, -2)):mkdir({ parents = true })
277301
local new_cwd = vim.fn.expand(fpath)
278302
current_picker.cwd = new_cwd
279-
current_picker:refresh(gen_new_finder(new_cwd), { reset_prompt = true })
303+
current_picker:refresh(opts.new_finder(new_cwd), { reset_prompt = true })
280304
end
281305
end
282306

@@ -332,17 +356,65 @@ files.current_buffer_fuzzy_find = function(opts)
332356
-- All actions are on the current buffer
333357
local bufnr = vim.api.nvim_get_current_buf()
334358
local filename = vim.fn.expand(vim.api.nvim_buf_get_name(bufnr))
359+
local filetype = vim.api.nvim_buf_get_option(bufnr, "filetype")
335360

336361
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
337362
local lines_with_numbers = {}
338363

339364
for lnum, line in ipairs(lines) do
340365
table.insert(lines_with_numbers, {
341366
lnum = lnum,
342-
line = line,
343367
bufnr = bufnr,
344368
filename = filename,
369+
text = line,
370+
})
371+
end
372+
373+
local ok, parser = pcall(vim.treesitter.get_parser, bufnr, filetype)
374+
if ok then
375+
local query = vim.treesitter.get_query(filetype, "highlights")
376+
377+
local root = parser:parse()[1]:root()
378+
379+
local highlighter = vim.treesitter.highlighter.new(parser)
380+
local highlighter_query = highlighter:get_query(filetype)
381+
382+
local line_highlights = setmetatable({}, {
383+
__index = function(t, k)
384+
local obj = {}
385+
rawset(t, k, obj)
386+
return obj
387+
end,
345388
})
389+
for id, node in query:iter_captures(root, bufnr, 0, -1) do
390+
local hl = highlighter_query.hl_cache[id]
391+
if hl then
392+
local row1, col1, row2, col2 = node:range()
393+
394+
if row1 == row2 then
395+
local row = row1 + 1
396+
397+
for index = col1, col2 do
398+
line_highlights[row][index] = hl
399+
end
400+
else
401+
local row = row1 + 1
402+
for index = col1, #lines[row] do
403+
line_highlights[row][index] = hl
404+
end
405+
406+
while row < row2 + 1 do
407+
row = row + 1
408+
409+
for index = 0, #lines[row] do
410+
line_highlights[row][index] = hl
411+
end
412+
end
413+
end
414+
end
415+
end
416+
417+
opts.line_highlights = line_highlights
346418
end
347419

348420
pickers.new(opts, {

0 commit comments

Comments
 (0)