0

Update VS Code Java formatter instructions

Turns out that `git cl format` uses clang-format to format Java code,
and the current instructions using JDT would produce formatting that can
be quite different. Fix the instructions to make VS Code format Java
files in the exact same way `git cl format` does.

Change-Id: I7d0476bf9c2d84d2c9dfe3ca13be4811079d45b0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4614846
Reviewed-by: Chase Phillips <cmp@chromium.org>
Commit-Queue: Chase Phillips <cmp@chromium.org>
Auto-Submit: Etienne Dechamps <edechamps@google.com>
Cr-Commit-Position: refs/heads/main@{#1158331}
This commit is contained in:
Etienne Dechamps
2023-06-15 18:39:45 +00:00
committed by Chromium LUCI CQ
parent 57286214e2
commit 2fba525ab5
2 changed files with 31 additions and 14 deletions
docs
tools/vscode

@ -232,37 +232,37 @@ of compile errors/warnings, Javadocs, etc.) when editing `.java` files in
Chromium:
1. **Add the following to your VS Code workspace `settings.json`:**
* `"java.import.gradle.enabled": false`
`"java.import.maven.enabled": false`
* `"java.import.gradle.enabled": false`
`"java.import.maven.enabled": false`
This will prevent the language server from attempting to build *all*
Gradle and Maven projects that can be found anywhere in the Chromium
source tree, which typically results in hilarity.
* `"java.jdt.ls.java.home": "<< ABSOLUTE PATH TO YOUR WORKING COPY OF CHROMIUM >>/src/third_party/jdk/current"`
* `"java.jdt.ls.java.home": "<< ABSOLUTE PATH TO YOUR WORKING COPY OF CHROMIUM >>/src/third_party/jdk/current"`
This one is optional but reduces the likelihood of problems by making sure
the language server uses the same JDK as the Chromium build system (as
opposed to some random JDK from your host system).
2. **Install the
[*Language Support for Java™ by Red Hat*](https://marketplace.visualstudio.com/items?itemName=redhat.java)
extension.**
extension.**
You do not need any other extension.
3. In the VS Code explorer (left-hand pane) **right-click on a folder (any folder)
and click *Add Folder to Java Source Path***.
and click *Add Folder to Java Source Path***.
This is just one way to force the extension to generate an internal build
project for the language server. The specific folder doesn't matter because
the source path configuration will be overwritten in step 6.
4. **Wait for the *Successfully added '...' to the project src_...'s source
path* pop-up notification to appear**.
path* pop-up notification to appear**.
Note that this can take some time (1-2 minutes), during which VS Code seems
to be idle.
5. **Build your code** in the usual way (i.e. using gn and ninja commands).
5. **Build your code** in the usual way (i.e. using gn and ninja commands).
This will produce build config files that are necessary for the next step. It
will also make autogenerated code visible to the language server.
6. **Generate the `.classpath` file** for the internal build project by running
`build/android/generate_vscode_classpath.py` with the appropriate arguments
from the root of your VS Code workspace folder.
from the root of your VS Code workspace folder.
For example, if your VS Code workspace is rooted in `src`, your build
output directory is `out/Debug-x86` and your build target is
`//components/cronet/android:cronet_javatests`, run:
`//components/cronet/android:cronet_javatests`, run:
`build/android/generate_vscode_classpath.py --output-dir out/Debug-x86 --build-config gen/components/cronet/android/cronet_javatests.build_config.json > ~/.vscode*/data/User/workspaceStorage/*/redhat.java/jdt_ws/.metadata/.plugins/org.eclipse.core.resources/.projects/src_*/.classpath`
7. **Reload** your VS Code window.
8. **Open a Java source file then wait a couple of minutes** for the language
@ -282,6 +282,28 @@ that risks overwriting the generated `.classpath` file.
[autogenerated](/base/android/jni_generator/README.md) `GEN_JNI` class. This
is a known quirk of the JNI generator.
#### Automatic formatting
Java code in Chromium is formatted using [clang-format](/docs/clang_format.md).
To get VS Code to use clang-format to format Java files, install the
[*Clang-Format* extension](https://marketplace.visualstudio.com/items?itemName=xaver.clang-format)
and set it as the default formatter for Java in your workspace `settings.json`:
```json
"[java]": {
"editor.defaultFormatter": "xaver.clang-format"
}
```
To avoid potential formatting differences due to clang-format version skew, it
makes sense to configure the extension to run clang-format in the same way
`git cl format` would. You can do this by adding the following to your
workspace `settings.json`:
```json
"clang-format.executable": "<< PATH TO YOUR CHROMIUM WORKING COPY >>/src/buildtools/linux64/clang-format"
```
## Setup For Chromium
VS Code is configured via JSON files. This paragraph contains JSON configuration

@ -72,11 +72,6 @@
"editor.formatOnSave": true,
"editor.formatOnSaveMode": "modifications",
// Format Java code according to Android style. Requires the "Language Support
// for Java(TM) by Red Hat" extension. Read the "Java/Android Support" section
// in //docs/vscode.md *before* installing the extension.
"java.format.settings.url": "https://raw.githubusercontent.com/android/platform_development/master/ide/eclipse/android-formatting.xml",
// Disable automatic task detection to speed up opening the task menu.
"task.autoDetect": "off",