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