Skip to content

Commit

Permalink
feat: refine automatic lcd, integrate with rooter
Browse files Browse the repository at this point in the history
* Fix default settings that were improperly set
* Use wider autocmd events (fixes #59) for `lcp`
    * add rooter integration (if vim-rooter is present, it’ll be
      reconfigured to play nicely with telescope-repo)
  • Loading branch information
cljoly committed May 21, 2023
1 parent f0e50ad commit d7fa23b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 17 deletions.
36 changes: 24 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,11 @@ You need to add these in your plugin management system[^2]:
'nvim-telescope/telescope.nvim'
'cljoly/telescope-repo.nvim'
```
And optionally, to load the extension:
And optionally, to load the extension somewhere in your `init.lua`:
```lua
require'telescope'.load_extension'repo'
```

A handy companion plugin is [vim-rooter](https://github.com/airblade/vim-rooter), as it’ll change the current directory according to the current file’s detected project (often, the root of the git repository). To get it to change each *buffer’s* directory, instead of the whole editor by default, add the following Lua to your configuration:
```lua
g['rooter_cd_cmd'] = 'lcd'
```

### Packer

For instance, with [Packer.nvim](https://github.com/wbthomason/packer.nvim):
Expand All @@ -85,6 +80,14 @@ use {
}
```

### Companion Plugin: vim-rooter

A handy companion plugin is [vim-rooter](https://github.com/airblade/vim-rooter), as it’ll change the current directory according to the current file’s detected project (often, the root of the git repository). To get it to change each *buffer’s* directory, instead of the whole editor by default, add the following Lua to your configuration:
```lua
vim.g['rooter_cd_cmd'] = 'lcd'
```
*Note*: the [`auto_lcd`](#auto_lcd) setting will integrate with vim-rooter, using it as a fallback for when Telescope-repo.nvim can’t find the project directory.

## External Dependencies

### Required
Expand All @@ -108,28 +111,29 @@ use {

### Global Configuration

You can change the default argument given to subcommands (like [`list`](#list) or [`cached_list`](#cached_list)) using the telescope `setup` function with a table like this:
You can change global settings and default arguments given to subcommands (like [`list`](#list) or [`cached_list`](#cached_list)) using the telescope `setup` function with a table like this:

```lua
-- Inside require("telescope").setup { … }
{
extensions = {
repo = {
settings = {
auto_lcd = true,
}
<subcommand> = {
<argument> = {
"new",
"default",
"value",
},
},
settings = {
auto_lcd = true,
}
},
},
}
```

for instance, you could do:
for instance, to search in a custom directory with the `list` command, you could do:

```lua
require("telescope").setup {
Expand All @@ -150,7 +154,15 @@ require("telescope").setup {
require("telescope").load_extension "repo"
```

**Note**: make sure to have `require("telescope").load_extension "repo"` *after* the call to `require("telescope").setup {…}`, otherwise the global configuration won’t be taken into account.
**Note**: make sure that `require("telescope").load_extension "repo"` is put *after* the call to `require("telescope").setup {…}` (or not present in `init.lua` at all), otherwise the global configuration won’t be taken into account.

### Settings

#### `auto_lcd`

If this is set to `true`, the current directory will be set per buffer when we detect that we are inside a project that was selected during the current session.

This integrates with [vim-rooter](#companion-plugin-vim-router): when vim-rooter is present, it is deactivated and used only as a fallback with the `:lcd` command.

### `list`

Expand Down
17 changes: 14 additions & 3 deletions lua/telescope/_extensions/repo/autocmd_lcd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,26 @@ end
-- Define autocmd to change the folder of the current file (with lcd).
function M.setup()
M.active = true
-- Deactivate autochdir
vim.opt.autochdir = false
-- Detect an active vim-rooter and use it only as a fallback
if vim.g.loaded_rooter == 1 then
vim.g.rooter_manual_only = 1
vim.g.rooter_cd_cmd = "lcd"
end

-- 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" }, {
-- Events inspired by vim-rooter
vim.api.nvim_create_autocmd({ "BufNewFile", "BufEnter", "BufReadPost" }, {
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)
if project_path then
vim.cmd.lcd(project_path)
elseif vim.g.loaded_rooter == 1 then
vim.cmd.Rooter()
end
end,
group = autocmd_group,
Expand Down
10 changes: 8 additions & 2 deletions lua/telescope/_extensions/repo/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ local M = {}

M.values = {}

local default_config = {
settings = {
auto_lcd = true,
}
}

function M.setup(opts)
M.values = opts or {}
if M.values.settings and M.values.settings.auto_lcd then
M.values = vim.tbl_extend("keep", opts, default_config)
if M.values.settings.auto_lcd then
require("telescope._extensions.repo.autocmd_lcd").setup()
end
end
Expand Down

0 comments on commit d7fa23b

Please sign in to comment.