Skip to content
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

Closed
wants to merge 4 commits into from
Closed

Add --no-code-fmt option #98

wants to merge 4 commits into from

Conversation

abhinav
Copy link
Contributor

@abhinav abhinav commented Oct 11, 2022

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-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.

abhinav added a commit to uber-go/fx that referenced this pull request Oct 11, 2022
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.
abhinav added a commit to uber-go/fx that referenced this pull request Oct 11, 2022
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.
@abhinav abhinav changed the title Add --no-gofmt option [don't land yet] Add --no-gofmt option Oct 11, 2022
Copy link
Collaborator

@saswatamcode saswatamcode left a 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? 🙂

@abhinav
Copy link
Contributor Author

abhinav commented Oct 13, 2022

@saswatamcode our primary reasons for wanting this are:

  • Examples aren't always valid Go code.
    gofmt will fail to reformat in these cases,
    so some examples will be fully formatted and some won't.
  • Examples are sometimes intentionally incorrect.
    In this case, reformatting would actually take away the point
    that the example wanted to make.

In either case, it makes sense to have an escape hatch.

@abhinav abhinav changed the title [don't land yet] Add --no-gofmt option Add --no-gofmt option Oct 13, 2022
@abhinav
Copy link
Contributor Author

abhinav commented Oct 13, 2022

Rebased on top of #96 and dropped the temporary pin-to-fork.

@abhinav
Copy link
Contributor Author

abhinav commented Oct 13, 2022

Just to elaborate on #98 (comment) with some example scenarios:

  • Incremental step-by-step instructions don't always have valid code. For example, imagine something like,

    1. Start a new main function.

      func main() {
    2. Initialize a new application.

      func main() {
          app := fx.New(
          )
    3. ...and so on

    In these cases, we won't have valid Go code until we get to the end,
    and gofmt will not reformat anything until that point.

  • In a style-guide context, you want to demonstrate good vs bad examples
    where having no reformatting is desirable.

(FWIW, all our code samples are coming from external files that are properly gofmt-ed.)

abhinav added a commit to uber-go/fx that referenced this pull request Oct 13, 2022
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.
Copy link
Collaborator

@saswatamcode saswatamcode left a 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
Copy link
Collaborator

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?

Copy link
Contributor Author

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.)

@@ -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) {
Copy link
Collaborator

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

@bwplotka
Copy link
Owner

👍🏽 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?

@abhinav
Copy link
Contributor Author

abhinav commented Oct 15, 2022

👍🏽 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.
Are you still okay with the --no-gofmt top-level flag?
For our documentation, we'll otherwise end up adding the per-snippet opt-out on all snippets.

@abhinav
Copy link
Contributor Author

abhinav commented Oct 15, 2022

I can call this --no-fmt and the per-snippet-opt-out mdox-fmt="false".
This way, if mdox adds reformatting for other languages, the same opt-out works.

@saswatamcode
Copy link
Collaborator

I think flag + directive makes sense here! The no-fmt flag would basically switch default to mdox-fmt=true.

@abhinav
Copy link
Contributor Author

abhinav commented Oct 16, 2022

On second thought, per-snippet opt-out is possible,
but it either requires new APIs in markdownfmt to have a hook before it runs a reformat,
or we change it so that mdox always opts out of reformatting from markdownfmt,
and does its own reformatting if necessary.

For now, I've updated this PR with additional tests as suggested,
and renamed the flag to --no-code-fmt to opt out of all code reformatting.
This will account for new code reformatting logic being added in the future.

I can explore per-snippet opt-out separately from the global opt-out added by this flag.

@abhinav abhinav changed the title Add --no-gofmt option Add --no-code-fmt option Oct 17, 2022
@abhinav abhinav mentioned this pull request Oct 25, 2022
@abhinav
Copy link
Contributor Author

abhinav commented Oct 25, 2022

@bwplotka @saswatamcode
Any appetite for file-level opt-out with --no-code-fmt?
I don't know if I have time to implement per-snippet opt-out in markdownfmt at this time.

@saswatamcode
Copy link
Collaborator

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.
@abhinav
Copy link
Contributor Author

abhinav commented Oct 26, 2022

Ack! WIll try to TAL this week, and see what would be nice to implement here! Thanks for the awesome work! 🙂

Thank you! I've rebased the PR now that #96 is in.

@abhinav
Copy link
Contributor Author

abhinav commented Dec 12, 2022

Superseded by #99

@abhinav abhinav closed this Dec 12, 2022
@abhinav abhinav deleted the nogofmt branch December 12, 2022 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants