1

I'm trying to write a code snippet to capitalize each word in the filename base where the words are separated by dashes. While I know the words in the filename base will be separated by dashes, I don't know how may words to expect in the filename base. There could be zero or more dashes in my filename base. So, I could have:

name.extension
some-file-name.extension
yet-another-file-name.extension
and-yet-even-another-file-name.extension

etc., and I'd like these to become

Some File Name
Yet Another File Name
And Yet Even Another File Name

etc., respectively, in my code.

I've tried in an attempt just to get the first word to capitalize, but this only drops the first letter of my filename base, and leaves the rest unchanged:

${TM_FILENAME_BASE/[\\w+]/${1:/capitalize}/}

What am I missing? How do I iterate with the snippets syntax?

the_meter413
  • 231
  • 3
  • 19
  • Are u sure that your regexp work in VSCode?? It does not work for me, my VSCode search does not support it. Do you have some particular plugin installed?? Why do u want to complicate your life with regexp in VSCode, that is famous for its "Lone quantifier object" and "incomplete quantifier" errors ? You can transform your file in the desired way in several other easier ways (with or without a regexp) – Myron_Ben4 Jul 15 '23 at 07:41
  • oof: https://github.com/microsoft/vscode/issues/80126. more oof: https://github.com/microsoft/vscode/issues/109766. more oof: https://github.com/microsoft/vscode/issues/134046 – starball Jul 15 '23 at 08:30
  • depending of your use case maybe [File Templates](https://marketplace.visualstudio.com/items?itemName=rioj7.vscode-file-templates) can be handy, you can add snippets in the templates – rioV8 Jul 15 '23 at 09:13

3 Answers3

1

To answer your questions in your answer about conditional transforms using this snippet body:

${TM_FILENAME_BASE/(\\w+)|(\\W+)/${1:/capitalize}${2:+ }/g}

The conditional replacements are explained here: snippets grammar.

                | '${' int ':+' if '}'
                | '${' int ':?' if ':' else '}'
                | '${' int ':-' else '}' | '${' int ':' else '}'

${2:+ } is an if transform. If there is a group 2 insert a space.

${2: } is one of the two else transforms from the last line above. So ${2: } means if there is a capture group 2 do nothing but else (meaning there is no group 2) then insert a space.

The g flag makes the transform run as many times as it finds a match. So whenever the (\\W+) part of your regex matches that means there was no group 1 (since you are using an alternate | in the regex and there was a group 2. So how does your snippet work if you use the ${2: } form? You have to work through each match.

Using some-file-name:

The first match will be capture group 1 some and no capture group 2. Now the entire transform gets applied to that match. Group 1 gets capitalized and since there is no group 2 on the first match, the else ${2: } is triggered and a space is inserted.

Then the next match will be no group 1 and a group 2 of -. The replacement part of the transform will not capitalize anything (since there is no group 1). The ${2: } will insert nothing (since there is a group 2). But it seems to be inserting group 2 - which is non-obvious to me and actually seems like a bug. It is operating as if it read: if group 2, insert group 2, else (no group 2) insert a space. That is a little surprising to me. In any case, it explains why the - which is matched (in the 2nd/4th/6th/etc. match) inserts the group 2 -.

Mark
  • 143,421
  • 24
  • 428
  • 436
-1

If you use the following body it works

  • a literal . and extension => ignore
  • catch a possible - followed by text until a . or -
  • substitute the - by a space and capitalize the word
"body": "${TM_FILENAME_BASE/(\\.\\w+$)|(-?)([^-.]+)/${2:+ }${3:/capitalize}/g}"

It works for ${TM_FILENAME} and ${TM_FILENAME_BASE}


Edit

The examples all contain extensions but it turns out that ${TM_FILENAME_BASE} strips the extension.

I have tested the snippet with ${TM_SELECTED_TEXT} and the examples.

An update of the snippet body:

"body": "${TM_FILENAME_BASE/(-?)([^-]+)/${1:+ }${2:/capitalize}/g}"
rioV8
  • 24,506
  • 3
  • 32
  • 49
-1

I managed to get this to work with the following, but I don't understand why it works. Please edit this response if you can help out.

This will return the desired string:

${TM_FILENAME_BASE/(\\w+)|(\\W+)/${1:/capitalize}${2:+ }/g}

Explanation:

  • TM_FILENAME_BASE is a Code snippets variable. It doesn't include the extension, so we don't need to worry about writing a regex expression to get rid of that; we get to start with (e.g.) some-file-name.
  • (\\w+)|(\\W+) defines two regex search groups:
    • (\\w+) returns any group of alphanumeric text
    • (\\W+) returns any group of non-alphanumeric text
  • ${1:/capitalize}${2:+ }/g capitalizes the first group, and if it finds a second group will remove and replace the dashes with a space. The final /g switch makes the entire expression greedy.

Here's the bit I don't get: I don't understand why I need the + in ${2:+ }. If I use ${2: }, though (no +), the return value comes out as Some -File -Name (note also final trailing space in addition to the other unexpected aspects of the return value).

the_meter413
  • 231
  • 3
  • 19
  • if you don't understand where did you get the `${2:+ }` from – rioV8 Jul 15 '23 at 17:18
  • `g` is not greedy – rioV8 Jul 15 '23 at 17:20
  • Not sure why a working answer is downvoted. @rioV8, First comment: I got the `${2:+ }` from reading through others' snippets. My use of it is a separate beast from my understanding of it. Second comment: can you revise the answer above to add some constructive input? For example, what is `g`? Feel free to revise your original answer if you prefer. Yours also works, but the search for the file extension is extraneous (there's no extension returned by `TM_FILENAME_BASE`), and you do not provide any explanation for how you arrived at your result. Thanks! – the_meter413 Jul 15 '23 at 17:49
  • if `TM_FILENAME_BASE` does not contain the extension why do your examples in the question ALL have extensions – rioV8 Jul 15 '23 at 21:41
  • why do these other snippets not explain the `g` and the `+` – rioV8 Jul 15 '23 at 21:42