VS Code snippet regex for relative path - regex

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!

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 ..

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

Related

Getting subdirectory with regex for snippet

To create a snippet in vscode that returns the name of a file I use:
{$TM_FILENAME}
To create a snippet in vscode that returns the name of a directory with its first capital letter I use:
${TM_DIRECTORY/.*\\/(.*)$/${1:/capitalize}/g}
But I need to get a subdirectory and leave all the letters in lowercase. For example,
a/b/c/d/e
how could i get the \d directory?
You can use
"LowercaseFolderPath": {
"scope": "",
"prefix": "lowercasefolderpath",
"body": [
"${TM_DIRECTORY/([^\\/\\\\]+)(?=[\\/\\\\][^\\/\\\\]*$)/${1:/downcase}/}"
],
"description": "Lower-case folder path"
},
Here, ([^\/\\]+)(?=[\/\\][^\/\\]*$) regex captures the last but one subdirectory into $1, and the ${1:/downcase} replacement turns it to lower case.
See the regex demo.

Replace backslash '\' with forward '/' slash in VS Code snippets with Regex

Background:
I am adding a custom JavaScript snippet in VS Code to insert the file path of the current file. VS Code provides variables to get the file path but the file path contains backslashes in the path. And I want to get the path with '/' instead of '\'
eg. 'hello\world.js' -> 'hello/world.js'
VS Code also provides variable transformations using regex. I tried to replace backslashes with forward slashes but I could not make it work. I also checked similar questions but I could not any result for this specific to variable transformations in VS Code snippets. And I could not figure it out with other similar solutions as I'm new to regex.
What I tried:
This works fine and replaces backslashes '\' with '_'
"filepath": {
"prefix": "filepath",
"body": ["/${RELATIVE_FILEPATH/([\\\\])/_/g}"],
"description": "Path of current file"
}
But if I change '_' with '/' it does not work.
"filepath": {
"prefix": "filepath",
"body": ["/${RELATIVE_FILEPATH/([\\\\])///g}"],
"description": "Path of current file"
}
I tried some variations by escaping slash and using regex groups but I could not make it work.
\\\\/\\//g is your expression. That one should mean the same, because it is a choice of only one character: [\\\\]/\\//g.

Upper camel case to lower camel case as VS Code snippet

I'm trying to create the following line as a snippet for VS Code:
MyFooVariable mytype `json:"myFooVariable"`
So I have the following snippet base
"Struct member declaration with json decorator": {
"prefix": "json",
"body": [
"${1} ${2} `json:\"${1}\"`"
],
"description": "Add suffix for json Marshaller"
}
On the second usage of ${1} I want to replace the upper camel case by lower camel case. I guess I should use regex to make a substitution but as soon as I'm trying to do anything with regex my brain just runs away.
Could you help me with that?
I know I should show you what I attempted but believe me, it's irrelevant.
You want to turn the first character of the input word to lower case. So, you may use a simple ^(.) regex to find that first char and capture it into Group 1 and then use ${1:/downcase} to replace with a lowercase version of that character:
"body": [
"${1} ${2} `json:\"${1/^(.)/${1:/downcase}/}\"`"
],
This is a "rough" demo of how it works.

VSCode Code-Snippets transform: downcase and capitalize at the same time

I have this:
${1/([A-Z]*)(?:_)([A-Z]+)*/${1:/downcase}${2:/downcase}/g}
How to make use downcase and capitalize on the same (2) group?
${1/([A-Z]*)(?:_)([A-Z]+)*/${1:/downcase}${2:/downcase/capitalize}/g}
I want to tansform ZXC_ASD to zxcAsd.
Try it like this:
"camelCaseSnail": {
"scope": "javascript,typescript",
"prefix": "log",
"body": "${1/([A-Z]*)(?:_)(?:([A-Z])([A-Z]+))*/${1:/downcase}${2:/capitalize}${3:/downcase}/g}"
}
Basically, I've changed the second capture group ([A-Z]+)* to a non-capture group that has two inner capture groups (?:([A-Z])([A-Z]+))*, a single letter for camel-case and the rest, which I refer in the replace/transform part: /downcase}${2:/capitalize}${3:/downcase}/
Apparently coming to vscode v1.58 is a /camelcase modifier. So your case is as easy as
"${1/(.*)/${1:/camelcase}/}"
Tested in the Insiders Build. See Add a camelCase transform for Snippet variables. See also https://stackoverflow.com/a/51228186/836330 for another example.
Old answer:
Using the undocumented (see Snippet regex: match arbitrary number of groups and transform to CamelCase) /pascalcase transform, it is quite easy:
"${1/([A-Z]*)(?:_)([A-Z]+)*/${1:/downcase}${2:/pascalcase}/g}"
as the /pascalcase will do both the /capitalize and the /downcase at once.

Regex in Sublime Text tmLanguage file doesn't use multiline

I'm trying to create a custom syntax language file to highlight and help with creating new documents in Sublime Text 2. I have come pretty far, but I'm stuck at a specific problem regarding Regex searches in the tmLanguage file. I simply want to be able to match a regex over multiple lines within a YAML document that I then convert to PList to use in Sublime Text as a package. It won't work.
This is my regex:
/(foo[^.#]*bar)/
And this is how it looks inside the tmLanguage YAML document:
patterns:
- include: '#test'
repository:
test:
comment: Tester pattern
name: constant.numeric.xdoc
match: (foo[^.#]*bar)
If I build this YAML to a tmLanguage file and use it as a package in Sublime Text, I create a document that uses this custom syntax, try it out and the following happens:
This WILL match:
foo 12345 bar
This WILL NOT match:
foo
12345
bar
In a Regex tester, they should and will both match, but in my tmLanguage file it does not work.
I also already tried to add modifiers to my regex in the tmLanguage file, but the following either don't work or break the document entirely:
match: (/foo[^.#]*bar/gm)
match: /(/foo[^.#]*bar/)/gm
match: /foo[^.#]*bar/gm
match: foo[^.#]*bar
Note: My Regex rule works in the tester, this problem occurs in the tmLanguage file in Sublime Text 2 only.
Any help is greatly appreciated.
EDIT: The reason I use a match instead of begin/end clauses is because I want to use capture groups to give them different names. If someone has a solution with begin and end clauses where you can still name 'foo', '12345' and 'bar' differently, that's fine by me too.
I found that this is impossible to do. This is directly from the TextMate Manual, which is the text editor Sublime Text is based on.
12.2 Language Rules
<...>
Note that the regular expressions are matched against only a single
line of the document at a time. That means it is not possible to use a
pattern that matches multiple lines. The reason for this is technical:
being able to restart the parser at an arbitrary line and having to
re-parse only the minimal number of lines affected by an edit. In most
situations it is possible to use the begin/end model to overcome this
limitation.
My situation is one of the few in which a begin/end model cannot overcome the limitation. Unfortunate.
Long time since asked, but are you sure you can't use begin/end? I had similar problems with begin/end until I got a better grasp of the syntax/logic. Here's a rough example from a json tmLanguage file I'm doing (don't know the proper YAML syntax).
"repository": {
"foobar": {
"begin": "foo(?=[^.#]*)", // not sure about what's needed for your circumstance. the lookahead probably only covers the foo line
"end": "bar",
"beginCaptures": {
"0": {
"name": "foo"
}
},
"endCaptures": {
"0": {
"name": "bar"
}
},
"patterns": [
{"include": "#test-after-foobarmet"}
]
},
"test-after-foobarmet": {
"comment": "this can apply to many lines before next bar so you may need more testing",
"comment2": "you could continue to have captures here that go to another deeper level...",
"name": "constant.numeric.xdoc",
"match": "anyOtherRegexNeeded?"
}
}
I didn't follow your
"i need to number the different sections between the '#' and '.'
characters."
, but you should be able to have a test in test-after-foobarmet with more captures if needed for naming different groups between foo bar.
There's are good explanation of TextMate Grammar here. May still suffer from some errors but explains it in a way that was helpful for me when I didn't know anything about the topic.