-
Notifications
You must be signed in to change notification settings - Fork 12
feat(gnodoc): generate docs from contract codebase #1160
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
Open
notJoon
wants to merge
26
commits into
main
Choose a base branch
from
doc-renderer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
b60f431
initial commit
notJoon c812c83
feat(gnodoc): defining doc model types
notJoon 0a74ecf
feat(render): markdown renderer poc
notJoon 60f40b5
fix(renderer): align symbols
notJoon ceec0a7
feat(cmd): implement basic CLI
notJoon 2660f98
feat(parser): CLI command parser
notJoon 99d384c
main
notJoon 01fcdd5
fix
notJoon dac3fa0
test(parser): update parser test
notJoon b49299f
feat(parser): auto detect `gnomod.toml`
notJoon cf8447c
fix(gnodoc): Implement Examples/Deprecated handling and preserve
notJoon 08a99ff
fix
notJoon 0424891
fix(parser): retrieve module path from gnomod toml file
notJoon 21fbc61
feat(gnodoc): tracing returning variable's name
notJoon f01ec81
fix(render): rendering error value once
notJoon 4a70fdb
fmt
notJoon 72c4248
update docs
notJoon 4f3bd34
fix(parser): handle address type
notJoon c295cc3
fix: modernize codebase
notJoon 9bf4029
fix: priority return infos in the comments
notJoon 4f7b889
fix: render params and returns value as a table
notJoon 6bd4a4d
Merge branch 'main' into doc-renderer
notJoon 37b4225
fix: update template to follow prev doc conventions
notJoon bf5627a
fix: formatting function signature
notJoon 71556fb
fix: showing actual callback type signature when render the docs
notJoon 0f2efc3
Merge branch 'main' into doc-renderer
notJoon 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| gnodoc |
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,114 @@ | ||
| # GnoDoc | ||
|
|
||
| GnoDoc is a local documentation generator for Go and Gno packages that reads source code directly and emits Markdown. Unlike `gno doc`, it does not require deployment on-chain, so it can document packages before release. The goal is to provide pkg.go.dev-like structure and readability while staying lightweight and deterministic. | ||
|
|
||
| When you run GnoDoc, it resolves the input package path in a predictable order. A directory path is used as-is, a module-relative path is resolved against a module root, and a Go import path is resolved via `go list` when needed. The module root is detected by walking upward from the package directory until a `gnomod.toml` is found; if `--module-root` is provided, that directory is used instead. The `module` field in `gnomod.toml` is used to build the final import path shown in the rendered Markdown. | ||
|
|
||
| File discovery is intentionally simple. GnoDoc looks for `.go` and `.gno` files and optionally includes test files when `--include-tests` is set. File exclusion uses filename glob patterns, and parsing continues in best-effort mode when `--ignore-parse-errors` is enabled. The AST is built with comment parsing enabled and normalized into a DocPackage model that preserves symbol relationships (types, methods, functions, constants, variables), source locations, and structured tags such as Examples and Deprecated blocks. | ||
|
|
||
| Markdown rendering follows a consistent section order: Overview, Index, Constants, Variables, Functions, Types, Examples, and Notes. The Index contains exported symbols and stable anchors, while constants and variables are grouped using their original declaration style. Function sections show signatures, description text, and a Returns block that summarizes named return values and concrete return expressions extracted from the function body. Types include fields, methods, and constructors, and examples are emitted as fenced code blocks with output where available. Source links are optionally rendered from file and line information using `--source-link-base`. | ||
|
|
||
| ## CLI Flags | ||
|
|
||
| ### Output | ||
| | Flag | Default | Description | | ||
| | --- | --- | --- | | ||
| | `--out=DIR` | current directory | Output directory for generated Markdown. | | ||
| | `--output-file=NAME` | `README.md` | Output filename for the default command. | | ||
| | `--filename=NAME` | `README.md` | Output filename for `export`. | | ||
| | `--format=md` | `md` | Output format for `export` (Markdown only). | | ||
|
|
||
| ### Parsing and Filtering | ||
| | Flag | Default | Description | | ||
| | --- | --- | --- | | ||
| | `--include-tests` | false | Include `_test.go` / `_test.gno` files. | | ||
| | `--ignore-parse-errors` | false | Continue parsing if some files fail. | | ||
| | `--exclude=PATTERN` | (none) | Exclude files by filename glob (comma-separated). | | ||
| | `--exported-only` | true | Only render exported symbols. | | ||
| | `--all` | false | Include unexported symbols. | | ||
|
|
||
| ### Paths and Links | ||
| | Flag | Default | Description | | ||
| | --- | --- | --- | | ||
| | `--module-root=DIR` | (auto) | Override module root detection. | | ||
| | `--source-link-base=URL` | (none) | Base URL for source links. | | ||
|
|
||
| ## Usage Examples | ||
|
|
||
| Generate documentation for a local package directory: | ||
| ```bash | ||
| gnodoc /path/to/package | ||
| ``` | ||
|
|
||
| Generate documentation using module-relative path: | ||
| ```bash | ||
| gnodoc --module-root /path/to/module contract/p/gnoswap/int256 | ||
| ``` | ||
|
|
||
| Export Markdown to a custom file and directory: | ||
| ```bash | ||
| gnodoc export --out docs --filename governance.md contract/r/gnoswap/gov/governance | ||
| ``` | ||
|
|
||
| Include tests and tolerate parse errors: | ||
| ```bash | ||
| gnodoc --include-tests --ignore-parse-errors /path/to/package | ||
| ``` | ||
|
|
||
| List packages under a directory: | ||
| ```bash | ||
| gnodoc list /path/to/module | ||
| ``` | ||
|
|
||
| ## Markdown Output Example (Simplified) | ||
| ~~~plain | ||
| # pkgname | ||
|
|
||
| `import "module/path/pkgname"` | ||
|
|
||
| Package overview text. | ||
|
|
||
| ## Index | ||
| - [TypeA](#typea) | ||
| - [FuncX](#funcx) | ||
|
|
||
| ## Constants | ||
| ```go | ||
| const ( | ||
| MAX = 1 | ||
| ) | ||
| ``` | ||
|
|
||
| ## Functions | ||
| ### FuncX | ||
| ```go | ||
| func FuncX(arg int) (result int, err error) | ||
| ``` | ||
| Description... | ||
|
|
||
| #### Returns | ||
| - named: result, err | ||
| - return: result, nil | ||
|
|
||
| ## Types | ||
| ### TypeA | ||
| ```go | ||
| type TypeA struct | ||
| ``` | ||
| Description... | ||
|
|
||
| #### Methods | ||
| ##### MethodY | ||
| ```go | ||
| func (t *TypeA) MethodY() error | ||
| ``` | ||
|
|
||
| #### Returns | ||
| - return: nil | ||
| ~~~ | ||
|
|
||
| ## Supported Features | ||
| GnoDoc currently supports Go and Gno parsing, module detection via `gnomod.toml`, export filtering, stable anchors, example extraction from test files, source links, and return expression tracking. | ||
|
|
||
| ## Current Limitations | ||
| GnoDoc does not render HTML, does not evaluate build tags or platform-specific files, and only supports filename-based exclude patterns. Return analysis is intentionally shallow and does not perform full control-flow or dataflow analysis. Example code formatting is best-effort and may preserve indentation from the source. |
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,30 @@ | ||
| package cmd | ||
|
|
||
| // ExitCode represents CLI exit codes. | ||
| type ExitCode int | ||
|
|
||
| const ( | ||
| // ExitSuccess indicates successful execution. | ||
| ExitSuccess ExitCode = 0 | ||
|
|
||
| // ExitError indicates a fatal error (input error, module detection failure, render failure). | ||
| ExitError ExitCode = 1 | ||
|
|
||
| // ExitPartialError indicates partial file parsing failure. | ||
| // Used when --ignore-parse-errors is set but some files failed. | ||
| ExitPartialError ExitCode = 2 | ||
| ) | ||
|
|
||
| // String returns the string representation of the exit code. | ||
| func (c ExitCode) String() string { | ||
| switch c { | ||
| case ExitSuccess: | ||
| return "success" | ||
| case ExitError: | ||
| return "error" | ||
| case ExitPartialError: | ||
| return "partial error" | ||
| default: | ||
| return "unknown" | ||
| } | ||
| } |
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,37 @@ | ||
| package cmd | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestExitCode_Values(t *testing.T) { | ||
| // Exit codes as per spec | ||
| if ExitSuccess != 0 { | ||
| t.Errorf("ExitSuccess should be 0, got %d", ExitSuccess) | ||
| } | ||
| if ExitError != 1 { | ||
| t.Errorf("ExitError should be 1, got %d", ExitError) | ||
| } | ||
| if ExitPartialError != 2 { | ||
| t.Errorf("ExitPartialError should be 2, got %d", ExitPartialError) | ||
| } | ||
| } | ||
|
|
||
| func TestExitCode_String(t *testing.T) { | ||
| tests := []struct { | ||
| code ExitCode | ||
| expected string | ||
| }{ | ||
| {ExitSuccess, "success"}, | ||
| {ExitError, "error"}, | ||
| {ExitPartialError, "partial error"}, | ||
| {ExitCode(99), "unknown"}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.expected, func(t *testing.T) { | ||
| got := tt.code.String() | ||
| if got != tt.expected { | ||
| t.Errorf("String() = %q, want %q", got, tt.expected) | ||
| } | ||
| }) | ||
| } | ||
| } |
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,76 @@ | ||
| package cmd | ||
|
|
||
| // GlobalOptions contains common options for all commands. | ||
| type GlobalOptions struct { | ||
| // IncludeTests includes test files (*_test.go, *_test.gno). | ||
| IncludeTests bool | ||
|
|
||
| // IgnoreParseErrors continues parsing even if some files fail. | ||
| IgnoreParseErrors bool | ||
|
|
||
| // ExportedOnly includes only exported symbols. | ||
| ExportedOnly bool | ||
|
|
||
| // SourceLinkBase is the base URL for source links. | ||
| SourceLinkBase string | ||
|
|
||
| // ModuleRoot specifies the module root directory. | ||
| ModuleRoot string | ||
|
|
||
| // Exclude patterns for files/directories. | ||
| Exclude []string | ||
|
|
||
| // OutputFile is the output filename. | ||
| OutputFile string | ||
| } | ||
|
|
||
| // DefaultGlobalOptions returns the default global options. | ||
| func DefaultGlobalOptions() *GlobalOptions { | ||
| return &GlobalOptions{ | ||
| IncludeTests: false, | ||
| IgnoreParseErrors: false, | ||
| ExportedOnly: true, | ||
| OutputFile: "README.md", | ||
| } | ||
| } | ||
|
|
||
| // ExportOptions contains options for the export command. | ||
| type ExportOptions struct { | ||
| GlobalOptions | ||
|
|
||
| // Format is the output format (md). | ||
| Format string | ||
|
|
||
| // OutputDir is the output directory. | ||
| OutputDir string | ||
|
|
||
| // Filename is the output filename. | ||
| Filename string | ||
| } | ||
|
|
||
| // DefaultExportOptions returns the default export options. | ||
| func DefaultExportOptions() *ExportOptions { | ||
| return &ExportOptions{ | ||
| GlobalOptions: *DefaultGlobalOptions(), | ||
| Format: "md", | ||
| OutputDir: ".", | ||
| Filename: "README.md", | ||
| } | ||
| } | ||
|
|
||
| // ListOptions contains options for the list command. | ||
| type ListOptions struct { | ||
| // IncludeTests includes test packages. | ||
| IncludeTests bool | ||
|
|
||
| // Exclude patterns for packages. | ||
| Exclude []string | ||
| } | ||
|
|
||
| // DefaultListOptions returns the default list options. | ||
| func DefaultListOptions() *ListOptions { | ||
| return &ListOptions{ | ||
| IncludeTests: false, | ||
| Exclude: nil, | ||
| } | ||
| } | ||
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,45 @@ | ||
| package cmd | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestGlobalOptions_Default(t *testing.T) { | ||
| opts := DefaultGlobalOptions() | ||
|
|
||
| if opts.IncludeTests { | ||
| t.Error("IncludeTests should be false by default") | ||
| } | ||
| if opts.IgnoreParseErrors { | ||
| t.Error("IgnoreParseErrors should be false by default") | ||
| } | ||
| if !opts.ExportedOnly { | ||
| t.Error("ExportedOnly should be true by default") | ||
| } | ||
| if opts.OutputFile != "README.md" { | ||
| t.Errorf("OutputFile should be 'README.md', got %q", opts.OutputFile) | ||
| } | ||
| } | ||
|
|
||
| func TestExportOptions_Default(t *testing.T) { | ||
| opts := DefaultExportOptions() | ||
|
|
||
| if opts.Format != "md" { | ||
| t.Errorf("Format should be 'md', got %q", opts.Format) | ||
| } | ||
| if opts.OutputDir != "." { | ||
| t.Errorf("OutputDir should be '.', got %q", opts.OutputDir) | ||
| } | ||
| if opts.Filename != "README.md" { | ||
| t.Errorf("Filename should be 'README.md', got %q", opts.Filename) | ||
| } | ||
| } | ||
|
|
||
| func TestListOptions_Default(t *testing.T) { | ||
| opts := DefaultListOptions() | ||
|
|
||
| if opts.IncludeTests { | ||
| t.Error("IncludeTests should be false by default") | ||
| } | ||
| if len(opts.Exclude) != 0 { | ||
| t.Error("Exclude should be empty by default") | ||
| } | ||
| } |
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.
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.
Potential confusion:
ExportOptionshas bothOutputFile(embedded) andFilename.ExportOptionsembedsGlobalOptionswhich hasOutputFile, but also declares its ownFilenamefield. Both are set to"README.md"inDefaultExportOptions(). This creates ambiguity about which field controls the output filename.Looking at
runner.goline 177, there's explicit synchronization:opts.OutputFile = opts.Filename. Consider either removing one field or documenting the relationship clearly.💡 Suggested fix: Remove duplicate field
// ExportOptions contains options for the export command. type ExportOptions struct { GlobalOptions // Format is the output format (md). Format string // OutputDir is the output directory. OutputDir string - - // Filename is the output filename. - Filename string } // DefaultExportOptions returns the default export options. func DefaultExportOptions() *ExportOptions { return &ExportOptions{ GlobalOptions: *DefaultGlobalOptions(), Format: "md", OutputDir: ".", - Filename: "README.md", } }