upload tizen1.0 source
[sdk/ide/product.git] / org.eclipse.cdt.ui / src / org / eclipse / cdt / internal / ui / text / correction / CorrectionCommandHandler.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *         Sergey Prigogin (Google)
11  *******************************************************************************/
12 package org.eclipse.cdt.internal.ui.text.correction;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Iterator;
17
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;
38
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;
48
49 import org.eclipse.cdt.internal.core.model.ASTCache.ASTRunnable;
50
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;
55
56 /**
57  * Handler to be used to run a quick fix or assist by keyboard shortcut
58  */
59 public class CorrectionCommandHandler extends AbstractHandler {
60         private final CEditor fEditor;
61         private final String fId;
62         private final boolean fIsAssist;
63
64         public CorrectionCommandHandler(CEditor editor, String id, boolean isAssist) {
65                 fEditor= editor;
66                 fId= id;
67                 fIsAssist= isAssist;
68         }
69
70         /* (non-Javadoc)
71          * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
72          */
73         public Object execute(ExecutionEvent event) throws ExecutionException {
74                 doExecute();
75                 return null;
76         }
77
78         /**
79          * Try to execute the correction command.
80          * 
81          * @return <code>true</code> iff the correction could be started
82          * @since 5.3
83          */
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)) {
90                                 return false;
91                         }
92                         ICompletionProposal proposal= findCorrection(fId, fIsAssist, (ITextSelection) selection, tu, model);
93                         if (proposal != null) {
94                                 invokeProposal(proposal, ((ITextSelection) selection).getOffset());
95                         }
96                 }
97                 return false;
98         }
99         
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);
103                 if (isAssist) {
104                         if (id.equals(LinkedNamesAssistProposal.ASSIST_ID)) {
105                                 return getLocalRenameProposal(context); // shortcut for local rename
106                         }
107                         CCorrectionProcessor.collectAssists(context, new ProblemLocation[0], proposals);
108                 } else {
109                         try {
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) {
114                                 return null;
115                         }
116                 }
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;
122                                 }
123                         }
124                 }
125                 return null;
126         }
127
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()]);
132         }
133         
134         private ICompletionProposal getLocalRenameProposal(final IInvocationContext context) {
135                 final ICCompletionProposal[] proposals= new ICCompletionProposal[1];
136
137                 ASTProvider.getASTProvider().runOnAST(context.getTranslationUnit(), ASTProvider.WAIT_ACTIVE_ONLY,
138                                 new NullProgressMonitor(), new ASTRunnable() {
139
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());
143
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());
149                                         }
150                                 }
151                                 return Status.OK_STATUS;
152                         }
153                 });
154                 return proposals[0];
155         }
156
157         private IDocument getDocument() {
158                 return CUIPlugin.getDefault().getDocumentProvider().getDocument(fEditor.getEditorInput());
159         }
160         
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);
166                                 return;
167                         }
168                 } else if (proposal instanceof ICompletionProposalExtension) {
169                         IDocument document= getDocument();
170                         if (document != null) {
171                                 ((ICompletionProposalExtension) proposal).apply(document, (char) 0, offset);
172                                 return;
173                         }
174                 }
175                 IDocument document= getDocument();
176                 if (document != null) {
177                         proposal.apply(document);
178                 }
179         }
180         
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();
188                                 }
189                         }
190                 }
191                 return null;
192         }
193 }