]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/text/template/preferences/TemplateVariableProcessor.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / text / template / preferences / TemplateVariableProcessor.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 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  *******************************************************************************/
11 package org.eclipse.jdt.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<TemplateVariableProposal> fgTemplateVariableProposalComparator= new Comparator<TemplateVariableProposal>() {
32                 public int compare(TemplateVariableProposal proposal0, TemplateVariableProposal proposal1) {
33                         return proposal0.getDisplayString().compareTo(proposal1.getDisplayString());
34                 }
35         };
36
37
38         /** the context type */
39         private TemplateContextType fContextType;
40
41         /**
42          * Sets the context type.
43          *
44          * @param contextType the context type
45          */
46         public void setContextType(TemplateContextType contextType) {
47                 fContextType= contextType;
48         }
49
50         /**
51          * Gets the context type.
52          *
53          * @return the context type
54          */
55         public TemplateContextType getContextType() {
56                 return fContextType;
57         }
58
59         /*
60          * @see IContentAssistProcessor#computeCompletionProposals(ITextViewer, int)
61          */
62         public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer,     int documentOffset) {
63
64                 if (fContextType == null)
65                         return null;
66
67                 List<TemplateVariableProposal> proposals= new ArrayList<TemplateVariableProposal>();
68
69                 String text= viewer.getDocument().get();
70                 int start= getStart(text, documentOffset);
71                 int end= documentOffset;
72
73                 String string= text.substring(start, end);
74                 int colon= string.indexOf(':');
75                 boolean includeBrace= true;
76                 int offset= start;
77                 String prefix= string;
78                 if (colon != -1) {
79                         includeBrace= false;
80                         offset= start + colon + 1;
81                         prefix= string.substring(colon + 1);
82                 } else {
83                         int escape= string.indexOf("${"); //$NON-NLS-1$
84                         if (escape != -1) {
85                                 offset= start + escape + 2;
86                                 includeBrace= false;
87                                 prefix= string.substring(escape + 2);
88                         }
89                 }
90                 if (prefix.equals("$")) //$NON-NLS-1$
91                         prefix= ""; //$NON-NLS-1$
92
93                 int length= end - offset;
94
95                 for (Iterator<TemplateVariableResolver> iterator= fContextType.resolvers(); iterator.hasNext(); ) {
96                         TemplateVariableResolver variable= iterator.next();
97
98                         if (variable.getType().startsWith(prefix))
99                                 proposals.add(new TemplateVariableProposal(variable, offset, length, viewer, includeBrace));
100                 }
101
102                 Collections.sort(proposals, fgTemplateVariableProposalComparator);
103                 return proposals.toArray(new ICompletionProposal[proposals.size()]);
104         }
105
106         /* Guesses the start position of the completion */
107         private int getStart(String string, int end) {
108                 int start= end;
109
110                 if (start >= 1 && string.charAt(start - 1) == '$')
111                         return start - 1;
112
113                 while ((start != 0) && Character.isUnicodeIdentifierPart(string.charAt(start - 1)))
114                         start--;
115
116                 if (start >= 1 && string.charAt(start - 1) == ':') {
117                         start--;
118                         while ((start != 0) && Character.isUnicodeIdentifierPart(string.charAt(start - 1)))
119                                 start--;
120                 }
121
122                 if (start >= 2 && string.charAt(start - 1) == '{' && string.charAt(start - 2) == '$')
123                         return start - 2;
124
125                 return end;
126         }
127
128         /*
129          * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
130          */
131         public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
132                 return null;
133         }
134
135         /*
136          * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
137          */
138         public char[] getCompletionProposalAutoActivationCharacters() {
139                 return new char[] {'$'};
140         }
141
142         /*
143          * @see IContentAssistProcessor#getContextInformationAutoActivationCharacters()
144          */
145         public char[] getContextInformationAutoActivationCharacters() {
146                 return null;
147         }
148
149         /*
150          * @see IContentAssistProcessor#getErrorMessage()
151          */
152         public String getErrorMessage() {
153                 return null;
154         }
155
156         /*
157          * @see IContentAssistProcessor#getContextInformationValidator()
158          */
159         public IContextInformationValidator getContextInformationValidator() {
160                 return null;
161         }
162
163 }
164