1
/*******************************************************************************
2
* Copyright (c) 2011 Jens Elmenthaler 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
* Jens Elmenthaler - http://bugs.eclipse.org/173458 (camel case completion)
11
*******************************************************************************/
12
package org.eclipse.cdt.internal.core.parser.util;
14
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
15
import org.eclipse.cdt.core.parser.util.IContentAssistMatcher;
16
import org.eclipse.cdt.core.parser.util.SegmentMatcher;
19
* The facade to the pattern matching algorithms of content assist.
21
* @author Jens Elmenthaler
23
* @noextend This class is not intended to be subclassed by clients.
26
public class ContentAssistMatcherFactory {
28
private static ContentAssistMatcherFactory instance = null;
30
private boolean showCamelCaseMatches = true;
32
private static class CamelCaseMatcher implements IContentAssistMatcher {
34
private final SegmentMatcher matcher;
36
public CamelCaseMatcher(char[] pattern) {
37
matcher = new SegmentMatcher(pattern);
40
public char[] getPrefixForBinarySearch() {
41
return matcher.getPrefixForBinarySearch();
44
public boolean matchRequiredAfterBinarySearch() {
45
return matcher.matchRequiredAfterBinarySearch();
48
public boolean match(char[] name) {
49
return matcher.match(name);
53
private static class PrefixMatcher implements IContentAssistMatcher {
55
private final char[] prefix;
57
public PrefixMatcher(char[] prefix) {
61
public char[] getPrefixForBinarySearch() {
65
public boolean matchRequiredAfterBinarySearch() {
69
public boolean match(char[] name) {
70
return CharArrayUtils.equals(name, 0, prefix.length, prefix, true);
75
private ContentAssistMatcherFactory() {
79
public static synchronized ContentAssistMatcherFactory getInstance() {
80
if (instance == null) {
81
instance = new ContentAssistMatcherFactory();
88
* This function is not supposed to be called from any functions except
89
* for ContentAssistMatcherPreference.updateOnPreferences.
91
* @param showCamelCaseMatches
93
public synchronized void setShowCamelCaseMatches(boolean showCamelCaseMatches) {
94
this.showCamelCaseMatches = showCamelCaseMatches;
99
* @return <code>true</code> if showCamelCaseMatches is set from the content assist preference page.
101
public boolean getShowCamelCaseMatches() {
102
return showCamelCaseMatches;
106
* @param pattern The pattern for which to create a matcher.
107
* @return A suitable matcher.
109
public synchronized IContentAssistMatcher createMatcher(char[] pattern) {
111
return showCamelCaseMatches ? new CamelCaseMatcher(pattern) : new PrefixMatcher(pattern);
115
* @param pattern The pattern for which to create a matcher.
116
* @return A suitable matcher.
118
public IContentAssistMatcher createMatcher(String pattern) {
119
return createMatcher(pattern.toCharArray());
123
* A helper method to match a name against the pattern typed by the user.
124
* If you need to match many names at once against the same pattern, use
125
* {@link #createMatcher(char[])} and re-use the returned matcher instead.
127
* @param pattern The user provided pattern.
128
* @param name The name to match against the pattern.
130
* @return <code>true</code> if the name matches the given pattern.
132
public boolean match(char[] pattern, char[] name) {
133
return createMatcher(pattern).match(name);