IntelliJ IDEA lets you easily edit your Scala code using different intention actions. It also lets you convert your code from Java to Scala, and use different Scala templates.

Strings in Scala

You can add different intentions for strings, perform different actions, and set a different format for multi-line strings:

Use a Scala worksheet to quickly evaluate your results.

  • Insert gap with concatenation ("+ +") into a string.
    1. Enter a string and press Alt+Enter. The list of appropriate intentions opens.

      images/gap_concatination.png

    2. Select Insert gap with concatenation ("+ +") and press Enter. Now you can insert a value into your string.

      images/scala_gap_concatenation_result.png

  • Invoke the Convert to interpolated string intention.
    1. Select a value with concatenation in your string and press Alt+Enter. The list of appropriate intentions opens.

      images/interpolated_string.png

    2. Select Convert to interpolated string, press Enter and view the result.

      images/interpolated_string_result.png

      The Convert to formatted string option will get you basic Java formatted string.
  • Convert a string into a multi-line string using Convert to """string""" intention and vice versa.
    1. Enter your string, press Alt+Enter and select Convert to """string""" option in the drop-down list of intentions.

      images/convert_to_string.png

    2. You also can convert the multi-line string into the regular string. Press Alt+Enter to open the drop-down list of intentions. Select Convert to "string" and press Enter.

      images/convert_to_reg_string.png

      View the result.

      images/string_result.png

      Converting to multi-line strings removes escaped sequences such as '\\' or '\n'.
    3. To enter a multi-line string, simply type triple quotes in your editor. If you press Enter, it will automatically invoke the stripMargin method. The stripMargin method removes the left-hand part of a multi-line string up to a specified delimiter.

      images/stripMargin_Method.png

      The white spaces are also preserved. Check the following example:

      images/white_spaces_perserve.png

  • Add the .replace("\r"," ") intention. This intention lets you keep the caret in the correct place on the next line in the multi-line strings regardless of what operating system you have at the moment.

    Simply enter a multi-line string, press Alt+Enter and select the appropriate intention from the drop-down list.

    images/add_replace_intention.png

  • Use Inject Language/Reference intention to insert a language or a reference into your multi-line string literals.
    1. Enter a multi-line string, press Alt+Enter and select Inject Language/Reference from the drop-down list. The list of available languages and references opens.

      images/lang_injection.png

    2. Choose the appropriate one and press Enter.
      To cancel the language injection, simply choose the Un-Inject Language/Reference intention.
  • Use the Multi-line stings tab in Scala settings to set a different format for multi-line strings' options such as Margin char indent or disable a multi-line strings support.
    1. In the main menu, select File | Setting | Editor | Code Style | Scala.
    2. On the Scala page, select the Multi-line strings tab.

      images/multi_line_strings.png

Implicit conversions

IntelliJ IDEA lets you invoke implicit conversion methods and parameters.

  • Implicit conversion methods
    1. Select an expression and press Shift+Ctrl+Q (Ctrl+Q for macOS) to invoke the list of applicable implicit conversions. The list shows the regular scope displayed on the top and the expanded scope that is displayed on the bottom of the list.

      Note that IntelliJ IDEA highlights an implicit conversion that was used for the selected expression. If IntelliJ IDEA cannot find the implicit conversion or if it finds more than one match then the list of Introduce Variable opens.

      images/choose_implicit_conv.png

    2. You can make the implicit conversion method explicit. Press Alt+Enter and select Make explicit or Make explicit (Import method):

      images/choose_implicit_with_make_exp.png

    3. If you select Make explicit then IntelliJ IDEA returns a method call with the class name. It might be helpful if you need to make sure that the compiler imports the particular implicit conversion method that you originally wanted:

      images/make_explicit.png

    4. If you select Make explicit (Import method) then the method is imported statically and IntelliJ IDEA returns just its call without the class name. Also, the next time you open the list of useful implicit conversions you will see this method in the regular scope:

      images/make_explicit_import_met.png

  • Implicit parameters
    1. Place a cursor to the method where implicit conversion was used and press Ctrl+Shift+P to invoke implicit parameters. It might be helpful for code analyzing when you want to find out what implicit parameters were passed to the particular call. IntelliJ IDEA also lets you view the recursive implicit parameters.

      Note that IntelliJ IDEA highlights the method call where implicit parameters were used.

      images/implicit_param.png

    2. If IntelliJ IDEA cannot find method calls where implicit parameters were passed, it displays a pop-up message:

      images/no_implicit_param.png

Type Info action

IntelliJ IDEA lets you work with type inferences using the Scala Show Type Info action.

  • If you selected the Show type info on mouse hover after, ms checkbox on the Editor tab in Settings | Languages and Frameworks | Scala, you can simply navigate with the mouse to a value to see its type information.
  • To invoke the Show Type Info action in the editor, navigate to the value and press :

    images/type_info_string.png

  • You can also see the type information on a value definition. Simply put the caret on a value definition and press :

    images/type_info_int.png

  • To add a type annotation, highlight the value and press Alt+Enter:

    images/type_info_add.png

    As a result, the type annotation is added:

    images/type_info_add_result.png

    You can remove the type annotation the same way you have added it, by pressing Alt+Enter.
  • You can also use Adjust types action to shorten types with full qualified names.

    images/type_adjust.png

    In this case the necessary imports are added.

    images/type_adjust_result.png

  • To see the type information on an expression, simply navigate to the expression, highlight it and press :

    images/type_info_expression.png