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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.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 fgTemplateVariableProposalComparator= new Comparator() {
32 public int compare(Object arg0, Object arg1) {
33 TemplateVariableProposal proposal0= (TemplateVariableProposal) arg0;
34 TemplateVariableProposal proposal1= (TemplateVariableProposal) arg1;
36 return proposal0.getDisplayString().compareTo(proposal1.getDisplayString());
39 public boolean equals(Object arg0) {
45 /** the context type */
46 private TemplateContextType fContextType;
49 * Sets the context type.
51 public void setContextType(TemplateContextType contextType) {
52 fContextType= contextType;
56 * Gets the context type.
58 public TemplateContextType getContextType() {
63 * @see IContentAssistProcessor#computeCompletionProposals(ITextViewer, int)
65 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
67 if (fContextType == null)
70 List proposals= new ArrayList();
72 String text= viewer.getDocument().get();
73 int start= getStart(text, documentOffset);
74 int end= documentOffset;
76 String string= text.substring(start, end);
77 String prefix= (string.length() >= 2)
82 int length= end - start;
84 for (Iterator iterator= fContextType.resolvers(); iterator.hasNext(); ) {
85 TemplateVariableResolver variable= (TemplateVariableResolver) iterator.next();
87 if (prefix == null || variable.getType().startsWith(prefix))
88 proposals.add(new TemplateVariableProposal(variable, offset, length, viewer));
91 Collections.sort(proposals, fgTemplateVariableProposalComparator);
92 return (ICompletionProposal[]) proposals.toArray(new ICompletionProposal[proposals.size()]);
95 /* Guesses the start position of the completion */
96 private int getStart(String string, int end) {
99 if (start >= 1 && string.charAt(start - 1) == '$')
102 while ((start != 0) && Character.isUnicodeIdentifierPart(string.charAt(start - 1)))
105 if (start >= 2 && string.charAt(start - 1) == '{' && string.charAt(start - 2) == '$')
112 * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
114 public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
119 * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
121 public char[] getCompletionProposalAutoActivationCharacters() {
122 return new char[] {'$'};
126 * @see IContentAssistProcessor#getContextInformationAutoActivationCharacters()
128 public char[] getContextInformationAutoActivationCharacters() {
133 * @see IContentAssistProcessor#getErrorMessage()
135 public String getErrorMessage() {
140 * @see IContentAssistProcessor#getContextInformationValidator()
142 public IContextInformationValidator getContextInformationValidator() {