rgroli/other.nvim

More "searching" alike patterns for target file?

Closed this issue ยท 10 comments

Hey ๐Ÿ‘‹๐Ÿพ

I'm very sorry if this is a duplicate, but I wonder if something like this is possible:

require('other-nvim').setup({
  mappings = {
    {
      pattern = 'src/.*/(.*).ext',
      target = 'test/.*/%2.spec.ext',
    },
})

So I want it to be possible that these two files are matching:

  • src/some/path/specific-file-name.ext
  • test/a/different/path/specific-file-name.spec.ext

Is that somehow possible to achieve?

Yes, you would do it this way

{
      pattern = 'src/some/(.*)/(.*).ext',
      target = 'test/a/%1/%2.spec.ext',
    },
rgroli commented

exactly ๐Ÿ˜„ Closing this for now..

Hey ๐Ÿ‘‹๐Ÿพ
Thanks for the reply. ๐Ÿ™๐Ÿพ
But wouldn't that now match the first matching group of the pattern in the target too? Because that's what I have and it is not working. I would like to loosen the pattern and find more potential results. So I can specify fewer and less concrete targets.

Trying to describe this in some points:

  • I define a folder where source files are inside
  • I only care about the file name, the path "in-between" doesn't matter
  • I define a folder where all kind of test files are inside
  • in this folder I try to find a file with a matching name plus some additional stuff

Having the above pattern again:

pattern = 'src/.*/(.*).ext',
target = 'test/.*/%2.spec.ext',

I want these to match:
./src/foo/file.ext -> ./test/what/ever/else/file.spec.ext or ./test/poop/file.spec.ext
./src/bar/baz/file.ext -> ./test/file.ext or ./test/holy/shit/that/is/long/file.spec.ext

Was that better explained? ๐Ÿ™ˆ

rgroli commented

Hi,

to be seable to sarch in subdirectories in the target you must use the recursive glob thing "**".
So basically this should work:

    {
	    pattern = "src/.*/(.*).ext",
	    target = "test/**/%1.spec.ext",
    },

and also the other way...

    {
	    pattern = "test/.*/(.*).spec.ext",
	    target = "src/**/%1.ext",
    },

Hope that helps ๐Ÿ˜„

Nice. That helped. Thank you very much! ๐Ÿ™๐Ÿพ

A final question if I may ask: how can I make it case insensitive? ๐Ÿ™ˆ
E.g. if the source file name would be FooBar.ext to also match fooBar.spec.ext.
A transformer does not do I guess.

rgroli commented

Sure, but actually this should just work.. the search is case-insensitive..

But not the matching of the captured group right? Because it definitely doesn't work for me. And I thought that's also the reason for the transformers? ๐Ÿค”

rgroli commented

@weilbith okay, let's narrow this issue down.. ๐Ÿ˜„

I have the following config:

	mappings = {
		{
			pattern = "src/.*/(.*).ext",
			target = "test/**/%1.spec.ext",
		},
		{
			pattern = "test/.*/(.*).spec.ext",
			target = "src/**/%1.ext",
		},
	},

With this I am able to switch back and forth for the following files:

/fixtures/uppercase-lowercase/src/foo/file.ext  
/fixtures/uppercase-lowercase/test/what/ever/else/file.spec.ext

and also

/fixtures/uppercase-lowercase/src/foo/FooBar.ext
/fixtures/uppercase-lowercase/test/what/ever/else/fooBar.spec.ext

does this work for you?

The transformers are used to transform a captured group before it is used in the target (which is then searched with glob). Although the repo example shows the lowercase transformer, it's just to demonstrate an easy use case. I never needed to use this in real life. They are generally used to transform between camel to kebap or removing some suffixes or prefixes.

I am on a mac, where you don't need to care about case sensitivity in filepaths by default. Maybe that could be the problem? What system are you using?

Hey ๐Ÿ‘‹๐Ÿพ
Sorry, I was on vacation without my laptop and thereby couldn't try it out.
Now I did. Thanks for your exact example. I literally created exactly the folders and files you have described and also the exact mappings. And yes, this still doesn't work for me for the second case.

I am on a mac, where you don't need to care about case sensitivity in filepaths by default. Maybe that could be the problem? What system are you using?

Very interesting. I didn't know about that. I'm working with Linux. So that could be it. But I see you added a testcase for this. I guess your CI is not running with MacOS, right?