13

I am trying to make a template for React useState:

const [item, setItem] = useState('value')

const [$state$, set$state$] = useState($init$);$end$

Since item is the same, only with a capital I, is it possible to auto-capitalize the first letter?

I am doing it in VS Code like this:

const [$1, set${1/(.*)/${1:/capitalize}/}] = useState($2)
ilyo
  • 35,851
  • 46
  • 106
  • 159
  • What are you trying to do, exactly? Are you trying to do a "find and replace"? – D. Pardal Apr 27 '20 at 18:04
  • when I am typing `state` I want the same text to appear in both places, only the second instance to start with a capital letter: `[item, setItem]` – ilyo Apr 27 '20 at 18:05
  • "when I am typing `state`" - where? Is this a question about React or about code editing/writing? – D. Pardal Apr 27 '20 at 18:08
  • 1
    @D.Pardal It's about IntelliJ live templates, independent of React, it's being used in React code but that's not really relevant, OP want a live template variable to be output in two different ways – Ruan Mendes Apr 27 '20 at 18:10
  • This was the exact use case that I was trying to find the answer for. Thanks for asking this! – Devin B. Aug 29 '23 at 23:48

1 Answers1

23

Create a second variable whose value depends on the first one.

For example, $state_ has its value initialized to capitalize(state) in the edit variables dialog

const [item, setItem] = useState('value')

const [$state$, set$state_$] = useState($init$);$end$

enter image description here

Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • Sorry for commenting an old post, can you please update this answer with the most recent solution? The "Edit Variables" window is totally different now and I can't really understand how to make it work. Thank you in advance! – Ares9323 Sep 05 '22 at 09:57
  • 1
    I'm using IDEA 2022 and the window looks pretty much the same. Please explain which part you're not understanding. A new question linking to this with your own screenshots would be the best, feel free to link it here so I can see it. – Ruan Mendes Sep 05 '22 at 13:31
  • Thank you for your kindness and the fast answer, I created a new question as your requested: https://stackoverflow.com/questions/73611330/how-to-capitalize-a-variable-in-a-rider-live-template – Ares9323 Sep 05 '22 at 15:01