Skip to content

Commit 573b1dc

Browse files
authored
Merge pull request #7548 from Achal1607/javavscode-96
Implement suppress warnings on for Unused hint and codeAction for suppress warnings annotations
2 parents 3242f90 + 372dc23 commit 573b1dc

File tree

5 files changed

+135
-83
lines changed

5 files changed

+135
-83
lines changed

ide/spi.editor.hints/src/org/netbeans/spi/editor/hints/Fix.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
package org.netbeans.spi.editor.hints;
2121

22+
import org.netbeans.modules.editor.hints.HintsControllerImpl;
23+
2224
/**
2325
* Allows to perform a change when the user selects the hint.
2426
* @author Jan Lahoda
@@ -39,4 +41,8 @@ public interface Fix {
3941
* determined.
4042
*/
4143
public abstract ChangeInfo implement() throws Exception;
44+
45+
default Iterable<? extends Fix> getSubfixes() {
46+
return HintsControllerImpl.getSubfixes(this);
47+
}
4248
}

java/java.hints/nbproject/project.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
spec.version.base=1.108.0
1919

20-
javac.source=1.8
20+
javac.release=17
2121

2222
nbroot=../..
2323
jbrowse.external=${nbroot}/retouche

java/java.hints/src/org/netbeans/modules/java/hints/bugs/Unused.java

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
package org.netbeans.modules.java.hints.bugs;
2020

2121
import com.sun.source.tree.Tree.Kind;
22-
import java.util.ArrayList;
2322
import java.util.List;
2423
import javax.lang.model.element.ElementKind;
2524
import org.netbeans.modules.java.editor.base.semantic.UnusedDetector;
@@ -34,6 +33,8 @@
3433
import org.netbeans.spi.java.hints.TriggerTreeKind;
3534
import org.openide.util.NbBundle.Messages;
3635

36+
import static org.netbeans.api.java.source.CompilationInfo.CacheClearPolicy.ON_TASK_END;
37+
3738
/**
3839
*
3940
* @author lahvac
@@ -52,24 +53,46 @@ public class Unused {
5253
@BooleanOption(displayName="#LBL_UnusedPackagePrivate", tooltip="#TP_UnusedPackagePrivate", defaultValue=DETECT_UNUSED_PACKAGE_PRIVATE_DEFAULT)
5354
public static final String DETECT_UNUSED_PACKAGE_PRIVATE = "detect.unused.package.private";
5455

55-
@TriggerTreeKind(Kind.COMPILATION_UNIT)
56+
@TriggerTreeKind({
57+
//class-like kinds:
58+
Kind.ANNOTATION_TYPE, Kind.CLASS, Kind.ENUM, Kind.INTERFACE, Kind.RECORD,
59+
Kind.VARIABLE,
60+
Kind.METHOD
61+
})
5662
public static List<ErrorDescription> unused(HintContext ctx) {
5763
List<UnusedDescription> unused = UnusedDetector.findUnused(ctx.getInfo(), () -> ctx.isCanceled());
58-
List<ErrorDescription> result = new ArrayList<>(unused.size());
59-
boolean detectUnusedPackagePrivate = ctx.getPreferences().getBoolean(DETECT_UNUSED_PACKAGE_PRIVATE, DETECT_UNUSED_PACKAGE_PRIVATE_DEFAULT);
64+
if (unused.isEmpty()) {
65+
return null;
66+
}
67+
boolean detectUnusedPackagePrivate = getTaskCachedBoolean(ctx, DETECT_UNUSED_PACKAGE_PRIVATE, DETECT_UNUSED_PACKAGE_PRIVATE_DEFAULT);
6068
for (UnusedDescription ud : unused) {
6169
if (ctx.isCanceled()) {
6270
break;
6371
}
72+
if (ud.unusedElementPath.getLeaf() != ctx.getPath().getLeaf()) {
73+
continue;
74+
}
6475
if (!detectUnusedPackagePrivate && ud.packagePrivate) {
6576
continue;
6677
}
6778
ErrorDescription err = convertUnused(ctx, ud);
6879
if (err != null) {
69-
result.add(err);
80+
return List.of(err);
7081
}
82+
break;
83+
}
84+
return null;
85+
}
86+
87+
// reading from AuxiliaryConfigBasedPreferences in inner loops is not cheap since it needs a mutex
88+
private static boolean getTaskCachedBoolean(HintContext ctx, String key, boolean defaultVal) {
89+
Object cached = ctx.getInfo().getCachedValue(key);
90+
if (cached instanceof Boolean val) {
91+
return val;
7192
}
72-
return result;
93+
boolean fromPrefs = ctx.getPreferences().getBoolean(key, defaultVal);
94+
ctx.getInfo().putCachedValue(key, fromPrefs, ON_TASK_END);
95+
return fromPrefs;
7396
}
7497

7598
@Messages({

java/java.hints/src/org/netbeans/modules/java/hints/infrastructure/JavaErrorProvider.java

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
import org.openide.filesystems.FileObject;
8181
import org.openide.text.PositionBounds;
8282
import org.openide.util.Exceptions;
83+
import org.openide.util.Pair;
8384
import org.openide.util.Union2;
8485

8586
/** Provides errors and hints for VSCode. This class is abstract to
@@ -116,15 +117,15 @@ public void run(ResultIterator it) throws Exception {
116117
AtomicBoolean cancel = new AtomicBoolean();
117118
context.registerCancelCallback(() -> cancel.set(true));
118119
HintsSettings settings;
119-
120+
120121
if (context.getHintsConfigFile() != null) {
121122
Preferences hintSettings = ToolPreferences.from(context.getHintsConfigFile().toURI()).getPreferences(HINTS_TOOL_ID, "text/x-java");
122123
settings = HintsSettings.createPreferencesBasedHintsSettings(hintSettings, true, null);
123124
} else {
124125
settings = HintsSettings.getGlobalSettings();
125126
}
126127
result.addAll(convert2Diagnostic(context.errorKind(), new HintsInvoker(settings, context.getOffset(), cancel).computeHints(cc), ed -> !disabled.contains(ed.getSeverity())));
127-
128+
128129
}
129130
break;
130131
}
@@ -200,24 +201,42 @@ private static List<CodeAction> convertFixes(ErrorDescription err, Consumer<Exce
200201
}
201202

202203
List<Fix> fixes = sortFixes(lfl.getFixes());
203-
List<CodeAction> result = new ArrayList<>();
204+
205+
List<Pair<Fix, Boolean>> fixesAndSubfixes = new ArrayList<>(); // true if main fix
204206

205207
for (Fix f : fixes) {
208+
fixesAndSubfixes.add(Pair.of(f, true));
209+
for (Fix subFix : f.getSubfixes()) {
210+
fixesAndSubfixes.add(Pair.of(subFix, false));
211+
}
212+
}
213+
214+
List<CodeAction> result = new ArrayList<>();
215+
boolean printSubFixes = false;
216+
217+
for (Pair<Fix, Boolean> textAndFix : fixesAndSubfixes) {
218+
if (!printSubFixes && !textAndFix.second()) {
219+
continue;
220+
}
221+
222+
Fix f = textAndFix.first();
223+
int startResultSize = result.size();
224+
String text = textAndFix.second() ? f.getText() : " -> " + f.getText();
225+
206226
if (f instanceof IncompleteClassPath.ResolveFix) {
207227
// We know that this is a project problem and that the problems reported by ProjectProblemsProvider should be resolved
208-
CodeAction action = new CodeAction(f.getText(), new Command(f.getText(), "nbls.project.resolveProjectProblems"));
228+
CodeAction action = new CodeAction(text, new Command(text, "nbls.project.resolveProjectProblems"));
209229
result.add(action);
210230
}
211231
if (f instanceof org.netbeans.modules.java.hints.errors.EnablePreview.ResolveFix) {
212232
org.netbeans.modules.java.hints.errors.EnablePreview.ResolveFix rf = (org.netbeans.modules.java.hints.errors.EnablePreview.ResolveFix) f;
213233
List<Object> params = rf.getNewSourceLevel() != null ? Arrays.asList(rf.getNewSourceLevel())
214-
: Collections.emptyList();
215-
CodeAction action = new CodeAction(f.getText(), new Command(f.getText(), "nbls.java.project.enable.preview", params));
234+
: Collections.emptyList();
235+
CodeAction action = new CodeAction(text, new Command(text, "nbls.java.project.enable.preview", params));
216236
result.add(action);
217237
}
218238
if (f instanceof ImportClass.FixImport) {
219239
//TODO: FixImport is not a JavaFix, create one. Is there a better solution?
220-
String text = f.getText();
221240
CharSequence sortText = ((ImportClass.FixImport) f).getSortText();
222241
ElementHandle<Element> toImport = ((ImportClass.FixImport) f).getToImport();
223242
f = new JavaFix(topLevelHandle[0], sortText != null ? sortText.toString() : null) {
@@ -233,16 +252,16 @@ protected void performRewrite(JavaFix.TransformationContext ctx) throws Exceptio
233252
}
234253
WorkingCopy copy = ctx.getWorkingCopy();
235254
CompilationUnitTree cut = GeneratorUtilities.get(copy).addImports(
236-
copy.getCompilationUnit(),
237-
Collections.singleton(resolved)
255+
copy.getCompilationUnit(),
256+
Collections.singleton(resolved)
238257
);
239258
copy.rewrite(copy.getCompilationUnit(), cut);
240259
}
241260
}.toEditorFix();
242261
}
243262
if (f instanceof JavaFixImpl) {
244263
JavaFix jf = ((JavaFixImpl) f).jf;
245-
CodeAction action = new LazyCodeAction(f.getText(), () -> {
264+
CodeAction action = new LazyCodeAction(text, () -> {
246265
try {
247266
List<TextEdit> edits = modify2TextEdits(js, wc -> {
248267
wc.toPhase(JavaSource.Phase.RESOLVED);
@@ -261,7 +280,7 @@ protected void performRewrite(JavaFix.TransformationContext ctx) throws Exceptio
261280
}
262281
if (f instanceof ModificationResultBasedFix) {
263282
ModificationResultBasedFix cf = (ModificationResultBasedFix) f;
264-
CodeAction codeAction = new LazyCodeAction(f.getText(), () -> {
283+
CodeAction codeAction = new LazyCodeAction(text, () -> {
265284
try {
266285
List<Union2<TextDocumentEdit, ResourceOperation>> documentChanges = new ArrayList<>();
267286
for (ModificationResult changes : cf.getModificationResults()) {
@@ -289,8 +308,8 @@ protected void performRewrite(JavaFix.TransformationContext ctx) throws Exceptio
289308
continue outer;
290309
} else {
291310
edits.add(new TextEdit(diff.getStartPosition().getOffset(),
292-
diff.getEndPosition().getOffset(),
293-
newText != null ? newText : ""));
311+
diff.getEndPosition().getOffset(),
312+
newText != null ? newText : ""));
294313
}
295314
}
296315
documentChanges.add(Union2.createFirst(new TextDocumentEdit(fileObject.toURI().toString(), edits))); //XXX: toURI
@@ -305,6 +324,9 @@ protected void performRewrite(JavaFix.TransformationContext ctx) throws Exceptio
305324
});
306325
result.add(codeAction);
307326
}
327+
if (textAndFix.second()) {
328+
printSubFixes = startResultSize != result.size();
329+
}
308330
}
309331

310332
return result;
@@ -369,8 +391,8 @@ private static List<TextEdit> fileModifications(ModificationResult changes, File
369391
for (ModificationResult.Difference diff : diffs) {
370392
String newText = diff.getNewText();
371393
edits.add(new TextEdit(diff.getStartPosition().getOffset(),
372-
diff.getEndPosition().getOffset(),
373-
newText != null ? newText : ""));
394+
diff.getEndPosition().getOffset(),
395+
newText != null ? newText : ""));
374396
}
375397
return edits;
376398
}

0 commit comments

Comments
 (0)