rgroli/other.nvim

How are patterns matched?

Closed this issue Β· 9 comments

I have a question because I can't seem to get the plugin working and maybe I failed to understand how the matching is done.

This is currently my setup

require('other-nvim').setup {
  mappings = {
    {
      pattern = '/foo/bar/v10/some-folder/(.*).ts$',
      target = '/foo/bar/v10/tests/some-folder/%1.test.ts',
    },
    {
      pattern = '/foo/bar/v10/tests/some-folder/(.*).test.ts$',
      target = '/foo/bar/v10/some-folder/%1.ts',
    },
  },
}

These patterns are part of the absolute path of the file, which is how I understand that it should work. If I add a print here

match = filename:match(mapping.pattern)
it always returns nil but filename here is an absolute path.

It only works if I match against (.*).ts$ which is of course not what I want to do.

I even ran this manually on the file outside

vim.pretty_print(
  (
    '/Users/ahmed/Some/deeply/nested/folder/structure/here/foo/bar/v10/some-folder/file.ts'
  ):match '/foo/bar/v10/some-folder/(.*).ts$'
)

And this also returns nil, so I'm a bit confused about how it works.

You're doing it right!

the thing is that in your case there is a "-" in the path. In lua you need to escape this with "%". So the working pattern would be:

require('other-nvim').setup {
  mappings = {
    {
      pattern = '/foo/bar/v10/some%-folder/(.*).ts$',
      target = '/foo/bar/v10/tests/some-folder/%1.test.ts',
    },
    {
      pattern = '/foo/bar/v10/tests/some%-folder/(.*).test.ts$',
      target = '/foo/bar/v10/some-folder/%1.ts',
    },
  },
}

I will update the documentation about this. Thank you for the hint!

Another thing you could do to optimize your pattern is making the "some-folder"-part dynamic too. Like that:

require('other-nvim').setup {
  mappings = {
    {
      pattern = '/foo/bar/v10/(.*)/(.*).ts$',
      target = '/foo/bar/v10/tests/%1/%2.test.ts',
    },
    {
      pattern = '/foo/bar/v10/tests/(.*)/(.*).test.ts$',
      target = '/foo/bar/v10/%1/%2.ts',
    },
  },
}

I haven't tested that - but it should work

the thing is that in your case there is a "-" in the path. In lua you need to escape this with "%"

I was sure it was something silly like this… πŸ˜„ thanks! do you have a list of characters that need to be escaped in lua? Or maybe a resource about this?

Because in my case the absolute URL contains a lot of folders with - so I assume I need to escape all of them? because it's still not working for me.

Maybe this should be baked in the plugin?

I will update the documentation about this. The characters which need to be escaped are:

( ) . % + - * ? [ ^ $

at least as far as I can tell according to this: https://www.lua.org/pil/20.2.html

When you're also using a capturing-group for the path part (like mentioned above) maybe you could get rid of escaping all together.

Not sure what I'm doing wrong but this still returns nil

vim.pretty_print(
  (
    '/Users/ahmed/Some/deeply/nested/folder/structure/here/foo/bar/v10/some%-folder/file.ts'
  ):match '/foo/bar/v10/some%-folder/(.*).ts$'
)

the string you're matching against already contains the escaping. Shouldn't it be?

vim.pretty_print( 
    ( 
      '/Users/ahmed/Some/deeply/nested/folder/structure/here/foo/bar/v10/some-folder/file.ts'
    ):match '/foo/bar/v10/some%-folder/(.*).ts$'
)

Thanks, it works now πŸ‘πŸΌ

I think your example here needs to be fixed, because I followed that one #4 (comment)

Great that it works now!

but honestly I don't know how to fix the example above. I just tried that with a directory structure that mimiks yours and it just works.

require('other-nvim').setup {
  mappings = {
    {
      pattern = '/foo/bar/v10/some%-folder/(.*).ts$',
      target = '/foo/bar/v10/tests/some%-folder/%1.test.ts',
    },
    {
      pattern = '/foo/bar/v10/tests/some%-folder/(.*).test.ts$',
      target = '/foo/bar/v10/some%-folder/%1.ts',
    },
  },
}

This is the only change needed, you escaped the - inside target while this is only needed inside pattern

require('other-nvim').setup {
  mappings = {
    {
      pattern = '/foo/bar/v10/some%-folder/(.*).ts$',
-      target = '/foo/bar/v10/tests/some%-folder/%1.test.ts',
+      target = '/foo/bar/v10/tests/some-folder/%1.test.ts',
    },
    {
      pattern = '/foo/bar/v10/tests/some%-folder/(.*).test.ts$',
-      target = '/foo/bar/v10/some%-folder/%1.ts',
+      target = '/foo/bar/v10/some-folder/%1.ts',
    },
  },
}

Ahh I see πŸ˜„
okay!strange enough it works nevertheless in my testcase. Anyway I updated the comment, thanks!