- FAQ 319: What is eScript?
- FAQ 320: Language integration phase 1: How do I compile and build programs?
- FAQ 321: How do I load source files edited outside Eclipse?
- FAQ 322: How do I run an external builder on my source files?
- FAQ 323: How do I implement a compiler that runs inside Eclipse?
- FAQ 324: How do I react to changes in source files?
- FAQ 325: How do I implement an Eclipse builder?
- FAQ 326: Where are project build specifications stored?
- FAQ 327: How do I add a builder to a given project?
- FAQ 328: How do I implement an incremental project builder?
- FAQ 329: How do I handle setup problems for a given builder?
- FAQ 330: How do I make my compiler incremental?
- FAQ 331: Language integration phase 2: How do I implement a DOM?
- FAQ 332: How do I implement a DOM for my language?
- FAQ 333: How can I ensure that my model is scalable?
- FAQ 334: Language integration phase 3: How do I edit programs?
- FAQ 335: How do I write an editor for my own language?
- FAQ 336: How do I add Content Assist to my language editor?
- FAQ 337: How do I add hover support to my text editor?
- FAQ 338: How do I create problem markers for my compiler?
- FAQ 339: How do I implement Quick Fixes for my own language?
- FAQ 340: How do I support refactoring for my own language?
- FAQ 341: How do I create an Outline view for my own language editor?
- FAQ 342: Language integration phase 4: What are the finishing touches?
- FAQ 343: What wizards do I define for my own language?
- FAQ 344: When does my language need its own nature?
- FAQ 345: When does my language need its own perspective?
- FAQ 346: How do I add documentation and help for my own language?
- FAQ 347: How do I support source-level debugging for my own language?
FAQ 339: How do I implement Quick Fixes for my own language?
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>
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:
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—in our sample, the WhatsUp attribute—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.
Note
FAQ 340 How do I support refactoring for my own language?