IntelliJ IDEA 2017.3 Help

Work with Scala code in the editor

IntelliJ IDEA lets you use different Scala intention actions, convert your code from Java to Scala, and use different Scala templates while working in the IntelliJ IDEA editor.

Strings in Scala

You can add different intentions for strings, perform different actions, and set a different format for multi-line strings.
Check the following examples:

  • Insert gap with concatenation ("+ +") into a string.
    1. Enter a string and press Alt+Enter. The list of appropriate intentions opens.
      /help/img/idea/2017.3/gap_concatination.png
    2. Select Insert gap with concatenation ("+ +") and press Enter. Now you can insert a value into your string.
      /help/img/idea/2017.3/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.
      /help/img/idea/2017.3/interpolated_string.png
    2. Select Convert to interpolated string, press Enter and view the result.
      /help/img/idea/2017.3/interpolated_string_result.png
  • Convert a string into a multi-line string using the Convert to """string""" intention and vice versa.
    1. Enter your string, press Alt+Enter and from the drop-down list of intentions, select Convert to """string""".
      /help/img/idea/2017.3/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.
      /help/img/idea/2017.3/convert_to_reg_string.png
      View the result.
      /help/img/idea/2017.3/string_result.png
    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.
      /help/img/idea/2017.3/stripMargin_Method.png
      The white spaces are also preserved. Check the following example:
      /help/img/idea/2017.3/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.

    /help/img/idea/2017.3/add_replace_intention.png
  • Use the 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 from the drop-down list select Inject Language/Reference.
      The list of available languages and references opens.
      /help/img/idea/2017.3/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.
      /help/img/idea/2017.3/multi_line_strings.png
    3. Edit the settings and click OK.
  • Turn simple string into the interpolated one adding a variable reference.

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.
      /help/img/idea/2017.3/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):
      /help/img/idea/2017.3/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 a particular implicit conversion method that you originally wanted:
      /help/img/idea/2017.3/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:
      /help/img/idea/2017.3/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.
      /help/img/idea/2017.3/implicit_param.png
    2. If IntelliJ IDEA cannot find method calls where implicit parameters were passed, it displays a pop-up message:
      /help/img/idea/2017.3/no_implicit_param.png

Type Info action

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

  • To invoke the Show Type Info action in the editor, navigate to the value and press Alt+Equals or N/A (for Mac OS):
    /help/img/idea/2017.3/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 Alt+Equals or N/A (for Mac OS):
    /help/img/idea/2017.3/type_info_int.png
  • To add a type annotation, highlight the value, press Alt+Enter and from the context menu select Add type annotation to value definition:
    /help/img/idea/2017.3/type_info_add.png
    As a result, the type annotation is added:
    /help/img/idea/2017.3/type_info_add_result.png
    To remove the type annotation, press Alt+Enter and select Remove type annotation from value definition.
  • You can also use the Adjust types action to shorten types with full qualified names. Select the code in question, press Alt+Enter and from the context menu, select Adjust types.
    /help/img/idea/2017.3/type_adjust.png
    In this case the necessary imports are added.
    /help/img/idea/2017.3/type_adjust_result.png

Create from usage

IntelliJ IDEA lets you create new code elements without declaring them first:

  1. In the editor, type a name of a new code element and press Alt+Enter.
  2. From the list of intentions, select the one you need.
    /help/img/idea/2017.3/create_from_usage.png
    Press Enter.

Structure view

IntelliJ IDEA lets you view a structure of your code:

  • To open the Structure tool window, press Alt+7.
    /help/img/idea/2017.3/view_code_structure.png
  • To navigate from the Structure tool window to the code item in the editor, press F4.
    To simply highlight the code, press Ctrl+Enter.

Java-to-Scala code conversion

IntelliJ IDEA lets you convert Java code into Scala:

  1. Copy your Java code (expression, method, class) and paste it into a Scala file.
    /help/img/idea/2017.3/java_to_scala_code_in.png
  2. IntelliJ IDEA displays the Convert the code from Java dialog suggesting a conversion.
    /help/img/idea/2017.3/convert_code_from_scala_dialog.png
    Click OK.
    /help/img/idea/2017.3/java_to_scala_code_out.png

If you do not want to use the copy/paste actions, you can open your Java file in the editor and select Refactor | Convert to Scala or press Ctrl+Shift+G.

In this case IntelliJ IDEA will create a Scala file with the converted code.

Scala templates

IntelliJ IDEA lets you use predefined Scala templates:

  1. In the editor start entering your code, press Ctrl+J.
    IntelliJ IDEA displays the list of available Live templates for Scala.
    /help/img/idea/2017.3/scala_template.png
  2. Select the one you need and press Enter.

You can also define a new template or edit the existing one:

  1. Select Settings/Preferences | Editor | Live Templates.
  2. From options on the right, open the list of Scala templates.
    /help/img/idea/2017.3/scala_templates_settings.png
  3. If you want to add a new template, click /help/img/idea/2017.3/add.png.
    If you want to edit the existing template, select the one you need and change the default definitions.
Last modified: 11 October 2017