Skip to content

Commit 6118a95

Browse files
committed
Add new settings for clean ups
- Add documentation (accessible through a command) for what each clean up does Depends on eclipse-jdtls/eclipse.jdt.ls#2298 Closes redhat-developer#2144 Signed-off-by: David Thompson <davthomp@redhat.com>
1 parent 2d99511 commit 6118a95

File tree

6 files changed

+151
-0
lines changed

6 files changed

+151
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ The following settings are supported:
213213

214214
New in 1.13.0
215215
* `java.compile.nullAnalysis.mode`: Specify how to enable the annotation-based null analysis. Supported values are `disabled` (disable the null analysis), `interactive` (asks when null annotation types are detected), `automatic` (automatically enable null analysis when null annotation types are detected). Defaults to `disabled`.
216+
* `java.cleanup.actionsOnSave`: The list of clean ups to be run on the current document when it's saved. Clean ups can automatically fix code style or programming mistakes. [Click here](document/_java.learnMoreAboutCleanUps.md#java-clean-ups) to learn more about what each clean up does.
216217

217218
Semantic Highlighting
218219
===============
+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# Java Clean Ups
2+
3+
Java clean ups are run on the current document whenever it's saved.
4+
They can fix a wide variety of issues, from code style to programming mistakes,
5+
and can even modernize the sources based on new Java language features.
6+
Here is some information on the supported cleanups and the details of what they do.
7+
8+
### `qualifyMembers`
9+
10+
Whenever a member (field or method) of a class is accessed from within the class,
11+
prefix the access with `this`.
12+
This is similar to how Python requires the programmer to access members using `self`.
13+
14+
For instance:
15+
16+
```java
17+
private int value;
18+
19+
public void getValue() {
20+
return value;
21+
}
22+
```
23+
24+
becomes:
25+
26+
```java
27+
private int value;
28+
29+
public void getValue() {
30+
return this.value;
31+
}
32+
```
33+
34+
### `qualifyStaticMembers`
35+
36+
Whenever there is a static variable or function, prefix the access with the name of the class that the static variable or function belongs to.
37+
38+
For instance:
39+
40+
```java
41+
import static java.lang.System.out;
42+
43+
public class MyClass {
44+
public static final double FACTOR = 0.5;
45+
46+
public double getNumber(double value) {
47+
out.println("moo");
48+
return value * FACTOR;
49+
}
50+
}
51+
```
52+
53+
becomes:
54+
55+
```java
56+
import static java.lang.System.out;
57+
58+
public class MyClass {
59+
public static final double FACTOR = 0.5;
60+
61+
public double getNumber(double value) {
62+
System.out.println("moo");
63+
return value * MyClass.FACTOR;
64+
}
65+
}
66+
```
67+
68+
### `addOverride`
69+
70+
When a method of a class that overrides a method from a parent class or provides an implementation for a method from an interface, add the `@Override` annotation.
71+
72+
For example:
73+
74+
```java
75+
public class MyRunner implements Runnable {
76+
public void run() {
77+
System.out.println("Hello, World!");
78+
}
79+
}
80+
```
81+
82+
becomes:
83+
84+
```java
85+
public class MyRunner implements Runnable {
86+
@Override
87+
public void run() {
88+
System.out.println("Hello, World!");
89+
}
90+
}
91+
```
92+
93+
### `addDeprecated`
94+
95+
When a method is marked `@deprecated` in the Javadoc, but doesn't have the `@Deprecated` annotation, add the `@Deprecated` annotation.
96+
This only works if the compiler has been configured to mark deprecated methods without the deprecated annotation
97+
as an info/warning/error in the JDT settings.
98+
99+
For example:
100+
101+
```java
102+
/**
103+
* Not used anymore, please stop using.
104+
*
105+
* @deprecated
106+
*/
107+
public boolean isAGoat() {
108+
return false;
109+
}
110+
```
111+
112+
becomes:
113+
114+
```java
115+
/**
116+
* Not used anymore, please stop using.
117+
*
118+
* @deprecated
119+
*/
120+
@Deprecated
121+
public boolean isAGoat() {
122+
return false;
123+
}
124+
```

package.json

+20
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,21 @@
997997
"default": "disabled",
998998
"markdownDescription": "Specify how to enable the annotation-based null analysis.",
999999
"scope": "window"
1000+
},
1001+
"java.cleanup.actionsOnSave": {
1002+
"type": "array",
1003+
"markdownDescription": "The list of clean ups to be run on the current document when it's saved. Clean ups can automatically fix code style or programming mistakes. Click [HERE](command:_java.learnMoreAboutCleanUps) to learn more about what each clean up does.",
1004+
"items": {
1005+
"type": "string",
1006+
"enum": [
1007+
"qualifyMembers",
1008+
"qualifyStaticMembers",
1009+
"addOverride",
1010+
"addDeprecated"
1011+
]
1012+
},
1013+
"default": [],
1014+
"scope": "window"
10001015
}
10011016
}
10021017
},
@@ -1129,6 +1144,11 @@
11291144
"command": "java.project.createModuleInfo.command",
11301145
"title": "%java.project.createModuleInfo.command%",
11311146
"category": "Java"
1147+
},
1148+
{
1149+
"command": "_java.learnMoreAboutCleanUps",
1150+
"title": "Learn more about Java Clean Ups",
1151+
"category": "Java"
11321152
}
11331153
],
11341154
"keybindings": [

src/commands.ts

+2
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,8 @@ export namespace Commands {
267267

268268
export const LEARN_MORE_ABOUT_REFACTORING = '_java.learnMoreAboutRefactorings';
269269

270+
export const LEARN_MORE_ABOUT_CLEAN_UPS = '_java.learnMoreAboutCleanUps';
271+
270272
export const TEMPLATE_VARIABLES = '_java.templateVariables';
271273

272274
export const NOT_COVERED_EXECUTION = '_java.notCoveredExecution';

src/extension.ts

+3
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {
172172
context.subscriptions.push(commands.registerCommand(Commands.MEATDATA_FILES_GENERATION, async () => {
173173
markdownPreviewProvider.show(context.asAbsolutePath(path.join('document', `_java.metadataFilesGeneration.md`)), 'Metadata Files Generation', "", context);
174174
}));
175+
context.subscriptions.push(commands.registerCommand(Commands.LEARN_MORE_ABOUT_CLEAN_UPS, async () => {
176+
markdownPreviewProvider.show(context.asAbsolutePath(path.join('document', `${Commands.LEARN_MORE_ABOUT_CLEAN_UPS}.md`)), 'Java Clean Ups', "java-clean-ups", context);
177+
}));
175178
if (!storagePath) {
176179
storagePath = getTempWorkspace();
177180
}

test/standard-mode-suite/extension.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ suite('Java Language Extension - Standard', () => {
8686
Commands.SHOW_SUBTYPE_HIERARCHY,
8787
Commands.SHOW_SUPERTYPE_HIERARCHY,
8888
Commands.SHOW_CLASS_HIERARCHY,
89+
Commands.LEARN_MORE_ABOUT_CLEAN_UPS,
8990
].sort();
9091
const foundJavaCommands = commands.filter((value) => {
9192
return JAVA_COMMANDS.indexOf(value)>=0 || value.startsWith('java.');

0 commit comments

Comments
 (0)