Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update nvim in CI workflow #61

Open
wants to merge 12 commits into
base: dev
Choose a base branch
from
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ jobs:
manager: sudo apt-get
packages: -y fd-find mlocate
- os: ubuntu-20.04
url: https://github.com/neovim/neovim/releases/download/v0.7.0/nvim-linux64.tar.gz
url: https://github.com/neovim/neovim/releases/download/v0.9.1/nvim-linux64.tar.gz
manager: sudo apt-get
packages: -y fd-find mlocate
- os: macos-10.15
url: https://github.com/neovim/neovim/releases/download/nightly/nvim-macos.tar.gz
manager: brew
packages: fd
- os: macos-10.15
url: https://github.com/neovim/neovim/releases/download/v0.7.0/nvim-macos.tar.gz
url: https://github.com/neovim/neovim/releases/download/v0.9.1/nvim-macos.tar.gz
manager: brew
packages: fd
steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ jobs:
token: ${{ secrets.GITHUB_TOKEN }}
# CLI arguments
args: --check .
version: 0.13.1
version: v0.14.3
35 changes: 27 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ end_insert -->
# 🦘 telescope-repo.nvim: jump around the repositories in your filesystem, without any setup
<!-- end_remove -->

![Neovim version](https://img.shields.io/badge/Neovim-0.5-57A143?style=flat&logo=neovim) [![](https://img.shields.io/badge/powered%20by-riss-lightgrey)](https://cj.rs/riss) ![GitHub tag (latest SemVer)](https://img.shields.io/github/v/tag/cljoly/telescope-repo.nvim?color=darkgreen&sort=semver)
![Neovim version](https://img.shields.io/badge/Neovim-0.7-57A143?style=flat&logo=neovim) [![](https://img.shields.io/badge/powered%20by-riss-lightgrey)](https://cj.rs/riss) ![GitHub tag (latest SemVer)](https://img.shields.io/github/v/tag/cljoly/telescope-repo.nvim?color=darkgreen&sort=semver)

<!-- insert
{{< rawhtml >}}
Expand Down Expand Up @@ -121,6 +121,9 @@ You can change the default argument given to subcommands (like [`list`](#list) o
"value",
},
},
settings = {
auto_lcd = true,
}
},
},
}
Expand Down Expand Up @@ -184,17 +187,17 @@ Transform the result paths into relative ones with this value as the base dir.

Default value: `vim.fn.getcwd()`

##### `fd_opts`
##### `fd_opts` & `find_exec_opts`

**This is a relatively advanced option that you should use with caution. There is no guarantee that a particular set of options would work the same across multiple versions**
**These are relatively advanced options that you should use with caution. There is no guarantee that a particular set of options would work the same across multiple versions of the plugin**

This passes additional options to the command `fd` that generates the repository list. It is inserted like so:
This passes additional exec options to the command `fd` that generates the repository list. It is inserted like so:

```
fd [set of default options] [fd_opts] --exec [some default command] [pattern] …
fd [set of default options] [fd_opts] --exec [find_exec_opts] [pattern] …
```

##### Example
###### `fd_opts` Example

Let’s say you have a git repository `S` inside another git repository `M` (for instance because of [#5](https://github.com/cljoly/telescope-repo.nvim/issues/5)), but `S` is in a directory that’s ignored in the `.gitignore` in `M`. `S` wouldn’t appear in the Telescope list of this extension by default, because it is ignored (`.gitignore` are taken into account by default).

Expand All @@ -204,7 +207,23 @@ To avoid taking into account the `.gitignore`, we need to pass `--no-ignore-vcs`
:lua require'telescope'.extensions.repo.list{fd_opts={'--no-ignore-vcs'}}
```

This will list `M` and `S` in the Telescope output! The downside is that listing repositories will be a little longer, as we don’t skip the git-ignored files anymore.
This will list `M` and `S` in the Telescope output. The downside is that listing repositories will be a little longer, as we don’t skip the git-ignored files anymore.

###### `fd_exec_opts` Example

This is mainly to accommodate different OS shells like PowerShell on windows.

Let’s say you try to use telescope repo in windows powershell, then the command could be something like this to make it work

```
fd --exec powershell /C "echo {//}" ; ^\.git$
```

To use an equivalent command with the plugin, run:

```
:lua require'telescope'.extensions.repo.list{fd_exec_opts={'powershell /C "echo {//}"'}}
```

##### `search_dirs`

Expand Down Expand Up @@ -291,7 +310,7 @@ if you encounter any problems. If it’s not the case by default, you should aut

Options are the similar to `repo list`, bearing in mind that we use `locate` instead of `fd`. Note that the following `list` options are not supported in `cached_list`:

* `fd_opts`, as we don’t use `fd` with `cached_list`,
* `fd_opts` and `fd_exec_opts`, as we don’t use `fd` with `cached_list`,
* `search_dirs`, as `locate` does not accept a directory to search in.

#### Examples
Expand Down
10 changes: 6 additions & 4 deletions lua/telescope/_extensions/repo.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
local main = require("telescope._extensions.repo.main")
local r_config = require("telescope._extensions.repo.config")
local health = require("telescope._extensions.repo.health")

local fallback_error = { "Falling back to `:Telescope repo list`, but this behavior may change in the future" }

return require("telescope").register_extension({
health = health.check,
setup = r_config.setup,
health = function()
require("telescope._extensions.repo.health").check()
end,
setup = function(opts)
require("telescope._extensions.repo.config").setup(opts)
end,
exports = {
list = main.list,
cached_list = main.cached_list,
Expand Down
50 changes: 50 additions & 0 deletions lua/telescope/_extensions/repo/autocmd_lcd.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
local Path = require("plenary.path")

local M = {}

M.active = false

-- List of absolute paths to projects opened with the extension
local project_paths = {}

-- Add a project path so that the autocmd will lcd to it if a file in that path is opened
function M.add_project(path)
local abs_path = Path:new(path)
abs_path = abs_path:absolute()
project_paths[abs_path] = true
end

-- Find the best suited project path. Can be passed a file
local function find_project(path_or_file)
local buf_path = Path:new(path_or_file)
local abs_buf_path = buf_path:absolute()
while not (project_paths[abs_buf_path] or abs_buf_path == "/") do
buf_path = buf_path:parent()
abs_buf_path = buf_path:absolute()
end
if abs_buf_path ~= "/" then
return abs_buf_path
end
return nil
end

-- Define autocmd to change the folder of the current file (with lcd).
function M.setup()
M.active = true
-- Ensure we create only one autocmd, even if the function is called multiple times
local autocmd_group = vim.api.nvim_create_augroup("telescope_repo_lcd", { clear = true })

vim.api.nvim_create_autocmd({ "BufNewFile", "BufRead" }, {
callback = function()
local path_or_file = vim.fn.expand("%")
local project_path = find_project(path_or_file)
if project_paths then
vim.cmd("lcd " .. project_path)
end
end,
group = autocmd_group,
desc = "lcd to the deepest project directory",
})
end

return M
7 changes: 5 additions & 2 deletions lua/telescope/_extensions/repo/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ local M = {}

M.values = {}

M.setup = function(opts)
M.values = opts
function M.setup(opts)
M.values = opts or {}
if M.values.settings and M.values.settings.auto_lcd then
require("telescope._extensions.repo.autocmd_lcd").setup()
end
end

return M
2 changes: 1 addition & 1 deletion lua/telescope/_extensions/repo/list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ M.prepare_command = function(opts)
-- .git file in them.
local find_repo_opts = { "--hidden", "--case-sensitive", "--absolute-path" }
local find_user_opts = opts.fd_opts or {}
local find_exec_opts = { "--exec", "echo", [[{//}]], ";" }
local find_exec_opts = opts.fd_exec_opts or { "--exec", "echo", [[{//}]], ";" }

-- Expand '~'
local search_dirs = {}
Expand Down
132 changes: 75 additions & 57 deletions lua/telescope/_extensions/repo/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ local t_utils = require("telescope.utils")
local Path = require("plenary.path")

-- Other modules in this plugin
local autocmd_lcd = require("telescope._extensions.repo.autocmd_lcd")
local utils = require("telescope._extensions.repo.utils")
local list = require("telescope._extensions.repo.list")
local cached_list = require("telescope._extensions.repo.cached_list")
Expand Down Expand Up @@ -130,78 +131,95 @@ local function project_live_grep(opts)
require("telescope.builtin").live_grep(opts)
end

local function call_picker(opts, command, prompt_title_supplement)
local function call_picker(list_opts, command, prompt_title_supplement, user_opts)
if list_opts == nil then
error([[
Incorrect call to call_picker, list_opts should be specified to pass relevant options to the first picker]])
end
if user_opts == nil then
error([[
Incorrect call to call_picker, user_opts should be specified to pass relevant options to the second picker]])
end

local prompt_title = "Git repositories"
if prompt_title_supplement ~= nil then
prompt_title = prompt_title .. prompt_title_supplement
end
pickers.new(opts, {
prompt_title = prompt_title,
finder = finders.new_oneshot_job(command, opts),
previewer = previewers.new_termopen_previewer({
get_command = function(entry)
local dir = Path:new(from_entry.path(entry))
local doc = search_markdown_readme(dir)
if doc then
return utils.find_markdown_previewer_for_document(doc.filename)
end
doc = search_generic_readme(dir)
if not doc then
-- TODO: doc may be previewed in a plain text. Can I use syntax highlight?
doc = search_doc(dir)
end
if not doc then
return { "echo", "" }
end
return utils.find_generic_previewer_for_document(doc.filename)
pickers
.new(list_opts, {
prompt_title = prompt_title,
finder = finders.new_oneshot_job(command, list_opts),
previewer = previewers.new_termopen_previewer({
get_command = function(entry)
local dir = Path:new(from_entry.path(entry))
local doc = search_markdown_readme(dir)
if doc then
return utils.find_markdown_previewer_for_document(doc.filename)
end
doc = search_generic_readme(dir)
if not doc then
-- TODO: doc may be previewed in a plain text. Can I use syntax highlight?
doc = search_doc(dir)
end
if not doc then
return { "echo", "" }
end
return utils.find_generic_previewer_for_document(doc.filename)
end,
}),
sorter = conf.file_sorter(list_opts),
attach_mappings = function(prompt_bufnr)
actions_set.select:replace(function(_, type)
local entry = actions_state.get_selected_entry()
local dir = from_entry.path(entry)
if autocmd_lcd.active and type ~= "" then
autocmd_lcd.add_project(dir)
end

if type == "default" then
actions._close(prompt_bufnr, false)
vim.schedule(function()
project_files(vim.tbl_extend("force", user_opts, { cwd = dir }))
end)
end
if type == "vertical" then
actions._close(prompt_bufnr, false)
vim.schedule(function()
project_live_grep(vim.tbl_extend("force", list_opts, { cwd = dir }))
end)
return
end
if type == "tab" then
vim.cmd("tabe " .. dir)
vim.cmd("tcd " .. dir)
project_files(vim.tbl_extend("force", list_opts, { cwd = dir }))
return
end
end)
return true
end,
}),
sorter = conf.file_sorter(opts),
attach_mappings = function(prompt_bufnr)
actions_set.select:replace(function(_, type)
local entry = actions_state.get_selected_entry()
local dir = from_entry.path(entry)
if type == "default" then
actions._close(prompt_bufnr, false)
vim.schedule(function()
project_files({ cwd = dir })
end)
end
if type == "vertical" then
actions._close(prompt_bufnr, false)
vim.schedule(function()
project_live_grep({ cwd = dir })
end)
return
end
if type == "tab" then
vim.cmd("tabe " .. dir)
vim.cmd("tcd " .. dir)
project_files({ cwd = dir })
return
end
end)
return true
end,
}):find()
})
:find()
end

-- List of repos built using locate (or variants)
M.cached_list = function(opts)
opts = vim.tbl_deep_extend("force", r_config.values.cached_list or {}, opts or {})
opts.entry_maker = t_utils.get_lazy_default(opts.entry_maker, gen_from_locate_wrapper, opts)
local locate_command = cached_list.prepare_command(opts)
local common_opts = opts or {}
local list_opts = vim.tbl_deep_extend("force", r_config.values.cached_list or {}, common_opts)
list_opts.entry_maker = t_utils.get_lazy_default(list_opts.entry_maker, gen_from_locate_wrapper, list_opts)
local locate_command = cached_list.prepare_command(list_opts)

call_picker(opts, locate_command, " (cached)")
call_picker(list_opts, locate_command, " (cached)", common_opts)
end

-- Always up to date list of repos built using fd
M.list = function(opts)
opts = vim.tbl_deep_extend("force", r_config.values.list or {}, opts or {})
opts.entry_maker = t_utils.get_lazy_default(opts.entry_maker, gen_from_fd, opts)
local fd_command = list.prepare_command(opts)
local common_opts = opts or {}
local list_opts = vim.tbl_deep_extend("force", r_config.values.list or {}, common_opts)
list_opts.entry_maker = t_utils.get_lazy_default(list_opts.entry_maker, gen_from_fd, list_opts)
local fd_command = list.prepare_command(list_opts)

call_picker(opts, fd_command, " (built on the fly)")
call_picker(list_opts, fd_command, " (built on the fly)", common_opts)
end

return M
2 changes: 1 addition & 1 deletion lua/telescope/_extensions/repo/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ M.find_locate_binary = function()
return find_binary({ "plocate", "lolcate", "glocate", "locate" })
end

M._generic_previewer = { { "bat", "--style", "header,grid" }, { "cat" } }
M._generic_previewer = { { "bat", "--style", "header,grid" }, { "batcat", "--style", "header,grid" }, { "cat" } }

M.find_generic_previewer_for_document = function(doc)
local l = find_binary(M._generic_previewer)
Expand Down