Prevent VS Code from changing indentation on Enter - visual-studio-code

Let's assume I have a JavaScript file with the following content and the cursor placed at the pipe symbol (|):
class ItemCtrl {
getPropertiesByItemId(id) {
return this.fetchItem(id)
.then(item => {
return this.getPropertiesOfItem(item);
});
}|
}
If I now hit enter, the code changes in the following way:
class ItemCtrl {
getPropertiesByItemId(id) {
return this.fetchItem(id)
.then(item => {
return this.getPropertiesOfItem(item);
});
}
|
}
It wrongly aligns the closing curly brace with the return statement, when it should be aligned with the method definition. I know that the formatting inside the function is not the best but I still would rather disable that feature to prevent weird things like that from happening.
I already set editor.autoIndent to false but it still keeps happening. Is there any other way, how I can turn this feature off entirely? (or make it work in a smarter way)

In VS Code 1.17, this bug caused "editor.autoIndent": false to not work
This should be fixed in VS Code 1.18

Related

VSCode aggressively deletes all code after unconditional return (no-unreachable)

Sometimes I have code like this:
function x() {
func1();
func2();
func3();
}
and when I'm debugging I put a return on the third line if I don't want/care about func2/func3:
function x() {
func1();
return;
func2();
func3();
}
On save, VSCode very rudely deletes func2 and func3!!! Yes I know it is unreachable code, but I am only adding that return there temporarily. A yellow underline would be nice. I can't seem to turn it off. Is there any way to turn it off or am I using it wrong? It seems like a really silly and overzealous feature to me.
Finally fixed the issue.
In settings.json (~/.config/Code/User/settings.json)
I had:
"source.fixAll": true,
changing this to
"source.fixAll.eslint": true,
Stops the editor removing the unreachable code unnecessarily

How do i change the way visual studio code auto corrects curly braces?

I prefer my curly braces looking like this:
function eg()
{
}
But when I try to format my code, vscode auto corrects to this:
function eg() {
}
Is there any plugins that I can download or setting that I can tweak to change this?
Try :
"javascript.format.placeOpenBraceOnNewLineForFunctions": true;
"javascript.format.placeOpenBraceOnNewLineForControlBlocks": true;
"typescript.format.placeOpenBraceOnNewLineForControlBlocks": true;
"typescript.format.placeOpenBraceOnNewLineForFunctions": true;

how to get vs code `ENTER` to respect/cleanup whitespace

BTW, this is not asking how to format code in VS, or the keybinding, so please don't mark it duplicate of this. It is specifically about formatting during typing.
Given the following code:
if(true) {
console.log('hello');
console.log('world');
}
If I go to the end of the 'log hello' line, hit DEL to bring 'log world' to the end of the line I will get (with | representing my cursor):
if(true) {
console.log('hello');| console.log('world');
}
Now, if I hit ENTER, I will get:
if(true) {
console.log('hello');
| console.log('world');
}
In other editors I would expect the white space to be 'eaten up' and the line to be reformatted, looking like this again:
if(true) {
console.log('hello');
|console.log('world');
}
Any thoughts on how to change this behavior?
This would also (hopefully) handle the pasting in of code that was not properly formatted, for instance, if I wanted to paste the following in:
console.log('arrrgg');
console.log('matey');
To the end of the if, I would see:
if(true) {
console.log('hello');
console.log('world');
console.log('arrrgg');
console.log('matey');
}
Instead of:
if(true) {
console.log('hello');
console.log('world');
console.log('arrrgg');
console.log('matey');
}
Certainly, I can hit Shift-Alt-F and reformat, but I don't always want to reformat an entire file....
I think I have been spoiled with VS and WebStorm automatically cleaning up and reformatting code quickly. Not sure if that is a valid
Is this even possible in Code?

How to format a Java file in Vim like Eclipse

I am using Vim to edit a Java file, but I find the way Vim formats Java files is very different from Eclipse.
If I select the following code and press =, Vim does not format the code the way I would like. Can anyone help me?
Before Format:
case RINGTONE_PICKED: {
Uri pickedUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
handleRingtonePicked(pickedUri);
break;
}
case PHOTO_PICKED_WITH_DATA: {
if (mPhotoEditorView != null) {
final Bitmap photo = data.getParcelableExtra("data");
mPhotoEditorView.setPhotoBitmap(photo);
} else {
// The contact that requested the photo is no longer present.
// TODO: Show error message
}
break;
}
After Format:
case RINGTONE_PICKED: {
Uri pickedUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
handleRingtonePicked(pickedUri);
break;
}
case PHOTO_PICKED_WITH_DATA: {
if (mPhotoEditorView != null) {
final Bitmap photo = data.getParcelableExtra("data");
mPhotoEditorView.setPhotoBitmap(photo);
} else {
// The contact that requested the photo is no longer present.
// TODO: Show error message
}
break;
}
This is what I want:
case RINGTONE_PICKED: {
Uri pickedUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
handleRingtonePicked(pickedUri);
break;
}
case PHOTO_PICKED_WITH_DATA: {
if (mPhotoEditorView != null) {
final Bitmap photo = data.getParcelableExtra("data");
mPhotoEditorView.setPhotoBitmap(photo);
} else {
// The contact that requested the photo is no longer present.
// TODO: Show error message
}
break;
}
Indentation in VIM (and in any other editor or IDE) is carried on by indentation rules coded by someone. There is no guarantee that any two system will follow same indentation practice, since there are different indentation practices around.
I also use VIM for minor editing Java files, and I'm not aware of any common alternative indentation script for Java, other than the one included in the official distribution. I you're familiar with VIM scripting you can try to edit the indentation script to your needs. It resides at $VIMRUNTIME/indent/java.vim.
By the way your example is a little uncommon. Using curly braces for cases of a switch statement is unnecessary. I guess VIM indentation scripts indent blocks by considering the block type and gets confused with this kind of uncommon blocks. Netbeans also get a little confused with this example, it aligns the case blocks in a reasonable way, but the closing curly brace of switch statement is completely out of alignment. That kind of odd behavior will not be so common with default VIM indentation. Actually if you remove the curly braces of the case statements VIM indentation aligns the statements quite well.

JsTree with dnd plugin, always copy

I have 2 trees using jsTree and dnd plugin.
I want that each drag operation to be a copy instead of a move.
There is a "copy_modifier" which works Ok when pressing a modifier key, but I want copy to be the default behavior without the modifier.
Any ideas?
Thanks,
Adrian
Found a solution on http://groups.google.com/group/jstree
I added the following section when configuring jsTree:
"crrm": {
"move": { "always_copy": "multitree" }
}
Hope this helps,
Adrian
another solution for the new version. it works, but not fully tested.
"core": {
"check_callback": function (operation, node, node_parent, node_position, more) {
if (more) {
if (more.is_multi) {
more.origin.settings.dnd.always_copy = true;
} else {
more.origin.settings.dnd.always_copy = false;
}
}
return true;
}
}
Adrian's solution won't work with the new versions.
There's that dnd plugins always copy flag
dnd.always_copy
Setting this flag will make all drag and drops copy operations instead of move. But if you are looking for a solution where you need internal tree elements to be moved on dnd but inter tree dnds to be copies than here's a hack:
Keep a global variable flag on your page
Handle copy_node.jstree events and update your global flag from
data.is_multi (data is the second arg of the event function)
Implement check_callback function and if operation is delete_node and your flag is set unset your flag and return false, preventing deletion from the dnd.