Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update regex to match any namespace #16

Merged
merged 1 commit into from
Sep 13, 2021

Conversation

williamthome
Copy link
Contributor

This regex uses the RELATIVE_FILEPATH instead of TM_FILEPATH variable and matches any level of nested folders based on my question in StackOverflow.

I also removed the "Test" at the end of the "ExUnit" snippet because my convention for file name includes "_test" at the end of the file and included the prefix ":", useful when the key and value are the same.

This regex uses the RELATIVE_FILEPATH instead of TM_FILEPATH variable and matches any level of nested folders based on my question in StackOverflow here https://stackoverflow.com/questions/69145815/vs-code-snippet-regex-for-relative-path.

I also removed the "Test" at the end of the "ExUnit" snippet because my convention for file name includes "_test" at the end of the file and included the prefix ":", useful when the key and value are the same.
@florinpatrascu
Copy link
Owner

Nice :)
I'll merge and publish today, as soon as I get the chance. Thank you.

@florinpatrascu florinpatrascu merged commit 6d1b9f1 into florinpatrascu:master Sep 13, 2021
@florinpatrascu
Copy link
Owner

v2.38 published, and thanks again ❤️

@williamthome
Copy link
Contributor Author

But wait, there is a bug.
The regex works well in defmod and ex_unit but not in supervisor, dynamic_supervisor and gen_server snippets :|
The regex is exactly the same!
Any ideas on this?

@williamthome williamthome mentioned this pull request Sep 13, 2021
@florinpatrascu
Copy link
Owner

no, no idea yet sorry.

@florinpatrascu
Copy link
Owner

actually this is interesting. I'll try to check later if i can find some time after work, but it seems the regex is confused by the variables in th ebody?! If you try this:

"DynamicSupervisor": {
    "prefix": "dys",
    "body": [
      "defmodule ${RELATIVE_FILEPATH/^(lib[\\/\\\\]|test[\\/\\\\])(\\w)|(\\.ex|\\.exs)$|([\\/\\\\])(\\w)|_(\\w)/${2:/upcase}${4:+.}${5:/upcase}${6:/upcase}/g} do",
      "\tdef start_link() do",
      "\t\tDynamicSupervisor.start_link(__MODULE__, [], name: __MODULE__)",
      "\tend",
      "end"
      ],
    "description": "Insert code for a DynamicSupervisor"
  }
}

the regex will work (no variables), but the moment you're introducing a variable in the body ... it will fail. For example:

    "body": [
      "defmodule ${RELATIVE_FILEPATH/^(lib[\\/\\\\]|test[\\/\\\\])(\\w)|(\\.ex|\\.exs)$|([\\/\\\\])(\\w)|_(\\w)/${2:/upcase}${4:+.}${5:/upcase}${6:/upcase}/g} do",
      "\tdef start_link(${init_args}) do",
      "\t\tDynamicSupervisor.start_link(__MODULE__, [], name: __MODULE__)",
      "\tend",
      "end"
      ],

if you try this you'll see how //upcase is becoming a .. placeholder, in the editor🥴

Is this a VSCode bug?

@williamthome
Copy link
Contributor Author

Ok, I found a solution.
The problem was in body args syntax.

Using "\tdef start_link(${1:init_args}) do", instead of "\tdef start_link(${init_args}) do", works.
Note the 1: before the init_args.

I will create a pull request to fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants