Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into fix-multiple-clients
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
williamboman committed Apr 14, 2021
2 parents b8413d2 + 07d5181 commit 22f0879
Show file tree
Hide file tree
Showing 26 changed files with 788 additions and 438 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
build/
doc/tags

.luacheckcache
1 change: 1 addition & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ ignore = {
}

globals = {
"_",
"TelescopeGlobalState",
"TelescopeCachedUppers",
"TelescopeCachedTails",
Expand Down
35 changes: 21 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Community driven built-in [pickers](#pickers), [sorters](#sorters) and [previewe
- [Gallery](https://github.com/nvim-telescope/telescope.nvim/wiki/Gallery)
- [FAQ](#faq)
- [Configuration recipes](https://github.com/nvim-telescope/telescope.nvim/wiki/Configuration-Recipes)
- [Contributing](#contribution)
- [Contributing](#contributing)

## Getting Started

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

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

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

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

### Options affecting Presentation

Expand Down Expand Up @@ -374,6 +374,14 @@ Built-in functions. Ready to be bound to any key you like. :smile:
| `builtin.live_grep` | Searches in current directory files. (respecting .gitignore) |
| `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 |

#### Options for builtin.live_grep

| Keys | Description | Options |
|------------------------|-------------------------------------------------------|--------------|
| `grep_open_files` | Restrict live_grep to currently open files. | boolean |
| `search_dirs` | List of directories to search in. | list |


### Vim Pickers

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

### Treesitter Picker

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

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

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

Telescope user autocmds:

| Event | Description |
|---------------------------------|----------------------------------------------------|
| `User TelescopeFindPre` | Do it before create Telescope all the float window |
| `User TelescopePreviewerLoaded` | Do it after Telescope previewer window create |
| Event | Description |
|---------------------------------|---------------------------------------------------------|
| `User TelescopeFindPre` | Do it before Telescope creates all the floating windows |
| `User TelescopePreviewerLoaded` | Do it after Telescope previewer window is created |


## Extensions

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

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

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

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

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

### How to change Telescope Highlights group?

There are 10 highlights group you can play around with in order to meet your needs:
There are 10 highlight groups you can play around with in order to meet your needs:

```viml
highlight TelescopeSelection guifg=#D79921 gui=bold " selected item
Expand Down
40 changes: 40 additions & 0 deletions lua/telescope/actions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,38 @@ actions.insert_value = function(prompt_bufnr)
return entry.value
end

--- Create and checkout a new git branch if it doesn't already exist
---@param prompt_bufnr number: The prompt bufnr
actions.git_create_branch = function(prompt_bufnr)
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
local new_branch = action_state.get_current_line()

if new_branch == "" then
print('Please enter the name of the new branch to create')
else
local confirmation = vim.fn.input(string.format('Create new branch "%s"? [y/n]: ', new_branch))
if string.len(confirmation) == 0 or string.sub(string.lower(confirmation), 0, 1) ~= 'y' then
print(string.format('Didn\'t create branch "%s"', new_branch))
return
end

actions.close(prompt_bufnr)

local _, ret, stderr = utils.get_os_command_output({ 'git', 'checkout', '-b', new_branch }, cwd)
if ret == 0 then
print(string.format('Switched to a new branch: %s', new_branch))
else
print(string.format(
'Error when creating new branch: %s Git returned "%s"',
new_branch,
table.concat(stderr, ' ')
))
end
end
end

--- Checkout an existing git branch
---@param prompt_bufnr number: The prompt bufnr
actions.git_checkout = function(prompt_bufnr)
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
local selection = action_state.get_selected_entry()
Expand All @@ -313,6 +345,8 @@ actions.git_checkout = function(prompt_bufnr)
end
end

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

--- Delete the currently selected branch
---@param prompt_bufnr number: The prompt bufnr
actions.git_delete_branch = function(prompt_bufnr)
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
local selection = action_state.get_selected_entry()
Expand All @@ -349,6 +385,8 @@ actions.git_delete_branch = function(prompt_bufnr)
end
end

--- Rebase to selected git branch
---@param prompt_bufnr number: The prompt bufnr
actions.git_rebase_branch = function(prompt_bufnr)
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
local selection = action_state.get_selected_entry()
Expand All @@ -369,6 +407,8 @@ actions.git_rebase_branch = function(prompt_bufnr)
end
end

--- Stage/unstage selected file
---@param prompt_bufnr number: The prompt bufnr
actions.git_staging_toggle = function(prompt_bufnr)
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
local selection = action_state.get_selected_entry()
Expand Down
98 changes: 85 additions & 13 deletions lua/telescope/builtin/files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ local Path = require('plenary.path')
local os_sep = Path.path.sep

local flatten = vim.tbl_flatten
local filter = vim.tbl_filter

local files = {}

Expand All @@ -29,10 +30,28 @@ end

-- Special keys:
-- opts.search_dirs -- list of directory to search in
-- opts.grep_open_files -- boolean to restrict search to open files
files.live_grep = function(opts)
local vimgrep_arguments = opts.vimgrep_arguments or conf.vimgrep_arguments
local search_dirs = opts.search_dirs
opts.cwd = opts.cwd and vim.fn.expand(opts.cwd)
local grep_open_files = opts.grep_open_files
opts.cwd = opts.cwd and vim.fn.expand(opts.cwd) or vim.loop.cwd()

local filelist = {}

if grep_open_files then
local bufnrs = filter(function(b)
if 1 ~= vim.fn.buflisted(b) then return false end
return true
end, vim.api.nvim_list_bufs())
if not next(bufnrs) then return end

local tele_path = require'telescope.path'
for _, bufnr in ipairs(bufnrs) do
local file = vim.api.nvim_buf_get_name(bufnr)
table.insert(filelist, tele_path.make_relative(file, opts.cwd))
end
end

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

prompt = escape_chars(prompt)

local args = flatten { vimgrep_arguments, prompt }
local search_list = {}

if search_dirs then
table.insert(args, search_dirs)
table.insert(search_list, search_dirs)
elseif os_sep == '\\' then
table.insert(args, '.')
table.insert(search_list, '.')
end

return args
if grep_open_files then
search_list = filelist
end

return flatten { vimgrep_arguments, prompt, search_list }
end,
opts.entry_maker or make_entry.gen_from_vimgrep(opts),
opts.max_results,
Expand All @@ -72,6 +95,7 @@ files.live_grep = function(opts)
}):find()
end


-- Special keys:
-- opts.search -- the string to search.
-- opts.search_dirs -- list of directory to search in
Expand Down Expand Up @@ -211,16 +235,16 @@ end
files.file_browser = function(opts)
opts = opts or {}

opts.depth = opts.depth or 1
opts.cwd = opts.cwd and vim.fn.expand(opts.cwd) or vim.loop.cwd()

local gen_new_finder = function(path)
opts.new_finder = opts.new_finder or function(path)
opts.cwd = path
local data = {}

scan.scan_dir(path, {
hidden = opts.hidden or false,
add_dirs = true,
depth = 1,
depth = opts.depth,
on_insert = function(entry, typ)
table.insert(data, typ == 'directory' and (entry .. os_sep) or entry)
end
Expand All @@ -242,8 +266,8 @@ files.file_browser = function(opts)
end

pickers.new(opts, {
prompt_title = 'Find Files',
finder = gen_new_finder(opts.cwd),
prompt_title = 'File Browser',
finder = opts.new_finder(opts.cwd),
previewer = conf.file_previewer(opts),
sorter = conf.file_sorter(opts),
attach_mappings = function(prompt_bufnr, map)
Expand All @@ -253,7 +277,7 @@ files.file_browser = function(opts)
local new_cwd = vim.fn.expand(action_state.get_selected_entry().path:sub(1, -2))
local current_picker = action_state.get_current_picker(prompt_bufnr)
current_picker.cwd = new_cwd
current_picker:refresh(gen_new_finder(new_cwd), { reset_prompt = true })
current_picker:refresh(opts.new_finder(new_cwd), { reset_prompt = true })
end)

local create_new_file = function()
Expand All @@ -276,7 +300,7 @@ files.file_browser = function(opts)
Path:new(fpath:sub(1, -2)):mkdir({ parents = true })
local new_cwd = vim.fn.expand(fpath)
current_picker.cwd = new_cwd
current_picker:refresh(gen_new_finder(new_cwd), { reset_prompt = true })
current_picker:refresh(opts.new_finder(new_cwd), { reset_prompt = true })
end
end

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

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

for lnum, line in ipairs(lines) do
table.insert(lines_with_numbers, {
lnum = lnum,
line = line,
bufnr = bufnr,
filename = filename,
text = line,
})
end

local ok, parser = pcall(vim.treesitter.get_parser, bufnr, filetype)
if ok then
local query = vim.treesitter.get_query(filetype, "highlights")

local root = parser:parse()[1]:root()

local highlighter = vim.treesitter.highlighter.new(parser)
local highlighter_query = highlighter:get_query(filetype)

local line_highlights = setmetatable({}, {
__index = function(t, k)
local obj = {}
rawset(t, k, obj)
return obj
end,
})
for id, node in query:iter_captures(root, bufnr, 0, -1) do
local hl = highlighter_query.hl_cache[id]
if hl then
local row1, col1, row2, col2 = node:range()

if row1 == row2 then
local row = row1 + 1

for index = col1, col2 do
line_highlights[row][index] = hl
end
else
local row = row1 + 1
for index = col1, #lines[row] do
line_highlights[row][index] = hl
end

while row < row2 + 1 do
row = row + 1

for index = 0, #lines[row] do
line_highlights[row][index] = hl
end
end
end
end
end

opts.line_highlights = line_highlights
end

pickers.new(opts, {
Expand Down
Loading

0 comments on commit 22f0879

Please sign in to comment.