Visual Code, Fold Comments - visual-studio-code

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?

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

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

Related

How could I close VsCode's Collapsible Block Comments"

When I open a new file, VsCode generally expands everything by default. When there is a large amount of code in a single file, I usually use ctrl+k, ctrl+ number to quickly collapse the specified level.
I love this feature, but it always collapses jsDoc comments, aka block comments or multiline comments.This makes the code unreadable, since a big part of the comment is for me to read.
For example:
VsCode's existing folding effects:
When I fold the code with ctrl+k, ctrl+ number, the jsDoc comment is folded like this.
The effect I want:
I want the comment code never to be folded when I fold the code.
All the articles on Google talk about folding comments, but I don't want to fold comments.
How can VsCode be set up so that it does not collapse multiple lines of comment code? Or it can be solved by a plug-in.
I did a lot of searches on Google and they all talked about how to fold comments, using //region to create a custom fold area, different from my question.
In VsCode, I found the "Collapse all block comments" shortcut, but not the "expand all block comments" shortcut

VS Code multi-line select past ends of lines

I've been transitioning to using VS Code for my python projects after previously working with the full Visual Studio, and some of the key bindings/features I'm used to in Visual Studio I can't find the equivalent for in VS Code. I'm not sure if the bindings are different or if the feature doesn't exist.
One feature in particular that I can't figure out is if it is possible to do multi-line select past line endings. In Visual Studio, using Alt + Click + Drag, lets you create a multi-line cursor or box selection, that can extend past line ends, implicitly adding spaces as needed so that the right-most side of the box stays uniform. In VS Code, if you drag the selection past the end of a line, the selection box won't go past the end of the line, even if other lines in the selection extend past it. In Visual Studio, this feature even goes further, as you could Alt + Click + Drag even in areas with no characters as all, creating a multiline cursor or box selection to the right of all existing line ends.
This isn't an essential feature, but it's very handy in making code easy to read. For example, when assigning several dictionary entries all at once, with varying key lengths. Is it possible to do something like this in VS Code?
Edit: The feature I'm looking for is virtual spaces (thank you Mark for providing the feature name), which seems to be an outstanding feature request in VS Code.
The short answer is no, vscode does not have a box select, past line ends, built-in. VS Code does not have the concept of virtual spaces which would be necessary to make this work yet.
Below is the issue, lots of comments, not that many upvotes. Upvote it.
https://github.com/microsoft/vscode/issues/5402

How to comment out code in VS Code without indent?

Currently, after commenting out the code (e.g. in BASH script) in VS Code I get:
function foo {
#function bar {
# echo 'Le Bar.';
#}
}
but I'd like to have this:
function foo {
# function bar {
# echo 'Le Bar.';
# }
}
I can't find proper settings for disabling comment indentation, what am I missing?
Edit: I created Feature Request for it, please upvote, if you're also interested in having it here: https://github.com/microsoft/vscode/issues/147168 — just 20 upvotes will move the request to backlog. :-)
Edit: "Just" 15 votes more and we gonna have it! (Well, almost) :-) Please be so kind to upvote! :-)
Edit: Just 2 votes more! :-) Speak up! Don't be shy! :-)
There are no features built into Visual Studio Code for not indenting comments.
However, if you really want to have them, you could follow these two links to other Stack Overflow answers (but it might just be overkill):
VSCode Preserve Indentation when Commenting out Lines
Make Comments of VSCode Start at Column Position 0
You can also make a GitHub Issue on Microsoft's official Visual Studio Code repository with the tag feature-request.
In summary, there isn't a feature like that in Visual Studio Code. However, there are a few solutions, but they just might be overkill for such a simple task.

how to freely format comments in cc-mode

I'm quite new to cc-mode and I'd like to configure it to allow me to freely format and use tabs in multiline comments. This is important to me because I want to use cog.py in my source file and need to be able to format the python source in the comment correctly. I'd be ok with comments not beeing autoindented at all, however I'd like to keep auto indenting the rest of the source code.
Example:
...
/*
[[[cog
import cog
for x in ['a','b','c']:
>cog.outl(x)
]]]
*/
...
In the line marked with > I'd like to press TAB to indent the line. cc-mode simply does nothing at all if i do so. I could use spaces there (which is inconvenient) but every (semi-)automatic re-indentation of this block would cause the spaces to vanish and therefore the python code to be incorrectly indented (which is what happens if i happen to press tab somewhere on this line after indenting it with spaces).
I tried to start emacs without my .init to be sure this is default behavior and not modified by my configuration so far. I've done google searches and read the documentation of the cc-mode variables / functions I stumbled upon (cc-mode online docs) while searching for a solution (i.e. c-indent-comments-syntactically-p, c-indent-command, c-tab-always-indent,...) but none of these seemed to solve my question.
EDIT1:
Thanks to abo-abo's idea of a "multi-major-mode" setup i've stumbled upon mmm-mode and have set up automatic switching to python mode for a cog section, which fixes most of my problems.
The only remaining problem is reindenting the whole file or a region containing a cog section. Can I somehow tell cc-mode to not change anything in comments while reindenting the file? mmm-mode + that would be a perfect solution for me.
You can use M-i to force a tab indent on the lines that you want, so you can use it to indent your comments.
You can also change your comments to use // instead. Just select your python code snippet, and do M-x comment-region:
// def foo(x):
// print 'hi'
Then the autoindent won't mess up your indentation.

Rebind keys in Emacs to change commenting style in source code

I am trying to document certain sections of my code using Doxygen. I want to use the javadoc
style of comments viz
/**
* My Documentation goes here
*/
Now in Emacs I can comment out a certain block of text by selecting it and by pressing Meta+;. This comments out the lines by placing a // in front of all the lines selected.
Since most of the time I will be writing comments in my code which will be documented using Doxygen, I want to rebind the Meta+; to give me a javadoc style of comments.
How do I do that?
Use doxymacs. You'll have to rebind keys to your taste, but the bindings it comes with are pretty good.
Code is not documentation. Wouldn't you want to use javadoc style comments only for documentation?
It sounds like you actually want a snippet/skeleton system to insert a java-doc template that you can flesh out. If so I recommend yasnippet.