-
Notifications
You must be signed in to change notification settings - Fork 11
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
Add --no-code-fmt option #98
Conversation
This temporarily pins to my forks of mdox and markdownfmt to include the following two changes: - bwplotka/mdox#98 - bwplotka/mdox/pull/98 These two combined add the ability for us to opt out of mdox reformatting all Go code blocks inside Markdown files. The .gitignore change is added because this version of mdox uses a local sqlite cache for speed.
This temporarily pins to my forks of mdox and markdownfmt to include the following two changes: - bwplotka/mdox#98 - Kunde21/markdownfmt#38 These two combined add the ability for us to opt out of mdox reformatting all Go code blocks inside Markdown files. The .gitignore change is added because this version of mdox uses a local sqlite cache for speed.
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.
Not sure I understand the motivations behind this change.
Why not reformat Go code with gofmt? It's enabled by default on every IDE and it is by design opinionated and non-configurable.
Based on PRs you linked, uber-go/fx#961, you mention that the gofmt formatting (via mdox) is inconsistent. Maybe addressing that issue, instead of disabling it altogether would be a better approach here? Wdyt? 🙂
@saswatamcode our primary reasons for wanting this are:
In either case, it makes sense to have an escape hatch. |
Rebased on top of #96 and dropped the temporary pin-to-fork. |
Just to elaborate on #98 (comment) with some example scenarios:
(FWIW, all our code samples are coming from external files that are properly gofmt-ed.) |
This temporarily pins to my fork of mdox to include bwplotka/mdox#98. Separately, this upgrades markdownfmt to pick up Kunde21/markdownfmt#38. These two combined add the ability for us to opt out of mdox reformatting all Go code blocks inside Markdown files. The .gitignore change is added because this version of mdox uses a local sqlite cache for speed.
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.
In a style-guide context, you want to demonstrate good vs bad examples
where having no reformatting is desirable.
This use case makes sense to some extent, but would a style guide redefine what is already covered by gofmt?
Anyways, it can still be a useful debugging option, so I do not want to block it! Let's wait for @bwplotka's opinions too. 🙂
Also some small suggestions!
@@ -1,10 +1,9 @@ | |||
module github.com/bwplotka/mdox | |||
|
|||
go 1.15 | |||
go 1.19 |
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 think you raised PR for this separately #96?
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.
Yeah, this PR is on top of that one. If #96 gets merged, this won't show those commits.
(Without stacking them, there are conflicts because of the dependency updates.)
pkg/mdformatter/mdformatter_test.go
Outdated
@@ -147,3 +147,18 @@ func TestCheck_SoftWraps(t *testing.T) { | |||
testutil.Ok(t, err) | |||
testutil.Equals(t, string(exp), diff.String()) | |||
} | |||
|
|||
func TestFormat_NoGofmt(t *testing.T) { |
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.
Thanks for the separate test case. I would also check what happens to this go code sample when --no-gofmt is disabled, just to ensure we aren't changing default behavior
👍🏽 I think it definitely makes sense to be able to disable gofmt - solid use cases. Perhaps doing this with extra directive on code snippet would do? Essentially opt-out for formatting per snippet? |
Thanks @bwplotka, I can add that to this PR too. |
I can call this |
I think flag + directive makes sense here! The |
On second thought, per-snippet opt-out is possible, For now, I've updated this PR with additional tests as suggested, I can explore per-snippet opt-out separately from the global opt-out added by this flag. |
@bwplotka @saswatamcode |
Ack! WIll try to TAL this week, and see what would be nice to implement here! Thanks for the awesome work! 🙂 |
Update to latest markdownfmt master to pull in Kunde21/markdownfmt#38.
Kunde21/markdownfmt attempts to reformat all snippets using the "go" language type. So it will rewrite anything like the following: ```go package main func foo() { // intentionally using two spaces } ``` This change adds a --no-gofmt command line option that opts out of this behavior in markdownfmt by replacing the list of code formatters. This change requires Kunde21/markdownfmt#38 to build.
The scope of this flag is expanding to all code samples. If mdox supports more code reformatting in the future, this flag opts out of all of them.
Adds a test to verify the behavior of mdox with and without poorly formatted Go code. By default, it'll reformat. With the "--no-code-fmt" flag, it won't.
Thank you! I've rebased the PR now that #96 is in. |
Superseded by #99 |
Kunde21/markdownfmt attempts to reformat all snippets using the "go"
language type.
So it will rewrite anything like the following:
This change adds a --no-code-fmt command line option that opts out of this
behavior in markdownfmt by replacing the list of code formatters.
The option is named no-code-fmt instead of no-gofmt to account
for other code formatters that mdox may introduce in the future.
This change requires Kunde21/markdownfmt#38.
This change also depends on #96.