~ubuntu-branches/debian/sid/eclipse-cdt/sid

« back to all changes in this revision

Viewing changes to core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/util/ContentAssistMatcherFactory.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2011-10-06 21:15:04 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20111006211504-8dutmljjih0zikfv
Tags: 8.0.1-1
* New upstream release.
* Split the JNI packages into a separate architecture dependent
  package and made eclipse-cdt architecture independent.
* Install JNI libraries into multiarch aware location
* Bumped Standards-Version to 3.9.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
7
 *
 
8
 * Contributors:
 
9
 *     Jens Elmenthaler - http://bugs.eclipse.org/173458 (camel case completion)
 
10
 *     IBM Corporation
 
11
 *******************************************************************************/
 
12
package org.eclipse.cdt.internal.core.parser.util;
 
13
 
 
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;
 
17
 
 
18
/**
 
19
 * The facade to the pattern matching algorithms of content assist.
 
20
 *  
 
21
 * @author Jens Elmenthaler
 
22
 * 
 
23
 * @noextend This class is not intended to be subclassed by clients.
 
24
 * @since 5.3
 
25
 */
 
26
public class ContentAssistMatcherFactory {
 
27
 
 
28
        private static ContentAssistMatcherFactory instance = null;
 
29
        
 
30
        private boolean showCamelCaseMatches = true;
 
31
                
 
32
        private static class CamelCaseMatcher implements IContentAssistMatcher {
 
33
 
 
34
                private final SegmentMatcher matcher;
 
35
                
 
36
                public CamelCaseMatcher(char[] pattern) {
 
37
                        matcher = new SegmentMatcher(pattern);
 
38
                }
 
39
                
 
40
                public char[] getPrefixForBinarySearch() {
 
41
                        return matcher.getPrefixForBinarySearch();
 
42
                }
 
43
                
 
44
                public boolean matchRequiredAfterBinarySearch() {
 
45
                        return matcher.matchRequiredAfterBinarySearch();
 
46
                }
 
47
 
 
48
                public boolean match(char[] name) {
 
49
                        return matcher.match(name);
 
50
                }
 
51
        }
 
52
        
 
53
        private static class PrefixMatcher implements IContentAssistMatcher {
 
54
 
 
55
                private final char[] prefix;
 
56
                
 
57
                public PrefixMatcher(char[] prefix) {
 
58
                        this.prefix = prefix;
 
59
                }
 
60
                
 
61
                public char[] getPrefixForBinarySearch() {
 
62
                        return prefix;
 
63
                }
 
64
 
 
65
                public boolean matchRequiredAfterBinarySearch() {
 
66
                        return false;
 
67
                }
 
68
 
 
69
                public boolean match(char[] name) {
 
70
                        return CharArrayUtils.equals(name, 0, prefix.length, prefix, true);
 
71
                }
 
72
                
 
73
        }
 
74
        
 
75
        private ContentAssistMatcherFactory() {
 
76
                
 
77
        }
 
78
 
 
79
        public static synchronized ContentAssistMatcherFactory getInstance() {
 
80
                if (instance == null) {
 
81
                        instance = new ContentAssistMatcherFactory();
 
82
                }
 
83
                
 
84
                return instance;
 
85
        }
 
86
        
 
87
        /**
 
88
     * This function is not supposed to be called from any functions except
 
89
     * for ContentAssistMatcherPreference.updateOnPreferences.
 
90
     *  
 
91
     * @param showCamelCaseMatches
 
92
     */
 
93
        public synchronized  void setShowCamelCaseMatches(boolean showCamelCaseMatches) {
 
94
                this.showCamelCaseMatches = showCamelCaseMatches;
 
95
        }
 
96
        
 
97
        /**
 
98
         * 
 
99
         * @return <code>true</code> if showCamelCaseMatches is set from the content assist preference page.
 
100
         */
 
101
        public boolean getShowCamelCaseMatches() {
 
102
                return showCamelCaseMatches;
 
103
        }
 
104
        
 
105
        /**
 
106
         * @param pattern The pattern for which to create a matcher.
 
107
         * @return A suitable matcher.
 
108
         */
 
109
        public synchronized IContentAssistMatcher createMatcher(char[] pattern) {
 
110
                
 
111
                return showCamelCaseMatches ? new CamelCaseMatcher(pattern) : new PrefixMatcher(pattern);
 
112
        }
 
113
 
 
114
        /**
 
115
         * @param pattern The pattern for which to create a matcher.
 
116
         * @return A suitable matcher.
 
117
         */
 
118
        public IContentAssistMatcher createMatcher(String pattern) {
 
119
                return createMatcher(pattern.toCharArray());
 
120
        }
 
121
 
 
122
        /**
 
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.  
 
126
         * 
 
127
         * @param pattern The user provided pattern.
 
128
         * @param name The name to match against the pattern.
 
129
         * 
 
130
         * @return <code>true</code> if the name matches the given pattern.
 
131
         */
 
132
        public boolean match(char[] pattern, char[] name) {
 
133
                return createMatcher(pattern).match(name);
 
134
        }
 
135
}