5e355e3d351de9486ea0998ff2575c903b97c410
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / ui / templates / template / BasicCompletionProcessor.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpeclipse.ui.templates.template;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import net.sourceforge.phpeclipse.ui.WebUI;
17
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.jface.resource.ImageRegistry;
20 import org.eclipse.jface.text.BadLocationException;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.IRegion;
23 import org.eclipse.jface.text.ITextSelection;
24 import org.eclipse.jface.text.ITextViewer;
25 import org.eclipse.jface.text.Region;
26 import org.eclipse.jface.text.contentassist.ICompletionProposal;
27 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
28 import org.eclipse.jface.text.templates.Template;
29 import org.eclipse.jface.text.templates.TemplateCompletionProcessor;
30 import org.eclipse.jface.text.templates.TemplateContext;
31 import org.eclipse.jface.text.templates.TemplateContextType;
32 import org.eclipse.jface.text.templates.TemplateException;
33 import org.eclipse.swt.graphics.Image;
34
35 /**
36  * A completion processor for XML templates.
37  */
38 public class BasicCompletionProcessor extends TemplateCompletionProcessor {
39         private static final String DEFAULT_IMAGE = "icons/template.gif"; //$NON-NLS-1$
40
41         private char[] fProposalAutoActivationSet;
42
43         // private PHPCompletionProposalComparator fComparator;
44         public BasicCompletionProcessor() {
45                 super();
46                 // fComparator = new PHPCompletionProposalComparator();
47         }
48
49         /**
50          * We watch for angular brackets since those are often part of XML
51          * templates.
52          */
53         protected String extractPrefix(ITextViewer viewer, int offset) {
54                 IDocument document = viewer.getDocument();
55                 int i = offset;
56                 if (i > document.getLength())
57                         return ""; //$NON-NLS-1$
58
59                 try {
60                         while (i > 0) {
61                                 char ch = document.getChar(i - 1);
62                                 if (ch != '<' && ch != '&' && ch != '{'
63                                                 && !Character.isJavaIdentifierPart(ch))
64                                         break;
65                                 i--;
66                         }
67
68                         return document.get(i, offset - i);
69                 } catch (BadLocationException e) {
70                         return ""; //$NON-NLS-1$
71                 }
72         }
73
74         /**
75          * Cut out angular brackets for relevance sorting, since the template name
76          * does not contain the brackets.
77          */
78         protected int getRelevance(Template template, String prefix) {
79                 // if (prefix.startsWith("<")) //$NON-NLS-1$
80                 // prefix= prefix.substring(1);
81                 if (template.getName().startsWith(prefix))
82                         return 90;
83                 return 0;
84         }
85
86         /**
87          * Simply return all templates.
88          */
89         protected Template[] getTemplates(String contextTypeId) {
90                 return WebUI.getDefault().getTemplateStore().getTemplates();
91         }
92
93         /**
94          * Return the XML context type that is supported by this plugin.
95          */
96         protected TemplateContextType getContextType(ITextViewer viewer,
97                         IRegion region) {
98                 return WebUI.getDefault().getContextTypeRegistry().getContextType(
99                                 XMLContextType.XML_CONTEXT_TYPE);
100         }
101
102         /**
103          * Always return the default image.
104          */
105         protected Image getImage(Template template) {
106                 ImageRegistry registry = WebUI.getDefault().getImageRegistry();
107                 Image image = registry.get(DEFAULT_IMAGE);
108                 if (image == null) {
109                         ImageDescriptor desc = WebUI.imageDescriptorFromPlugin(
110                                         "org.eclipse.ui.examples.javaeditor", DEFAULT_IMAGE); //$NON-NLS-1$
111                         registry.put(DEFAULT_IMAGE, desc);
112                         image = registry.get(DEFAULT_IMAGE);
113                 }
114                 return image;
115         }
116
117         /*
118          * (non-Javadoc)
119          * 
120          * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(org.eclipse.jface.text.ITextViewer,
121          *      int)
122          */
123         public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer,
124                         int offset) {
125                 ITextSelection selection = (ITextSelection) viewer
126                                 .getSelectionProvider().getSelection();
127
128                 // adjust offset to end of normalized selection
129                 if (selection.getOffset() == offset)
130                         offset = selection.getOffset() + selection.getLength();
131
132                 String prefix = extractPrefix(viewer, offset);
133                 prefix = prefix.toLowerCase();
134                 IRegion region = new Region(offset - prefix.length(), prefix.length());
135                 TemplateContext context = createContext(viewer, region);
136                 if (context == null)
137                         return new ICompletionProposal[0];
138
139                 context.setVariable("selection", selection.getText()); // name of the
140                                                                                                                                 // selection
141                                                                                                                                 // variables
142                                                                                                                                 // {line,
143                                                                                                                                 // word}_selection
144                                                                                                                                 // //$NON-NLS-1$
145
146                 Template[] templates = getTemplates(context.getContextType().getId());
147
148                 List matches = new ArrayList();
149                 for (int i = 0; i < templates.length; i++) {
150                         Template template = templates[i];
151                         try {
152                                 context.getContextType().validate(template.getPattern());
153                         } catch (TemplateException e) {
154                                 continue;
155                         }
156
157                         if (template.getName().startsWith(prefix)) { // &&
158                                                                                                                         // template.matches(prefix,
159                                                                                                                         // context.getContextType().getId()))
160                                 matches.add(createProposal(template, context, region,
161                                                 getRelevance(template, prefix)));
162                         }
163                 }
164
165                 return (ICompletionProposal[]) matches
166                                 .toArray(new ICompletionProposal[matches.size()]);
167
168         }
169
170         /**
171          * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
172          */
173         public char[] getCompletionProposalAutoActivationCharacters() {
174                 return fProposalAutoActivationSet;
175         }
176
177         /**
178          * Sets this processor's set of characters triggering the activation of the
179          * completion proposal computation.
180          * 
181          * @param activationSet
182          *            the activation set
183          */
184         public void setCompletionProposalAutoActivationCharacters(
185                         char[] activationSet) {
186                 fProposalAutoActivationSet = activationSet;
187         }
188
189         /**
190          * Order the given proposals.
191          */
192         // private ICompletionProposal[] order(ICompletionProposal[] proposals) {
193         // Arrays.sort(proposals, fComparator);
194         // return proposals;
195         // }
196         /**
197          * Tells this processor to order the proposals alphabetically.
198          * 
199          * @param order
200          *            <code>true</code> if proposals should be ordered.
201          */
202         // public void orderProposalsAlphabetically(boolean order) {
203         // fComparator.setOrderAlphabetically(order);
204         // }
205 }