HaxeCheckstyle / haxe-formatter

Haxe code formatter based on tokentree
https://haxecheckstyle.github.io/haxe-formatter-docs/#codesamples.CommonSamples.allman_curlies
MIT License
72 stars 16 forks source link

Two empty lines after functions/vars not working #665

Open sebthom opened 1 year ago

sebthom commented 1 year ago

Describe the bug

Setting any of the following emptyLines/classEmptyLines options to insert/keep two lines instead of one are ignored by the formatter:

"afterStaticFunctions": 2,
"afterStaticVars": 2,
"afterVars": 2,
"betweenStaticFunctions": 2,
"betweenFunctions": 2

Input file

class Example {

   static final staticField1 = "";
   static final staticField2 = "";

   final field1 = "";
   final field2 = "";

   static function staticFunc1() {
      trace("hi");
   }

   static function staticFunc2() {
      trace("hi");
   }

   function func1() {
      trace("hi");
   }

   function func2() {
      trace("hi");
   }
}

Broken output

class Example {

   static final staticField1 = "";
   static final staticField2 = "";

   final field1 = "";
   final field2 = "";

   static function staticFunc1() {
      trace("hi");
   }

   static function staticFunc2() {
      trace("hi");
   }

   function func1() {
      trace("hi");
   }

   function func2() {
      trace("hi");
   }
}

Expected output

class Example {

   static final staticField1 = "";
   static final staticField2 = "";

   final field1 = "";
   final field2 = "";

   static function staticFunc1() {
      trace("hi");
   }

   static function staticFunc2() {
      trace("hi");
   }

   function func1() {
      trace("hi");
   }

   function func2() {
      trace("hi");
   }
}

Optional: hxformat.json

{
    "emptyLines": {
        "beforeDocCommentEmptyLines": "ignore",
        "classEmptyLines": {
            "beginType": 1,
            "afterVars": 2,
            "afterStaticFunctions": 2,
            "afterStaticVars": 2,
            "betweenStaticFunctions": 2,
            "betweenFunctions": 2
        },
        "afterLeftCurly": "keep",
        "betweenTypes": 2,
        "maxAnywhereInFile": 2
    }
}
AlexHaxe commented 1 year ago

unless you set "existingBetweenFields": "remove" current behaviour of formatter is to keep just one empty line between fields and that will beat any of the other settings.

your current sample would work if you removed all empty lines between fields from input. problem is: next formatter run will reduce all double empty lines to just one, because of keep setting.

keep option probably should count number of empty lines in input to add correct amount of empty lines to output.

sebthom commented 1 year ago

Thanks for your hints! when I set existingBetweenFields to remove it actually works. It however feels very counter intuitive. I feel like the name and values of that option are misleading. Also, even though it is called existingBetweenFields it also affects empty lines between functions.

Unfortunately when setting existingBetweenFields to remove, as the name implies, all empty lines between fields are removed. So I cannot have something like

class Example {

   final field1 = "";
   final field2 = "";

   final field3 = "";
   final field4 = "";

   function func1() {
      trace("hi");
   }

   function func2() {
      trace("hi");
   }
}
AlexHaxe commented 1 year ago

Also, even though it is called existingBetweenFields it also affects empty lines between functions.

functions are fields.

Unfortunately when setting existingBetweenFields to remove, as the name implies, all empty lines between fields are removed.

yeah, that is the downside of remove, you can't have custom empty lines to separate different groups of fields. that's why keep is default. it needs a overhaul to support more than one empty line between fields.