Home > Blockchain >  VS code editing java on save
VS code editing java on save

Time:09-25

I am typing some class or method, and using line breaks to help me see where everything is, and it keeps getting edited out when I save. I've tried looking for an answer in the extensions and settings, but can't figure out what might be deleting the new lines.

I'll have something like:

public class SomeClass
{
    public static void main (String[] args)
    {
        Some code here;
    }
}

and it will delete the first line break after SomeClass and after args) it will edit it to look like this:

public class SomeClass {
    public static void main (String[] args) {
        some code here;
    }
}

How do I turn off the editing on save that deletes my new lines?

Thanks!

Here are the .json

settings
{

    "files.autoSave": "afterDelay",
    "editor.suggestSelection": "first",
    "vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
    "files.exclude": {
        "**/.classpath": true,
        "**/.project": true,
        "**/.settings": true,
        "**/.factorypath": true
    },
    "editor.bracketPairColorization.enabled": true,
    "editor.padding.bottom": 5,
    "editor.padding.top": 5,
    "editor.roundedSelection": false,
    "javascript.format.placeOpenBraceOnNewLineForControlBlocks": true,
    "javascript.format.placeOpenBraceOnNewLineForFunctions": true,
    "files.insertFinalNewline": true,
    "editor.renderFinalNewline": false,
    "editor.trimAutoWhitespace": false,
    "launch": {
    
        "configurations": [],
        "compounds": []
    }
}

CodePudding user response:

Press Ctrl Shift P in VS Code. It will open settings. There disable auto formatting of code, as in below image.

enter image description here

CodePudding user response:

Hit Ctrl Shift P and type Save Without Formatting. Hit Enter(Return) and it should save without applying any formatting. Doing this every time doesn't make sense. So maybe you would like to change the key binding from File -> Preferences -> Keyborad Shortcuts.

The doc talks more on that here

EDIT 1: START

If you happen to have a liking for a particular style(like eclipse-java-google-style), you can consider setting up the java.format.settings.url option under File->Preferences->Settings -> Java.

You can also create your own formatting profile in eclipse and export the profile as xml file and provide the path to the xml file.

But in order to be able to do that you will first need to install the extension Language Support for Java(TM) by Red Hat.

Creating profile in eclipse: Generally speaking you create profiles in Eclipse by going to the Windows -> Preferences -> Java -> Code Style -> Formatter -> New -> Brace Positions -> Choose the option you want(perhaps Next line is what you are looking for, click on the checkbox to modify all with the same value) -> In the same window you will see the export button(somewhere top right corner) -> export your new profile to some safe location of your choice. It will be saved as an XML file.

Copy the full path of the file and paste it in the java.format.settings.url section mentioned above.

Having a profile setup will save you lot of time. You will never have to manually format your braces/ indentation or any other piece. Define the formatting once and forget about it.

This profile setup also gives you the privilege of using the default Save of VSCode. It will format on save but as per your defined profile.

Kind of Win-Win situation for both you and VSCode's default settings.

EDIT 1: END

  •  Tags:  
  • java
  • Related