Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 73723792566339866d9a82b391bd45022ee41be3 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*******************************************************************************
 * Copyright (c) 2011 SAP AG
 * 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:
 *     Lazar Kirchev, SAP AG - initial API and implementation
 *******************************************************************************/

package org.eclipse.equinox.console.completion;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.eclipse.equinox.console.completion.common.Completer;

/**
 * This class provides completion for arbitrary strings
 *
 */
public class StringsCompleter implements Completer {

	private Set<String> strings;
	private boolean isCaseSensitive;
	
	public StringsCompleter(Set<String> strings, boolean isCaseSensitive) {
		this.strings = strings;
		this.isCaseSensitive = isCaseSensitive;
	}
	
	public Map<String, Integer> getCandidates(String buffer, int cursor) {
		String currentToken = CommandLineParser.getCurrentToken(buffer, cursor);
		if (currentToken == null) {
			return new HashMap<String, Integer>();
		}
		if (!isCaseSensitive) {
			currentToken = currentToken.toLowerCase();
		}
		
		int startIndex = cursor - currentToken.length();
		
		// if currentToken is empty string, then there is nothing to complete
		// the only exception is if the previous character is $, which signifies
		// that a variable name is expected; in this case all strings will be 
		// returned as candidates
		if(currentToken.equals("") && buffer.charAt(startIndex - 1) != '$') {
			return new HashMap<String, Integer>();
		}
		
		Map<String, Integer> result = new HashMap<String, Integer>();

		for(String candidate : strings) {
			if (isCaseSensitive) {
				if (candidate.startsWith(currentToken)) {
					result.put(candidate, startIndex);
				}
			} else {
				if (candidate.toLowerCase().startsWith(currentToken)) {
					result.put(candidate, startIndex);
				}
			}
		}
		
		return result;
	}

}

Back to the top