-
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
Changes from 1 commit
6772f51
647c726
315bff8
14bdbed
5257a22
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -33,12 +33,31 @@ defmodule Protobuf.Protoc.Generator.Comment do | |||||||||||||||||||||||||||||||||||
defp format_comment(location) do | ||||||||||||||||||||||||||||||||||||
[location.leading_comments, location.trailing_comments | location.leading_detached_comments] | ||||||||||||||||||||||||||||||||||||
|> Enum.reject(&is_nil/1) | ||||||||||||||||||||||||||||||||||||
|> Enum.map(&String.replace(&1, ~r/^\s*\*/, "", global: true)) | ||||||||||||||||||||||||||||||||||||
|> Enum.join("\n\n") | ||||||||||||||||||||||||||||||||||||
|> String.replace(~r/\n{3,}/, "\n") | ||||||||||||||||||||||||||||||||||||
|> normalize_indentation() | ||||||||||||||||||||||||||||||||||||
|> String.trim() | ||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
defp normalize_indentation(comments) do | ||||||||||||||||||||||||||||||||||||
indentation = | ||||||||||||||||||||||||||||||||||||
String.split(comments, "\n") | ||||||||||||||||||||||||||||||||||||
|> 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.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. 👍🏼
Uh oh!
There was an error while loading. Please reload this page.