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;
27 public class TemplateVariableProcessor implements IContentAssistProcessor {
29 private static Comparator fgTemplateVariableProposalComparator = new Comparator() {
30 public int compare(Object arg0, Object arg1) {
31 TemplateVariableProposal proposal0 = (TemplateVariableProposal) arg0;
32 TemplateVariableProposal proposal1 = (TemplateVariableProposal) arg1;
34 return proposal0.getDisplayString().compareTo(
35 proposal1.getDisplayString());
38 public boolean equals(Object arg0) {
43 /** the context type */
44 private TemplateContextType fContextType;
47 * Sets the context type.
49 public void setContextType(TemplateContextType contextType) {
50 fContextType = contextType;
54 * Gets the context type.
56 public TemplateContextType getContextType() {
61 * @see IContentAssistProcessor#computeCompletionProposals(ITextViewer, int)
63 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer,
66 if (fContextType == null)
69 List proposals = new ArrayList();
71 String text = viewer.getDocument().get();
72 int start = getStart(text, documentOffset);
73 int end = documentOffset;
75 String string = text.substring(start, end);
76 String prefix = (string.length() >= 2) ? string.substring(2) : null;
79 int length = end - start;
81 for (Iterator iterator = fContextType.resolvers(); iterator.hasNext();) {
82 TemplateVariableResolver variable = (TemplateVariableResolver) iterator
85 if (prefix == null || variable.getType().startsWith(prefix))
86 proposals.add(new TemplateVariableProposal(variable, offset,
90 Collections.sort(proposals, fgTemplateVariableProposalComparator);
91 return (ICompletionProposal[]) proposals
92 .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) == '$')
103 && Character.isUnicodeIdentifierPart(string.charAt(start - 1)))
106 if (start >= 2 && string.charAt(start - 1) == '{'
107 && string.charAt(start - 2) == '$')
114 * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
116 public IContextInformation[] computeContextInformation(ITextViewer viewer,
117 int documentOffset) {
122 * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
124 public char[] getCompletionProposalAutoActivationCharacters() {
125 return new char[] { '$' };
129 * @see IContentAssistProcessor#getContextInformationAutoActivationCharacters()
131 public char[] getContextInformationAutoActivationCharacters() {
136 * @see IContentAssistProcessor#getErrorMessage()
138 public String getErrorMessage() {
143 * @see IContentAssistProcessor#getContextInformationValidator()
145 public IContextInformationValidator getContextInformationValidator() {