Skip to content

Commit

Permalink
indentation and wrapping fixes (#677)
Browse files Browse the repository at this point in the history
* fixed comment indentation in empty switch cases, fixes #392, #174
* moved Java compile to Jvm
* fixed doc comment indentation with conditionals, fixes #499
* fixed empty lines between static functions in abstracts, fixes #356
* fixed multiline string literals with empty lines, fixes #368
* fixed expressionIf for array comprehension, fixes #365
* added wrapping.mapWrap, fixes #675
* added formatter:off testcase, #676
  • Loading branch information
AlexHaxe authored Feb 10, 2024
1 parent 4562d6b commit 897f5d1
Show file tree
Hide file tree
Showing 26 changed files with 564 additions and 19 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/formatter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
build:
strategy:
matrix:
haxe-version: ['4.3.2', 'nightly']
haxe-version: ['4.3.3', 'nightly']
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
Expand Down Expand Up @@ -51,10 +51,9 @@ jobs:
- name: Build JSON schema
run: npx haxe buildSchema.hxml
- name: Run eval tests
if: (matrix.haxe-version == '4.3.2')
if: (matrix.haxe-version == '4.3.3')
run: npx haxe -D codecov_json test.hxml
- name: Run Java tests
if: (matrix.haxe-version == 'nightly')
run: npx haxe -D codecov_json testJava.hxml
- name: Run Node version with --check
run: node run.js -s . --check
Expand All @@ -74,5 +73,5 @@ jobs:
# name: formatter.js
# path: bin/formatter.js
- name: Upload results to codecov
if: success() && matrix.platform == 'ubuntu-latest' && (matrix.haxe-version == '4.3.2')
if: success() && matrix.platform == 'ubuntu-latest' && (matrix.haxe-version == '4.3.3')
run: bash <(curl -s https://codecov.io/bash) || echo "Codecov did not collect coverage reports"
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

## dev branch / next version (1.x.x)

## version 1.16.0 (2024-02-10)

- Added `wrapping.mapWrap`, fixes [#675](https://github.com/HaxeCheckstyle/haxe-formatter/issues/675) ([#677](https://github.com/HaxeCheckstyle/haxe-formatter/issues/677))
- Fixed comment indentation in empty switch cases, fixes [#392](https://github.com/HaxeCheckstyle/haxe-formatter/issues/392), [#174](https://github.com/HaxeCheckstyle/haxe-formatter/issues/174) ([#677](https://github.com/HaxeCheckstyle/haxe-formatter/issues/677))
- Fixed multiline comment indentation with conditionals, fixes [#499](https://github.com/HaxeCheckstyle/haxe-formatter/issues/499) ([#677](https://github.com/HaxeCheckstyle/haxe-formatter/issues/677))
- Fixed empty lines between static functions in abstract, fixes [#356](https://github.com/HaxeCheckstyle/haxe-formatter/issues/356) ([#677](https://github.com/HaxeCheckstyle/haxe-formatter/issues/677))
- Fixed multiline string literals with empty lines, fixes [#368](https://github.com/HaxeCheckstyle/haxe-formatter/issues/368) ([#677](https://github.com/HaxeCheckstyle/haxe-formatter/issues/677))
- Fixed expressionIf for array comprehension, fixes [#365](https://github.com/HaxeCheckstyle/haxe-formatter/issues/365) ([#677](https://github.com/HaxeCheckstyle/haxe-formatter/issues/677))

## version 1.15.0 (2023-09-05)

- Added `sameLine.ifElseSemicolonNextLine` to allow breaking `if (true) foo; else foo;`, fixes [#612](https://github.com/HaxeCheckstyle/haxe-formatter/issues/612) ([#668](https://github.com/HaxeCheckstyle/haxe-formatter/issues/668))
Expand Down
3 changes: 1 addition & 2 deletions buildJava.hxml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
buildCommon.hxml
-D jvm
-java out
--jvm out/Cli.jar
-main formatter.Cli
1 change: 1 addition & 0 deletions display.hxml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
-lib munit
-lib safety
-lib test-adapter
-lib instrument
6 changes: 3 additions & 3 deletions haxe_libraries/tokentree.hxml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# @install: lix --silent download "haxelib:/tokentree#1.2.10" into tokentree/1.2.10/haxelib
-cp ${HAXE_LIBCACHE}/tokentree/1.2.10/haxelib/src
-D tokentree=1.2.10
# @install: lix --silent download "haxelib:/tokentree#1.2.11" into tokentree/1.2.11/haxelib
-cp ${HAXE_LIBCACHE}/tokentree/1.2.11/haxelib/src
-D tokentree=1.2.11
4 changes: 2 additions & 2 deletions haxelib.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"style"
],
"description": "A code formatter for Haxe",
"version": "1.15.0",
"releasenote": " Added sameLine.ifElseSemicolonNextLine - see CHANGELOG for details.",
"version": "1.16.0",
"releasenote": "Added wrapping.mapWrap; fixed some indentation issues - see CHANGELOG for details.",
"contributors": [
"AlexHaxe",
"Gama11"
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@haxecheckstyle/haxe-formatter",
"version": "1.15.0",
"version": "1.16.0",
"description": "A code formatter for Haxe",
"repository": {
"type": "git",
Expand Down
63 changes: 63 additions & 0 deletions resources/default-hxformat.json
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,69 @@
}
]
},
"mapWrap": {
"defaultWrap": "noWrap",
"rules": [
{
"conditions": [
{
"cond": "hasMultilineItems",
"value": 1
}
],
"type": "onePerLine"
},
{
"conditions": [
{
"cond": "totalItemLength <= n",
"value": 80
}
],
"type": "noWrap"
},
{
"conditions": [
{
"cond": "anyItemLength <= n",
"value": 30
},
{
"cond": "itemCount >= n",
"value": 10
}
],
"type": "fillLineWithLeadingBreak"
},
{
"conditions": [
{
"cond": "anyItemLength >= n",
"value": 30
}
],
"type": "onePerLine"
},
{
"conditions": [
{
"cond": "itemCount >= n",
"value": 4
}
],
"type": "onePerLine"
},
{
"conditions": [
{
"cond": "exceedsMaxLineLength",
"value": 1
}
],
"type": "onePerLine"
}
]
},
"maxLineLength": 160,
"metadataCallParameter": {
"defaultWrap": "noWrap",
Expand Down
4 changes: 4 additions & 0 deletions resources/hxformat-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,10 @@
"description": "type parameter wrapping rules",
"$ref": "#/definitions/formatter.config.WrapRules"
},
"mapWrap": {
"description": "map literal wrapping rules does not affect map comprehension, use \"sameLine.comprehensionFor\"",
"$ref": "#/definitions/formatter.config.WrapRules"
},
"objectLiteral": {
"description": "object literal wrapping rules",
"$ref": "#/definitions/formatter.config.WrapRules"
Expand Down
12 changes: 12 additions & 0 deletions src/formatter/codedata/ParsedCode.hx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class ParsedCode {
case Binop(OpGte):
skipCount = 1;
case Const(CMarkup(_)):
elimateMulitlineEmptyLines(token.pos);
var skipIndex = index + 1;
while (true) {
if (tokens[skipIndex].pos.min >= token.pos.max) {
Expand All @@ -89,12 +90,23 @@ class ParsedCode {
if (v.startsWith("-")) {
skipCount = 1;
}
case Comment(_) | Const(CString(_)):
elimateMulitlineEmptyLines(token.pos);
skipCount = 0;
default:
skipCount = 0;
}
}
}

function elimateMulitlineEmptyLines(pos:Position) {
var start:LinePos = getLinePos(pos.min);
var end:LinePos = getLinePos(pos.max);
for (i in start.line...end.line) {
emptyLines.remove(i);
}
}

public function getTokenTree(entryPoint:Null<TokenTreeEntryPoint> = null):TokenTree {
if (tokens == null) {
return null;
Expand Down
18 changes: 17 additions & 1 deletion src/formatter/codedata/TokenList.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package formatter.codedata;

import formatter.config.Config;
import formatter.config.WhitespacePolicy;
import formatter.marker.Indenter;
import formatter.marker.MarkLineEnds;
#if debugLog
import haxe.PosInfos;
import sys.io.File;
Expand Down Expand Up @@ -598,7 +600,8 @@ class TokenList {
info.additionalIndent = indent;
}

public function increaseIndentBetween(start:Null<TokenTree>, end:Null<TokenTree>, depth:Int, ?pos:PosInfos) {
public function increaseIndentBetween(start:Null<TokenTree>, end:Null<TokenTree>, depth:Int, config:Config, parsedCode:ParsedCode, indenter:Indenter,
?pos:PosInfos) {
if ((depth == 0) || (start == null) || (start.index < 0) || (end == null) || (end.index < 0)) {
return;
}
Expand All @@ -607,6 +610,7 @@ class TokenList {
startIndex++;
}
var endIndex:Int = end.index;
var outputLineEnds:String = MarkLineEnds.outputLineSeparator(config.lineEnds, parsedCode);
for (index in startIndex...endIndex) {
var info:Null<TokenInfo> = tokens[index];
if (info == null) {
Expand All @@ -616,6 +620,18 @@ class TokenList {
logAction(pos, info.token, '${info.additionalIndent} -> ${info.additionalIndent + depth}');
#end
info.additionalIndent += depth;
switch (info.token.tok) {
case Comment(s):
var lines:Array<String> = info.text.split(outputLineEnds);
var addIndent:String = indenter.makeIndentString(depth);
if (lines.length > 0) {
for (line in 1...lines.length) {
lines[line] = addIndent + lines[line];
}
info.text = lines.join(outputLineEnds);
}
default:
}
}
}

Expand Down
36 changes: 36 additions & 0 deletions src/formatter/config/WrapConfig.hx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,42 @@ typedef WrapConfig = {
@:optional
var arrayWrap:WrapRules;

/**
map literal wrapping rules
does not affect map comprehension, use "sameLine.comprehensionFor"
**/
@:default({
defaultWrap: NoWrap,
rules: [
{
conditions: [{cond: HasMultiLineItems, value: 1}],
type: OnePerLine
},
{
conditions: [{cond: TotalItemLengthLessThan, value: 80}],
type: NoWrap
},
{
conditions: [{cond: AnyItemLengthLessThan, value: 30}, {cond: ItemCountLargerThan, value: 10}],
type: FillLineWithLeadingBreak
},
{
conditions: [{cond: AnyItemLengthLargerThan, value: 30}],
type: OnePerLine
},
{
conditions: [{cond: ItemCountLargerThan, value: 4}],
type: OnePerLine
},
{
conditions: [{cond: ExceedsMaxLineLength, value: 1}],
type: OnePerLine
}
]
})
@:optional
var mapWrap:WrapRules;

/**
detect arrays in matrix configuration from source
noMatrixWrap = no detection
Expand Down
6 changes: 6 additions & 0 deletions src/formatter/marker/MarkSameLine.hx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ class MarkSameLine extends MarkerBase {
switch (parent.tok) {
case Kwd(KwdReturn):
return true;
case BkOpen:
return true;
case BrOpen:
if (parent.parent.tok.match(Kwd(KwdFor))) {
return isExpression(parent);
}
case Kwd(KwdMacro):
return isExpression(parent);
case Arrow:
Expand Down
2 changes: 1 addition & 1 deletion src/formatter/marker/MarkerBase.hx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class MarkerBase {
}

public function increaseIndentBetween(start:Null<TokenTree>, end:Null<TokenTree>, depth:Int, ?pos:PosInfos) {
parsedCode.tokenList.increaseIndentBetween(start, end, depth, pos);
parsedCode.tokenList.increaseIndentBetween(start, end, depth, config, parsedCode, indenter, pos);
}

public function findTokenAtOffset(offset:Int):Null<TokenInfo> {
Expand Down
44 changes: 44 additions & 0 deletions src/formatter/marker/wrapping/MarkWrapping.hx
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,21 @@ class MarkWrapping extends MarkWrappingBase {
}

function arrayWrapping(token:TokenTree) {
switch (TokenTreeCheckUtils.getBkOpenType(token)) {
case ArrayAccess:
return;
case ArrayLiteral:
arrayLiteralWrapping(token);
case Comprehension:
arrayLiteralWrapping(token);
case MapLiteral:
mapLiteralWrapping(token);
case Unknown:
arrayLiteralWrapping(token);
}
}

function arrayLiteralWrapping(token:TokenTree) {
var bkClose:Null<TokenTree> = getCloseToken(token);
if ((token.children == null) || (token.children.length <= 0)) {
return;
Expand Down Expand Up @@ -353,6 +368,35 @@ class MarkWrapping extends MarkWrappingBase {
return true;
}

function mapLiteralWrapping(token:TokenTree) {
var bkClose:Null<TokenTree> = getCloseToken(token);
if ((token.children == null) || (token.children.length <= 0)) {
return;
}
var items:Array<WrappableItem> = makeWrappableItems(token);
var itemsWithoutMetadata:Array<WrappableItem> = [];
for (item in items) {
switch (item.first.tok) {
case At:
if (item.firstLineLength > 30) {
lineEndBefore(token);
lineEndBefore(item.first);
}
default:
itemsWithoutMetadata.push(item);
}
}
applyWrappingPlace({
origin: MapWrapping,
start: token,
end: bkClose,
items: itemsWithoutMetadata,
rules: config.wrapping.mapWrap,
useTrailing: true,
overrideAdditionalIndent: null
});
}

override function calcLineLength(token:TokenTree):Int {
if (token == null) {
return 0;
Expand Down
3 changes: 3 additions & 0 deletions src/formatter/marker/wrapping/MarkWrappingBase.hx
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,8 @@ class MarkWrappingBase extends MarkerBase {
"FunctionSignatureWrapping";
case ImplementsWrapping:
"ImplementsWrapping";
case MapWrapping:
"MapWrapping";
case MetadataCallParameterWrapping:
"MetadataCallParameterWrapping";
case MethodChainWrapping:
Expand Down Expand Up @@ -975,6 +977,7 @@ typedef WrappingPlace = {
enum WrappingOrigin {
AnonTypeWrapping;
ArrayWrapping;
MapWrapping;
CallParameterWrapping;
CasePatternWrapping;
FunctionSignatureWrapping;
Expand Down
Loading

0 comments on commit 897f5d1

Please sign in to comment.