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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.text.template.preferences;
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;
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;
29 public class TemplateVariableProcessor implements IContentAssistProcessor {
31 private static Comparator<TemplateVariableProposal> fgTemplateVariableProposalComparator= new Comparator<TemplateVariableProposal>() {
32 public int compare(TemplateVariableProposal proposal0, TemplateVariableProposal proposal1) {
33 return proposal0.getDisplayString().compareTo(proposal1.getDisplayString());
38 /** the context type */
39 private TemplateContextType fContextType;
42 * Sets the context type.
44 * @param contextType the context type
46 public void setContextType(TemplateContextType contextType) {
47 fContextType= contextType;
51 * Gets the context type.
53 * @return the context type
55 public TemplateContextType getContextType() {
60 * @see IContentAssistProcessor#computeCompletionProposals(ITextViewer, int)
62 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
64 if (fContextType == null)
67 List<TemplateVariableProposal> proposals= new ArrayList<TemplateVariableProposal>();
69 String text= viewer.getDocument().get();
70 int start= getStart(text, documentOffset);
71 int end= documentOffset;
73 String string= text.substring(start, end);
74 int colon= string.indexOf(':');
75 boolean includeBrace= true;
77 String prefix= string;
80 offset= start + colon + 1;
81 prefix= string.substring(colon + 1);
83 int escape= string.indexOf("${"); //$NON-NLS-1$
85 offset= start + escape + 2;
87 prefix= string.substring(escape + 2);
90 if (prefix.equals("$")) //$NON-NLS-1$
91 prefix= ""; //$NON-NLS-1$
93 int length= end - offset;
95 for (Iterator<TemplateVariableResolver> iterator= fContextType.resolvers(); iterator.hasNext(); ) {
96 TemplateVariableResolver variable= iterator.next();
98 if (variable.getType().startsWith(prefix))
99 proposals.add(new TemplateVariableProposal(variable, offset, length, viewer, includeBrace));
102 Collections.sort(proposals, fgTemplateVariableProposalComparator);
103 return proposals.toArray(new ICompletionProposal[proposals.size()]);
106 /* Guesses the start position of the completion */
107 private int getStart(String string, int end) {
110 if (start >= 1 && string.charAt(start - 1) == '$')
113 while ((start != 0) && Character.isUnicodeIdentifierPart(string.charAt(start - 1)))
116 if (start >= 1 && string.charAt(start - 1) == ':') {
118 while ((start != 0) && Character.isUnicodeIdentifierPart(string.charAt(start - 1)))
122 if (start >= 2 && string.charAt(start - 1) == '{' && string.charAt(start - 2) == '$')
129 * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
131 public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
136 * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
138 public char[] getCompletionProposalAutoActivationCharacters() {
139 return new char[] {'$'};
143 * @see IContentAssistProcessor#getContextInformationAutoActivationCharacters()
145 public char[] getContextInformationAutoActivationCharacters() {
150 * @see IContentAssistProcessor#getErrorMessage()
152 public String getErrorMessage() {
157 * @see IContentAssistProcessor#getContextInformationValidator()
159 public IContextInformationValidator getContextInformationValidator() {