Skip to content

Commit

Permalink
fixing another edge case related to comments + directives
Browse files Browse the repository at this point in the history
refactor docprinter a little
ignore some properties in syntaxNodeComparer so that it gives you a better indication of the code that is failing the validation
  • Loading branch information
belav committed May 27, 2021
1 parent 6a76b3b commit af8cf63
Show file tree
Hide file tree
Showing 6 changed files with 2,452 additions and 15,430 deletions.
7 changes: 7 additions & 0 deletions Src/CSharpier.Tests/TestFiles/Directives/Directives.cst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ public class ClassName
private int x; // trailing comment here shouldn't give extra indent to the endregion after it
#endregion

public bool SomeProperty =>
#if !DEBUG
someValue
&& // trailing comment with endif should work properly
#endif
someOtherValue;

void MethodWithOnlyDisabled()
{
#if DEBUG
Expand Down
20 changes: 13 additions & 7 deletions Src/CSharpier/DocPrinter/DocPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,9 @@ void Push(Doc doc, PrintMode printMode, Indent indent)
break;
}

// I don't understand exactly why, but this ensures we don't print extra spaces after a trailing comment
// this ensures we don't print extra spaces after a trailing comment
// newLineNextStringValue & skipNextNewLine are set to true when we print a trailing comment
// when they are set we new line the next string we find. If we new line and then print a " " we end up with an extra space
if (newLineNextStringValue && skipNextNewLine && stringDoc.Value == " ")
{
break;
Expand Down Expand Up @@ -296,7 +298,7 @@ void Push(Doc doc, PrintMode printMode, Indent indent)
}

// This line was forced into the output even if we were in flattened mode, so we need to tell the next
// group that no matter what, it needs to remeasure because the previous measurement didn't accurately
// group that no matter what, it needs to remeasure because the previous measurement didn't accurately
// capture the entire expression (this is necessary for nested groups)
shouldRemeasure = true;
goto case PrintMode.MODE_BREAK;
Expand All @@ -319,10 +321,8 @@ void Push(Doc doc, PrintMode printMode, Indent indent)
}
else
{
if (
(!newLineNextStringValue || !skipNextNewLine)
&& (!printerOptions.TrimInitialLines || output.Length > 0)
) {
if (!skipNextNewLine || !newLineNextStringValue)
{
TrimOutput(output);
output.Append(endOfLine).Append(command.Indent.Value);
currentWidth = command.Indent.Length;
Expand Down Expand Up @@ -370,7 +370,13 @@ void Push(Doc doc, PrintMode printMode, Indent indent)
output.Append(endOfLine);
}

return string.Join(string.Empty, output);
var result = output.ToString();
if (printerOptions.TrimInitialLines)
{
result = result.TrimStart('\n', '\r');
}

return result;
}

// TODO 1 in prettier this deals with unicode characters that are double width
Expand Down
6 changes: 5 additions & 1 deletion Src/CSharpier/DocSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ string PrintIndentedDocTree(Doc doc)
case NullDoc:
return indent + "Doc.Null";
case StringDoc stringDoc:
return indent + "\"" + stringDoc.Value?.Replace("\"", "\\\"") + "\"";
if (stringDoc.IsDirective)
{
return $"{indent}Doc.Directive({stringDoc.Value.Replace("\"", "\\\"")})";
}
return indent + "\"" + stringDoc.Value.Replace("\"", "\\\"") + "\"";
case HardLine hardLine:
return indent
+ "Doc.HardLine"
Expand Down
Loading

0 comments on commit af8cf63

Please sign in to comment.