WebStorm 9.0.0 Web Help

You can replace a SASS expression with a local or global variable.

To perform this refactoring, you can use:

Example

Before After
    $blue: #3bbfce
    $margin: 16px

    .border
    padding: $margin / 2
    margin: $margin / 2
    border-color: $blue
    $blue: #3bbfce
    $margin: 16px;
    $var: $margin / 2

    .border
    padding: $var
    margin: $var
    border-color: $blue
To extract a variable using in-place refactoring
  1. In the editor, select the expression to be replaced with a variable. You can do that yourself or use the smart expression selection feature to let WebStorm help you. So, do one of the following:
    • Highlight the expression. Then choose Refactor | Extract | Variable from the main or the context menu.

      Alternatively, pressCtrl+Alt+VCtrl+Alt+VShift+Alt+VCtrl+Alt+VCtrl+Alt+VCtrl+Alt+VShift+Alt+VShift+Alt+L or Ctrl+Alt+VAlt Command VAlt Command VAlt Command L.

    • Place the cursor before or within the expression. Choose Refactor | Extract Variable from the main or the context menu, or pressCtrl+Alt+VCtrl+Alt+VShift+Alt+VCtrl+Alt+VCtrl+Alt+VCtrl+Alt+VShift+Alt+VShift+Alt+L or Ctrl+Alt+VAlt Command VAlt Command VAlt Command L. In the Expressions pop-up menu, select the expression. To do that, click the required expression. Alternatively, use the Up and Down arrow keys to navigate to the expression of interest, and then press EnterEnterEnterEnterEnterEnterEnterEnterEnterEnterEnter to select it.

      Note

      The Expressions pop-up menu contains all the expressions appropriate for the current cursor position in the editor.

      When you navigate through the suggested expressions in the pop-up, the code highlighting in the editor changes accordingly.

  2. If more than one occurrence of the selected expression is found, select Replace this occurrence only or Replace all occurrences in the Multiple occurrences found pop-up menu. To select the required option, just click it. Alternatively, use the Up and Down arrow keys to navigate to the option of interest, and press EnterEnterEnterEnterEnterEnterEnterEnterEnterEnterEnter to select it.
  3. Select the place in the source code, where the new variable will be declared. The declaration can be global (a variable is available throughout the whole file), or local (a variable is declared immediately before use, and is available in the current block only).
  4. Specify the name of the variable. Do one of the following:
    • Select one of the suggested names from the pop-up list. To do that, double-click the suitable name. Alternatively, use the Up and Down arrow keys to navigate to the name of interest, and EnterEnterEnterEnterEnterEnterEnterEnterEnterEnterEnter to select it. When finished, press EscapeEscapeEscapeEscapeEscape, Escape or Ctrl+GEscapeEscapeEscapeEscapeEscapeEscape.
    • Edit the name by typing. The name is shown in the box with red borders and changes as you type. When finished, press EscapeEscapeEscapeEscapeEscape, Escape or Ctrl+GEscapeEscapeEscapeEscapeEscapeEscape.
To extract a variable using the dialog box

If the Enable in place refactorings check box is cleared in the Editor settings, the Extract Variable refactoring is performed by means of the dialog box.

enableInplaceRefactoringCleared

  1. Select the desired expression, and invoke Extract Variable refactoring as described above.
  2. If more than one expression is detected for the current cursor position, the Expressions list appears. If this is the case, select the required expression. To do that, click the expression. Alternatively, use the Up and Down arrow keys to navigate to the expression of interest, and then press EnterEnterEnterEnterEnterEnterEnterEnterEnterEnterEnter to select it.
  3. In the Extract Variable dialog for SASS:
    • Specify the variable name. You can select one of the suggested names from the list or type the name in the Name field.
    • Specify the place for declaration. Select the desired place (global or local) from the drop-down list.
    • If more than one occurrence of the selected expression is found, you can select to replace all the found occurrences by selecting the corresponding check box. If you want to replace only the current occurrence, clear the Replace all occurrences check box.
    • Click OK.

      introduceVariableJSDialog.png

See Also

Procedures:

Reference:

Web Resources: