VSCode java formatter for addons development

I’m using vscode on osx to edit some addons code. I’ve been trying to get the java formatter to work but I’m having problems.

I’ve installed the Java Extension Pack from Microsoft.

I downloaded / cloned https://github.com/openhab/openhab-addons and opened the local directory on my computer with vscode.

When the java extension is installed and enabled, it started modifying all the .classpath and .project files within the addons folder. However, I was able to invoke the java code formatter.

Whenever I discarded the changes made to all the .classpath files, they get re-instated.

So I set the java server launchmode to “LightWeight”. This stopped the modifications of .classpath files but the code formatter stopped working too.

Here’s my (user level) settings.json:

{
    "telemetry.enableCrashReporter": false,
    "window.zoomLevel": 1,
    "editor.minimap.enabled": false,
    "editor.suggestSelection": "first",
    "vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
    "java.autobuild.enabled": false,
    "python.languageServer": "Microsoft",
    "terminal.integrated.shell.osx": "/bin/bash",
    "editor.formatOnType": true,
    "editor.formatOnPaste": true,
    // "C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: Google, IndentWidth: 4, ColumnLimit: 0, AlignConsecutiveAssignments: true, AlignConsecutiveBitFields: true, AlignConsecutiveDeclarations: true, AlignConsecutiveMacros: true, AlignTrailingComments: true}",
    "C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: Google, IndentWidth: 4, ColumnLimit: 0, AlignConsecutiveMacros: true, AlignTrailingComments: true}",
    "files.associations": {},
    "todo-tree.tree.showScanModeButton": false,
    "C_Cpp.updateChannel": "Insiders",
    "editor.formatOnSave": true,
    "python.formatting.provider": "black",
    "python.formatting.blackArgs": [
        "--line-length",
        "119"
    ],
    "java.debug.settings.console": "integratedTerminal",
    "java.format.enabled": true,
    "java.format.comments.enabled": true,
    "java.format.onType.enabled": true,
    "java.saveActions.organizeImports": true,
    "java.configuration.checkProjectSettingsExclusions": false,
    "java.codeGeneration.generateComments": true,
    "java.format.settings.url": "https://raw.githubusercontent.com/openhab/static-code-analysis/master/codestyle/src/main/resources/openhab_codestyle.xml",
    "java.format.settings.profile": "openHAB",
    "[java]": {
        "editor.defaultFormatter": "redhat.java",
        "editor.detectIndentation": false,
        "editor.tabSize": 4
    },
    "java.import.gradle.enabled": false,
    "java.import.maven.enabled": false,
    "java.server.launchMode": "LightWeight",
    "[xml]": {
        "editor.defaultFormatter": "redhat.vscode-xml"
    },
    "workbench.editorAssociations": [
        {
            "viewType": "jupyter.notebook.ipynb",
            "filenamePattern": "*.ipynb"
        }
    ],
    "java.project.importOnFirstTimeStartup": "disabled",
}

What should I do in order to get the Java code formatter to work?

I don’t need vscode to do the build for me, at least at this point. I have always done the build process through the command line mvn install in the corresponding bundle directory that I want to build.

I don’t know about the vscode formatting. But you can also run mvn spotless:apply to format the code.

The vscode formatter is mentioned here, which is where I got my settings from:

I just couldn’t make it (the vscode java extension) work without the java extension making changes to all the .classpath files. I recently had a new laptop with a fresh installation. I think I did make it work on my old (now broken) laptop.

I use vscode and have seen it modifying the .classpath files, but those files are now included in .gitignore so I just don’t see that they have been changed.

What problem does it actually cause when those files are modified?

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.