diff --git a/lib/src/Handlers/WebServiceHandler.dart b/lib/src/Handlers/WebServiceHandler.dart index 411ac3f..24b0f0a 100644 --- a/lib/src/Handlers/WebServiceHandler.dart +++ b/lib/src/Handlers/WebServiceHandler.dart @@ -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( @@ -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 { @@ -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 { diff --git a/lib/src/Tools/StringTools.dart b/lib/src/Tools/StringTools.dart index 417cc81..795294c 100644 --- a/lib/src/Tools/StringTools.dart +++ b/lib/src/Tools/StringTools.dart @@ -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++) { @@ -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)}'); } } diff --git a/test/FormatVisitor/Comments/Class_test.dart b/test/FormatVisitor/Comments/Class_test.dart index 3fcad2b..1682310 100644 --- a/test/FormatVisitor/Comments/Class_test.dart +++ b/test/FormatVisitor/Comments/Class_test.dart @@ -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', () @@ -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)); });*/ } ); diff --git a/test/FormatVisitor/Indentation/TODO_test.dart b/test/FormatVisitor/Indentation/TODO_test.dart index ff8c550..3406562 100644 --- a/test/FormatVisitor/Indentation/TODO_test.dart +++ b/test/FormatVisitor/Indentation/TODO_test.dart @@ -16,7 +16,7 @@ void main() testConfigs: [ 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') ] ); diff --git a/test/Formatters/CompilationUnitFormatter_test.dart b/test/Formatters/CompilationUnitFormatter_test.dart index 04c0779..c7f48ad 100644 --- a/test/Formatters/CompilationUnitFormatter_test.dart +++ b/test/Formatters/CompilationUnitFormatter_test.dart @@ -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( @@ -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(14, 'class C{}')] + inputNodeCreator: AstCreator.createCompilationUnit, + inputMiddle: '/*Comment\n A*/class C{}', + name: 'Block comment before class declaration / formatted', + astVisitors: >[TestVisitor(14, 'class C{}')] ),*/ TestGroupConfig( @@ -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(0, 'class C{}')] + inputNodeCreator: AstCreator.createCompilationUnit, + inputMiddle: 'class C{}/*Comment\n A*/', + name: 'Block comment after class declaration / formatted', + astVisitors: >[TestVisitor(0, 'class C{}')] ),*/ /*TestGroupConfig( - inputNodeCreator: AstCreator.createCompilationUnit, - inputMiddle: 'class C{void m(){}/*Comment\nA*/}class D{}', - name: 'Block comment after class declaration', - astVisitors: >[ - TestVisitor(0, 'class C{}'), - TestVisitor(8, 'void m(){}'), - TestVisitor(32, 'class D{}') - ], - testConfigs: [ - 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(0, 'class C{}'), + TestVisitor(8, 'void m(){}'), + TestVisitor(32, 'class D{}') + ], + testConfigs: [ + TestConfig.none(), + TestConfig('class C{}/*Comment\nA*/\n') + ] ),*/ TestGroupConfig( @@ -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( @@ -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(18, 'class C{}')] + inputNodeCreator: AstCreator.createCompilationUnit, + inputMiddle: '/**DocComment\n A*/class C{}', + name: 'Doc block comment before class declaration / formatted'', + astVisitors: >[TestVisitor(18, 'class C{}')] ),*/ TestGroupConfig( @@ -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(0, 'class C{}')] + inputNodeCreator: AstCreator.createCompilationUnit, + inputMiddle: 'class C{}/**DocComment\n A*/', + name: 'Doc block comment after class declaration / formatted', + astVisitors: >[TestVisitor(0, 'class C{}')] ),*/ TestGroupConfig( diff --git a/test/TestTools/TestParameters.dart b/test/TestTools/TestParameters.dart index 4dabbca..0401e1b 100644 --- a/test/TestTools/TestParameters.dart +++ b/test/TestTools/TestParameters.dart @@ -4,12 +4,12 @@ class TestParameters static final Set comments = { - '/**/', - '/**//**/', - '/*Comment*/', - '/*Comment1*//*Comment2*/', - '//\n', - '//Comment\n', - '//Comment1\n//Comment2\n' + '/**/', + '/**//**/', + '/*Comment*/', + '/*Comment1*//*Comment2*/', + '//\n', + '//Comment\n', + '//Comment1\n//Comment2\n' }; } diff --git a/test/TestTools/Visitors/MetaAstVisitor.dart b/test/TestTools/Visitors/MetaAstVisitor.dart index b27b44f..1e839e6 100644 --- a/test/TestTools/Visitors/MetaAstVisitor.dart +++ b/test/TestTools/Visitors/MetaAstVisitor.dart @@ -32,7 +32,7 @@ class MetaAstVisitor extends SimpleVisitor final AstVisitor 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)})'); diff --git a/test/Tools/StringTools/RemoveLeadingWhitespace/RemoveLeadingWhitespace_BlockComments_test.dart b/test/Tools/StringTools/RemoveLeadingWhitespace/RemoveLeadingWhitespace_BlockComments_test.dart index 67fafd1..7a26201 100644 --- a/test/Tools/StringTools/RemoveLeadingWhitespace/RemoveLeadingWhitespace_BlockComments_test.dart +++ b/test/Tools/StringTools/RemoveLeadingWhitespace/RemoveLeadingWhitespace_BlockComments_test.dart @@ -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); @@ -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); @@ -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); @@ -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);