A telescope extension that helps you to switch between files.
- With command
:Telescope switch
- With lua
require("telescope").extensions.switch.switch()
{
'sshelll/telescope-switch.nvim',
dependencies = { 'nvim-telescope/telescope.nvim' },
lazy = true,
},
require('telescope').setup {
defaults = {
-- ...
},
pickers = {
-- ...
},
-- config telescope-switch here ⬇️
extensions = {
switch = {
matchers = {
{
name = 'go test',
from = '(.*).go$',
to = '%1_test.go'
ignore_by = { 'go impl' }, -- ignore this matcher if 'go impl' matches any files
},
{
name = 'go impl',
from = '(.*)_test.go$',
to = '%1.go'
},
{
name = "plugin config",
from = "/lua/plugins.lua$",
to = "/lua/plugin-config/*.lua", -- this is overriden by 'search'
search = "/lua/plugin-config" -- this works
},
{
name = "plugin list",
from = "/lua/plugin%-config/.*.lua$",
to = "/lua/plugins.lua",
},
{
name = "rust test",
from = "/src/(.*).rs$",
to = "/tests/*.rs",
},
},
picker = {
seperator = "⇒",
layout_strategy = 'horizontal', -- telescope layout_strategy
layout_config = { -- telescope layout_config
width = 0.5,
height = 0.4,
preview_width = 0.6,
},
preview = true, -- set to false to disable telescope preview
}
}
}
}
require('telescope').load_extension('switch')
1. From && To
Basically I use ${current_file_path}:gsub(from, to)
in lua to get the target pattern
, so if you have any doubts about it, you can try to run this line of code in your lua REPL to test.
After getting the target pattern
, I'll call find ${target_pattern} -type f -maxdepth 0
to list all existed matched files, that's why you can use both of *.go
and %1.go
in to
field.
Here is one simple tip:
if you have ( ) . % + - * ? [ ^ $
in from
field, please add %
to escape them.
2. Ignore By
An array of matcher.name
that you want to check.
If any of them matches any files, then the current matcher will be ignored.
3. Search
If search
was set, then to
will be ignored.
search
is used to find all files with the given path, and this path should start with the 2nd level of vim.fn.getcwd()
.
For example:
.
├── go.mod
├── go.sum
├── .gitignore
├── ast
│ ├── parse.go
│ └── parse_test.go
└── util
├── math.go
└── math_test.go
If you want to jump from ./ast/xx.go
to ./util/xx.go
, then config like this:
{
name = "ast to util",
from = "/ast/(.*).go$",
search = "/util"
}
Why search? Because sometimes we don't have a common rule to jump!
4. Builtin Matchers
See lua/telescope/_extensions/switch/matcher.lua
for more detail.
You can use require('telescope._extensions.switch.matcher').go_test
to use the builtin matcher:
switch = {
matchers = {
require('telescope._extensions.switch.matcher').go_test,
require('telescope._extensions.switch.matcher').go_impl,
{
name = "rust test",
from = "/src/(.*).rs$",
search = "/tests",
},
},
}
Different matchers should have different name + from + to + search
, otherwise it'll be filtered.
See telescope.nvim for more detail.
If you have any great ideas about some common and useful matchers, please open a PR~~
Inspired by other.nvim