diff --git a/README.md b/README.md index b5bdd8d..5dbbe59 100644 --- a/README.md +++ b/README.md @@ -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): @@ -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 @@ -108,12 +111,16 @@ 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, + } = { = { "new", @@ -121,15 +128,12 @@ You can change the default argument given to subcommands (like [`list`](#list) o "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 { @@ -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` diff --git a/lua/telescope/_extensions/repo/autocmd_lcd.lua b/lua/telescope/_extensions/repo/autocmd_lcd.lua index 839611b..618702f 100644 --- a/lua/telescope/_extensions/repo/autocmd_lcd.lua +++ b/lua/telescope/_extensions/repo/autocmd_lcd.lua @@ -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, diff --git a/lua/telescope/_extensions/repo/config.lua b/lua/telescope/_extensions/repo/config.lua index d06a91c..e9fd0b3 100644 --- a/lua/telescope/_extensions/repo/config.lua +++ b/lua/telescope/_extensions/repo/config.lua @@ -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