Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 972b39fc6a002fcbde0fa2ff08c1ac1971fbe01e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*******************************************************************************
 * Copyright (c) 2000, 2012 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Sebastian Davids <sdavids@gmx.de> - Bug 37432 getInvertEqualsProposal
 *     Benjamin Muskalla <b.muskalla@gmx.net> - Bug 36350 convertToStringBufferPropsal
 *     Sergey Prigogin (Google)
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.text.correction;

import java.util.ArrayList;
import java.util.Collection;

import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNodeSelector;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.model.ILanguage;
import org.eclipse.cdt.internal.core.model.ASTCache.ASTRunnable;
import org.eclipse.cdt.internal.ui.editor.ASTProvider;
import org.eclipse.cdt.internal.ui.editor.CEditor;
import org.eclipse.cdt.internal.ui.text.correction.proposals.LinkedNamesAssistProposal;
import org.eclipse.cdt.internal.ui.text.correction.proposals.RenameRefactoringProposal;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.text.ICCompletionProposal;
import org.eclipse.cdt.ui.text.IInvocationContext;
import org.eclipse.cdt.ui.text.IProblemLocation;
import org.eclipse.cdt.ui.text.IQuickAssistProcessor;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.ui.IEditorPart;

/**
 * see org.eclipse.cdt.ui.text.IQuickAssistProcessor
 */
public class QuickAssistProcessor implements IQuickAssistProcessor {

	public QuickAssistProcessor() {
		super();
	}

	@Override
	public boolean hasAssists(final IInvocationContext context) throws CoreException {
		IStatus status = ASTProvider.getASTProvider().runOnAST(context.getTranslationUnit(),
				ASTProvider.WAIT_ACTIVE_ONLY, new NullProgressMonitor(), new ASTRunnable() {

					@Override
					public IStatus runOnAST(ILanguage lang, IASTTranslationUnit astRoot) throws CoreException {
						IASTNodeSelector selector = astRoot.getNodeSelector(null);
						IASTName name = selector.findEnclosingName(context.getSelectionOffset(),
								context.getSelectionLength());

						// Activate the proposal only if a simple name is selected.
						if (name != null && name == name.getLastName()) {
							IBinding binding = name.resolveBinding();
							if (binding != null) {
								return Status.OK_STATUS;
							}
						}
						return Status.CANCEL_STATUS;
					}
				});
		return status.isOK();
	}

	@Override
	public ICCompletionProposal[] getAssists(final IInvocationContext context,
			final IProblemLocation[] problemLocations) throws CoreException {
		final ArrayList<ICCompletionProposal> proposals = new ArrayList<>();

		ASTProvider.getASTProvider().runOnAST(context.getTranslationUnit(), ASTProvider.WAIT_ACTIVE_ONLY,
				new NullProgressMonitor(), new ASTRunnable() {

					@Override
					public IStatus runOnAST(ILanguage lang, IASTTranslationUnit astRoot) throws CoreException {
						if (astRoot == null) {
							return Status.CANCEL_STATUS;
						}

						IASTNodeSelector selector = astRoot.getNodeSelector(null);
						IASTName name = selector.findEnclosingName(context.getSelectionOffset(),
								context.getSelectionLength());

						// Activate the proposal only if a simple name is selected.
						if (name != null && name == name.getLastName()) {
							IBinding binding = name.resolveBinding();
							if (binding != null) {
								boolean noErrorsAtLocation = noErrorsAtLocation(problemLocations);

								// Quick assists that show up also if there is an error/warning
								getRenameLocalProposals(context, problemLocations, noErrorsAtLocation, proposals);
								getRenameRefactoringProposal(context, problemLocations, noErrorsAtLocation, proposals);
							}
						}
						return Status.OK_STATUS;
					}
				});

		return proposals.isEmpty() ? null : proposals.toArray(new ICCompletionProposal[proposals.size()]);
	}

	private boolean noErrorsAtLocation(IProblemLocation[] locations) {
		if (locations != null) {
			for (int i = 0; i < locations.length; i++) {
				if (locations[i].isError()) {
					return false;
				}
			}
		}
		return true;
	}

	private static void getRenameLocalProposals(IInvocationContext context, IProblemLocation[] locations,
			boolean noErrorsAtLocation, Collection<ICCompletionProposal> proposals) {
		LinkedNamesAssistProposal proposal = new LinkedNamesAssistProposal(context.getTranslationUnit());
		if (!noErrorsAtLocation) {
			proposal.setRelevance(1);
		}

		proposals.add(proposal);
	}

	private static boolean getRenameRefactoringProposal(IInvocationContext context, IProblemLocation[] locations,
			boolean noErrorsAtLocation, Collection<ICCompletionProposal> proposals) throws CoreException {
		IEditorPart editor = CUIPlugin.getActivePage().getActiveEditor();
		if (!(editor instanceof CEditor))
			return false;

		if (proposals == null) {
			return true;
		}
		RenameRefactoringProposal proposal = new RenameRefactoringProposal((CEditor) editor);
		if (!noErrorsAtLocation) {
			proposal.setRelevance(1);
		}

		proposals.add(proposal);
		return true;
	}
}

Back to the top