comparison org.eclipse.jface.text/src/org/eclipse/jface/text/templates/TemplateCompletionProcessor.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
1 /*******************************************************************************
2 * Copyright (c) 2000, 2005 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 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module org.eclipse.jface.text.templates.TemplateCompletionProcessor;
14
15 import org.eclipse.jface.text.templates.SimpleTemplateVariableResolver; // packageimport
16 import org.eclipse.jface.text.templates.TemplateBuffer; // packageimport
17 import org.eclipse.jface.text.templates.TemplateContext; // packageimport
18 import org.eclipse.jface.text.templates.TemplateContextType; // packageimport
19 import org.eclipse.jface.text.templates.Template; // packageimport
20 import org.eclipse.jface.text.templates.TemplateVariable; // packageimport
21 import org.eclipse.jface.text.templates.PositionBasedCompletionProposal; // packageimport
22 import org.eclipse.jface.text.templates.TemplateException; // packageimport
23 import org.eclipse.jface.text.templates.TemplateTranslator; // packageimport
24 import org.eclipse.jface.text.templates.DocumentTemplateContext; // packageimport
25 import org.eclipse.jface.text.templates.GlobalTemplateVariables; // packageimport
26 import org.eclipse.jface.text.templates.InclusivePositionUpdater; // packageimport
27 import org.eclipse.jface.text.templates.TemplateProposal; // packageimport
28 import org.eclipse.jface.text.templates.ContextTypeRegistry; // packageimport
29 import org.eclipse.jface.text.templates.JFaceTextTemplateMessages; // packageimport
30 import org.eclipse.jface.text.templates.TextTemplateMessages; // packageimport
31 import org.eclipse.jface.text.templates.TemplateVariableType; // packageimport
32 import org.eclipse.jface.text.templates.TemplateVariableResolver; // packageimport
33
34
35 import java.lang.all;
36 import java.util.Collections;
37 import java.util.List;
38 import java.util.ArrayList;
39 import java.util.Set;
40
41
42
43
44
45 import org.eclipse.swt.graphics.Image;
46 import org.eclipse.jface.text.BadLocationException;
47 import org.eclipse.jface.text.IDocument;
48 import org.eclipse.jface.text.IRegion;
49 import org.eclipse.jface.text.ITextSelection;
50 import org.eclipse.jface.text.ITextViewer;
51 import org.eclipse.jface.text.Region;
52 import org.eclipse.jface.text.contentassist.ICompletionProposal;
53 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
54 import org.eclipse.jface.text.contentassist.IContextInformation;
55 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
56
57
58 /**
59 * A completion processor that computes template proposals. Subclasses need to
60 * provide implementations for {@link #getTemplates(String)},
61 * {@link #getContextType(ITextViewer, IRegion)} and {@link #getImage(Template)}.
62 *
63 * @since 3.0
64 */
65 public abstract class TemplateCompletionProcessor : IContentAssistProcessor {
66
67 private static final class ProposalComparator : Comparator {
68 public int compare(Object o1, Object o2) {
69 return (cast(TemplateProposal) o2).getRelevance() - (cast(TemplateProposal) o1).getRelevance();
70 }
71 }
72
73 private static Comparator fgProposalComparator_;
74 private static Comparator fgProposalComparator(){
75 if(fgProposalComparator_ is null ) {
76 synchronized( TemplateCompletionProcessor.classinfo ){
77 if(fgProposalComparator_ is null ) {
78 fgProposalComparator_ = new ProposalComparator();
79 }
80 }
81 }
82 return fgProposalComparator_;
83 }
84
85 /*
86 * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(org.eclipse.jface.text.ITextViewer,
87 * int)
88 */
89 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
90
91 ITextSelection selection= cast(ITextSelection) viewer.getSelectionProvider().getSelection();
92
93 // adjust offset to end of normalized selection
94 if (selection.getOffset() is offset)
95 offset= selection.getOffset() + selection.getLength();
96
97 String prefix= extractPrefix(viewer, offset);
98 Region region= new Region(offset - prefix.length(), prefix.length());
99 TemplateContext context= createContext(viewer, region);
100 if (context is null)
101 return new ICompletionProposal[0];
102
103 context.setVariable("selection", selection.getText()); // name of the selection variables {line, word}_selection //$NON-NLS-1$
104
105 Template[] templates= getTemplates(context.getContextType().getId());
106
107 List matches= new ArrayList();
108 for (int i= 0; i < templates.length; i++) {
109 Template template_= templates[i];
110 try {
111 context.getContextType().validate(template_.getPattern());
112 } catch (TemplateException e) {
113 continue;
114 }
115 if (template_.matches(prefix, context.getContextType().getId()))
116 matches.add( cast(Object) createProposal(template_, context, cast(IRegion) region, getRelevance(template_, prefix)));
117 }
118
119 Collections.sort(matches, fgProposalComparator);
120
121 return arraycast!(ICompletionProposal)( matches.toArray());
122 }
123
124 /**
125 * Creates a new proposal.
126 * <p>
127 * Forwards to {@link #createProposal(Template, TemplateContext, IRegion, int)}.
128 * Do neither call nor override.
129 * </p>
130 *
131 * @param template the template to be applied by the proposal
132 * @param context the context for the proposal
133 * @param region the region the proposal applies to
134 * @param relevance the relevance of the proposal
135 * @return a new <code>ICompletionProposal</code> for
136 * <code>template</code>
137 * @deprecated use the version specifying <code>IRegion</code> as third parameter
138 * @since 3.1
139 */
140 protected ICompletionProposal createProposal(Template template_, TemplateContext context, Region region, int relevance) {
141 return createProposal(template_, context, cast(IRegion) region, relevance);
142 }
143
144 /**
145 * Creates a new proposal.
146 * <p>
147 * The default implementation returns an instance of
148 * {@link TemplateProposal}. Subclasses may replace this method to provide
149 * their own implementations.
150 * </p>
151 *
152 * @param template the template to be applied by the proposal
153 * @param context the context for the proposal
154 * @param region the region the proposal applies to
155 * @param relevance the relevance of the proposal
156 * @return a new <code>ICompletionProposal</code> for
157 * <code>template</code>
158 */
159 protected ICompletionProposal createProposal(Template template_, TemplateContext context, IRegion region, int relevance) {
160 return new TemplateProposal(template_, context, region, getImage(template_), relevance);
161 }
162
163 /**
164 * Returns the templates valid for the context type specified by <code>contextTypeId</code>.
165 *
166 * @param contextTypeId the context type id
167 * @return the templates valid for this context type id
168 */
169 protected abstract Template[] getTemplates(String contextTypeId);
170
171 /**
172 * Creates a concrete template context for the given region in the document. This involves finding out which
173 * context type is valid at the given location, and then creating a context of this type. The default implementation
174 * returns a <code>DocumentTemplateContext</code> for the context type at the given location.
175 *
176 * @param viewer the viewer for which the context is created
177 * @param region the region into <code>document</code> for which the context is created
178 * @return a template context that can handle template insertion at the given location, or <code>null</code>
179 */
180 protected TemplateContext createContext(ITextViewer viewer, IRegion region) {
181 TemplateContextType contextType= getContextType(viewer, region);
182 if (contextType !is null) {
183 IDocument document= viewer.getDocument();
184 return new DocumentTemplateContext(contextType, document, region.getOffset(), region.getLength());
185 }
186 return null;
187 }
188
189 /**
190 * Returns the context type that can handle template insertion at the given region
191 * in the viewer's document.
192 *
193 * @param viewer the text viewer
194 * @param region the region into the document displayed by viewer
195 * @return the context type that can handle template expansion for the given location, or <code>null</code> if none exists
196 */
197 protected abstract TemplateContextType getContextType(ITextViewer viewer, IRegion region);
198
199 /**
200 * Returns the relevance of a template given a prefix. The default
201 * implementation returns a number greater than zero if the template name
202 * starts with the prefix, and zero otherwise.
203 *
204 * @param template the template to compute the relevance for
205 * @param prefix the prefix after which content assist was requested
206 * @return the relevance of <code>template</code>
207 * @see #extractPrefix(ITextViewer, int)
208 */
209 protected int getRelevance(Template template_, String prefix) {
210 if (template_.getName().startsWith(prefix))
211 return 90;
212 return 0;
213 }
214
215 /**
216 * Heuristically extracts the prefix used for determining template relevance
217 * from the viewer's document. The default implementation returns the String from
218 * offset backwards that forms a java identifier.
219 *
220 * @param viewer the viewer
221 * @param offset offset into document
222 * @return the prefix to consider
223 * @see #getRelevance(Template, String)
224 */
225 protected String extractPrefix(ITextViewer viewer, int offset) {
226 int i= offset;
227 IDocument document= viewer.getDocument();
228 if (i > document.getLength())
229 return ""; //$NON-NLS-1$
230
231 try {
232 while (i > 0) {
233 char ch= document.getChar(i - 1);
234 if (!Character.isJavaIdentifierPart(ch))
235 break;
236 i--;
237 }
238
239 return document.get(i, offset - i);
240 } catch (BadLocationException e) {
241 return ""; //$NON-NLS-1$
242 }
243 }
244
245 /**
246 * Returns the image to be used for the proposal for <code>template</code>.
247 *
248 * @param template the template for which an image should be returned
249 * @return the image for <code>template</code>
250 */
251 protected abstract Image getImage(Template template_);
252
253 /*
254 * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeContextInformation(org.eclipse.jface.text.ITextViewer, int)
255 */
256 public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
257 return null;
258 }
259
260 /*
261 * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
262 */
263 public char[] getCompletionProposalAutoActivationCharacters() {
264 return null;
265 }
266
267 /*
268 * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationAutoActivationCharacters()
269 */
270 public char[] getContextInformationAutoActivationCharacters() {
271 return null;
272 }
273
274 /*
275 * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getErrorMessage()
276 */
277 public String getErrorMessage() {
278 return null;
279 }
280
281 /*
282 * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationValidator()
283 */
284 public IContextInformationValidator getContextInformationValidator() {
285 return null;
286 }
287 }