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