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