How to comment out code in VS Code without indent? - visual-studio-code

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.

Related

configure prettier to push curly braces on new lines & not clear empty lines

I am using prettier with VSCode, How can I configure it to format my code like this :
function test()
{
if()
{
MYCODE GOES HERE;
}
}
I want the { and } on new lines, and an empty line after { and before }.
Currently, it moves the curly brackets to same lines if condition or function name, and also remove the empty lines after/before { and }.
Prettier is considered an " opinionated " formatter, which means it doesn't let you choose things like that. If you want more control over the formatting, you can use a different formatter.
The built-in VS code formatter allows you to do what you're looking for, just search the settings for " function new line " and similar options.
There are of course many other formatting extensions available in the VS code marketplace as well. Whichever you choose, you will have to select it has your default formatter in your VS code settings.
As mentioned in this answer, VS Code's formatter itself works quite well, but if you want this to be part of workflow, then using ESLint might make things easier. There's a rule called brace-style.
You can then run eslint ./path/to/your/file --fix to format your code, or eslint . --fix to format code in the entire project directory.
Disclaimer: I use ESLint for code formatting most of the time and it works for me. I actually use it to find & fix problems too so it's like killing two birds with one stone, but note that ESLint is more about finding problems in the code and fixing them, so using ESLint just for code formatting might not be the best idea.

Visual Code, Fold Comments

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

How to mark code in GitHub bug report?

When creating a bug report on GitHub, is there any way one can easily mark a block of code as a code? I.e. it would indent the marked block by four spaces. Ideally something similar to {} icon on StackExchange. Is there something like that? Indenting every single row can of copy-pasted code can be pretty annoying.
Three back ticks ``` on the line above and below the code create a "fenced code block" in GitHub.
You can also do syntax highlighting, ex ```ruby
Documentation here:
https://help.github.com/articles/github-flavored-markdown#fenced-code-blocks

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.

Eclipse Plugin or else for coding footnote

I love the Manning In Action Series. One of appealing area is that the code listing inside the book always has a footnote where there is a detailed explanation by a reference number. See below the example:
So I'm wondering if there is an Eclipse plugin or online blogging service that allows footnote taking in the source code?
I think the closest you'll get is the the eclipse "NotePad" plugin. It doesn't create footnotes directly, but rather just takes plaintext notes and puts them in the metadata. You'll find it at: http://eclipsenotepad.sourceforge.net/
Better, though, is to use eclipse's built-in indexing, and leave yourself
/* #TODO 1) take note of this notey note */
comments near important lines.
"But sleepynate", you may say, "that's madness! I don't have anything TODO there!"
While this is true, eclipse automatically indexes certain comment tags, like #TODO and #FIXME, and puts them in your "Tasks" window. (Window > Show View > Tasks if yours is currently hidden. You can then double-click any one of them to jump to that line. You can also arbitrarily add tasks from the Edit menu with whatever label you want, in any resource and location within your project.