5

EDIT — This is not possible with coc-snippets. It's possible with Ultisnips.

I've got the following vim snippet that I'm using (with coc-snippets) for React:

snippet STATE_HOOK "useState hook" b
const [${1:state}, set${1:`!v expand('%:t:r')`}] = useState($2)
endsnippet

This could be used to quickly create the following (incorrect) code:

const [color, setcolor] = useState("green");

The problem is that the setcolor needs to be camelcased, like this: setColor

How would one write this snippet so that the expanded input is capitalized?

Harrison Cramer
  • 3,792
  • 9
  • 33
  • 61
  • This is now possible with regular vscode snippets. Support has been added for case modifiers like `\u`. – Mark Apr 02 '21 at 18:52

2 Answers2

2

I was testing some different things and you can use:

snippet STATE_HOOK "useState hook" b
const [$1, set${1/\w+/\u$0/g}] = useState("$2")
endsnippet 

How it works: Checking the documentation I found that you should overrite the text with the same text but capitalized, so \w+takes all the text (I think that '+' is unnecesary) and overrite it with \u$0 (is the same text but capitalized)

enter image description here

Chcamiloam
  • 564
  • 2
  • 5
  • This is great. The reason that I'd been running into problems on my own (and this answer pointed me in the right direction) was because I'd been trying to use coc-snippets, which doesn't support capitalization. – Harrison Cramer Sep 17 '20 at 13:40
  • 1
    doesn't work on my end, using coc-snippets – Tony Ngomana Apr 01 '21 at 23:16
  • Hey, yeah its because coc-snippets doesn't support the \u modifier (uppercase modifier). I'd recommend just installing Utilsnips instead, ended up going that route and it works. See here: https://github.com/neoclide/coc-snippets/issues/62 – Harrison Cramer Apr 02 '21 at 18:40
-1

Strings like foo-bar or foo_bar are easy to turn into fooBar and fooBar is easy to turn into foo-bar or foo_bar because the two parts are easily identifiable, which gives you a structure to work with.

There is no such thing with usecolor. There is no separator and everything is of the same case so you can get quite a bunch of valid camel-cased names out of that string: usecOlor, uSeCoLor, etc.

You have three options:

  1. camel-case your filenames so that you don't have to perform any transformation,
  2. kebab-case or snake-case your filenames to make them easy to transform with a simple substitution,
  3. come up with extensive rules and find out how to transform random strings in a satisfactory and reproducible way.

I would go with number 1.

romainl
  • 186,200
  • 21
  • 280
  • 313
  • Thanks for the response, however the caveat here is that I'm only looking to capitalize the word after "set." The "set" string will always be "set" and never anything else. That's the reason I'm thinking this is definitely possible... Meaning, if the pattern is always set{variable} it should be possible to capitalize the variable string. – Harrison Cramer Sep 14 '20 at 17:39