Strange indentation behaviour for empty lines

Hi,

I have set up Webstorm to indent empty lines so my caret does not jump around when moving in the code. Unfortunately the indentation when formatting in my code is always off one level, indentation after hitting enter works correctly. While I wanted to create a minimalistic example I noticed the indentation is changing for code with doc comments.

What I found is that this code works (pipes show the indentation for the empty line):

Ext.define('foo', {
doBar: function() {
var bar = this;
|
bar.doBar();
}
});

But when I add a doc comment indentation is broken:

Ext.define('foo', {
/**
* foo bar
*/
doBar: function() {
var bar = this;
|
bar.doBar();
}
});

Is there any workaround to fix the indentation?

0
14 comments

Enter works equally well for me when using your examples - new line is correctly indented on Enter.

What IDE version do you use? Did you try re-formatting the code prior to hitting Enter? Enter indents a new line according to code style preferences, if your current code formatting doesn't match the preferences, new lines will be misaligned

>I have set up Webstorm to indent empty lines

Please can you clarify what you did namely?

0
Avatar
Permanently deleted user

To clearify: Indentation is only broken after reformatting.

Enter after reformatting is working correctly. The caret position does not matter, even hitting Enter on the wrongly indented line indents the appearing line correctly.

> Please can you clarify what you did namely?

I checked the setting Keep indents on empty lines in Editor > Code Style > JavaScript > Tabs and Indents. I tried messing around with everything I could find that has to do with indent and removing of blank spaces but nothing worked so far.

> What IDE version do you use?

WebStorm 2019.2
Build #WS-192.5728.87, built on July 22, 2019
0

>To clearify: Indentation is only broken after reformatting.

still not sure I follow you.. Do you mean that existing indents on _empty_ lines change to incorrect ones after Code | Reformat Code? This doesn't happen to me... Indents are kept as is if Keep indents on empty lines is enabled

0
Avatar
Permanently deleted user

> Do you mean that existing indents on _empty_ lines change to incorrect ones after Code | Reformat Code?

Yes that is exactly what is happening to me.

0

.js file that shows up the issue plus your code style settings (<IDE config dir>\codestyles\<scheme name>.xml if you are using IDE-level scheme, or .idea\codeStyles\Project.xml when using a project scheme) would be helpful.

0
Avatar
Permanently deleted user

The minimal example that shows my issue is in the first post. 

Project.xml

<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<option name="LINE_SEPARATOR" value="&#10;" />
<ScssCodeStyleSettings>
<option name="HEX_COLOR_LOWER_CASE" value="true" />
</ScssCodeStyleSettings>
<codeStyleSettings language="JavaScript">
<indentOptions>
<option name="KEEP_INDENTS_ON_EMPTY_LINES" value="true" />
</indentOptions>
</codeStyleSettings>
<codeStyleSettings language="SCSS">
<option name="KEEP_BLANK_LINES_IN_CODE" value="0" />
<indentOptions>
<option name="INDENT_SIZE" value="4" />
<option name="CONTINUATION_INDENT_SIZE" value="4" />
<option name="KEEP_INDENTS_ON_EMPTY_LINES" value="true" />
</indentOptions>
</codeStyleSettings>
</code_scheme>
</component>

Code style settings:

<code_scheme name="helix" version="173">
<option name="FORMATTER_TAGS_ENABLED" value="true" />
<JSCodeStyleSettings version="0">
<option name="FORCE_SEMICOLON_STYLE" value="true" />
<option name="SPACE_BEFORE_FUNCTION_LEFT_PARENTH" value="false" />
<option name="USE_DOUBLE_QUOTES" value="false" />
<option name="FORCE_QUOTE_STYlE" value="true" />
<option name="ENFORCE_TRAILING_COMMA" value="Remove" />
<option name="VAR_DECLARATION_WRAP" value="2" />
<option name="OBJECT_LITERAL_WRAP" value="2" />
<option name="SPACES_WITHIN_OBJECT_LITERAL_BRACES" value="true" />
<option name="CHAINED_CALL_DOT_ON_NEW_LINE" value="false" />
</JSCodeStyleSettings>
<editorconfig>
<option name="ENABLED" value="false" />
</editorconfig>
<codeStyleSettings language="CSS">
<option name="KEEP_BLANK_LINES_IN_CODE" value="1" />
<indentOptions>
<option name="CONTINUATION_INDENT_SIZE" value="4" />
</indentOptions>
</codeStyleSettings>
<codeStyleSettings language="CoffeeScript">
<indentOptions>
<option name="KEEP_INDENTS_ON_EMPTY_LINES" value="true" />
</indentOptions>
</codeStyleSettings>
<codeStyleSettings language="Gherkin">
<indentOptions>
<option name="KEEP_INDENTS_ON_EMPTY_LINES" value="true" />
</indentOptions>
</codeStyleSettings>
<codeStyleSettings language="HTML">
<indentOptions>
<option name="CONTINUATION_INDENT_SIZE" value="4" />
</indentOptions>
</codeStyleSettings>
<codeStyleSettings language="Haml">
<indentOptions>
<option name="KEEP_INDENTS_ON_EMPTY_LINES" value="true" />
</indentOptions>
</codeStyleSettings>
<codeStyleSettings language="JSON">
<indentOptions>
<option name="INDENT_SIZE" value="4" />
<option name="KEEP_INDENTS_ON_EMPTY_LINES" value="true" />
</indentOptions>
</codeStyleSettings>
<codeStyleSettings language="JavaScript">
<option name="RIGHT_MARGIN" value="120" />
<option name="KEEP_LINE_BREAKS" value="false" />
<option name="KEEP_FIRST_COLUMN_COMMENT" value="false" />
<option name="KEEP_BLANK_LINES_IN_CODE" value="1" />
<option name="CALL_PARAMETERS_WRAP" value="1" />
<option name="METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE" value="true" />
<option name="METHOD_CALL_CHAIN_WRAP" value="5" />
<option name="BINARY_OPERATION_WRAP" value="1" />
<option name="TERNARY_OPERATION_WRAP" value="1" />
<option name="TERNARY_OPERATION_SIGNS_ON_NEXT_LINE" value="true" />
<option name="FOR_STATEMENT_WRAP" value="1" />
<option name="ARRAY_INITIALIZER_WRAP" value="1" />
<option name="WRAP_COMMENTS" value="true" />
<option name="IF_BRACE_FORCE" value="3" />
<option name="DOWHILE_BRACE_FORCE" value="3" />
<option name="WHILE_BRACE_FORCE" value="3" />
<option name="FOR_BRACE_FORCE" value="3" />
<option name="WRAP_ON_TYPING" value="0" />
<indentOptions>
<option name="SMART_TABS" value="true" />
<option name="KEEP_INDENTS_ON_EMPTY_LINES" value="true" />
</indentOptions>
</codeStyleSettings>
<codeStyleSettings language="PHP">
<indentOptions>
<option name="KEEP_INDENTS_ON_EMPTY_LINES" value="true" />
</indentOptions>
</codeStyleSettings>
<codeStyleSettings language="SCSS">
<option name="KEEP_BLANK_LINES_IN_CODE" value="1" />
<indentOptions>
<option name="INDENT_SIZE" value="4" />
<option name="CONTINUATION_INDENT_SIZE" value="4" />
<option name="KEEP_INDENTS_ON_EMPTY_LINES" value="true" />
</indentOptions>
</codeStyleSettings>
<codeStyleSettings language="Twig">
<indentOptions>
<option name="KEEP_INDENTS_ON_EMPTY_LINES" value="true" />
</indentOptions>
</codeStyleSettings>
<codeStyleSettings language="TypeScript">
<indentOptions>
<option name="KEEP_INDENTS_ON_EMPTY_LINES" value="true" />
</indentOptions>
</codeStyleSettings>
<codeStyleSettings language="XML">
<indentOptions>
<option name="KEEP_INDENTS_ON_EMPTY_LINES" value="true" />
</indentOptions>
</codeStyleSettings>
</code_scheme>

 

 

0

You have included both the project and IDE code style schemes. Which one do you actually use? See what scheme is selected in Settings | Editor | Code Style, Scheme:

0
Avatar
Permanently deleted user

I use the helix scheme:

0

Still no luck in recreating the issue... Indents on empty lines look OK after re-formatting the code when using your scheme:

 

Do you have any file watchers enabled? Can you recreate the issue in a new project?

0
Avatar
Permanently deleted user

I have no file watchers enabled and the issue cannot be recreated in a new project.

0
Avatar
Permanently deleted user

I might have found why this is happening. I have set up the reformatter to only affect the changed text like this:

When I change it to Whole file the indentation is correct. But sadly I do not want to reformat the whole file...

0
Avatar
Permanently deleted user

I've the same problem in IntelliJ with Java code. I'm using IntelliJ IDEA 2019.2 (Ultimate Edition) (Build #IU-192.5728.98, built on July 23, 2019) with the Save Actions plugin. These are the steps to reproduce the error:

  • Create new plain Java project
  • Activate Save Actions: Settings / Other Settings / Save Actions / Activate save actions on save (...) and enable Reformat only changed code (only if VCS configured)
  • Make sure indents for empty lines are enabled (Settings / Editor / Code Style / Java / Tabs and Indents)
  • Initialize Git: VCS / Import into Version Control / Create Git Repository
  • Create new class "Test" in src folder
  • Add the following code (spaces in empty lines as underscores):
public class Test
{
private final Integer myInt;
__
public Test (Integer myInt)
{
assert myInt != null : "myInt != null";
____
this.myInt = myInt;
}
}
  • Commit the file in Git
  • add "+ 123" to the field assignment
  • explicitly save (CTRL + S) to trigger reformat
  • see result:
public class Test
{
private final Integer myInt;
__
public Test (Integer myInt)
{
assert myInt != null : "myInt != null";
__
this.myInt = myInt + 123;
}
}

In the line before the field assignment, now are just two spaces instead of four.

If I reformat the whole file manually now (Code / Reformat Code) the indents are correct again.

And as you can see it doesn't need a comment for the error to show up.

0
Avatar
Permanently deleted user

These are my code style settings.

<code_scheme name="Format-Rules" version="173">
<option name="OTHER_INDENT_OPTIONS">
<value>
<option name="INDENT_SIZE" value="2" />
<option name="CONTINUATION_INDENT_SIZE" value="2" />
<option name="TAB_SIZE" value="2" />
</value>
</option>
<option name="RIGHT_MARGIN" value="100" />
<option name="WRAP_WHEN_TYPING_REACHES_RIGHT_MARGIN" value="true" />
<JavaCodeStyleSettings>
<option name="CLASS_NAMES_IN_JAVADOC" value="3" />
<option name="CLASS_COUNT_TO_USE_IMPORT_ON_DEMAND" value="10000" />
<option name="NAMES_COUNT_TO_USE_IMPORT_ON_DEMAND" value="10000" />
<option name="PACKAGES_TO_USE_IMPORT_ON_DEMAND">
<value />
</option>
<option name="IMPORT_LAYOUT_TABLE">
<value>
<package name="java" withSubpackages="true" static="false" />
<emptyLine />
<package name="javax" withSubpackages="true" static="false" />
<emptyLine />
<package name="" withSubpackages="true" static="false" />
<emptyLine />
</value>
</option>
<option name="JD_PRESERVE_LINE_FEEDS" value="true" />
<option name="JD_INDENT_ON_CONTINUATION" value="true" />
</JavaCodeStyleSettings>
<Properties>
<option name="KEEP_BLANK_LINES" value="true" />
</Properties>
<codeStyleSettings language="JAVA">
<option name="KEEP_LINE_BREAKS" value="false" />
<option name="KEEP_FIRST_COLUMN_COMMENT" value="false" />
<option name="KEEP_CONTROL_STATEMENT_IN_ONE_LINE" value="false" />
<option name="KEEP_BLANK_LINES_IN_DECLARATIONS" value="1" />
<option name="KEEP_BLANK_LINES_IN_CODE" value="1" />
<option name="KEEP_BLANK_LINES_BEFORE_RBRACE" value="1" />
<option name="BLANK_LINES_BEFORE_PACKAGE" value="1" />
<option name="BLANK_LINES_AROUND_CLASS" value="2" />
<option name="BRACE_STYLE" value="2" />
<option name="CLASS_BRACE_STYLE" value="2" />
<option name="METHOD_BRACE_STYLE" value="2" />
<option name="ELSE_ON_NEW_LINE" value="true" />
<option name="WHILE_ON_NEW_LINE" value="true" />
<option name="CATCH_ON_NEW_LINE" value="true" />
<option name="FINALLY_ON_NEW_LINE" value="true" />
<option name="ALIGN_MULTILINE_PARAMETERS" value="false" />
<option name="ALIGN_MULTILINE_RESOURCES" value="false" />
<option name="SPACE_WITHIN_ARRAY_INITIALIZER_BRACES" value="true" />
<option name="SPACE_BEFORE_METHOD_CALL_PARENTHESES" value="true" />
<option name="SPACE_BEFORE_METHOD_PARENTHESES" value="true" />
<option name="SPACE_BEFORE_ARRAY_INITIALIZER_LBRACE" value="true" />
<option name="CALL_PARAMETERS_WRAP" value="1" />
<option name="METHOD_PARAMETERS_WRAP" value="1" />
<option name="RESOURCE_LIST_WRAP" value="5" />
<option name="EXTENDS_LIST_WRAP" value="1" />
<option name="THROWS_LIST_WRAP" value="1" />
<option name="EXTENDS_KEYWORD_WRAP" value="1" />
<option name="THROWS_KEYWORD_WRAP" value="1" />
<option name="METHOD_CALL_CHAIN_WRAP" value="1" />
<option name="BINARY_OPERATION_WRAP" value="1" />
<option name="TERNARY_OPERATION_WRAP" value="5" />
<option name="ARRAY_INITIALIZER_WRAP" value="1" />
<option name="ASSIGNMENT_WRAP" value="1" />
<option name="METHOD_ANNOTATION_WRAP" value="5" />
<option name="CLASS_ANNOTATION_WRAP" value="5" />
<option name="FIELD_ANNOTATION_WRAP" value="5" />
<option name="PARAMETER_ANNOTATION_WRAP" value="5" />
<option name="VARIABLE_ANNOTATION_WRAP" value="5" />
<option name="SPACE_BEFORE_ANOTATION_PARAMETER_LIST" value="true" />
<indentOptions>
<option name="INDENT_SIZE" value="2" />
<option name="CONTINUATION_INDENT_SIZE" value="2" />
<option name="TAB_SIZE" value="2" />
<option name="SMART_TABS" value="true" />
<option name="KEEP_INDENTS_ON_EMPTY_LINES" value="true" />
</indentOptions>
</codeStyleSettings>
</code_scheme>
0
Avatar
Permanently deleted user

I created a Issue in YouTrack

0

Please sign in to leave a comment.