remarkjs / vscode-remark

Lint and format markdown code with remark
https://marketplace.visualstudio.com/items?itemName=unifiedjs.vscode-remark
MIT License
54 stars 11 forks source link

Support formating a section of markdown #132

Open LokiMidgard opened 1 month ago

LokiMidgard commented 1 month ago

Initial checklist

Problem

VSCode has a format section command this is currently not supported by this extension.

If supported this allows

Solution

It is said in the vscode extensions docs that the extension need to respond to the textDocument/rangeFormatting request.

Alternatives

For formatOnSave the mode can be set to file, to allow formatting on save, without the benefit of formating only changed text.

remcohaszing commented 1 month ago

The remark language server can’t support range formatting, because remark doesn’t support formatting text ranges. Personally I’m not very interested in this feature, but I would be willing to implement this if unified and remark support it. I don’t really understand how this should work or if this is even possible with the current architecture.

cc @wooorm @ChristianMurphy

wooorm commented 1 month ago

Can’t work? Footnote definitions, regular definitions, could be anywhere else. What if the current selection looks like it starts with frontmatter, even though it isn’t at the start of the document? What if you select something that looks like “blocks” but is inline, and try to format it? Say, asd |# qwe|. It seems very difficult. I don’t really see the use.

LokiMidgard commented 1 month ago

I'm not 100% sure, but If I understand it correctly, the rangeformating request will provide the whole file and a range that should be formated. So the complete document is available for parsing.

It should just return text edits, for the selected text. So without looking a the current implementation, if there are several edits on the whole file they could be filtered to just return those in the range. Assuming they do not depend o another.

So there should be no problem misinterpreting inline text for blogs or something as frontmatter that isn't.

But it still looks like this is the wrong repo for this issue…

remcohaszing commented 1 month ago

I’m ok with tracking this here. If this feature appears upstream, it eventually needs to end up here.

Currently the whole file is parsed and reformatted when formatting. This is one big edit. It’s possible to calculate text diffs and convert those to formatting ranges. This is done for example in unified-consistency and eslint-plugin-prettier. The problem is that these changes don’t have any context, only plain text diffs. The result can be invalid. I trust a human to apply them, but not an automation.