Skip to content

Commit

Permalink
Adding "fix spaces".
Browse files Browse the repository at this point in the history
  • Loading branch information
eggnstone committed Sep 9, 2024
1 parent 14f5f88 commit 8b06207
Show file tree
Hide file tree
Showing 33 changed files with 191 additions and 50 deletions.
38 changes: 21 additions & 17 deletions lib/src/FormatState.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class FormatState
const String methodName = 'acceptListWithComma';

if (nodes.isNotEmpty)
_consumeSpaces(nodes.first, spaces);
consumeSpaces(nodes.first, spaces);

AstNode? lastNode;
for (final AstNode node in nodes)
Expand Down Expand Up @@ -551,7 +551,7 @@ class FormatState

if (entity is AstNode)
{
_consumeSpaces(entity, spaces);
consumeSpaces(entity, spaces);
entity.accept(astVisitor);
}
else
Expand Down Expand Up @@ -994,29 +994,33 @@ class FormatState
return result;
}

void _consumeSpaces(SyntacticEntity entity, int? spaces)
void consumeSpaces(SyntacticEntity entity, int? spaces)
{
if (spaces == null)
return;

final String filler = getText(lastConsumedPosition, entity.offset);
if (StringTools.trimSpaces(filler).isEmpty)
if (StringTools.trimSpaces(filler).isNotEmpty)
{
if (Constants.DEBUG_FORMAT_STATE_SPACING)
{
final String lastText = _textBuffers.last.toString();
logInternal(' entity: ${StringTools.toDisplayString(getText(entity.offset, entity.end))}');
logInternal(' lastText: ${StringTools.toDisplayString(lastText)}');
logInternal(' filler: ${StringTools.toDisplayString(getText(lastConsumedPosition, entity.offset))}');
}
if (Constants.DEBUG_FORMAT_STATE_SPACING) logInternal('consumeSpaces: filler has non-spaces: ${StringTools.toDisplayString(filler)}');
return;
}

if (Constants.DEBUG_FORMAT_STATE_SPACING)
{
final String lastText = _textBuffers.last.toString();
logInternal('consumeSpaces:');
logInternal(' entity: ${StringTools.toDisplayString(getText(entity.offset, entity.end))}');
logInternal(' lastText: ${StringTools.toDisplayString(lastText)}');
logInternal(' filler: ${StringTools.toDisplayString(getText(lastConsumedPosition, entity.offset))}');
}

consumeText(lastConsumedPosition, entity.offset, '', 'consumeSpaces', spaces: spaces);
consumeText(lastConsumedPosition, entity.offset, '', 'consumeSpaces', spaces: spaces);

if (Constants.DEBUG_FORMAT_STATE_SPACING)
{
final String lastText = _textBuffers.last.toString();
logInternal(' lastText: ${StringTools.toDisplayString(lastText)}');
}
if (Constants.DEBUG_FORMAT_STATE_SPACING)
{
final String lastText = _textBuffers.last.toString();
logInternal(' lastText: ${StringTools.toDisplayString(lastText)}');
}
}
}
8 changes: 6 additions & 2 deletions lib/src/FormatVisitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import 'Formatters/EnumDeclarationFormatter.dart';
import 'Formatters/ExportDirectiveFormatter.dart';
import 'Formatters/ExpressionFunctionBodyFormatter.dart';
import 'Formatters/ExpressionStatementFormatter.dart';
import 'Formatters/ExtendsClauseFormatter.dart';
import 'Formatters/ExtensionDeclarationFormatter.dart';
import 'Formatters/FieldDeclarationFormatter.dart';
import 'Formatters/ForElementFormatter.dart';
Expand Down Expand Up @@ -69,6 +70,7 @@ import 'Formatters/ReturnStatementFormatter.dart';
import 'Formatters/SetOrMapLiteralFormatter.dart';
import 'Formatters/ShowCombinatorFormatter.dart';
import 'Formatters/SimpleStringLiteralFormatter.dart';
import 'Formatters/SuperFormalParameterFormatter.dart';
import 'Formatters/SwitchExpressionFormatter.dart';
import 'Formatters/SwitchPatternCaseFormatter.dart';
import 'Formatters/SwitchStatementFormatter.dart';
Expand Down Expand Up @@ -109,6 +111,7 @@ class FormatVisitor extends AstVisitor<void>
late final EmptyFunctionBodyFormatter _emptyFunctionBodyFormatter = EmptyFunctionBodyFormatter(config, this, _formatState);
late final EmptyStatementFormatter _emptyStatementFormatter = EmptyStatementFormatter(config, this, _formatState);
late final EnumDeclarationFormatter _enumDeclarationFormatter = EnumDeclarationFormatter(config, this, _formatState);
late final ExtendsClauseFormatter _extendsClauseFormatter = ExtendsClauseFormatter(config, this, _formatState);
late final ExtensionDeclarationFormatter _extensionDeclarationFormatter = ExtensionDeclarationFormatter(config, this, _formatState);
late final ExportDirectiveFormatter _exportDirectiveFormatter = ExportDirectiveFormatter(config, this, _formatState);
late final ExpressionFunctionBodyFormatter _expressionFunctionBodyFormatter = ExpressionFunctionBodyFormatter(config, this, _formatState);
Expand Down Expand Up @@ -159,6 +162,7 @@ class FormatVisitor extends AstVisitor<void>
late final ShowCombinatorFormatter _showCombinatorFormatter = ShowCombinatorFormatter(config, this, _formatState);
late final SimpleStringLiteralFormatter _simpleStringLiteralFormatter = SimpleStringLiteralFormatter(config, this, _formatState);
//late final StringInterpolationFormatter _stringInterpolationFormatter = StringInterpolationFormatter(config, this, _formatState);
late final SuperFormalParameterFormatter _superFormalParameterFormatter = SuperFormalParameterFormatter(config, this, _formatState);
late final SwitchExpressionFormatter _switchExpressionFormatter = SwitchExpressionFormatter(config, this, _formatState);
late final SwitchStatementFormatter _switchStatementFormatter = SwitchStatementFormatter(config, this, _formatState);
late final SwitchPatternCaseFormatter _switchPatternCaseFormatter = SwitchPatternCaseFormatter(config, this, _formatState);
Expand Down Expand Up @@ -375,7 +379,7 @@ class FormatVisitor extends AstVisitor<void>

@override
void visitExtendsClause(ExtendsClause node)
=> _defaultFormatter.format(node);
=> _extendsClauseFormatter.format(node);

@override
void visitExtensionDeclaration(ExtensionDeclaration node)
Expand Down Expand Up @@ -771,7 +775,7 @@ class FormatVisitor extends AstVisitor<void>

@override
void visitSuperFormalParameter(SuperFormalParameter node)
=> _defaultFormatter.format(node);
=> _superFormalParameterFormatter.format(node);

@override
void visitSwitchCase(SwitchCase node)
Expand Down
3 changes: 2 additions & 1 deletion lib/src/Formatters/AugmentationImportDirectiveFormatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:analyzer/dart/ast/ast.dart';

import '../Constants/Constants.dart';
import '../Data/Config.dart';
import '../Data/ConfigExtension.dart';
import '../FormatState.dart';
import '../Tools/StringTools.dart';
import 'IFormatter.dart';
Expand All @@ -29,7 +30,7 @@ class AugmentationImportDirectiveFormatter extends IFormatter
formatState.copyEntity(node.importKeyword, astVisitor, '$methodName/node.importKeyword');
formatState.copyEntity(node.augmentKeyword, astVisitor, '$methodName/node.augmentKeyword');
formatState.copyEntity(node.uri, astVisitor, '$methodName/node.uri');
formatState.copySemicolon(node.semicolon, config, '$methodName/node.semicolon');
formatState.copySemicolon(node.semicolon, config, '$methodName/node.semicolon', config.space0);

if (Constants.DEBUG_I_FORMATTER) log('END $methodName(${StringTools.toDisplayString(node, Constants.MAX_DEBUG_LENGTH)})', --formatState.logIndent);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/src/Formatters/BreakStatementFormatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:analyzer/dart/ast/ast.dart';

import '../Constants/Constants.dart';
import '../Data/Config.dart';
import '../Data/ConfigExtension.dart';
import '../FormatState.dart';
import '../Tools/StringTools.dart';
import 'IFormatter.dart';
Expand All @@ -27,7 +28,7 @@ class BreakStatementFormatter extends IFormatter

formatState.copyEntity(node.breakKeyword,astVisitor, '$methodName/node.breakKeyword');
formatState.copyEntity(node.label, astVisitor, '$methodName/node.label');
formatState.copySemicolon(node.semicolon, config, '$methodName/node.semicolon');
formatState.copySemicolon(node.semicolon, config, '$methodName/node.semicolon', config.space0);

if (Constants.DEBUG_I_FORMATTER) log('END $methodName(${StringTools.toDisplayString(node, Constants.MAX_DEBUG_LENGTH)})', --formatState.logIndent);
}
Expand Down
9 changes: 5 additions & 4 deletions lib/src/Formatters/ClassDeclarationFormatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:analyzer/dart/ast/ast.dart';

import '../Constants/Constants.dart';
import '../Data/Config.dart';
import '../Data/ConfigExtension.dart';
import '../FormatState.dart';
import '../Tools/StringTools.dart';
import 'IFormatter.dart';
Expand Down Expand Up @@ -40,11 +41,11 @@ class ClassDeclarationFormatter extends IFormatter
//if (pushLevel)
formatState.pushLevel('$methodName/node.classKeyword/after');

formatState.copyEntity(node.name, astVisitor, '$methodName/node.name');
formatState.copyEntity(node.name, astVisitor, '$methodName/node.name', config.space1);
formatState.copyEntity(node.typeParameters, astVisitor, '$methodName/node.typeParameters');
formatState.copyEntity(node.extendsClause, astVisitor, '$methodName/node.extendsClause');
formatState.copyEntity(node.withClause, astVisitor, '$methodName/node.withClause');
formatState.copyEntity(node.implementsClause, astVisitor, '$methodName/node.implementsClause');
formatState.copyEntity(node.extendsClause, astVisitor, '$methodName/node.extendsClause', config.space1);
formatState.copyEntity(node.withClause, astVisitor, '$methodName/node.withClause', config.space1);
formatState.copyEntity(node.implementsClause, astVisitor, '$methodName/node.implementsClause', config.space1);

//if (pushLevel)
formatState.popLevelAndIndent();
Expand Down
3 changes: 2 additions & 1 deletion lib/src/Formatters/ClassTypeAliasFormatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:analyzer/dart/ast/ast.dart';

import '../Constants/Constants.dart';
import '../Data/Config.dart';
import '../Data/ConfigExtension.dart';
import '../FormatState.dart';
import '../Tools/StringTools.dart';
import 'IFormatter.dart';
Expand Down Expand Up @@ -41,7 +42,7 @@ class ClassTypeAliasFormatter extends IFormatter
formatState.copyEntity(node.superclass, astVisitor, '$methodName/node.superclass');
formatState.copyEntity(node.withClause, astVisitor, '$methodName/node.withClause');
formatState.copyEntity(node.implementsClause, astVisitor, '$methodName/node.implementsClause');
formatState.copySemicolon(node.semicolon, config, '$methodName/node.semicolon');
formatState.copySemicolon(node.semicolon, config, '$methodName/node.semicolon', config.space0);

if (Constants.DEBUG_I_FORMATTER) log('END $methodName(${StringTools.toDisplayString(node, Constants.MAX_DEBUG_LENGTH)})', --formatState.logIndent);
}
Expand Down
5 changes: 4 additions & 1 deletion lib/src/Formatters/ConstructorDeclarationFormatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ class ConstructorDeclarationFormatter extends IFormatter
formatState.copyEntity(node.externalKeyword, astVisitor, '$methodName/node.externalKeyword');
formatState.copyEntity(node.constKeyword, astVisitor, '$methodName/node.constKeyword');
formatState.copyEntity(node.factoryKeyword, astVisitor, '$methodName/node.factoryKeyword');
formatState.copyEntity(node.returnType, astVisitor, '$methodName/node.returnType');

final int? spacesForReturnType = config.fixSpaces ? (node.offset == node.returnType.offset ? 0 : 1) : null;
formatState.copyEntity(node.returnType, astVisitor, '$methodName/node.returnType', spacesForReturnType);

formatState.copyEntity(node.period, astVisitor, '$methodName/node.period');
formatState.copyEntity(node.name, astVisitor, '$methodName/node.name');
formatState.copyEntity(node.parameters, astVisitor, '$methodName/node.parameters');
Expand Down
3 changes: 2 additions & 1 deletion lib/src/Formatters/ContinueStatementFormatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:analyzer/dart/ast/ast.dart';

import '../Constants/Constants.dart';
import '../Data/Config.dart';
import '../Data/ConfigExtension.dart';
import '../FormatState.dart';
import '../Tools/StringTools.dart';
import 'IFormatter.dart';
Expand All @@ -27,7 +28,7 @@ class ContinueStatementFormatter extends IFormatter

formatState.copyEntity(node.continueKeyword, astVisitor, '$methodName/node.continueKeyword');
formatState.copyEntity(node.label, astVisitor, '$methodName/node.label');
formatState.copySemicolon(node.semicolon, config, '$methodName/node.semicolon');
formatState.copySemicolon(node.semicolon, config, '$methodName/node.semicolon', config.space0);

if (Constants.DEBUG_I_FORMATTER) log('END $methodName(${StringTools.toDisplayString(node, Constants.MAX_DEBUG_LENGTH)})', --formatState.logIndent);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/src/Formatters/EmptyFunctionBodyFormatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:analyzer/dart/ast/ast.dart';

import '../Constants/Constants.dart';
import '../Data/Config.dart';
import '../Data/ConfigExtension.dart';
import '../FormatState.dart';
import '../Tools/StringTools.dart';
import 'IFormatter.dart';
Expand All @@ -25,7 +26,7 @@ class EmptyFunctionBodyFormatter extends IFormatter
if (node is! EmptyFunctionBody)
throw FormatException('Not an EmptyFunctionBody: ${node.runtimeType}');

formatState.copySemicolon(node.semicolon, config, '$methodName/node.semicolon');
formatState.copySemicolon(node.semicolon, config, '$methodName/node.semicolon', config.space0);

if (Constants.DEBUG_I_FORMATTER) log('END $methodName(${StringTools.toDisplayString(node, Constants.MAX_DEBUG_LENGTH)})', --formatState.logIndent);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/src/Formatters/EmptyStatementFormatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:analyzer/dart/ast/ast.dart';

import '../Constants/Constants.dart';
import '../Data/Config.dart';
import '../Data/ConfigExtension.dart';
import '../FormatState.dart';
import '../Tools/StringTools.dart';
import 'IFormatter.dart';
Expand All @@ -25,7 +26,7 @@ class EmptyStatementFormatter extends IFormatter
if (node is! EmptyStatement)
throw FormatException('Not an EmptyStatement: ${node.runtimeType}');

formatState.copySemicolon(node.semicolon, config, '$methodName/node.semicolon');
formatState.copySemicolon(node.semicolon, config, '$methodName/node.semicolon', config.space0);

if (Constants.DEBUG_I_FORMATTER) log('END $methodName(${StringTools.toDisplayString(node, Constants.MAX_DEBUG_LENGTH)})', --formatState.logIndent);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/src/Formatters/EnumDeclarationFormatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:analyzer/dart/ast/token.dart';

import '../Constants/Constants.dart';
import '../Data/Config.dart';
import '../Data/ConfigExtension.dart';
import '../FormatState.dart';
import '../Tools/StringTools.dart';
import 'IFormatter.dart';
Expand Down Expand Up @@ -34,7 +35,7 @@ class EnumDeclarationFormatter extends IFormatter
// TODO: precedingComments: on semicolon, too. Check other formatters, too.
final Token endTokenForConstants = node.semicolon ?? node.rightBracket.precedingComments ?? node.rightBracket;
formatState.acceptListWithComma(node.constants, endTokenForConstants, astVisitor, '$methodName/node.constants');
formatState.copySemicolon(node.semicolon, config, '$methodName/node.semicolon');
formatState.copySemicolon(node.semicolon, config, '$methodName/node.semicolon', config.space0);
formatState.acceptList(node.members, astVisitor, '$methodName/node.members');
formatState.copyClosingBraceAndPopLevel(node.rightBracket, config, '$methodName/node.rightBracket');

Expand Down
3 changes: 2 additions & 1 deletion lib/src/Formatters/ExportDirectiveFormatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:analyzer/dart/ast/ast.dart';

import '../Constants/Constants.dart';
import '../Data/Config.dart';
import '../Data/ConfigExtension.dart';
import '../FormatState.dart';
import '../Tools/StringTools.dart';
import 'IFormatter.dart';
Expand All @@ -30,7 +31,7 @@ class ExportDirectiveFormatter extends IFormatter
formatState.copyEntity(node.uri, astVisitor, '$methodName/node.uri');
formatState.acceptList(node.combinators, astVisitor, '$methodName/node.combinators');
formatState.acceptList(node.configurations, astVisitor, '$methodName/node.configurations');
formatState.copySemicolon(node.semicolon, config, '$methodName/node.semicolon');
formatState.copySemicolon(node.semicolon, config, '$methodName/node.semicolon', config.space0);

if (Constants.DEBUG_I_FORMATTER) log('END $methodName(${StringTools.toDisplayString(node, Constants.MAX_DEBUG_LENGTH)})', --formatState.logIndent);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/src/Formatters/ExpressionFunctionBodyFormatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:analyzer/dart/ast/ast.dart';

import '../Constants/Constants.dart';
import '../Data/Config.dart';
import '../Data/ConfigExtension.dart';
import '../FormatState.dart';
import '../Tools/StringTools.dart';
import 'IFormatter.dart';
Expand All @@ -28,7 +29,7 @@ class ExpressionFunctionBodyFormatter extends IFormatter
formatState.copyEntity(node.keyword, astVisitor, '$methodName/node.keyword');
formatState.copyEntity(node.functionDefinition, astVisitor, '$methodName/node.functionDefinition');
formatState.copyEntity(node.expression, astVisitor, '$methodName/node.expression');
formatState.copySemicolon(node.semicolon, config, '$methodName/node.semicolon');
formatState.copySemicolon(node.semicolon, config, '$methodName/node.semicolon', config.space0);

if (Constants.DEBUG_I_FORMATTER) log('END $methodName(${StringTools.toDisplayString(node, Constants.MAX_DEBUG_LENGTH)})', --formatState.logIndent);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/src/Formatters/ExpressionStatementFormatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:analyzer/dart/ast/ast.dart';

import '../Constants/Constants.dart';
import '../Data/Config.dart';
import '../Data/ConfigExtension.dart';
import '../FormatState.dart';
import '../Tools/StringTools.dart';
import 'IFormatter.dart';
Expand All @@ -26,7 +27,7 @@ class ExpressionStatementFormatter extends IFormatter
throw FormatException('Not an ExpressionStatement: ${node.runtimeType}');

formatState.copyEntity(node.expression, astVisitor, '$methodName/node.expression');
formatState.copySemicolon(node.semicolon, config, '$methodName/node.semicolon');
formatState.copySemicolon(node.semicolon, config, '$methodName/node.semicolon', config.space0);

if (Constants.DEBUG_I_FORMATTER) log('END $methodName(${StringTools.toDisplayString(node, Constants.MAX_DEBUG_LENGTH)})', --formatState.logIndent);
}
Expand Down
34 changes: 34 additions & 0 deletions lib/src/Formatters/ExtendsClauseFormatter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// ignore_for_file: always_put_control_body_on_new_line

import 'package:analyzer/dart/ast/ast.dart';

import '../Constants/Constants.dart';
import '../Data/Config.dart';
import '../Data/ConfigExtension.dart';
import '../FormatState.dart';
import '../Tools/StringTools.dart';
import 'IFormatter.dart';

class ExtendsClauseFormatter extends IFormatter
{
final AstVisitor<void> astVisitor;
final Config config;
final FormatState formatState;

ExtendsClauseFormatter(this.config, this.astVisitor, this.formatState);

@override
void format(AstNode node)
{
const String methodName = 'ExtendsClauseFormatter.format';
if (Constants.DEBUG_I_FORMATTER) log('START $methodName(${StringTools.toDisplayString(node, Constants.MAX_DEBUG_LENGTH)})', formatState.logIndent++);

if (node is! ExtendsClause)
throw FormatException('Not an ExtendsClause: ${node.runtimeType}');

formatState.copyEntity(node.extendsKeyword, astVisitor, '$methodName/node.extendsKeyword');
formatState.copyEntity(node.superclass, astVisitor, '$methodName/node.superclass', config.space1);

if (Constants.DEBUG_I_FORMATTER) log('END $methodName(${StringTools.toDisplayString(node, Constants.MAX_DEBUG_LENGTH)})', --formatState.logIndent);
}
}
3 changes: 2 additions & 1 deletion lib/src/Formatters/FieldDeclarationFormatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:analyzer/dart/ast/ast.dart';

import '../Constants/Constants.dart';
import '../Data/Config.dart';
import '../Data/ConfigExtension.dart';
import '../FormatState.dart';
import '../Tools/StringTools.dart';
import 'IFormatter.dart';
Expand All @@ -30,7 +31,7 @@ class FieldDeclarationFormatter extends IFormatter
formatState.copyEntity(node.externalKeyword, astVisitor, '$methodName/node.externalKeyword');
formatState.copyEntity(node.staticKeyword, astVisitor, '$methodName/node.staticKeyword');
formatState.copyEntity(node.fields, astVisitor, '$methodName/node.fields');
formatState.copySemicolon(node.semicolon, config, '$methodName/node.semicolon');
formatState.copySemicolon(node.semicolon, config, '$methodName/node.semicolon', config.space0);

if (Constants.DEBUG_I_FORMATTER) log('END $methodName(${StringTools.toDisplayString(node, Constants.MAX_DEBUG_LENGTH)})', --formatState.logIndent);
}
Expand Down
Loading

0 comments on commit 8b06207

Please sign in to comment.