diff --git a/TODO.txt b/TODO.txt index ca3bf89..c2eb0ef 100644 --- a/TODO.txt +++ b/TODO.txt @@ -17,6 +17,7 @@ TODO - ensure DEBUG is off when publishing - safety: don't accept result if length is 0 or very short, e.g. when throwing UnimplementedError in StringTools. - No blank lines between {{ and }} +- No blank line after { and before } - Investigate line endings. Keep existing style, but also offer option to convert. DONE (I think) diff --git a/lib/src/FormatState.dart b/lib/src/FormatState.dart index ae7b3d0..d4d4cec 100644 --- a/lib/src/FormatState.dart +++ b/lib/src/FormatState.dart @@ -203,10 +203,20 @@ class FormatState logInternal(' filler/2: ${StringTools.toDisplayString(filler)}'); } - final int pos = filler.indexOf('\n'); - if (Constants.DEBUG_FORMAT_STATE) logInternal(' pos: $pos'); - if (pos >= 0) + final int lineBreakPos = filler.indexOf('\n'); + if (Constants.DEBUG_FORMAT_STATE) logInternal(' lineBreakPos: $lineBreakPos'); + if (lineBreakPos >= 0) + { + if (Constants.DEBUG_FORMAT_STATE) logInternal(' Line break already in filler => not adding line break'); + return; + } + + // TODO: test + if (nextToken.toString() == ';') + { + if (Constants.DEBUG_FORMAT_STATE) logInternal(' nextToken is ";" => not adding line break'); return; + } if (nextToken.offset == _parseResult.content.length) { diff --git a/lib/src/Formatters/SwitchExpressionFormatter.dart b/lib/src/Formatters/SwitchExpressionFormatter.dart index b6a24b0..cf19cb3 100644 --- a/lib/src/Formatters/SwitchExpressionFormatter.dart +++ b/lib/src/Formatters/SwitchExpressionFormatter.dart @@ -31,11 +31,10 @@ class SwitchExpressionFormatter extends IFormatter formatState.copyEntity(node.leftParenthesis, astVisitor, '$methodName/node.leftParenthesis'); formatState.copyEntity(node.expression, astVisitor, '$methodName/node.expression'); formatState.copyEntity(node.rightParenthesis, astVisitor, '$methodName/node.rightParenthesis'); - formatState.copyEntity(node.leftBracket, astVisitor, '$methodName/node.leftBracket'); - //formatState.pushLevel('$methodName/node.members'); + + formatState.copyOpeningBraceAndPushLevel(node.leftBracket, config, '$methodName/node.leftBracket'); formatState.acceptListWithComma(node.cases, node.rightBracket, astVisitor, '$methodName/node.cases'); - //formatState.popLevelAndIndent(); - formatState.copyEntity(node.rightBracket, astVisitor, '$methodName/node.rightBracket'); + formatState.copyClosingBraceAndPopLevel(node.rightBracket, config, '$methodName/node.rightBracket'); if (Constants.DEBUG_I_FORMATTER) log('END $methodName(${StringTools.toDisplayString(node, Constants.MAX_DEBUG_LENGTH)})', --formatState.logIndent); } diff --git a/test/FormateState/AddNewLineAfterToken_test.dart b/test/FormateState/AddNewLineAfterToken_test.dart new file mode 100644 index 0000000..be0ffdb --- /dev/null +++ b/test/FormateState/AddNewLineAfterToken_test.dart @@ -0,0 +1,49 @@ +import 'package:analyzer/dart/analysis/results.dart'; +import 'package:analyzer/dart/analysis/utilities.dart' as AnalyzerUtilities; // ignore: library_prefixes +import 'package:analyzer/dart/ast/token.dart'; +import 'package:dart_format/src/FormatState.dart'; +import 'package:test/test.dart'; + +import '../TestTools/TestTools.dart'; + +void main() +{ + TestTools.init(); + + group('FormatState', () + { + // TODO: better tests, tests with non-; + test('addNewLineAfterToken with ;', () + { + const String inputText = 'int i=0;int j=0;'; + final Token inputToken1 = Token(TokenType.STRING, 0); + final Token inputToken2 = Token(TokenType.SEMICOLON, 6); + final Token inputToken3 = Token(TokenType.STRING, 7); + final Token inputToken4 = Token(TokenType.SEMICOLON, 14); + inputToken1.next = inputToken2; + inputToken2.next = inputToken3; + inputToken3.next = inputToken4; + + final ParseStringResult parseResult = AnalyzerUtilities.parseString(content: inputText); + final FormatState formatState = FormatState( + parseResult, + indentationSpacesPerLevel: 4, + removeTrailingCommas: true + ); + + formatState.addText('int i=0', 'SOURCE'); + TestTools.expect(formatState.getResult(), equals('int i=0')); + formatState.addText(';', 'SOURCE'); + formatState.addNewLineAfterToken(inputToken1, 'SOURCE', add: true); + TestTools.expect(formatState.getResult(), equals('int i=0;')); + + formatState.addText('int j=0', 'SOURCE'); + TestTools.expect(formatState.getResult(), equals('int i=0;int j=0')); + formatState.addText(';', 'SOURCE'); + formatState.addNewLineAfterToken(inputToken3, 'SOURCE', add: true); + TestTools.expect(formatState.getResult(), equals('int i=0;int j=0;')); + } + ); + } + ); +} diff --git a/test/Formatters/SwitchExpressionFormatter_test.dart b/test/Formatters/SwitchExpressionFormatter_test.dart new file mode 100644 index 0000000..4713cc4 --- /dev/null +++ b/test/Formatters/SwitchExpressionFormatter_test.dart @@ -0,0 +1,33 @@ +import 'package:analyzer/dart/ast/ast.dart'; +import 'package:dart_format/src/Formatters/SwitchExpressionFormatter.dart'; + +import '../TestTools/AstCreator.dart'; +import '../TestTools/TestConfig.dart'; +import '../TestTools/TestGroupConfig.dart'; +import '../TestTools/TestTools.dart'; +import '../TestTools/Visitors/TestVisitor.dart'; + +void main() +{ + TestTools.init(); + + final List testGroupConfigs = [ + TestGroupConfig( + inputNodeCreator: AstCreator.createSwitchExpressionInReturnStatementExpressionInFunction, + inputLeading: 'int f(){return ', + inputMiddle: 'switch(0){0=>0}', + inputTrailing: ';}', + name: 'SwitchPatternCase', + astVisitors: >[ + TestVisitor(22, '0'), + TestVisitor(25, '0=>0'), + ], + testConfigs: [ + TestConfig.none(), + TestConfig('switch(0)\n{\n 0=>0\n}') + ] + ) + ]; + + TestTools.runTestGroupsForFormatter(testGroupConfigs, 'SwitchExpressionFormatter', SwitchExpressionFormatter.new, StackTrace.current); +} diff --git a/test/TestTools/AstCreator.dart b/test/TestTools/AstCreator.dart index 7e29acc..a0eda7e 100644 --- a/test/TestTools/AstCreator.dart +++ b/test/TestTools/AstCreator.dart @@ -100,6 +100,9 @@ class AstCreator static OnClause createOnClauseInMixinDeclaration(String s) => createMixinDeclaration(s).onClause!; + static ReturnStatement createReturnStatementInFunction(String s) + => createStatementInFunction(s) as ReturnStatement; + static SimpleFormalParameter createSimpleFormalParameterInFunction(String s) => createFormalParameterInFunction(s) as SimpleFormalParameter; @@ -109,6 +112,12 @@ class AstCreator static Statement createStatementInWhileInFunction(String s) => createWhileStatementInFunction(s).body; + static Expression createReturnStatementExpressionInFunction(String s) + => createReturnStatementInFunction(s).expression!; + + static SwitchExpression createSwitchExpressionInReturnStatementExpressionInFunction(String s) + => createReturnStatementExpressionInFunction(s) as SwitchExpression; + static SwitchStatement createSwitchStatementInFunction(String s) => createStatementInFunction(s) as SwitchStatement;