Skip to content

Commit 3a73f6a

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 #2144 Signed-off-by: David Thompson <[email protected]>
1 parent c582159 commit 3a73f6a

File tree

7 files changed

+152
-1
lines changed

7 files changed

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

package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,21 @@
980980
],
981981
"markdownDescription": "Specify the Nullable annotation types to be used for null analysis. If more than one annotation is specified, then the topmost annotation will be used first if it exists in your project dependencies.",
982982
"scope": "window"
983+
},
984+
"java.cleanup.enabled": {
985+
"type": "array",
986+
"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.",
987+
"items": {
988+
"type":"string",
989+
"enum": [
990+
"memberAccessUsesThis",
991+
"staticAccessUsesClassName",
992+
"addOverrideAnnotation",
993+
"addDeprecatedAnnotation"
994+
]
995+
},
996+
"default": [],
997+
"scope": "resource"
983998
}
984999
}
9851000
},
@@ -1112,6 +1127,11 @@
11121127
"command": "java.project.createModuleInfo.command",
11131128
"title": "%java.project.createModuleInfo.command%",
11141129
"category": "Java"
1130+
},
1131+
{
1132+
"command": "_java.learnMoreAboutCleanUps",
1133+
"title": "%_java.learnMoreAboutCleanUps%",
1134+
"category": "Java"
11151135
}
11161136
],
11171137
"keybindings": [

package.nls.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@
2222
"java.action.showSupertypeHierarchy": "Show Supertype Hierarchy",
2323
"java.action.showSubtypeHierarchy": "Show Subtype Hierarchy",
2424
"java.action.changeBaseType": "Base on this Type",
25-
"java.project.createModuleInfo.command": "Create module-info.java"
25+
"java.project.createModuleInfo.command": "Create module-info.java",
26+
"_java.learnMoreAboutCleanUps": "Learn more about Java Clean Ups"
2627
}

src/commands.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ export namespace Commands {
262262

263263
export const LEARN_MORE_ABOUT_REFACTORING = '_java.learnMoreAboutRefactorings';
264264

265+
export const LEARN_MORE_ABOUT_CLEAN_UPS = '_java.learnMoreAboutCleanUps';
266+
265267
export const TEMPLATE_VARIABLES = '_java.templateVariables';
266268

267269
export const NOT_COVERED_EXECUTION = '_java.notCoveredExecution';

src/extension.ts

Lines changed: 3 additions & 0 deletions
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/lightweight-mode-suite/extension.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ suite('Java Language Extension - LightWeight', () => {
2222
Commands.OPEN_FORMATTER,
2323
Commands.CLEAN_WORKSPACE,
2424
Commands.SWITCH_SERVER_MODE,
25+
// Commands.LEARN_MORE_ABOUT_CLEAN_UPS,
2526
].sort();
2627
const foundJavaCommands = commands.filter((value) => {
2728
return JAVA_COMMANDS.indexOf(value)>=0 || value.startsWith('java.');

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

Lines changed: 1 addition & 0 deletions
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)