Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "FAQ How do I implement Quick Fixes for my own language?"

 
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
The JDT has support for so-called Quick Fixes. Whenever a marker is generated,
+
The JDT has support for so-called Quick Fixes. Whenever a marker is generated, a set of resolutions is associated with it for users to click on and choose an automatic fix of the problem as shown in Figure 19.5.
a set of resolutions is associated with it for users to click on and choose
+
Quick Fixes are implemented through the <tt>org.eclipse.ui.ide.markerResolution</tt> extension point:
an automatic fix of the problem as shown in Figure 19.5.
+
Quick Fixes are implemented through the <tt>org.eclipse.ui.ide.markerResolution</tt>  
+
extension point:
+
 
<pre>
 
<pre>
 
   &lt;extension point="org.eclipse.ui.ide.markerResolution"&gt;
 
   &lt;extension point="org.eclipse.ui.ide.markerResolution"&gt;
Line 13: Line 10:
  
 
 
&nbsp;&nbsp;&nbsp;&nbsp;<img src=../images/quick-fix.png>
+
&nbsp;&nbsp;&nbsp;&nbsp;[[Image:Quickfix1.jpg]]
  
 
&nbsp;&nbsp;&nbsp;&nbsp;'''Figure 19.5'''&nbsp;&nbsp;
 
&nbsp;&nbsp;&nbsp;&nbsp;'''Figure 19.5'''&nbsp;&nbsp;
Line 20: Line 17:
  
  
The implementation class implements the <tt>IMarkerResolutionGenerator</tt> interface.
+
The implementation class implements the <tt>IMarkerResolutionGenerator</tt> interface. Use the <tt>IMarkerResolutionGenerator2</tt> when resolutions are expensive to implement. See the javadoc for the interface for an explanation. Here is what the implementation class may look like:
Use the <tt>IMarkerResolutionGenerator2</tt> when resolutions are expensive to  
+
implement. See the javadoc for the interface for an explanation.  
+
Here is what the implementation class may look like:
+
 
<pre>
 
<pre>
   class QuickFixer implements IMarkerResolutionGenerator {
+
   public class QuickFixer implements IMarkerResolutionGenerator {
 
       public IMarkerResolution[] getResolutions(IMarker mk) {
 
       public IMarkerResolution[] getResolutions(IMarker mk) {
 
         try {
 
         try {
Line 40: Line 34:
 
   }
 
   }
 
</pre>
 
</pre>
An array of Quick Fixes has to be returned for the problem associated with the
+
An array of Quick Fixes has to be returned for the problem associated with the current marker.  
current marker.  
+
  
Each marker resolution, or Quick Fix, implements <tt>IMarkerResolution</tt>
+
Each marker resolution, or Quick Fix, implements <tt>IMarkerResolution</tt> or, when a description and an image are available, <tt>IMarkerResolution2</tt>. Here is what the implementation may look like:
or, when a description and an image are available, <tt>IMarkerResolution2</tt>.
+
Here is what the implementation may look like:
+
 
<pre>
 
<pre>
 
   public class QuickFix implements IMarkerResolution {
 
   public class QuickFix implements IMarkerResolution {
Line 61: Line 52:
 
   }
 
   }
 
</pre>
 
</pre>
The problem indicator&#151;in our sample, the <tt>WhatsUp</tt> attribute&#151;  
+
The problem indicator&#151;in our sample, the <tt>WhatsUp</tt> attribute&#151; is associated with the marker by the parser. Typically, the Quick Fix handler that resolves the problem, as shown in this example, lives somewhere in the UI. Following this paradigm is advisable as it separates the  problem detection in the compiler/parser from how it is presented to the user.
is associated with the marker by the parser.  
+
Typically, the Quick Fix handler that resolves the problem, as shown in this
+
example, lives somewhere in the UI. Following this paradigm is advisable
+
as it separates the  problem detection in the compiler/parser  
+
from how it is presented to the user.
+
  
Quick Fixes can be inspected and executed by using the context menu  
+
Quick Fixes can be inspected and executed by using the context menu on a given problem in  the Problems view. Note how the JDT uses a context menu and the double-click action on a marker to active Quick Fix.
on a given problem in  the Problems view. Note how the JDT uses
+
a context menu and the double-click action on a marker to active  
+
Quick Fix.
+
  
 
== See Also: ==
 
== See Also: ==
  
[[FAQ_How_do_I_support_refactoring_for_my_own_language%3F]]
+
[[FAQ How do I support refactoring for my own language?]]
  
 
<hr><font size=-2>This FAQ was originally published in [http://www.eclipsefaq.org Official Eclipse 3.0 FAQs]. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the [http://www.eclipse.org/legal/epl-v10.html Eclipse Public License v1.0].</font>
 
<hr><font size=-2>This FAQ was originally published in [http://www.eclipsefaq.org Official Eclipse 3.0 FAQs]. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the [http://www.eclipse.org/legal/epl-v10.html Eclipse Public License v1.0].</font>

Latest revision as of 10:50, 1 October 2009

The JDT has support for so-called Quick Fixes. Whenever a marker is generated, a set of resolutions is associated with it for users to click on and choose an automatic fix of the problem as shown in Figure 19.5. Quick Fixes are implemented through the org.eclipse.ui.ide.markerResolution extension point:

   <extension point="org.eclipse.ui.ide.markerResolution">
      <markerResolutionGenerator
         markerType="org.eclipse.core.resources.problemmarker"
         class="org.eclipse.escript.quickfix.QuickFixer"/>
   </extension>


    Quickfix1.jpg

    Figure 19.5   Quick Fixes in the Eclipse Java editor


The implementation class implements the IMarkerResolutionGenerator interface. Use the IMarkerResolutionGenerator2 when resolutions are expensive to implement. See the javadoc for the interface for an explanation. Here is what the implementation class may look like:

   public class QuickFixer implements IMarkerResolutionGenerator {
      public IMarkerResolution[] getResolutions(IMarker mk) {
         try {
            Object problem = mk.getAttribute("WhatsUp");
            return new IMarkerResolution[] {
               new QuickFix("Fix #1 for "+problem),
               new QuickFix("Fix #2 for "+problem),
            };
         }
         catch (CoreException e) {
            return new IMarkerResolution[0];
         }
      }
   }

An array of Quick Fixes has to be returned for the problem associated with the current marker.

Each marker resolution, or Quick Fix, implements IMarkerResolution or, when a description and an image are available, IMarkerResolution2. Here is what the implementation may look like:

   public class QuickFix implements IMarkerResolution {
      String label;
      QuickFix(String label) {
         this.label = label;
      }
      public String getLabel() {
         return label;
      }
      public void run(IMarker marker) {
         MessageDialog.openInformation(null, "QuickFix Demo",
            "This quick-fix is not yet implemented");
      }
   }

The problem indicator&#151;in our sample, the WhatsUp attribute&#151; is associated with the marker by the parser. Typically, the Quick Fix handler that resolves the problem, as shown in this example, lives somewhere in the UI. Following this paradigm is advisable as it separates the problem detection in the compiler/parser from how it is presented to the user.

Quick Fixes can be inspected and executed by using the context menu on a given problem in the Problems view. Note how the JDT uses a context menu and the double-click action on a marker to active Quick Fix.

See Also:

FAQ How do I support refactoring for my own language?


This FAQ was originally published in Official Eclipse 3.0 FAQs. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the Eclipse Public License v1.0.

Back to the top