21

Visual Studio Code 1.24.1

While I was working on something today. It prompted me to do an update which I did (Update was to 1.24.1). I'm not sure if I hit a shortcut accidentally at about this same time or if this was caused by the update.

But I seem to no longer be able to use comments as a fold point.

However again, I'm not sure if I hit a shortcut of some sort, or if this was caused by the patch.

and my googlefu did not help me find an answer for visual studio code. Found many old topics about visual studio (prof not code) and for other editors. but couldn't find a topic specific to VSC.

I liked to use comments as fold points \ section headers.

Example of comments I used to use as fold points

Is this a bug in VSC 1.24.1 or did I hit a shortcut I'm unaware of?

James
  • 211
  • 1
  • 2
  • 4
  • 2
    Forgot to say I'm working with javascript – James Jul 02 '18 at 15:57
  • If Javascript is key here, why don't you tag "javascript" – lurker Jul 02 '18 at 16:20
  • 8
    From what I've read, in javascript you should delimit fold sections with `//#region REGION NAME` and `//#endregion`. – lurker Jul 02 '18 at 16:23
  • I just forgot to tag it as I was juggling three things at once and I couldn't find an edit button to go edit the original question. So commented and had to move to the next task unfortunately so couldn't be as through. and yeah that is what I might do, but when I found that Visual Studio Code was letting me fold on comments I liked how formatted, because I could close all and use the comments like an index. When all closed made it very easy to find what I was looking for without using search. will probably just move to {//------------- Example } so that it folds on the { – James Jul 02 '18 at 19:29
  • But from what someone told me on a slack. it seems that the folding on comment was a bug. so something i liked it just gone. So question answered, it was a bug that was "fixed" by the patch. – James Jul 02 '18 at 19:31
  • 1
    @lurker why didn't you make it as answer. It's the best solution and needs to be marked as answer. – Vijay Kumar Kanta May 31 '19 at 10:35
  • The region-markers are differently for each language. See https://code.visualstudio.com/docs/editor/codebasics#_folding – Dominik Jul 06 '23 at 20:38

2 Answers2

39

VS Code has a method of marking the start and end of a region that can be folded and expanded easily. And also provide it a name/short description that'll always be visible.

I've tried this on Javascript code and I believe it'll work on any language in VS Code. And you can have anything in between the start and end of region - comments, few lines or code, entire functions.

In the absence of proper code folding, this is a pretty good work around that'll work for all languages on VS Code.

//#region <REGION NAME>

      < You can put any type of code in here - some lines of code, functions or single or multi-line comments.
//#endregion

For python, simply omit the // in the demarcation lines, since # is a valid comment indicator:

#region <REGION NAME>
...
# any code goes here, including multiple comment lines
...
#endregion

I don't code in PHP but was able to fold the code like this. You can try other variations for other languages. For vs code the following keywords matter "#region " and "#endregion". Figure out a way to put these keywords as comments in your code and it will enable folding, what comes between these keywords does not matter. so feel free to play around:

<?php
#region <Any name that helps you>

ECHO "Hello!<br>";
EcHo "Hope this helps you.<br>";

#endregion
?>
Curious101
  • 1,538
  • 2
  • 18
  • 36
2

A kind of hack I found for react is using empty tags for example

<>

{/*

Your commented code

*/}

</>

This allows you to fold the commented code between the empty tags. You can go a step further and add regions as mentioned in the other answer to add some kind of description to it

Vikranth
  • 1,538
  • 1
  • 14
  • 23