nus-cs2103-AY2324S2 / forum

16 stars 0 forks source link

Configuring IntelliJ autoformatter or checkstyle for lambdas #181

Closed rertyy closed 8 months ago

rertyy commented 9 months ago

How do I configure checkstyle or IntelliJ auto-formatter to handle lambdas correctly?

I already have checked that Settings > Editor > Code Style > Java > Tabs and Indents > Continuation Indent is set to 8.

As a workaround, extracting the lambda to a separate method has no issues with checkstyle.

A contrived example which illustrates this as below

public void testCheckstyle() {
    int[] a = {1, 2, 3};
    Arrays.stream(a)
            .forEach(integerNumber -> {
                        System.out.println(integerNumber); // actual 28: expected 20
                        System.out.println(integerNumber);

                    }
            ); // actual 16: expected 12
}
nus-se-bot commented 9 months ago

Tagging a few other students (randomly selected) who have completed related increments, in case they can help: @chaaaaun @BryanL2303 @Anant1902 @headcube1 @Kappaccinoh @Joseph31416 @yleeyilin @jiahui0309 @Lin-Shuang-Shuang @miffi Others are welcome to pitch in too.

lshaoqin commented 9 months ago

Hi @rertyy, I believe the default for continuation indent in checkstyle is 4, so it may help to set the continuation indent in IntelliJ to 4 as well. Let me know if this works!

flexibo commented 9 months ago

Hi @rertyy I am a senior from the previous batch!

For my settings, the continuation indent is set to 8. You might want to check the indent size, it should be 4.

As I'm not 100% sure what the issue is, here is the rest of my tab related settings and lmk if its the same as yours

Tab size: 4 Indent: 4 Continuation indent: 8 Label indent: 0

disable all checkboxes

kiwibang commented 9 months ago

Hi @rertyy , I am also a senior from the previous batch!

Seconding @flexibo and @lshaoqin on this. You can change the continuation indent to 4 in IntelliJ.

I've tested it myself and the indenting seems to work for me!

For context, these are my IntelliJ settings (on MacOS but I believe this is the same for windows too)

Screenshot 2024-02-05 at 11 39 12 AM
rertyy commented 9 months ago

Hi all, thanks for the suggestions.

Strangely enough checkstyle doesn't give warnings when continuation indent is 4, but it doesn't adhere to the coding standard

Indentation for wrapped lines should be 8 spaces

as given here

Setting indent to 8 has no errors apart from lambdas Setting indent to 4 has no errors anywhere

hansstanley commented 9 months ago

Hi @rertyy, I'm another senior from a previous batch.

In the .editorconfig file from your project root directory, the ij_continuation_indent_size has been set to 8 as shown:

[*]
charset = utf-8
end_of_line = crlf
indent_size = 4
indent_style = space
insert_final_newline = true
max_line_length = 120
tab_width = 4
ij_continuation_indent_size = 8         <-- try setting this to 4 instead
ij_formatter_off_tag = @formatter:off
ij_formatter_on_tag = @formatter:on
ij_formatter_tags_enabled = true
ij_smart_tabs = false
ij_visual_guides = 
ij_wrap_on_typing = false

Perhaps this is overriding your IDE settings? I get this warning in my settings, which led me to check the .editorconfig file.

Screenshot 2024-02-05 at 12 38 43

Hope this helps!

rertyy commented 9 months ago

Hi @hansstanley

Thanks for the comment. I'm aware of .editorconfig.

I suspect it's how the checkstyle is configured rather than my editor/.editorconfig settings because continuation indent of 4 doesn't adhere to the code style from here, as mentioned in my previous comment

I think I will stick to extracting the lambda into a separate method. Which does not have the checkstyle warning.

hansstanley commented 9 months ago

To adhere to the course code style, perhaps you could try typing the lambda function as such, instead of splitting the closing curly and round braces onto different lines:

Arrays.stream(a)
        .forEach(i -> {
            System.out.println();
            System.out.println();
        });

Strangely enough checkstyle doesn't give warnings when continuation indent is 4, but it doesn't adhere to the coding standard

I think this is because the given checkstyle.xml does not strictly check for continuation indent (forceStrictCondition is not specified and defaults to false). Here's the relevant snippet:

checkstyle.xml

<module name="Indentation">
  <property name="caseIndent" value="0" />
  <property name="throwsIndent" value="8" />
</module>

From the checkstyle docs, the forceStrictCondition is default to false:

Screenshot 2024-02-05 at 13 17 44
rertyy commented 8 months ago

That's good information, thanks!