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 ee9e580 commit ba4ad5a
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 46 deletions.
113 changes: 90 additions & 23 deletions lib/src/Tools/StringTools.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// ignore_for_file: always_put_control_body_on_new_line

import '../../dart_format.dart';
import '../Constants/Constants.dart';
import '../Data/IntTuple.dart';
import 'LogTools.dart';
Expand Down Expand Up @@ -35,15 +36,22 @@ class StringTools
return IntTuple(indexInput, indexResult);
}

static String _removeLeadingWhitespaceNonComment(String s)
static StringBuffer _removeLeadingWhitespaceInCode(String s)
{
if (Constants.DEBUG_STRING_TOOLS) logInternal('_removeLeadingWhitespaceNonComment: ${StringTools.toDisplayString(s)}');

final StringBuffer sb = StringBuffer();
if (Constants.DEBUG_STRING_TOOLS)
{
logInternal(' _removeLeadingWhitespaceInCode: ${StringTools.toDisplayString(s)}');
logInternal(' IN: ${StringTools.toDisplayString(s)}');
}

final List<String> lines = s.split('\n');
if (lines.length < 2)
return s;
{
if (Constants.DEBUG_STRING_TOOLS) logInternal(' OUT: ${StringTools.toDisplayString(s)}');
return StringBuffer(s);
}

final StringBuffer sb = StringBuffer();

for (int i = 0; i < lines.length; i++)
{
Expand All @@ -61,18 +69,26 @@ class StringTools
}
}

return sb.toString();
if (Constants.DEBUG_STRING_TOOLS) logInternal(' OUT: ${StringTools.toDisplayString(sb.toString())}');
return sb;
}

static String _removeLeadingWhitespaceComment(String s)
static StringBuffer _removeLeadingWhitespaceInBlockComment(String s)
{
if (Constants.DEBUG_STRING_TOOLS) logInternal('_removeLeadingWhitespaceComment: ${StringTools.toDisplayString(s)}');

final StringBuffer sb = StringBuffer();
if (Constants.DEBUG_STRING_TOOLS)
{
logInternal(' _removeLeadingWhitespaceInBlockComment: ${StringTools.toDisplayString(s)}');
logInternal(' IN: ${StringTools.toDisplayString(s)}');
}

final List<String> lines = s.split('\n');
if (lines.length < 2)
return s;
{
if (Constants.DEBUG_STRING_TOOLS) logInternal(' OUT: ${StringTools.toDisplayString(s)}');
return StringBuffer(s);
}

final StringBuffer sb = StringBuffer();

int minIndentation = -1;
// Start at index 1 to skip the first line.
Expand All @@ -88,32 +104,32 @@ class StringTools
}

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

if (Constants.DEBUG_STRING_TOOLS) logInternal('minIndentation: $minIndentation');
//if (Constants.DEBUG_STRING_TOOLS) logInternal('minIndentation: $minIndentation');

for (int i = 0; i < lines.length; i++)
{
final String line = lines[i];
if (Constants.DEBUG_STRING_TOOLS) logInternal('> #$i: ${StringTools.toDisplayString(line)}');
//if (Constants.DEBUG_STRING_TOOLS) logInternal('> #$i: ${StringTools.toDisplayString(line)}');

if (i == 0)
{
sb.write(line);
if (Constants.DEBUG_STRING_TOOLS) logInternal('< #$i: ${StringTools.toDisplayString(line)}');
//if (Constants.DEBUG_STRING_TOOLS) logInternal('< #$i: ${StringTools.toDisplayString(line)}');
}
else
{
sb.write('\n');
if (Constants.DEBUG_STRING_TOOLS) logInternal('< #$i: \\n');
//if (Constants.DEBUG_STRING_TOOLS) logInternal('< #$i: \\n');
final String shortenedLine = line.length <= minIndentation ? '' : line.substring(minIndentation);
sb.write(shortenedLine);
if (Constants.DEBUG_STRING_TOOLS) logInternal('< #$i: ${StringTools.toDisplayString(shortenedLine)}');
//if (Constants.DEBUG_STRING_TOOLS) logInternal('< #$i: ${StringTools.toDisplayString(shortenedLine)}');
}
}

return sb.toString();
if (Constants.DEBUG_STRING_TOOLS) logInternal(' OUT: ${StringTools.toDisplayString(sb.toString())}');
return sb;
}

static String removeLeadingWhitespace(String s, [String source = 'TEST'])
Expand All @@ -124,10 +140,9 @@ class StringTools
logInternal('IN: ${StringTools.toDisplayString(s)}');
}

final String fixedS = s.splitMapJoin(
RegExp(r'/\*.*?\*/', dotAll: true),
onMatch: (Match m) => _removeLeadingWhitespaceComment(m.group(0)!),
onNonMatch: _removeLeadingWhitespaceNonComment
final String fixedS = _splitBlockCommentsAndJoin(s,
onMatch: _removeLeadingWhitespaceInBlockComment,
onNonMatch: _removeLeadingWhitespaceInCode
);

if (Constants.DEBUG_STRING_TOOLS) logInternal('OUT: ${StringTools.toDisplayString(fixedS)}');
Expand Down Expand Up @@ -181,4 +196,56 @@ class StringTools

return r;
}

static String _splitBlockCommentsAndJoin(String s, {
required StringBuffer Function(String s) onMatch,
required StringBuffer Function(String s) onNonMatch})
{
if (s.isEmpty)
return s;

final StringBuffer sb = StringBuffer();

final StringBuffer currentBlock = StringBuffer();
int commentDepth = 0;
for (int i = 0; i< s.length; i++)
{
if (s[i] == '/' && i + 1 < s.length && s[i + 1] == '*')
{
if (commentDepth == 0 && currentBlock.isNotEmpty)
{
sb.write(onNonMatch(currentBlock.toString()));
currentBlock.clear();
}

commentDepth++;
currentBlock.write(s[i]);
}
else if (s[i] == '*' && i + 1 < s.length && s[i + 1] == '/')
{
if (commentDepth <= 0)
throw DartFormatException.error('Unbalanced block comments: commentDepth <= 0: $commentDepth');

commentDepth--;
if (commentDepth == 0 && currentBlock.isNotEmpty)
{
sb.write(onMatch('$currentBlock*/'));
currentBlock.clear();
i++;
}
else
currentBlock.write(s[i]);
}
else
currentBlock.write(s[i]);
}

if (commentDepth > 0)
throw DartFormatException.error('Unbalanced block comments: commentDepth > 0: $commentDepth');

if (currentBlock.isNotEmpty)
sb.write(onNonMatch(currentBlock.toString()));

return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,38 @@ void main()
final String dummySpace = ' ' * 10;
final String space4 = ' ' * 4;

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

final String actualText = StringTools.removeLeadingWhitespace(inputText);

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

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

final String actualText = StringTools.removeLeadingWhitespace(inputText);

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

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

final String actualText = StringTools.removeLeadingWhitespace(inputText);

Expand All @@ -45,26 +54,43 @@ void main()
test('Exact whitespace as needed, content less indented than end', ()
{
final String inputText =
'$dummySpace/*\n'
'a\n'
'${space4}*/$dummySpace';
'$dummySpace/*START\n'
'TEXT\n'
'${space4}END*/$dummySpace';

final String actualText = StringTools.removeLeadingWhitespace(inputText);

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

test('More whitespace than 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('More whitespace than needed, content more indented than end', ()
{
final String inputText =
'$dummySpace/*\n'
'${space4}${space4}a\n'
'${space4}*/$dummySpace';
'$dummySpace/*START\n'
'${space4}${space4}TEXT\n'
'${space4}END*/$dummySpace';
final String expectedText =
'$dummySpace/*\n'
'${space4}a\n'
'*/$dummySpace';
'$dummySpace/*START\n'
'${space4}TEXT\n'
'END*/$dummySpace';

final String actualText = StringTools.removeLeadingWhitespace(inputText);

Expand All @@ -75,13 +101,13 @@ void main()
test('More whitespace than needed, content less indented than end', ()
{
final String inputText =
'$dummySpace/*\n'
'${space4}a\n'
'${space4}${space4}*/$dummySpace';
'$dummySpace/*START\n'
'${space4}TEXT\n'
'${space4}${space4}END*/$dummySpace';
final String expectedText =
'$dummySpace/*\n'
'a\n'
'${space4}*/$dummySpace';
'$dummySpace/*START\n'
'TEXT\n'
'${space4}END*/$dummySpace';

final String actualText = StringTools.removeLeadingWhitespace(inputText);

Expand Down

0 comments on commit ba4ad5a

Please sign in to comment.