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

Table of Contents formatter can conflict with Prettier #1040

Closed
dylankerr-bis opened this issue Nov 10, 2021 · 3 comments
Closed

Table of Contents formatter can conflict with Prettier #1040

dylankerr-bis opened this issue Nov 10, 2021 · 3 comments
Labels
Area: Table of contents Pertaining to table of contents (TOC generation and detection, related heading operations). Needs Discussion We haven't decided what to do. Upstream Pertaining to an upstream component, or blocked by an upstream issue.

Comments

@dylankerr-bis
Copy link

This is somewhere between a bug and a feature request - I'll explain how to reproduce the issue before talking about possible solutions.

Problem

With certain settings (specifically, Prettier set to use 4-space indentation and toc.updateOnSave enabled), the Table of Contents formatter/updater and Prettier conflict and attempt to format the list in different ways.

How to reproduce

If I create the Markdown file below:

# Heading

- [Heading](#heading)
    - [Subheading](#subheading)

## Subheading

Content.

... and then save the file, Prettier will re-format the file as follows (which is my desired format):

# Heading

-   [Heading](#heading)
    -   [Subheading](#subheading)

## Subheading

Content.

... and the Table of Contents formatter will then immediately set it back to the original state.

Configuration

settings.json:

{
    "editor.formatOnSave": true,
    "markdown.extension.italic.indicator": "_",
    "markdown.extension.list.indentationSize": "inherit",
    "markdown.extension.orderedList.marker": "one",
    "[markdown]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    }
}

.prettierrc:

{
    "singleQuote": true,
    "trailingComma": "es5",
    "printWidth": 100,
    "tabWidth": 4,
    "endOfLine": "auto",
    "proseWrap": "always"
}

Proposal

I think in general, the Table of Contents updater is more concerned with the content (making the ToC correct) than the formatting, so it might make sense to move the update process before VSCode's formatOnSave runs, thereby allowing the user (through their choice of formatter) to control the format.

If this is not possible or practical, more configuration in the ToC formatting would allow me to generate a ToC that Prettier does not need to modify.

References

@Lemmingh Lemmingh added Needs Discussion We haven't decided what to do. Upstream Pertaining to an upstream component, or blocked by an upstream issue. labels Nov 11, 2021
@yzhang-gh
Copy link
Owner

Have you tried to change markdown.extension.toc.updateOnSave?

@yzhang-gh yzhang-gh removed the Upstream Pertaining to an upstream component, or blocked by an upstream issue. label Nov 11, 2021
@Lemmingh Lemmingh added Area: Table of contents Pertaining to table of contents (TOC generation and detection, related heading operations). Upstream Pertaining to an upstream component, or blocked by an upstream issue. labels Nov 11, 2021
@Lemmingh
Copy link
Collaborator

This is a quite complex topic.

Our TOC template is hard-coded

the Table of Contents formatter/updater and Prettier conflict and attempt to format the list in different ways.

allow me to generate a ToC that Prettier does not need to modify.

We code the template mainly for accuracy and reliability.

async function detectTocRanges(doc: TextDocument): Promise<[Array<Range>, string]> {

vscode-markdown/src/toc.ts

Lines 279 to 283 in cea94a7

const row = [
docConfig.tab.repeat(relativeLevel) + indentationFix,
(tocConfig.orderedList ? (orderedListMarkerIsOne ? '1' : currHeadingOrder) + '.' : tocConfig.listMarker) + ' ',
tocConfig.plaintext ? entry.visibleText : `[${entry.visibleText}](#${entry.slug})`
];

As for changing it, @yzhang-gh is hesitant, and I'm not motivated.

The internal of the onWillSaveTextDocument event is out of our control

move the update process before VSCode's formatOnSave runs

The toc.updateOnSave feature is just a listener on the event.

workspace.onWillSaveTextDocument(onWillSave),

I remember a maintainer of esbenp.prettier-vscode opened an issue at VS Code to discuss the on-save behavior long time ago, but cannot find it now.

@dylankerr-bis
Copy link
Author

Ok, thanks for the detail.

For now I will work around this by changing my indent width in Markdown files to 2 spaces, as that makes the ToC generator and Prettier agree.

I agree that too much flexibility in the ToC format would be impractical. If I really want to get 4-space indentation working with this I'll start a discussion on the VSCode side about formatOnSave vs. onWillSaveTextDocument ordering (there might be other use cases for configuring the order of those events).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Table of contents Pertaining to table of contents (TOC generation and detection, related heading operations). Needs Discussion We haven't decided what to do. Upstream Pertaining to an upstream component, or blocked by an upstream issue.
Projects
None yet
Development

No branches or pull requests

3 participants