Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you customize code folding?

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.

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

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

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

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?

like image 998
Stephen Ellis Avatar asked Apr 24 '16 15:04

Stephen Ellis


People also ask

How do I create a fold in Vscode?

Toggle Fold (Ctrl+K Ctrl+L) folds or unfolds the region at the cursor. Fold Recursively (Ctrl+K Ctrl+[) folds the innermost uncollapsed region at the cursor and all regions inside that region. Unfold Recursively (Ctrl+K Ctrl+]) unfolds the region at the cursor and all regions inside that region.

What is folding maximum regions in VS code?

The number of foldable regions is limited to a maximum of 5000.

What is code folding used for?

Code or text folding, or less commonly holophrasting, is a feature of some graphical user interfaces that allows the user to selectively hide ("fold") or display ("unfold") parts of a document. This allows the user to manage large amounts of text while viewing only those subsections that are currently of interest.

How do you fold code in Python?

It maps alt+1 to fold the first python indent level (class definitions and functions), alt+2 to fold the second level (class methods), and alt+0 to unfold everything. It makes sure it only folds one level and doesn't fold any of the nested sub levels. You can still use za to toggle folding for the current block.


2 Answers

There are three ways to achieve customized folding in a VSCode extension.

  1. You can define regex as folding markers in a [language-name].configuration.json file. (However, we don't have much customization with this approach)

{
  "folding": {
    "markers": {
      "start": "starting regex",
      "end": "ending regex"
    }
  }
}
  1. You can define a FoldingRangeProvider from within the extension as described in this answer. FoldingRange in vscode package supports folding customization with startLine, endLine, and foldingKind.

  2. You can use Language Server support with textDocument/foldingRange. FoldingRange in the vscode-languageserver-protocol supports folding customization with startLine, endLine, startCharacter, endCharacter, and foldingKind.

Check this for more details.

like image 127
prabushi samarakoon Avatar answered Sep 28 '22 12:09

prabushi samarakoon


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));
    }
}
like image 41
Tomáš Hübelbauer Avatar answered Sep 28 '22 11:09

Tomáš Hübelbauer