otavioschwanck/telescope-alternate.nvim

Ability to explicitly trigger switching to an alternate

dsummersl opened this issue · 5 comments

Love the idea of this plugin - I'm a regular projectionist plugin user, and I love the way regexes are handled here (and the telescope integration as well, of course).

One ability I'm missing for my workflow is the :E or :A X feature -- I map <leader>6 to jump to a preferred alternate file. Is there some lua command in the codebase I could call to explicitly trigger such a transition? For instance if I had something like this:

require('telescope-alternate').setup({
    mappings = {
      { 'src/(.*)/(.*).tsx', { { 'src/[1]/[2].stories.tsx', 'Preferred' }, } },
      { 'src/(.*)/(.*).stories.tsx', { { 'src/[1]/[2].tsx', 'Preferred' }, } },
    },
  })

I could do something like nvim_set_keymap('n', ',6', '<cmd>lua require("XXX"):jump_to_alternate("Preferred")<CR>', opts).

Thanks much.

I didn't understand the request very well. Could you explain a little more? Maybe do some examples of workflow

No problem...the projectionist plugin(https://github.com/tpope/vim-projectionist) has a concept of 'alternate' files and 'related' files. Here is an example:

let g:projectionist_heuristics = {
     ...
      \   'app/*/schemas.py': {
      \     'type': 'schemas',
      \     'alternate': [
      \       'tests/{dirname}test_{basename}.py',
      \       'tests/{dirname}/test_{basename}.py',
      \       'tests/{dirname}/test_{dirname}_{basename}.py',
      \     ],
      \     'related': [
      \       'app/{}/queries.py',
      \       'app/{}/routes.py',
      \       'app/{}/services.py',
      \       'app/{}/models.py',
      \     ]
      \   },
      ...
     \  }

The plugin offers two commands based on this configuration file. The first is :E *type* - it essentially works like your plugin, without the telescope integration.

The second command it offers is the :A command, for alternate. The plugin with jump to the first pattern where a file actually exists.

The use case for this is I think fairly straight forward: for any one file, there is probably one 'related' file that is most important. In my case usually thats tests, or if its react, maybe its storybook components. I don't want to search for a related file, I want to jump directly to a specific kind of related file that I care the most about.

I was hoping that there is already a lua function for this plugin that I could set up my own mapping for a specific file type (not asking for any new feature, or new behavior!)...if there were I could setup my own equivalent mapping...

@dsummersl The config is a bit clunky, but I think that this fork might do what you want. IIRC, it allows defining paths for specific types of related files and key bindings to jump to those "types".

Love the idea of this plugin - I'm a regular projectionist plugin user, and I love the way regexes are handled here (and the telescope integration as well, of course).

One ability I'm missing for my workflow is the :E or :A X feature -- I map <leader>6 to jump to a preferred alternate file. Is there some lua command in the codebase I could call to explicitly trigger such a transition? For instance if I had something like this:

require('telescope-alternate').setup({
    mappings = {
      { 'src/(.*)/(.*).tsx', { { 'src/[1]/[2].stories.tsx', 'Preferred' }, } },
      { 'src/(.*)/(.*).stories.tsx', { { 'src/[1]/[2].tsx', 'Preferred' }, } },
    },
  })

I could do something like nvim_set_keymap('n', ',6', '<cmd>lua require("XXX"):jump_to_alternate("Preferred")<CR>', opts).

Thanks much.

i will add this, this weekend.

Hi @otavioschwanck, thanks for this plugin.

I created the fork that @lougreenwood mentioned above. It involved a minor update to finders.lua and telescope.lua.

This allows a user to choose a preferred type when reaching for an alternate. At that moment, they can choose to jump directly to the matching template OR test OR route OR ... for a given file.

A user can directly open a single match type by adding focus=type when calling your plugin. The specified type focuses telesscope-alternate to mappings.type where that nested mappings array contains a single match for each source file.

From a source file, a user can choose to jump directly to the test file with <leader>t or route file with <leader>r or template with <leader>h.... The <leader>t works from any file that is setup with a test alternate, and so on.

These 2 commits on my fork show how minor the changes were along with the the neovim and telescope alternate mappings I used.

I did include an all nested telescope-alternate mappings to allow a user to view all matches as the default. I realized that I should have left the all nested mappings at the route so that this was not a breaking change to user's mappings files. I was toying with another way to do this to support the variety of files system layouts Ember users have, but so far my fork is working well on my current project.

If you would like a pull request let me know. Thanks again for your plugin.