Skip to content

Commit

Permalink
Fixed indentation of block comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
eggnstone committed Feb 27, 2024
1 parent c941f20 commit ee9e580
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 86 deletions.
30 changes: 15 additions & 15 deletions lib/src/Handlers/WebServiceHandler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ class WebServiceHandler
);

/*stdin.handleError(
(Object error, StackTrace stackTrace)
{
logError('$METHOD_NAME: Quitting because stdin.handleError()/onError called: $error');
terminate = true;
terminateWithError = true;
}
(Object error, StackTrace stackTrace)
{
logError('$METHOD_NAME: Quitting because stdin.handleError()/onError called: $error');
terminate = true;
terminateWithError = true;
}
);*/

stdin.listen(
Expand Down Expand Up @@ -335,14 +335,14 @@ class WebServiceHandler
/*// ignore: avoid_catching_errors
on Error catch (e, st)
{
// necessary?
logErrorObject(METHOD_NAME, e, st);
// necessary?
logErrorObject(METHOD_NAME, e, st);
final DartFormatException dartFormatException = DartFormatException.error(e.toString());
request.response.statusCode = HttpStatus.ok;
request.response.headers.contentType = ContentType.text;
request.response.headers.add('X-DartFormat-Result', 'Fail');
request.response.headers.add('X-DartFormat-Exception', jsonEncode(dartFormatException.toJson()));
final DartFormatException dartFormatException = DartFormatException.error(e.toString());
request.response.statusCode = HttpStatus.ok;
request.response.headers.contentType = ContentType.text;
request.response.headers.add('X-DartFormat-Result', 'Fail');
request.response.headers.add('X-DartFormat-Exception', jsonEncode(dartFormatException.toJson()));
}*/
finally
{
Expand Down Expand Up @@ -389,8 +389,8 @@ class WebServiceHandler
/*// ignore: avoid_catching_errors
on Error catch (e, st)
{
// necessary?
logErrorObject(METHOD_NAME, e, st);
// necessary?
logErrorObject(METHOD_NAME, e, st);
}*/
finally
{
Expand Down
23 changes: 15 additions & 8 deletions lib/src/Tools/StringTools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,24 @@ class StringTools
if (lines.length < 2)
return s;

int minIndentation = 0;
int minIndentation = -1;
// Start at index 1 to skip the first line.
for (int i = 1; i < lines.length; i++)
{
final int indentation = lines[i].length - lines[i].trimLeft().length;
if (i == 1 || indentation < minIndentation)
minIndentation = indentation;
final String line = lines[i];
if (line.trim().isNotEmpty)
{
final int indentation = line.length - line.trimLeft().length;
if (minIndentation == -1 || indentation < minIndentation)
minIndentation = indentation;
}
}

if (minIndentation == -1)
//return s;
minIndentation =0;

if (Constants.DEBUG_STRING_TOOLS) logInternal('minIndentation: $minIndentation');
//final String minIndentationString = ' ' * minIndentation;
//if (Constants.DEBUG_STRING_TOOLS) logInternal('minIndentationString: ${StringTools.toDisplayString(minIndentationString)}');

for (int i = 0; i < lines.length; i++)
{
Expand All @@ -101,8 +107,9 @@ class StringTools
{
sb.write('\n');
if (Constants.DEBUG_STRING_TOOLS) logInternal('< #$i: \\n');
sb.write(line.substring(minIndentation));
if (Constants.DEBUG_STRING_TOOLS) logInternal('< #$i: ${StringTools.toDisplayString(line.substring(minIndentation))}');
final String shortenedLine = line.length <= minIndentation ? '' : line.substring(minIndentation);
sb.write(shortenedLine);
if (Constants.DEBUG_STRING_TOOLS) logInternal('< #$i: ${StringTools.toDisplayString(shortenedLine)}');
}
}

Expand Down
24 changes: 12 additions & 12 deletions test/FormatVisitor/Comments/Class_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ void main()
// TODO
/*test('Comment after newline before class declaration', ()
{
final String inputText = '\n${comment}class C{}';
final String expectedText = '\n${comment}class C{}';
final String inputText = '\n${comment}class C{}';
final String expectedText = '\n${comment}class C{}';
final String actualText = formatter.format(inputText);
final String actualText = formatter.format(inputText);
TestTools.expectX(actualText, equals(expectedText));
TestTools.expectX(actualText, equals(expectedText));
});*/

test('Comment before annotation before class declaration', ()
Expand Down Expand Up @@ -106,22 +106,22 @@ void main()
// TODO
/*test('Comment after class declaration', ()
{
final String inputText = 'class C{}$comment';
final String expectedText = 'class C{}$comment';
final String inputText = 'class C{}$comment';
final String expectedText = 'class C{}$comment';
final String actualText = formatter.format(inputText);
final String actualText = formatter.format(inputText);
TestTools.expectX(actualText, equals(expectedText));
TestTools.expectX(actualText, equals(expectedText));
});
test('Comment before newline after class declaration', ()
{
final String inputText = 'class C{}$comment\n';
final String expectedText = 'class C{}$comment\n';
final String inputText = 'class C{}$comment\n';
final String expectedText = 'class C{}$comment\n';
final String actualText = formatter.format(inputText);
final String actualText = formatter.format(inputText);
TestTools.expectX(actualText, equals(expectedText));
TestTools.expectX(actualText, equals(expectedText));
});*/
}
);
Expand Down
2 changes: 1 addition & 1 deletion test/FormatVisitor/Indentation/TODO_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void main()
testConfigs: <TestConfig>[
TestConfig.none(),
TestConfig('class C\n{\n}/*\n*//*\n*/')
//TestConfig('class C\n{\n void m()\n {\n }\n/*Comment*/\n}\n')
//TestConfig('class C\n{\n void m()\n {\n }\n/*Comment*/\n}\n')
]
);

Expand Down
68 changes: 34 additions & 34 deletions test/Formatters/CompilationUnitFormatter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ void main()
]
),
/*TestGroupConfig(
inputNodeCreator: AstCreator.createCompilationUnit,
inputMiddle: '/*Comment\n A*/',
name: 'Lonely block comment / formatted'
inputNodeCreator: AstCreator.createCompilationUnit,
inputMiddle: '/*Comment\n A*/',
name: 'Lonely block comment / formatted'
),*/

TestGroupConfig(
Expand All @@ -39,10 +39,10 @@ void main()
]
),
/*TestGroupConfig(
inputNodeCreator: AstCreator.createCompilationUnit,
inputMiddle: '/*Comment\n A*/class C{}',
name: 'Block comment before class declaration / formatted',
astVisitors: <TestVisitor<AstNode>>[TestVisitor<ClassDeclaration>(14, 'class C{}')]
inputNodeCreator: AstCreator.createCompilationUnit,
inputMiddle: '/*Comment\n A*/class C{}',
name: 'Block comment before class declaration / formatted',
astVisitors: <TestVisitor<AstNode>>[TestVisitor<ClassDeclaration>(14, 'class C{}')]
),*/

TestGroupConfig(
Expand All @@ -56,25 +56,25 @@ void main()
]
),
/*TestGroupConfig(
inputNodeCreator: AstCreator.createCompilationUnit,
inputMiddle: 'class C{}/*Comment\n A*/',
name: 'Block comment after class declaration / formatted',
astVisitors: <TestVisitor<AstNode>>[TestVisitor<ClassDeclaration>(0, 'class C{}')]
inputNodeCreator: AstCreator.createCompilationUnit,
inputMiddle: 'class C{}/*Comment\n A*/',
name: 'Block comment after class declaration / formatted',
astVisitors: <TestVisitor<AstNode>>[TestVisitor<ClassDeclaration>(0, 'class C{}')]
),*/

/*TestGroupConfig(
inputNodeCreator: AstCreator.createCompilationUnit,
inputMiddle: 'class C{void m(){}/*Comment\nA*/}class D{}',
name: 'Block comment after class declaration',
astVisitors: <TestVisitor<AstNode>>[
TestVisitor<ClassDeclaration>(0, 'class C{}'),
TestVisitor<MethodDeclaration>(8, 'void m(){}'),
TestVisitor<ClassDeclaration>(32, 'class D{}')
],
testConfigs: <TestConfig>[
TestConfig.none(),
TestConfig('class C{}/*Comment\nA*/\n')
]
inputNodeCreator: AstCreator.createCompilationUnit,
inputMiddle: 'class C{void m(){}/*Comment\nA*/}class D{}',
name: 'Block comment after class declaration',
astVisitors: <TestVisitor<AstNode>>[
TestVisitor<ClassDeclaration>(0, 'class C{}'),
TestVisitor<MethodDeclaration>(8, 'void m(){}'),
TestVisitor<ClassDeclaration>(32, 'class D{}')
],
testConfigs: <TestConfig>[
TestConfig.none(),
TestConfig('class C{}/*Comment\nA*/\n')
]
),*/

TestGroupConfig(
Expand All @@ -87,9 +87,9 @@ void main()
]
),
/*TestGroupConfig(
inputNodeCreator: AstCreator.createCompilationUnit,
inputMiddle: '/**DocComment\n A*/',
name: 'Lonely doc block comment / formatted'
inputNodeCreator: AstCreator.createCompilationUnit,
inputMiddle: '/**DocComment\n A*/',
name: 'Lonely doc block comment / formatted'
),*/

TestGroupConfig(
Expand All @@ -104,10 +104,10 @@ void main()
]
),
/*TestGroupConfig(
inputNodeCreator: AstCreator.createCompilationUnit,
inputMiddle: '/**DocComment\n A*/class C{}',
name: 'Doc block comment before class declaration / formatted'',
astVisitors: <TestVisitor<AstNode>>[TestVisitor<ClassDeclaration>(18, 'class C{}')]
inputNodeCreator: AstCreator.createCompilationUnit,
inputMiddle: '/**DocComment\n A*/class C{}',
name: 'Doc block comment before class declaration / formatted'',
astVisitors: <TestVisitor<AstNode>>[TestVisitor<ClassDeclaration>(18, 'class C{}')]
),*/

TestGroupConfig(
Expand All @@ -121,10 +121,10 @@ void main()
]
),
/*TestGroupConfig(
inputNodeCreator: AstCreator.createCompilationUnit,
inputMiddle: 'class C{}/**DocComment\n A*/',
name: 'Doc block comment after class declaration / formatted',
astVisitors: <TestVisitor<AstNode>>[TestVisitor<ClassDeclaration>(0, 'class C{}')]
inputNodeCreator: AstCreator.createCompilationUnit,
inputMiddle: 'class C{}/**DocComment\n A*/',
name: 'Doc block comment after class declaration / formatted',
astVisitors: <TestVisitor<AstNode>>[TestVisitor<ClassDeclaration>(0, 'class C{}')]
),*/

TestGroupConfig(
Expand Down
14 changes: 7 additions & 7 deletions test/TestTools/TestParameters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ class TestParameters

static final Set<String> comments = <String>
{
'/**/',
'/**//**/',
'/*Comment*/',
'/*Comment1*//*Comment2*/',
'//\n',
'//Comment\n',
'//Comment1\n//Comment2\n'
'/**/',
'/**//**/',
'/*Comment*/',
'/*Comment1*//*Comment2*/',
'//\n',
'//Comment\n',
'//Comment1\n//Comment2\n'
};
}
2 changes: 1 addition & 1 deletion test/TestTools/Visitors/MetaAstVisitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class MetaAstVisitor<T extends AstNode> extends SimpleVisitor
final AstVisitor<void> visitor = astVisitors![_currentVisitorIndex];
/*
if (visitor is! T)
throw Exception('Visitor #${_currentVisitorIndex + 1} expected to be of type $T expected but is ${visitor.runtimeType}.');
throw Exception('Visitor #${_currentVisitorIndex + 1} expected to be of type $T expected but is ${visitor.runtimeType}.');
*/

logInfo('# ${visitor.runtimeType}(${StringTools.toDisplayString(node, Constants.MAX_DEBUG_LENGTH)})');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,32 @@ void main()

group('RemoveLeadingWhitespace (Block Comments)', ()
{
const String DUMMY_SPACE = ' ';
const String SPACE4 = ' ';
final String dummySpace = ' ' * 10;
final String space4 = ' ' * 4;

test('Exact whitespace as needed, with empty line', ()
{
final String inputText =
'$dummySpace/*START\n'
'\n'
'${space4}END*/$dummySpace';
final String expectedText =
'$dummySpace/*START\n'
'\n'
'END*/$dummySpace';

final String actualText = StringTools.removeLeadingWhitespace(inputText);

TestTools.expect(actualText, equals(expectedText));
}
);

test('Exact whitespace as needed, content more indented than end', ()
{
const String inputText = '$DUMMY_SPACE/*\n${SPACE4}a\n*/$DUMMY_SPACE';
final String inputText =
'$dummySpace/*\n'
'${space4}a\n'
'*/$dummySpace';

final String actualText = StringTools.removeLeadingWhitespace(inputText);

Expand All @@ -24,7 +44,10 @@ void main()

test('Exact whitespace as needed, content less indented than end', ()
{
const String inputText = '$DUMMY_SPACE/*\na\n${SPACE4}*/$DUMMY_SPACE';
final String inputText =
'$dummySpace/*\n'
'a\n'
'${space4}*/$dummySpace';

final String actualText = StringTools.removeLeadingWhitespace(inputText);

Expand All @@ -34,8 +57,14 @@ void main()

test('More whitespace than needed, content more indented than end', ()
{
const String inputText = '$DUMMY_SPACE/*\n${SPACE4}${SPACE4}a\n${SPACE4}*/$DUMMY_SPACE';
const String expectedText = '$DUMMY_SPACE/*\n${SPACE4}a\n*/$DUMMY_SPACE';
final String inputText =
'$dummySpace/*\n'
'${space4}${space4}a\n'
'${space4}*/$dummySpace';
final String expectedText =
'$dummySpace/*\n'
'${space4}a\n'
'*/$dummySpace';

final String actualText = StringTools.removeLeadingWhitespace(inputText);

Expand All @@ -45,8 +74,14 @@ void main()

test('More whitespace than needed, content less indented than end', ()
{
const String inputText = '$DUMMY_SPACE/*\n${SPACE4}a\n${SPACE4}${SPACE4}*/$DUMMY_SPACE';
const String expectedText = '$DUMMY_SPACE/*\na\n${SPACE4}*/$DUMMY_SPACE';
final String inputText =
'$dummySpace/*\n'
'${space4}a\n'
'${space4}${space4}*/$dummySpace';
final String expectedText =
'$dummySpace/*\n'
'a\n'
'${space4}*/$dummySpace';

final String actualText = StringTools.removeLeadingWhitespace(inputText);

Expand Down

0 comments on commit ee9e580

Please sign in to comment.