Update examples to better match godoc/pkg.go.dev
The current implementation was stripping out all comments inside the example function, this fixes that behaviour.
It also strips out the leading and trailing braces to better match how godoc and pkg.go.dev render examples (for example https://pkg.go.dev/k8s.io/[email protected]/tools/clientcmd)
This implementation takes inspiration from https://github.com/golang/gddo/blob/20d68f94ee1f7547de2b1c68627253df20c8d45e/doc/code.go#L314-L329
Thanks for the PR @ThatsMrTalbot!
I played around some with your code and wasn't able to find any cases where it's different from the existing behavior. Can you point me to a package where this code generates a different output than the current code for the tool?
I have created a test case that shows what I am seeing:
https://gist.github.com/ThatsMrTalbot/fa0ade54937d1251220ac61c0e631ef1
If you run that in an empty directory it will print out the output from the master branch, and from my branch based off a simple example function.
In the comments I have put the output, as you can see the output is missing comments without the changes, and has additional wrapping braces.
{
fmt.Println(i)
Foo()
}
vs
// Lots
// Of
// Comments
// Here
fmt.Println(i)
// Call Bar
Foo()
Thanks for the detailed repro @ThatsMrTalbot! I've updated your change with a test that demonstrates the difference and fixed a linting error. Things otherwise look good 💯
Codecov Report
Merging #69 (f172b91) into master (88639c6) will decrease coverage by
0.36%. The diff coverage is0.00%.
@@ Coverage Diff @@
## master #69 +/- ##
==========================================
- Coverage 58.62% 58.26% -0.36%
==========================================
Files 20 20
Lines 1815 1826 +11
==========================================
Hits 1064 1064
- Misses 666 677 +11
Partials 85 85
| Impacted Files | Coverage Δ | |
|---|---|---|
| lang/example.go | 8.88% <0.00%> (-2.88%) |
:arrow_down: |