WebStorm 2017.3.2 Live Templates变量多函数应用及引用问题
Absolutely! Both of these requirements are totally achievable in WebStorm 2017.3.2's Live Templates—let me break down how to do each one clearly:
1. Applying Multiple Predefined Functions to a Single Input
The trick here is to create multiple template variables that all reference your original input variable, each with its own predefined function. Here's a step-by-step example:
- Open the Live Templates settings (go to
File > Settings > Editor > Live Templates). - Create a new template (click the
+button, chooseLive Template). - In the "Template text" field, enter something like this (adjust to your use case):
originalInput: "$FOO$", underscoreVersion: "$FOO_UNDERSCORE$", camelCaseVersion: "$FOO_CAMEL$" - Click the
Edit variablesbutton to configure each variable:- For
$FOO$: Leave the "Expression" field empty (this is your user-input variable), and make sure "Skip if defined" is not checked—this lets you type in the original value when using the template. - For
$FOO_UNDERSCORE$: In the "Expression" field, entercapitalizeAndUnderscore(FOO)(note: no dollar signs aroundFOOhere), then check "Skip if defined" so WebStorm auto-generates this value without prompting you. - For
$FOO_CAMEL$: In the "Expression" field, entercamelCase(FOO), and also check "Skip if defined".
- For
When you use this template, you'll only be prompted to enter a value for $FOO$, and WebStorm will automatically populate the other two variables with the transformed values.
2. Referencing Template Variables Within the Same Template
This is exactly what we did in the example above! You can reference any existing template variable in another variable's expression by using its name (without dollar signs).
If you wanted a variable like $FOO_REFERENCE$ that uses the same input as $FOO$ but with a different function, just define it in the "Edit variables" dialog with an expression like yourDesiredFunction(FOO), then use $FOO_REFERENCE$ anywhere in your template text.
You can even reference variables directly in the template text itself—like using $FOO$ multiple times in different places, each time showing the original input value.
Just keep in mind that variable references in expressions are case-sensitive, so make sure the name matches exactly what you defined for the original variable.
内容的提问来源于stack exchange,提问作者Ryder Brooks

