如何在VS Code中启用ASP.NET Core Razor语法的代码折叠?

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

How to enable code folding in ASP.NET Core Razor syntax in VS Code?

问题

我正在尝试在VS Code中编辑.NET Core项目的Razor页面(.cshtml)。当我打开文件时,它选择了ASP.NET Razor(aspnetcorerazor)语言模式。它可以完美地进行语法高亮,但有一个重要的功能缺失。

当我在页面编号附近悬停时,我看不到代码折叠的折叠控件。因此,无法折叠代码。

当我将语言模式切换回HTML时,可以恢复代码折叠,但我将失去Razor语法的所有语法高亮。

是否有人遇到相同的问题?目前,作为一种临时解决方法,我不得不在HTML和ASP.NET Core Razor语言模式之间来回切换以进行代码折叠。

英文:

I am trying to edit Razor pages (.cshtml) in VS Code for a .NET Core project. When I open the file it chooses the ASP.NET Razor (aspnetcorerazor) language mode. It does syntax highlighting perfectly, but there's one important feature missing.

When I hover near the page number I cannot see the folding controls for code folding. So, there is no way to fold the code.

When I change my language mode back to HTML, I get the code folding back, but then I lose all the syntax highlighting of Razor syntax.

Has anybody experienced the same issue? Currently, as a workaround I have to switch back and forth between the HTML and ASP.NET Core Razor language modes for code folding.

答案1

得分: 1

你可以使用 VSCode 扩展程序 Better Folding 来实现这个功能。

要仅配置为适用于 cshtml/razor 文件,请将以下内容添加到你的 .vscode/settings.json 文件中:

"[aspnetcorerazor]": {
	"explicitFolding.rules": [
		{
			"begin": "/*",
			"end": "*/"
		},
		{
			"indentation": true,
			"offSide": true
		}
	]
}

这将允许根据缩进和花括号折叠代码。

offSide(默认值:false)决定空行是否属于上一个块还是下一个块。由默认缩进提供程序使用,并由语言的配置定义(不可由扩展访问)。

当然,你也可以为整个项目文件夹配置,只需删除语言选择器:

"explicitFolding.rules": [
	{
		"begin": "/*",
		"end": "*/"
	},
	{
		"indentation": true,
		"offSide": true
	}
]
英文:

You may use the VSCode Extension Better Folding for that.

To configure for cshtm/razor files only, just put these lines to your .vscode/settings.json:

"[aspnetcorerazor]": {
		"explicitFolding.rules": [
			{
				"begin": "{/*",
				"end": "*/}"
			},
			{
				"indentation": true,
				"offSide": true
			}
		]
	}

That will add the abillity to fold the code by indention and by curly brackets.

> offSide (default: false) decide whether empty lines belong to the previous or the next block. Used by the default indentation provider and defined by language’s configuration (not accessible by an extension).

Of course you can config that for the whole project folder by removing the language selector:

"explicitFolding.rules": [
	{
		"begin": "{/*",
		"end": "*/}"
	},
	{
		"indentation": true,
		"offSide": true
	}
]

huangapple
  • 本文由 发表于 2023年2月19日 15:31:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498617.html
匿名

发表评论

匿名网友

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

确定