Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 87c576ad820620fc8e395eedba527870ceba3ff0 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*******************************************************************************
 * Copyright (c) 2005, 2012 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Mohammed Mostafa <mmostafa@ca.ibm.com> - bug 296522
 *******************************************************************************/
package org.eclipse.pde.internal.ui.editor.text;

import java.util.*;
import org.eclipse.core.resources.IMarker;
import org.eclipse.jface.text.*;
import org.eclipse.jface.text.DefaultInformationControl.IInformationPresenter;
import org.eclipse.jface.text.contentassist.*;
import org.eclipse.jface.text.quickassist.*;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.pde.internal.ui.PDEPluginImages;
import org.eclipse.pde.internal.ui.correction.AbstractPDEMarkerResolution;
import org.eclipse.pde.internal.ui.correction.ResolutionGenerator;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IMarkerResolution;
import org.eclipse.ui.IMarkerResolution2;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.texteditor.SimpleMarkerAnnotation;
import org.eclipse.ui.texteditor.spelling.SpellingAnnotation;

public class PDEQuickAssistAssistant extends QuickAssistAssistant {

	private Image fCreateImage;
	private Image fRenameImage;
	private Image fRemoveImage;

	class PDECompletionProposal implements ICompletionProposal, ICompletionProposalExtension3, ICompletionProposalExtension4, Comparable<Object> {

		Position fPosition;
		IMarkerResolution fResolution;
		IMarker fMarker;

		public PDECompletionProposal(IMarkerResolution resolution, Position pos, IMarker marker) {
			fPosition = pos;
			fResolution = resolution;
			fMarker = marker;
		}

		public void apply(IDocument document) {
			fResolution.run(fMarker);
		}

		public Point getSelection(IDocument document) {
			return new Point(fPosition.offset, 0);
		}

		public String getAdditionalProposalInfo() {
			if (fResolution instanceof IMarkerResolution2)
				return ((IMarkerResolution2) fResolution).getDescription();
			return null;
		}

		public String getDisplayString() {
			return fResolution.getLabel();
		}

		public Image getImage() {
			if (fResolution instanceof AbstractPDEMarkerResolution) {
				switch (((AbstractPDEMarkerResolution) fResolution).getType()) {
					case AbstractPDEMarkerResolution.CREATE_TYPE :
						return fCreateImage;
					case AbstractPDEMarkerResolution.REMOVE_TYPE :
						return fRemoveImage;
					case AbstractPDEMarkerResolution.RENAME_TYPE :
						return fRenameImage;
				}
			}
			if (fResolution instanceof IMarkerResolution2) {
				IMarkerResolution2 resolution = (IMarkerResolution2) fResolution;
				return resolution.getImage();
			}
			return null;
		}

		public IContextInformation getContextInformation() {
			return null;
		}

		public IInformationControlCreator getInformationControlCreator() {
			return null;
		}

		public int getPrefixCompletionStart(IDocument document, int completionOffset) {
			return 0;
		}

		public CharSequence getPrefixCompletionText(IDocument document, int completionOffset) {
			return null;
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension4#isAutoInsertable()
		 */
		public boolean isAutoInsertable() {
			return true;
		}

		/* (non-Javadoc)
		 * @see java.lang.Object#equals(java.lang.Object)
		 */
		public boolean equals(Object obj) {
			if (!(obj instanceof PDECompletionProposal)) {
				return false;
			}
			PDECompletionProposal proposal = (PDECompletionProposal) obj;
			return proposal.fPosition.equals(fPosition) && proposal.fResolution.equals(fResolution);
		}

		/* (non-Javadoc)
		 * @see java.lang.Comparable#compareTo(java.lang.Object)
		 */
		public int compareTo(Object arg0) {
			if (!(arg0 instanceof PDECompletionProposal))
				return -1;
			PDECompletionProposal obj = (PDECompletionProposal) arg0;
			if (getDisplayString() != null) {
				if (obj.getDisplayString() != null) {
					return getDisplayString().compareTo(obj.getDisplayString());
				}
				return 0;
			}
			return -1;
		}

	}

	class PDEQuickAssistProcessor implements IQuickAssistProcessor {

		ResolutionGenerator fGenerator = new ResolutionGenerator();
		HashMap<IMarker, IMarkerResolution[]> fResMap = new HashMap<IMarker, IMarkerResolution[]>();

		public String getErrorMessage() {
			return null;
		}

		public boolean canFix(Annotation annotation) {
			boolean bRetVal = false;
			if (annotation instanceof SimpleMarkerAnnotation) {
				// check local resolutions first
				IMarker marker = ((SimpleMarkerAnnotation) annotation).getMarker();
				IMarkerResolution[] localResolutions = fGenerator.getResolutions(marker);
				if (localResolutions.length > 0) {
					bRetVal = true;
				}
				// check the contributed resolutions if needed
				if (!bRetVal) {
					IMarkerResolution[] contributedResolutions = IDE.getMarkerHelpRegistry().getResolutions(marker);
					if (contributedResolutions.length > 0) {
						bRetVal = true;
					}
				}
			}
			return bRetVal;

		}

		private void populateDataModelForAnnotation(SimpleMarkerAnnotation annotation) {
			// grab the local resolutions first
			IMarker marker = annotation.getMarker();
			if (!fResMap.containsKey(marker)) {
				ArrayList<IMarkerResolution> resolutions = new ArrayList<IMarkerResolution>(5);
				IMarkerResolution[] localResolutions = fGenerator.getResolutions(marker);
				resolutions.addAll(Arrays.asList(localResolutions));

				// grab the contributed resolutions
				IMarkerResolution[] contributedResolutions = IDE.getMarkerHelpRegistry().getResolutions(marker);
				for (int i = 0; i < contributedResolutions.length; i++) {
					IMarkerResolution resolution = contributedResolutions[i];
					// only add contributed marker resolutions if they don't come from PDE
					if (!(resolution instanceof AbstractPDEMarkerResolution) && !resolutions.contains(contributedResolutions[i]))
						resolutions.add(contributedResolutions[i]);
				}
				if (resolutions.size() > 0) {
					fResMap.put(marker, resolutions.toArray(new IMarkerResolution[resolutions.size()]));
				}
			}
		}

		public boolean canAssist(IQuickAssistInvocationContext invocationContext) {
			return false;
		}

		public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationContext invocationContext) {
			IAnnotationModel amodel = invocationContext.getSourceViewer().getAnnotationModel();
			IDocument doc = invocationContext.getSourceViewer().getDocument();

			int offset = invocationContext.getOffset();
			Iterator<?> it = amodel.getAnnotationIterator();
			TreeSet<ICompletionProposal> proposalSet = new TreeSet<ICompletionProposal>(new Comparator<Object>() {

				public int compare(Object o1, Object o2) {
					if (o1 instanceof ICompletionProposal && o2 instanceof ICompletionProposal) {
						ICompletionProposal proposal1 = (ICompletionProposal) o1;
						ICompletionProposal proposal2 = (ICompletionProposal) o2;
						return proposal1.getDisplayString().compareToIgnoreCase(proposal2.getDisplayString());
					}
					return 0;
				}

			});
			while (it.hasNext()) {
				Object key = it.next();
				if (!(key instanceof SimpleMarkerAnnotation)) {
					if (key instanceof SpellingAnnotation) {
						SpellingAnnotation annotation = (SpellingAnnotation) key;
						if (amodel.getPosition(annotation).overlapsWith(offset, 1)) {
							ICompletionProposal[] proposals = annotation.getSpellingProblem().getProposals();
							for (int index = 0; index < proposals.length; index++) {
								proposalSet.add(proposals[index]);
							}
						}
					}
					continue;
				}

				SimpleMarkerAnnotation annotation = (SimpleMarkerAnnotation) key;
				populateDataModelForAnnotation(annotation);
				IMarker marker = annotation.getMarker();

				IMarkerResolution[] mapping = fResMap.get(marker);
				if (mapping != null) {
					Position pos = amodel.getPosition(annotation);
					try {
						int line = doc.getLineOfOffset(pos.getOffset());
						int start = pos.getOffset();
						String delim = doc.getLineDelimiter(line);
						int delimLength = delim != null ? delim.length() : 0;
						int end = doc.getLineLength(line) + start - delimLength;
						if (offset >= start && offset <= end) {
							for (int i = 0; i < mapping.length; i++) {
								PDECompletionProposal proposal = new PDECompletionProposal(mapping[i], pos, marker);
								if (!proposalSet.contains(proposal)) {
									proposalSet.add(proposal);
								}
							}
						}
					} catch (BadLocationException e) {
					}

				}
			}

			return proposalSet.toArray(new ICompletionProposal[proposalSet.size()]);
		}
	}

	public PDEQuickAssistAssistant() {
		setQuickAssistProcessor(new PDEQuickAssistProcessor());
		fCreateImage = PDEPluginImages.DESC_ADD_ATT.createImage();
		fRemoveImage = PDEPluginImages.DESC_DELETE.createImage();
		fRenameImage = PDEPluginImages.DESC_REFRESH.createImage();
		setInformationControlCreator(new AbstractReusableInformationControlCreator() {
			public IInformationControl doCreateInformationControl(Shell parent) {
				return new DefaultInformationControl(parent, (IInformationPresenter) null);
			}
		});
	}

	public void dispose() {
		fCreateImage.dispose();
		fRemoveImage.dispose();
		fRenameImage.dispose();
	}
}

Back to the top