VS Code snippet regex for relative path

I'm coding in Elixir/Phoenix Framework using VS Code and trying to transform the following relative path

lib/shop_web/live/product_live/index.ex

into

ShopWeb.Live.ProductLive.Index

using snippets.

The closest to that was the regex below

"${RELATIVE_FILEPATH/^(lib\\/|test\\/)(\\w)|(.ex|.exs)$|\\/(\\w)|_(\\w)/${2:/upcase}${4:/upcase}${5:/upcase}/g}"

who gives me the following output

ShopWebLiveProductLiveIndex

I could not find a way to insert the missing dots.

Can anyone help me?

Thanks in advance!

Upvotes: 0

Views: 716

Answers (2)

Thanks to @Mark, my snippet to create a module in Elixir or Phoenix Framework looks like this now:

"Module": {
    "prefix": "defmodule",
    "description": "Create a module by the Elixir naming convention",
    "body": [
        "defmodule ${RELATIVE_FILEPATH/^([^\\/\\\\]+[\\/\\\\])|(\\.ex|\\.exs)$|([^._\\/\\\\]+)|_|([\\/\\\\])/${3:/capitalize}${4:+.}/g} do",
        "\t$1",
        "end"
    ],
}

As the naming convention, the output for the file in my question lib/shop_web/live/product_live/index.ex will be:

defmodule ShopWeb.Live.ProductLive.Index do

end

Upvotes: 0

Mark
Mark

Reputation: 182131

Try this:

"test7": {
  "prefix": "al",
  "body": [
    // my version
    "${RELATIVE_FILEPATH/^([^\\/\\\\]+[\\/\\\\])|(\\.ex|\\.exs)$|([^._\\/\\\\]+)|_|([\\/\\\\])/${3:/capitalize}${4:+.}/g}",

    // your version tweaked
    "${RELATIVE_FILEPATH/^(lib[\\/\\\\]|test[\\/\\\\])(\\w)|(\\.ex|\\.exs)$|([\\/\\\\])(\\w)|_(\\w)/${2:/upcase}${4:+.}${5:/upcase}${6:/upcase}/g}",
  ],
  "description": "alert line"
}

[Note: I made these work for both path.separators / and \. If you don't need that you could shorten the snippet by a lot.]

Your version was very close. I changed it to \\.ex just to make the dots explicit.

I also added a 4th capturing group ([\\/\\\\]) just before the 5th as in ([\\/\\\\])(\\w).

Now that 4th group can be used in a conditional ${4:+.} to add the .'s where the path separators were.

My version is a little shorter - it matches but doesn't use whatever directory is first, be it lib or test or whatever. If that doesn't work for you it is easy to modify that bit of the regexp. I shortened it to 4 capture groups.

([^._\\/\\\\]+)|_|([\\/\\\\]) the end of my version:

([^._\\/\\\\]+) : match characters other than ._\/, or
_ : match it but we aren't using it so no need for a capture group, or
([\\/\\\\]) : match just the path separator in group 4 to use in the conditional.

${4:+.} : conditional, if there is a group 4 (a path separator) add a ..

Upvotes: 1

Related Questions