Skip to content

Commit

Permalink
✂ Don't chop non-whitespace when trimming text
Browse files Browse the repository at this point in the history
Fixes #33
  • Loading branch information
peteroupc authored and kzu committed Jan 28, 2021
1 parent ad4e732 commit b29aa5f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
19 changes: 19 additions & 0 deletions src/NuDoq.Tests/ReaderFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,19 @@ public void when_reading_indexer_property_then_succeeds()
Assert.Equal("indexed", method.ToText());
}

[Fact]
public void when_reading_weird_indenting_then_preserves_text()
{
var member = DocReader.Read(Assembly.GetExecutingAssembly(), new ReaderOptions { KeepNewLinesInText = true });
var method = member.Elements.OfType<Method>()
.FirstOrDefault(m => m.Info?.DeclaringType == typeof(CustomXml) && m.Info?.Name == nameof(CustomXml.WeirdIndenting));

Assert.NotNull(method);
Assert.Equal(@"Begin
End", method.ToText());
}


class CountingVisitor : Visitor
{
readonly string platform;
Expand Down Expand Up @@ -724,6 +737,12 @@ protected override void VisitElement(Element element)
/// </remarks>
public class CustomXml
{
/// <summary>
/// Begin
/// End
/// </summary>
public void WeirdIndenting() { }

/// <preliminary />
public void Preliminary() { }

Expand Down
22 changes: 19 additions & 3 deletions src/NuDoq/DocReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,23 @@ string TrimText(string content)
/// <summary>
/// Trims the code by removing extra indent.
/// </summary>
string TrimCode(string content) => TrimLines(content, StringSplitOptions.None, Environment.NewLine);
string TrimCode(string content)
=> TrimLines(content, StringSplitOptions.None, Environment.NewLine);

string TrimWhitespaceTo(string content, int index)
{
if (string.IsNullOrEmpty(content))
return content;

for (var i = 0; i < index; i++)
{
// Check for end of whitespace
if (!char.IsWhiteSpace(content[i]))
return content.Substring(i);
}

return content.Substring(index);
}

string TrimLines(string content, StringSplitOptions splitOptions, string joinWith)
{
Expand Down Expand Up @@ -330,9 +346,9 @@ string TrimLines(string content, StringSplitOptions splitOptions, string joinWit
else if (line.Length < indent)
return string.Empty;
else
return line.Substring(indent);
return TrimWhitespaceTo(line, indent);
})
.ToArray());
}
}
}
}

0 comments on commit b29aa5f

Please sign in to comment.