8080import org .openide .filesystems .FileObject ;
8181import org .openide .text .PositionBounds ;
8282import org .openide .util .Exceptions ;
83+ import org .openide .util .Pair ;
8384import 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