Can you customize code folding?

后端 未结 3 1320
栀梦
栀梦 2021-02-14 03:58

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

3条回答
  •  囚心锁ツ
    2021-02-14 04:19

    FoldingRangeProvider can be used if you are looking to contribute custom folding logic in an extension.

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

    Here's how you'd use one.

    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));
        }
    }
    

提交回复
热议问题