Syntax highlighting is changeable.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / template / preferences / TemplateVariableProcessor.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.phpdt.internal.ui.text.template.preferences;
12
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.Comparator;
16 import java.util.Iterator;
17 import java.util.List;
18
19 import org.eclipse.jface.text.ITextViewer;
20 import org.eclipse.jface.text.contentassist.ICompletionProposal;
21 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
22 import org.eclipse.jface.text.contentassist.IContextInformation;
23 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
24 import org.eclipse.jface.text.templates.TemplateContextType;
25 import org.eclipse.jface.text.templates.TemplateVariableResolver;
26
27
28
29 public class TemplateVariableProcessor implements IContentAssistProcessor {     
30
31         private static Comparator fgTemplateVariableProposalComparator= new Comparator() {
32                 public int compare(Object arg0, Object arg1) {
33                         TemplateVariableProposal proposal0= (TemplateVariableProposal) arg0;
34                         TemplateVariableProposal proposal1= (TemplateVariableProposal) arg1;
35                         
36                         return proposal0.getDisplayString().compareTo(proposal1.getDisplayString());
37                 }
38
39                 public boolean equals(Object arg0) {
40                         return false;
41                 }
42         };
43
44         
45         /** the context type */
46         private TemplateContextType fContextType;
47         
48         /**
49          * Sets the context type.
50          */
51         public void setContextType(TemplateContextType contextType) {
52                 fContextType= contextType;      
53         }
54         
55         /**
56          * Gets the context type.
57          */
58         public TemplateContextType getContextType() {
59                 return fContextType;    
60         }       
61         
62         /*
63          * @see IContentAssistProcessor#computeCompletionProposals(ITextViewer, int)
64          */
65         public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer,     int documentOffset) {
66
67                 if (fContextType == null)
68                         return null;
69
70                 List proposals= new ArrayList();                
71                 
72                 String text= viewer.getDocument().get();
73                 int start= getStart(text, documentOffset);
74                 int end= documentOffset;
75
76                 String string= text.substring(start, end);
77                 String prefix= (string.length() >= 2)
78                         ? string.substring(2)
79                         : null;
80
81                 int offset= start;
82                 int length= end - start;
83
84                 for (Iterator iterator= fContextType.resolvers(); iterator.hasNext(); ) {
85                         TemplateVariableResolver variable= (TemplateVariableResolver) iterator.next();
86
87                         if (prefix == null || variable.getType().startsWith(prefix))
88                                 proposals.add(new TemplateVariableProposal(variable, offset, length, viewer));
89                 }
90
91                 Collections.sort(proposals, fgTemplateVariableProposalComparator);
92                 return (ICompletionProposal[]) proposals.toArray(new ICompletionProposal[proposals.size()]);
93         }
94
95         /* Guesses the start position of the completion */
96         private int getStart(String string, int end) {
97                 int start= end;
98
99                 if (start >= 1 && string.charAt(start - 1) == '$')
100                         return start - 1;
101                                 
102                 while ((start != 0) && Character.isUnicodeIdentifierPart(string.charAt(start - 1)))
103                         start--;
104
105                 if (start >= 2 && string.charAt(start - 1) == '{' && string.charAt(start - 2) == '$')
106                         return start - 2;
107                         
108                 return end;
109         }
110
111         /*
112          * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
113          */
114         public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
115                 return null;
116         }
117
118         /*
119          * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
120          */
121         public char[] getCompletionProposalAutoActivationCharacters() {
122                 return new char[] {'$'};
123         }
124
125         /*
126          * @see IContentAssistProcessor#getContextInformationAutoActivationCharacters()
127          */
128         public char[] getContextInformationAutoActivationCharacters() {
129                 return null;
130         }
131
132         /*
133          * @see IContentAssistProcessor#getErrorMessage()
134          */
135         public String getErrorMessage() {
136                 return null;
137         }
138
139         /*
140          * @see IContentAssistProcessor#getContextInformationValidator()
141          */
142         public IContextInformationValidator getContextInformationValidator() {
143                 return null;
144         }
145
146 }
147