Markdown代码围栏在VS Code(或其他嵌入式语言代码)中的格式化内容

huangapple go评论73阅读模式
英文:

Formatting contents of a Markdown code fences in VS Code (or other embedded language code)

问题

在 VS Code 中,我有一个代码框,此处为 javascript 标记的代码框。我想要能够选择代码框的内容并进行格式化,让我的默认格式化程序处理它,但它一直尝试使用我拥有的 Markdown 格式化程序。理想情况下,VS Code 会知道这里有一个代码框,并根据语言标识符对每个代码框的内容应用正确的格式化程序,但这可能要求太多。

我想要的事情可能有可能吗?我尝试选择文本并进行“格式化所选内容...”,但那没有任何作用。我能想到的唯一办法是打开一个新文件,将其标记为 JavaScript,粘贴代码框中的代码,进行格式化,然后将其放回 Markdown 代码框中。肯定有更简单的方法。

更新: 从评论中看来,我想要的可能相当复杂。所以,我只想有一个“使用...格式化所选内容”选项。虽然我看到当我在 TypeScript 文件中工作时,但当我在 Markdown 文件中时,在上下文菜单中找不到它。即使我按下 Ctrl+Shift+P 并查找“格式化所选内容”,我也找不到它。我该如何获得这个选项?

英文:

In VS Code, I have a code fence, in this case it is a javascript-marked fence. I'd like to be able to select the contents of the fence and format it and have my default formatter do its thing, but it keeps trying to use the Markdown formatter that I have. Ideally, VS Code would know that there is a fence there and apply the correct formatter to the contents of each fence based on the language identifier, but that might be asking too much.

Is what I'm after even possible? I've tried selecting the text and doing "Format Selection...", but that doesn't do anything. The only thing I can think of is to open a new file, mark it as JavaScript, paste in the code from the fence, format it, and then put it back in the markdown fence. There has to be an easier way.

Update: It seems from the comments that what I'm after could be quite complex. So, I'd just like to have a "Format Selection With..." option. While I see that when I'm working in TypeScript files, I don't see it in my context menu when I'm in a Markdown file. Even when I do <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+P and look for "Format Selection With", I don't find it. How do I get that?

答案1

得分: 1

I've heard other people use workarounds for things like this to temporarily change the language mode (you can click the language mode indicator at the bottom right, or use the Change Language Mode command in the command palette) to the language you want to format for, and then select the text you want to format, and then use the Format Selection command (or associated keyboard shortcut - whatever it is on your machine - maybe ctrl/cmd+k, ctrl/cmd+f), and then switch the language mode back. It's unwieldy, but it might be the best there is now.

See also this feature-request issue ticket: Is there a "Format Selection As ..." command? #131814. You can give it a thumbs up to show support, and subscribe to it to get notified of discussion and progress. But please avoid noisy comments like "+1" or "bump".

I'm pretty sure the language-tagged code fences in VS Code's built-in Markdown language support use a VS Code feature called "embedded languages". I initially thought that VS Code would be able to provide this feature to select formatters for embedded code in different languages, but apparently it's more complicated than I thought, and there are situations where if VS Code made that kind of assumption, it could lead to bad formatting. I googled "github vscode issues "format" embedded language -highlight" and found the issue ticket Code formatting for multiple languages in one file
#14447
, where the maintainers state:

The current story is that for a PHP file, the PHP formatter has to deal with the html content and format it. Generic solutions are difficult and will result in bugs due to lack of understanding on how the two languages play together. Can the embedded content be indented? Can new lines be added? Most of the time only the main language has an understanding of that.

So if you miss HTML formatting in PHP, file an issue against the provider of the PHP formatter.

The right next step if you want something like this might be to raise a feature-request issue ticket to the extension maintainers of the markdown extension you're using that provides markdown formatting, asking for them for whatever behavior you're looking for. If you do, please comment here with a link to your issue ticket for posterity.

英文:

I've heard other people use workarounds for things like this to temporarly change the language mode (you can click the language mode indicator at the bottom right, or use the Change Language Mode command in the command palette) to the language you want to format for, and then select the text you want to format, and then use the Format Selection command (or associated keyboard shortcut- whatever it is on your machine- maybe <kbd>ctrl/cmd</kbd>+<kbd>k</kbd>, <kbd>ctrl/cmd</kbd>+<kbd>f</kbd>), and then switch the language mode back. It's unwieldy, but it might be the best there is now.

See also this feature-request issue ticket: Is there a "Format Selection As ..." command? #131814. You can give it a thumbs up to show support, and subscribe to it to get notified of discussion and progress. But please avoid noisy comments like "+1" or "bump".

I'm pretty sure the language-tagged code fences in VS Code's builtin Markdown language support use a VS Code feature called "embedded languages". I initially thought that VS Code would be able to provide this feature to select formatters for embedded code in different languages, but apparently it's more complicated than I thought, and there are situations where if VS Code made that kind of assumption, it could lead to bad formatting. I googled "github vscode issues &quot;format&quot; embedded language -highlight" and found the issue ticket Code formatting for multiple languages in one file
#14447
, where the maintainers state:

> The current story is that for a PHP file, the PHP formatter has to deal with the html content and format it.
Generic solutions are difficult and will result in bugs due to lack of understanding on how the two languages play together. Can the embedded content be indented? Can new lines be added? Most of the time only the main language has an understanding of that.
>
> So if you miss html formatting in PHP, file an issue against the provider of the PHP formatter.

The right next step if you want something like this might be to raise a feature-request issue ticket to the extension maintainers of the markdown extension you're using that provides markdown formatting asking for them for whatever behaviour you're looking for. If you do, please comment here with a link to your issue ticket for posterity.

huangapple
  • 本文由 发表于 2023年5月25日 23:36:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76334033.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定