3

I've just started getting into vsCode snippets. They seem really handy.

Is there a way to ensure that what a user entered at a tabstop starts with a lowercase value.

Here's my test case/ sandbox :

 "junk": {
    "prefix": "junk",
    "body": [
      "original:${1:type some string here then tab}",
      "lower:${1/(.*)/${1:/downcase}/}",
      "upper:${1/(.*)/${1:/upcase}/}",
      "capitalized:${1/(.*)/${1:/capitalize}/}",
      "camel:${1/(.*)/${1:/camelcase}/}",
      "pascal:${1/(.*)/${1:/pascalcase}/}",
    ],
    "description": "junk"
  }

and here's what it produces:

original:SomeValue
lower:somevalue
upper:SOMEVALUE
capitalized:SomeValue
camel:somevalue
pascal:Somevalue

"camel" is pretty close but I want to preserve the capital if the user entered a camelcase value.

I just want the first character lower no matter what.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
samantha
  • 171
  • 1
  • 1
  • 8
  • A quick note for someone like me who was annoyed it kept clearing out a pattern like this on my second input: `const factory $1.$2($3) = _${2/(.*)/${2:/pascalcase}/};` this is because after you tab away is when it applies the transformation and the `2/:pascalscase` isn't related to the second input but the match from the pattern, thus there was no group match for a "2" thus it would clear that input. So this is what ended up working: `const factory $1.$2($3) = _${2/(.*)/${1:/pascalcase}/};` – CTS_AE Feb 09 '23 at 01:03

2 Answers2

3

The answer is:

${1/(.)(.*)/${1:/downcase}$2/}
General Grievance
  • 4,555
  • 31
  • 31
  • 45
samantha
  • 171
  • 1
  • 1
  • 8
0

Just to clarify, if you look at this commit: https://github.com/microsoft/vscode/commit/3d6389bb336b8ca9b12bc1e772f7056d5c03d3ee

function _toCamelCase(value: string): string {
        const match = value.match(/[a-z0-9]+/gi);
  console.log(match)
        if (!match) {
            return value;
        }
        return match.map((word, index) => {
            if (index === 0) {
                return word.toLowerCase();
            } else {
                return word.charAt(0).toUpperCase()
                    + word.substr(1).toLowerCase();
            }
        })
            .join('');
    }

the camelcase transform is intended for input like

some-value
some_value
some.value

I think any non [a-z0-9]/i will work as the separator between words. So your case of SomeValue is not the intended use of camelcase: according to the function above the entire SomeValue is one match (the match is case-insensitve) and then that entire word is lowercased.

Mark
  • 143,421
  • 24
  • 428
  • 436