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

autoformat on save #30

Open
alixander opened this issue Dec 8, 2022 · 7 comments · May be fixed by #37
Open

autoformat on save #30

alixander opened this issue Dec 8, 2022 · 7 comments · May be fixed by #37
Labels
enhancement New feature or request
Milestone

Comments

@alixander
Copy link

Latest D2 version exposes a command for autoformatting D2 scripts. Intended to be used like Go's, i.e. format on save

terrastruct/d2#292

@andorsk
Copy link
Owner

andorsk commented Dec 8, 2022

yes! good callout. thanks @alixander

@andorsk andorsk added this to the 0.2 milestone Dec 8, 2022
@andorsk
Copy link
Owner

andorsk commented Dec 8, 2022

@alixander this is actually tricker than it looks.

For .d2 files, it's pretty clear what to do, but there are also d2 regions in emacs mode, which it's less clear. I think we would want to autoformat all the d2 regions as well on save.

I think a few functions are probably required to support this :

  1. format-buffer
  2. format-region
  3. format-file
  4. format-all-d2-regions ;; goes through all d2 codeblocks and formats them.

@andorsk andorsk added the enhancement New feature or request label Dec 13, 2022
@avelino
Copy link

avelino commented Jan 4, 2023

Example:

@andorsk
Copy link
Owner

andorsk commented Jan 4, 2023

@avelino yep. I'm aware of hooks you can configure on save with a hook. I think again, the tricky part here is region support. If that's not important for most people, I'm happy to implement a quick format command on the d2-mode. Personally, I tend to use d2 mostly in regions, which is why I've held back on implementing the simple solution.

Proposal here:

  1. Implement format support with caveat that region's won't be supported for now.
  2. Region formatting will be a roadmap item. I'm guessing it will be must more involved, as we'll have to write something to scan the file and format appropriately. Not sure what the right solution is there.

Are @avelino and @alixander ok with excluding region support?

@alixander
Copy link
Author

Is regions like a markdown block? I do mostly edit just the d2 files standalone.

@andorsk
Copy link
Owner

andorsk commented Jan 5, 2023

@alixander exactly. Regions are blocks.

As an example:

This is a graph

```d2
A --> B
```
Commentary here.

Another graph:

```d2
A --> B
```

Commentary here.

I mostly do graphs in regions, so for me ( personally ) unless there's region support it becomes a lot less useful. I didn't know people use standalone files frequently. Might make sense to support this for standalone files.

I'll see about getting a PR started this weekend.

@andorsk andorsk linked a pull request Jan 7, 2023 that will close this issue
@andorsk
Copy link
Owner

andorsk commented Jan 7, 2023

Started work on a PR here: #37

@andorsk andorsk linked a pull request Jan 7, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants