Skip to content

Commit

Permalink
Fixed indent for switch expressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
eggnstone committed Jul 18, 2024
1 parent efbe0c0 commit 9628351
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 7 deletions.
1 change: 1 addition & 0 deletions TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 13 additions & 3 deletions lib/src/FormatState.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
7 changes: 3 additions & 4 deletions lib/src/Formatters/SwitchExpressionFormatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
49 changes: 49 additions & 0 deletions test/FormateState/AddNewLineAfterToken_test.dart
Original file line number Diff line number Diff line change
@@ -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;'));
}
);
}
);
}
33 changes: 33 additions & 0 deletions test/Formatters/SwitchExpressionFormatter_test.dart
Original file line number Diff line number Diff line change
@@ -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<TestGroupConfig> testGroupConfigs = <TestGroupConfig>[
TestGroupConfig(
inputNodeCreator: AstCreator.createSwitchExpressionInReturnStatementExpressionInFunction,
inputLeading: 'int f(){return ',
inputMiddle: 'switch(0){0=>0}',
inputTrailing: ';}',
name: 'SwitchPatternCase',
astVisitors: <TestVisitor<void>>[
TestVisitor<IntegerLiteral>(22, '0'),
TestVisitor<SwitchExpressionCase>(25, '0=>0'),
],
testConfigs: <TestConfig>[
TestConfig.none(),
TestConfig('switch(0)\n{\n 0=>0\n}')
]
)
];

TestTools.runTestGroupsForFormatter(testGroupConfigs, 'SwitchExpressionFormatter', SwitchExpressionFormatter.new, StackTrace.current);
}
9 changes: 9 additions & 0 deletions test/TestTools/AstCreator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down

0 comments on commit 9628351

Please sign in to comment.