How to control the comment bars // in VS Code - eclipse

I´m used to Eclipse, where when you press Ctrl+Shift+C it add two bars to the most left side of the file but keep your indentation (I´m using the Eclipse Keymaps extension for using Eclipse shortcuts), for example:
//Comment here
fn main() {
// Comment here
println!("Hello Stackoverflow!");
// Imagine more indentation here
}
On VS Code comments are handled like this:
//Comment here
fn main() {
//Comment here
println!("Hello Stackoverflow!");
//Imagine more indentation here
}
I don´t like it and it´s a deal breaker for me, I love the editor otherwise.
Is there a way to change this?
I tried a couple of extension for customizing comments and playing around with the settings, but nothing worked for me.
Thank you in advance

Related

customizing VSC formatting on Save

I have an existing PHP codebase where methods are formatted:
public function myIssue() {
// some code...
}
VSC changes this on save to:
public function myIssue()
{
// some code...
}
Is there some way I can turn off this specific VSC formatting rule from occurring on save and allow all the other formatting changes VSC suggests to still occur?
Not the answer you are looking for but an explanation from vscode's perspective:
Visual studio code is implementing what is called a PSR coding style and is considered to be the standard for PHP. As you can see over at the documentation at 4.1 (functions), it mentions that:
The opening brace MUST go on its own line, and the closing brace MUST
go on the next line following the body.

Prettier spacing

I'm starting to using VSC and looking for a good formatter for js, jsx, css, html.. etc
Tried several auto-indent extensions for VSC and for now settled with the Prettier, so I have few questions:
How to disable spacing:
on arrow functions to const a=()=>{}; instead const a = () => {};
on } else { to clear one: }else{
on if (), for ()
in function arguments (not important)
...I really hate this useless spacing
plus:
How to refresh file in VSC? (exit without saving + open again - with one button (is it 'revert file', probably..))
How to disable (or setup) amount empty lines between blocks onFormatPrettier (1 by default)
(for devs) Why are there so few settings(for me :) ) in the extension?
Thanks
Modification only
Official answer and "solution"

Can Visual Studio Code be prevented from indenting a level deeper after opening braces?

If I enter "foo ()\n\t{\n"
foo ()
{
<- cursor is now here
I'd like it to be at the same level of indent as the line above (4 spaces in this example), not pushed in a level (8 spaces in this example). So:
foo ()
{
<- cursor here and all will be well in the world
I've tried completely turning off all the indent/tab settings I can find. I'm even prepared to hack around with an extension so a pointer to any in-depth documentation around type/tab commands would also be welcome.
Well, erm, there is an Editor Setting in Preferences, where you can adjust the tabs "editor.tabSize": 4, but I don't there is any level scaling, but I've also discovered a little "hack". If you make double slashes after
{//
and hit return it looks like this:
{//
Hello_world!!
}
Kind of silly, I guess.

How do you hide arbitrary section of code in VS Code?

By "VS Code" I mean the lightweight text editor not the monolithic IDE, unfortunately searching this on google will bring up many pieces of irrelevant information about how to do this in Visual Studio.
To the question itself, Anybody knows how to hide arbitrary selected lines of code in "Visual Studio Code", preferably into a plus sign like collapsing does?
Note: this is different than collapsing nested code which probably could be achieved by Ctrl+K,Ctrl+<num> , what I need here is to hide specific block of code of choice, no matter nested or not.
EDIT: I see there are people who don't understand my requirements.
for example, you may think what I want is this:
before hiding:
for i in j:
for k in i:
for l in k:
somestuff...
after hiding:
[+] for i in j: ...
What I actually want is this:
before hiding:
# doing stuff about a
a = ClassA()
a.bar()
a.i = 2
a.j = 3
a.k = 5
after hiding:
[+] ... ( doing stuff about a )
2017.10.17 EDIT:
turns out VS Code implemented a very similar feature called "Folding Regions" in VS Code 1.17.1 Update. link
You can use the following delimiters for code folding:
C/C++: #pragma region and #pragma endregion
C#: #region and #endregion
CSS: /* #region */ and /* #endregion */
Java: //region and //endregion
JavaScript: //#region and //#endregion and //region and //endregion
PHP: #region and #endregion
Powershell: #region and #endregion
Python: #region and #endregion
VB: #Region and #End Region
See https://github.com/Microsoft/vscode/issues/12146 ([folding] fold regions)
In the Insiders Build v1.70 now is the functionality and command to truly hide arbitrary lines of code. The command is
Create Manual Folding Range from Selection
editor.createFoldingRangeFromSelection
It is bound to Ctrl+K Ctrl+, by default. Select any lines you want to fold.
You can unfold those lines either by clicking the gutter folding controls or this command when the cursor is somewhere on the folded line:
Remove Manual Folding Ranges
editor.removeManualFoldingRanges
The above command is bound to Ctrl+K Ctrl+. by default.
Unfortunately, it doesn't appear that VSCode currently allows you to hide an arbitrary selection of code like Visual Studio does via Ctrl+M,Ctrl+H. For now you could use the code folding feature, which depends on indentation. That is, if you indent the code you want to hide, you could then collapse it via Ctrl+Shift+[, like so:
Obviously, this is kind of an ugly solution to your problem; it requires multiple steps and makes actual changes to the file. Also, it's clearly useless if you're writing in a whitespace-dependent language like Python, but I don't think you are going to find a better solution short of finding an extension (or writing one yourself). It also might be worth posting an issue on the official VSCode GitHub repo if this feature is important to you.
Here is the vs code documentation for folding a selection.
To hide: Highlight the lines you want to fold then press Ctrl+K then Ctrl+,
To unhide: Just click the ">" icon to the left of the row of the folded code or press Ctrl+K then Ctrl+.
Comment with a delimiter with decreased indentation.
Hiding then works as in nested, with the little arrow on the left.
see here
# Below here comes the code to hide.
a = ClassA()
a.bar()
a.i = 2
a.j = 3
a.k = 5

When folding a line in VS Code is it possible to override the indentation and choose which lines are included in that fold?

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?
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));
}
}
On August 6th 2022 the feature "Fold Selection" was added to "V.S. Code" as part of the sem-minor v1.70.0 release. This new feature gives users complete control over line folds, by total I mean, when & where. Fold Selection allows you to fold whatever code you want, wherever you want.
Below is a GIF image that was appended to the official v1.70.0 release notes
I copy & pasted this image because..,
A. The image shows how the new feature works, and...
B. because it shows that the feature works much like line folding does in IDEs — i.e. VS-22, Intelli-J, CLion, etc...
V.S. Code is actually the first editor I ever used, and I stuck with it for the last 5 years, but one thing I noticed on day 1 of test driving V.S. Code was that it did not have this feature.
Using the new Fold Selection Feature
You can use the feature via the quick input, just type "Fold Selection" until the option pops up for you to select, however, I perfer customizing a keybinding for it.
Here is the default configuration for fold selection in the default keyboard shortcuts JSON document:
{
"key": "ctrl+k ctrl+,",
"command": "editor.createFoldingRangeFromSelection",
"when": "editorTextFocus && foldingEnabled"
}
How to configure the above snippet is beyond the scope of this post, but I suggest keeping the when statement as it is configured above (which is the default).
You can use the keybinding shown in the JSON snippet w/o any configuration, which would be:
CTRL + K CTRL+,
...however, vscode has to attach most all commands to some keyboard shortcut. Most people cannot remember all of the commands and shortcuts, so for features you use often, it makes since to attach it to more practicle option, I like to use something like
CTRL + SHIFT + SPACE SPACE
Its almost like quickly pressing space twice.
Anyways, this is a far better option than what was available before, cheers!
CLICK HERE TO READ THE OFFICIAL RELEASE NOTES
There are three ways to achieve customized folding in a VSCode extension.
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"
}
}
}
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.
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.
Unfortunately, not at the moment. There is a an open issue in github for this very topic.