1 /*******************************************************************************
2 * Copyright (c) 2000, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 * Sergey Prigogin (Google)
11 *******************************************************************************/
12 package org.eclipse.cdt.internal.ui.text.correction;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Iterator;
18 import org.eclipse.core.commands.AbstractHandler;
19 import org.eclipse.core.commands.ExecutionEvent;
20 import org.eclipse.core.commands.ExecutionException;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.NullProgressMonitor;
24 import org.eclipse.core.runtime.Status;
25 import org.eclipse.jface.bindings.TriggerSequence;
26 import org.eclipse.jface.text.BadLocationException;
27 import org.eclipse.jface.text.IDocument;
28 import org.eclipse.jface.text.ITextSelection;
29 import org.eclipse.jface.text.ITextViewer;
30 import org.eclipse.jface.text.contentassist.ICompletionProposal;
31 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension;
32 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension2;
33 import org.eclipse.jface.text.source.Annotation;
34 import org.eclipse.jface.text.source.IAnnotationModel;
35 import org.eclipse.jface.viewers.ISelection;
36 import org.eclipse.ui.PlatformUI;
37 import org.eclipse.ui.keys.IBindingService;
39 import org.eclipse.cdt.core.dom.ast.IASTName;
40 import org.eclipse.cdt.core.dom.ast.IASTNodeSelector;
41 import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
42 import org.eclipse.cdt.core.dom.ast.IBinding;
43 import org.eclipse.cdt.core.model.ILanguage;
44 import org.eclipse.cdt.core.model.ITranslationUnit;
45 import org.eclipse.cdt.ui.CUIPlugin;
46 import org.eclipse.cdt.ui.text.ICCompletionProposal;
47 import org.eclipse.cdt.ui.text.IInvocationContext;
49 import org.eclipse.cdt.internal.core.model.ASTCache.ASTRunnable;
51 import org.eclipse.cdt.internal.ui.actions.ActionUtil;
52 import org.eclipse.cdt.internal.ui.editor.ASTProvider;
53 import org.eclipse.cdt.internal.ui.editor.CEditor;
54 import org.eclipse.cdt.internal.ui.text.correction.proposals.LinkedNamesAssistProposal;
57 * Handler to be used to run a quick fix or assist by keyboard shortcut
59 public class CorrectionCommandHandler extends AbstractHandler {
60 private final CEditor fEditor;
61 private final String fId;
62 private final boolean fIsAssist;
64 public CorrectionCommandHandler(CEditor editor, String id, boolean isAssist) {
71 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
73 public Object execute(ExecutionEvent event) throws ExecutionException {
79 * Try to execute the correction command.
81 * @return <code>true</code> iff the correction could be started
84 public boolean doExecute() {
85 ISelection selection= fEditor.getSelectionProvider().getSelection();
86 ITranslationUnit tu= CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(fEditor.getEditorInput());
87 IAnnotationModel model= CUIPlugin.getDefault().getDocumentProvider().getAnnotationModel(fEditor.getEditorInput());
88 if (selection instanceof ITextSelection && tu != null && model != null) {
89 if (!ActionUtil.isEditable(fEditor)) {
92 ICompletionProposal proposal= findCorrection(fId, fIsAssist, (ITextSelection) selection, tu, model);
93 if (proposal != null) {
94 invokeProposal(proposal, ((ITextSelection) selection).getOffset());
100 private ICompletionProposal findCorrection(String id, boolean isAssist, ITextSelection selection, ITranslationUnit tu, IAnnotationModel model) {
101 CorrectionContext context= new CorrectionContext(tu, selection.getOffset(), selection.getLength());
102 Collection<ICCompletionProposal> proposals= new ArrayList<ICCompletionProposal>(10);
104 if (id.equals(LinkedNamesAssistProposal.ASSIST_ID)) {
105 return getLocalRenameProposal(context); // shortcut for local rename
107 CCorrectionProcessor.collectAssists(context, new ProblemLocation[0], proposals);
110 boolean goToClosest= selection.getLength() == 0;
111 Annotation[] annotations= getAnnotations(selection.getOffset(), goToClosest);
112 CCorrectionProcessor.collectProposals(context, model, annotations, true, false, proposals);
113 } catch (BadLocationException e) {
117 for (Iterator<ICCompletionProposal> iter= proposals.iterator(); iter.hasNext();) {
118 Object curr= iter.next();
119 if (curr instanceof ICommandAccess) {
120 if (id.equals(((ICommandAccess) curr).getCommandId())) {
121 return (ICompletionProposal) curr;
128 private Annotation[] getAnnotations(int offset, boolean goToClosest) throws BadLocationException {
129 ArrayList<Annotation> resultingAnnotations= new ArrayList<Annotation>();
130 CCorrectionAssistant.collectQuickFixableAnnotations(fEditor, offset, goToClosest, resultingAnnotations);
131 return resultingAnnotations.toArray(new Annotation[resultingAnnotations.size()]);
134 private ICompletionProposal getLocalRenameProposal(final IInvocationContext context) {
135 final ICCompletionProposal[] proposals= new ICCompletionProposal[1];
137 ASTProvider.getASTProvider().runOnAST(context.getTranslationUnit(), ASTProvider.WAIT_ACTIVE_ONLY,
138 new NullProgressMonitor(), new ASTRunnable() {
140 public IStatus runOnAST(ILanguage lang, IASTTranslationUnit astRoot) throws CoreException {
141 IASTNodeSelector selector= astRoot.getNodeSelector(null);
142 IASTName name= selector.findEnclosingName(context.getSelectionOffset(), context.getSelectionLength());
144 // Activate the proposal only if a simple name is selected.
145 if (name != null && name == name.getLastName()) {
146 IBinding binding= name.resolveBinding();
147 if (binding != null) {
148 proposals[0]= new LinkedNamesAssistProposal(context.getTranslationUnit());
151 return Status.OK_STATUS;
157 private IDocument getDocument() {
158 return CUIPlugin.getDefault().getDocumentProvider().getDocument(fEditor.getEditorInput());
161 private void invokeProposal(ICompletionProposal proposal, int offset) {
162 if (proposal instanceof ICompletionProposalExtension2) {
163 ITextViewer viewer= fEditor.getViewer();
164 if (viewer != null) {
165 ((ICompletionProposalExtension2) proposal).apply(viewer, (char) 0, 0, offset);
168 } else if (proposal instanceof ICompletionProposalExtension) {
169 IDocument document= getDocument();
170 if (document != null) {
171 ((ICompletionProposalExtension) proposal).apply(document, (char) 0, offset);
175 IDocument document= getDocument();
176 if (document != null) {
177 proposal.apply(document);
181 public static String getShortCutString(String proposalId) {
182 if (proposalId != null) {
183 IBindingService bindingService= (IBindingService) PlatformUI.getWorkbench().getAdapter(IBindingService.class);
184 if (bindingService != null) {
185 TriggerSequence[] activeBindingsFor= bindingService.getActiveBindingsFor(proposalId);
186 if (activeBindingsFor.length > 0) {
187 return activeBindingsFor[0].format();