-
Notifications
You must be signed in to change notification settings - Fork 150
Add protobuf code comments as Elixir module documentation #352
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a1423ba
work
btkostner 262ab56
more work
btkostner f1011bf
more work
btkostner 78cc4ae
remove debug docs
btkostner 25fa6fc
Merge remote-tracking branch 'origin/main' into documentation
v0idpwn 69da2cf
Remove grpc dep
v0idpwn 9c4b483
Refactor test to avoid dependency on grpc library
v0idpwn 0c135ba
Merge branch 'main' into documentation
v0idpwn 4288108
Ensure moduledoc is set even if modules don't have comments
v0idpwn a78f56a
Fix warning during code generation with multi-line comments
v0idpwn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
defmodule Protobuf.Protoc.Generator.Comment do | ||
@moduledoc false | ||
|
||
alias Protobuf.Protoc.Context | ||
|
||
@doc """ | ||
Parses comment information from `Google.Protobuf.FileDescriptorProto` | ||
into a map with path keys. | ||
""" | ||
@spec parse(Google.Protobuf.FileDescriptorProto.t()) :: %{optional(String.t()) => String.t()} | ||
def parse(file_descriptor_proto) do | ||
file_descriptor_proto | ||
|> get_locations() | ||
|> Enum.reject(&empty_comment?/1) | ||
|> Map.new(fn location -> | ||
{Enum.join(location.path, "."), format_comment(location)} | ||
end) | ||
end | ||
|
||
defp get_locations(%{source_code_info: %{location: value}}) when is_list(value), | ||
do: value | ||
|
||
defp get_locations(_value), do: [] | ||
|
||
defp empty_comment?(%{leading_comments: value}) when not is_nil(value) and value != "", | ||
do: false | ||
|
||
defp empty_comment?(%{trailing_comments: value}) when not is_nil(value) and value != "", | ||
do: false | ||
|
||
defp empty_comment?(%{leading_detached_comments: value}), do: Enum.empty?(value) | ||
|
||
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") | ||
|> String.trim() | ||
end | ||
|
||
@doc """ | ||
Finds a comment via the context. Returns an empty string if the | ||
comment is not found or if `include_docs?` is set to false. | ||
""" | ||
@spec get(Context.t()) :: String.t() | ||
def get(%{include_docs?: false}), do: "" | ||
|
||
def get(%{comments: comments, current_comment_path: path}), | ||
do: get(comments, path) | ||
|
||
@doc """ | ||
Finds a comment via a map of comments and a path. Returns an | ||
empty string if the comment is not found | ||
""" | ||
@spec get(%{optional(String.t()) => String.t()}, String.t()) :: String.t() | ||
def get(comments, path), do: Map.get(comments, path, "") | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.