-
Notifications
You must be signed in to change notification settings - Fork 152
Preserve original indentation in generated documentation #414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Preserve original indentation in generated documentation #414
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The change looks great, but can we add a test for it?
|> Enum.reject(&String.match?(&1, ~r/^\s*$/)) | ||
|> Enum.map(fn line -> | ||
Regex.run(~r/^\s*/, line, capture: :first) | ||
|> List.first() | ||
|> String.length() | ||
end) | ||
|> Enum.min(fn -> 0 end) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To do a single pass on the list, use Enum.min_by/3
instead of these three passes on the list.
|> Enum.reject(&String.match?(&1, ~r/^\s*$/)) | |
|> Enum.map(fn line -> | |
Regex.run(~r/^\s*/, line, capture: :first) | |
|> List.first() | |
|> String.length() | |
end) | |
|> Enum.min(fn -> 0 end) | |
Enum.min_by(split_comments, fn line -> | |
if line =~ ~r/^\s*$/ do | |
0 | |
else | |
~r/^\s*/ | |
|> Regex.run(line, capture: :first) | |
|> List.first() | |
|> String.length() | |
end | |
end) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to work on this a bit more. 647c726 replaces this phrase with an Enum.reduce
because we need the following properties:
- Empty (or whitespace-only) lines should not affect the minimum
- We want to end up with a number, rather than the original line.
The reduce leans on the global sort order (:unset
is greater than any integer) which may not be the most clear. Another option would be to use a tagged accumulator. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried turning this into a single regex expression:
case Regex.run(~r/^(\s+)(?=\S)/, line, capture: :first) do
[first] -> String.length(first)
_ -> 0
end
Should be equivalent and a bit faster
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aj-foster the comment from @v0idpwn still applies, you can use a single regex run instead of the if
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made this change in 14bdbed. 👍🏼
Thanks @whatyouhide. I adapted an existing docs-related test in order to avoid messing with another message in the test proto. Resolved the feedback items, but did have to go a different route for one of them (commented above). |
Hello 👋🏼
This PR modifies the processing of comments to preserve the original indentation. Here's the methodology:
Most comment sets have a single space at the beginning of each line, ex.
// Test
. However, there are some situations where all lines may have multiple spaces or no spaces at the beginning. To handle this without disrupting further indents, we look at the minimum number of spaces used for any line that has non-whitespace characters.Once we trim the minimum number of spaces determined above, all other indentation is preserved. It is no longer necessary to trim while padding the final comments.
You can see the results of these changes in #415.
Would love your feedback ❤️