Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9e1623a1684454cef48e0a32b508b06b3d354962 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*******************************************************************************
 * Copyright (c) 2000, 2015 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.examples.templateeditor.template;

import java.util.Arrays;
import java.util.Comparator;

import org.eclipse.jface.text.templates.TemplateContext;
import org.eclipse.jface.text.templates.TemplateVariableResolver;

/**
 * Looks up existing ant variables and proposes them. The proposals are sorted by
 * their prefix-likeness with the variable type.
 */
public class AntVariableResolver extends TemplateVariableResolver {
	@Override
	protected String[] resolveAll(TemplateContext context) {
		String[] proposals= new String[] { "${srcDir}", "${dstDir}" }; //$NON-NLS-1$ //$NON-NLS-2$

		Arrays.sort(proposals, new Comparator<String>() {

			@Override
			public int compare(String o1, String o2) {
				return getCommonPrefixLength(getType(), o2) - getCommonPrefixLength(getType(), o1);
			}

			private int getCommonPrefixLength(String type, String var) {
				int i= 0;
				CharSequence vSeq= var.subSequence(2, var.length() - 1); // strip away ${}
				while (i < type.length() && i < vSeq.length())
					if (Character.toLowerCase(type.charAt(i)) == Character.toLowerCase(vSeq.charAt(i)))
						i++;
					else
						break;
				return i;
			}
		});

		return proposals;
	}
}

Back to the top