您可以自定义代码折叠吗? [英] Can you customize code folding?

查看:30
本文介绍了您可以自定义代码折叠吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 Visual Studio Code 中自定义代码折叠的工作方式?

Is it possible to customize the way code folding works in Visual Studio Code?

我使用一种通用模式来定义各种不同文档类型的代码区域.

I use a common pattern of defining regions of code across a variety of different document types.

  • 所以,对于 XML,我用 <包裹文本部分/code>

  • So, for XML I wrap sections of text with <!-- #region --> and <!-- #endregion -->

对于c#,我使用#region#endregion

For c#, I use #region to #endregion,

对于 TypeScript/Javascript,我使用 /* #region *//* #endregion */.

For TypeScript/Javascript, I use /* #region */ and /* #endregion */.

在完整的 Visual Studio(不是 VS Code)中,我有一个自定义扩展,它可以跨文档类型窥探模式,并基于此创建折叠,允许我创建整洁的自定义文档轮廓.我想在 Visual Studio Code 中使用相同的模式.是否可以创建一个自定义的 VS Code 扩展来检测这些注释模式,并根据这些模式以某种方式标记折叠?

In full Visual Studio (not VS Code), I have a custom extension which snoops for the pattern across document types, and creates folds based on that, allowing me to create neat, custom document outlines. I'd like to use the same pattern in Visual Studio Code. Is it possible to create a custom VS Code extension which detects these comment patterns, and somehow tags folds based on the patterns?

推荐答案

<如果您希望在扩展中贡献自定义折叠逻辑,可以使用 code>FoldingRangeProvider.

请务必将 package.json 中的 engines 中的 VS Code 版本设置为 1.23,即引入此功能的版本.

Be sure to set your VS Code version in engines in package.json to 1.23, the version that introduced this.

这是您使用的方法.

export function activate(context: ExtensionContext) {
    languages.registerFoldingRangeProvider({ scheme: 'file', language: 'markdown' }, new MyFoldingRangeProvider());
}

class MyFoldingRangeProvider implements FoldingRangeProvider {
    provideFoldingRanges(document: TextDocument, context: FoldingContext, token: CancellationToken): FoldingRange[] {
        return detectRanges().map(({ lineStart, lineEnd }) => new FoldingRange(lineStart, lineEnd));
    }
}

这篇关于您可以自定义代码折叠吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆