Java Code Examples for org.eclipse.jdt.core.compiler.CharOperation#camelCaseMatch()

The following examples show how to use org.eclipse.jdt.core.compiler.CharOperation#camelCaseMatch() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractJavaCompletionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Matches <code>prefix</code> against <code>string</code> and replaces the matched region
 * by prefix. Case is preserved as much as possible. This method returns <code>string</code> if camel case completion
 * is disabled. Examples when camel case completion is enabled:
 * <ul>
 * <li>getCamelCompound("NuPo", "NullPointerException") -> "NuPointerException"</li>
 * <li>getCamelCompound("NuPoE", "NullPointerException") -> "NuPoException"</li>
 * <li>getCamelCompound("hasCod", "hashCode") -> "hasCode"</li>
 * </ul>
 *
 * @param prefix the prefix to match against
 * @param string the string to match
 * @return a compound of prefix and any postfix taken from <code>string</code>
 * @since 3.2
 */
protected final String getCamelCaseCompound(String prefix, String string) {
	if (prefix.length() > string.length())
		return string;

	// a normal prefix - no camel case logic at all
	String start= string.substring(0, prefix.length());
	if (start.equalsIgnoreCase(prefix))
		return string;

	final char[] patternChars= prefix.toCharArray();
	final char[] stringChars= string.toCharArray();

	for (int i= 1; i <= stringChars.length; i++)
		if (CharOperation.camelCaseMatch(patternChars, 0, patternChars.length, stringChars, 0, i))
			return prefix + string.substring(i);

	// Not a camel case match at all.
	// This should not happen -> stay with the default behavior
	return string;
}
 
Example 2
Source File: UnresolvedReferenceNameFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void acceptName(char[] name) {
	// the null check is added to fix bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=166570
	if (name == null) return;

	if (!CharOperation.prefixEquals(this.completionEngine.completionToken, name, false /* ignore case */)
			&& !(this.completionEngine.options.camelCaseMatch && CharOperation.camelCaseMatch(this.completionEngine.completionToken, name))) return;

	if (this.acceptedNames.includes(name)) return;

	this.acceptedNames.add(name);

	// accept result
	this.requestor.acceptName(name);
}
 
Example 3
Source File: Index.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isMatch(char[] pattern, char[] word, int matchRule) {
	if (pattern == null) return true;
	int patternLength = pattern.length;
	int wordLength = word.length;
	if (patternLength == 0) return matchRule != SearchPattern.R_EXACT_MATCH;
	if (wordLength == 0) return (matchRule & SearchPattern.R_PATTERN_MATCH) != 0 && patternLength == 1 && pattern[0] == '*';

	// need to mask some bits of pattern rule (bug 79790)
	switch(matchRule & MATCH_RULE_INDEX_MASK) {
		case SearchPattern.R_EXACT_MATCH :
			return patternLength == wordLength && CharOperation.equals(pattern, word, false);
		case SearchPattern.R_PREFIX_MATCH :
			return patternLength <= wordLength && CharOperation.prefixEquals(pattern, word, false);
		case SearchPattern.R_PATTERN_MATCH :
			return CharOperation.match(pattern, word, false);
		case SearchPattern.R_CAMELCASE_MATCH:
		// same part count is not activated because index key may have uppercase letters after the type name
		case SearchPattern.R_CAMELCASE_SAME_PART_COUNT_MATCH:
			if (CharOperation.camelCaseMatch(pattern, word, false)) {
				return true;
			}
			return patternLength <= wordLength && CharOperation.prefixEquals(pattern, word, false);
		case SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE :
			return pattern[0] == word[0] && patternLength == wordLength && CharOperation.equals(pattern, word);
		case SearchPattern.R_PREFIX_MATCH | SearchPattern.R_CASE_SENSITIVE :
			return pattern[0] == word[0] && patternLength <= wordLength && CharOperation.prefixEquals(pattern, word);
		case SearchPattern.R_PATTERN_MATCH | SearchPattern.R_CASE_SENSITIVE :
			return CharOperation.match(pattern, word, true);
		case SearchPattern.R_CAMELCASE_MATCH | SearchPattern.R_CASE_SENSITIVE :
		// same part count is not activated because index key may have uppercase letters after the type name
		case SearchPattern.R_CAMELCASE_SAME_PART_COUNT_MATCH | SearchPattern.R_CASE_SENSITIVE :
			return (pattern[0] == word[0] && CharOperation.camelCaseMatch(pattern, word, false));
	}
	return false;
}
 
Example 4
Source File: MethodFilter.java    From jdt-codemining with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Return how the given name matches the given pattern.
 * 
 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=79866"
 *
 * @param pattern
 * @param name
 * @return Possible values are:
 *         <ul>
 *         <li>{@link #ACCURATE_MATCH}</li>
 *         <li>{@link #IMPOSSIBLE_MATCH}</li>
 *         <li>{@link #POSSIBLE_MATCH} which may be flavored with following
 *         values:
 *         <ul>
 *         <li>{@link #EXACT_FLAVOR}: Given name is equals to pattern</li>
 *         <li>{@link #PREFIX_FLAVOR}: Given name prefix equals to pattern</li>
 *         <li>{@link #CAMELCASE_FLAVOR}: Given name matches pattern as Camel
 *         Case</li>
 *         <li>{@link #PATTERN_FLAVOR}: Given name matches pattern as Pattern
 *         (i.e. using '*' and '?' characters)</li>
 *         </ul>
 *         </li>
 *         </ul>
 */
protected boolean matchNameValue(char[] pattern, char[] name) {
	if (pattern == null)
		return true; // ACCURATE_MATCH; // null is as if it was "*"
	if (name == null)
		return false; // IMPOSSIBLE_MATCH; // cannot match null name
	if (name.length == 0) { // empty name
		if (pattern.length == 0) { // can only matches empty pattern
			return true; // ACCURATE_MATCH;
		}
		return false; // IMPOSSIBLE_MATCH;
	} else if (pattern.length == 0) {
		return false; // IMPOSSIBLE_MATCH; // need to have both name and pattern length==0 to be
						// accurate
	}
	boolean matchFirstChar = !this.isCaseSensitive || pattern[0] == name[0];
	boolean sameLength = pattern.length == name.length;
	boolean canBePrefix = name.length >= pattern.length;
	switch (this.matchMode) {
	case SearchPattern.R_EXACT_MATCH:
		if (sameLength && matchFirstChar && CharOperation.equals(pattern, name, this.isCaseSensitive)) {
			return true; // POSSIBLE_MATCH | EXACT_FLAVOR;
		}
		break;

	case SearchPattern.R_PREFIX_MATCH:
		if (canBePrefix && matchFirstChar && CharOperation.prefixEquals(pattern, name, this.isCaseSensitive)) {
			return true; // POSSIBLE_MATCH;
		}
		break;

	case SearchPattern.R_PATTERN_MATCH:
		// TODO_PERFS (frederic) Not sure this lowercase is necessary
		if (!this.isCaseSensitive) {
			pattern = CharOperation.toLowerCase(pattern);
		}
		if (CharOperation.match(pattern, name, this.isCaseSensitive)) {
			return true; // POSSIBLE_MATCH;
		}
		break;

	case SearchPattern.R_REGEXP_MATCH:
		if (Pattern.matches(new String(pattern), new String(name))) {
			return true; // POSSIBLE_MATCH;
		}
		break;

	case SearchPattern.R_CAMELCASE_MATCH:
		if (CharOperation.camelCaseMatch(pattern, name, false)) {
			return true; // POSSIBLE_MATCH;
		}
		// only test case insensitive as CamelCase same part count already verified
		// prefix case sensitive
		if (!this.isCaseSensitive && CharOperation.prefixEquals(pattern, name, false)) {
			return true; // POSSIBLE_MATCH;
		}
		break;

	case SearchPattern.R_CAMELCASE_SAME_PART_COUNT_MATCH:
		if (CharOperation.camelCaseMatch(pattern, name, true)) {
			return true; // POSSIBLE_MATCH;
		}
		break;
	}
	return false; // IMPOSSIBLE_MATCH;
}
 
Example 5
Source File: PackageReferenceLocator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
protected int matchLevelForTokens(char[][] tokens) {
	if (this.pattern.pkgName == null) return ACCURATE_MATCH;

	switch (this.matchMode) {
		case SearchPattern.R_EXACT_MATCH:
		case SearchPattern.R_PREFIX_MATCH:
			if (CharOperation.prefixEquals(this.pattern.pkgName, CharOperation.concatWith(tokens, '.'), this.isCaseSensitive)) {
				return POSSIBLE_MATCH;
			}
			break;

		case SearchPattern.R_PATTERN_MATCH:
			char[] patternName = this.pattern.pkgName[this.pattern.pkgName.length - 1] == '*'
				? this.pattern.pkgName
				: CharOperation.concat(this.pattern.pkgName, ".*".toCharArray()); //$NON-NLS-1$
			if (CharOperation.match(patternName, CharOperation.concatWith(tokens, '.'), this.isCaseSensitive)) {
				return POSSIBLE_MATCH;
			}
			break;

		case SearchPattern.R_REGEXP_MATCH :
			// TODO (frederic) implement regular expression match
			break;

		case SearchPattern.R_CAMELCASE_MATCH:
			char[] packageName = CharOperation.concatWith(tokens, '.');
			if (CharOperation.camelCaseMatch(this.pattern.pkgName, packageName, false)) {
				return POSSIBLE_MATCH;
			}
			// only test case insensitive as CamelCase already verified prefix case sensitive
			if (!this.isCaseSensitive && CharOperation.prefixEquals(this.pattern.pkgName, packageName, false)) {
				return POSSIBLE_MATCH;
			}
			break;

		case SearchPattern.R_CAMELCASE_SAME_PART_COUNT_MATCH:
			if (CharOperation.camelCaseMatch(this.pattern.pkgName, CharOperation.concatWith(tokens, '.'), true)) {
				return POSSIBLE_MATCH;
			}
			break;
	}
	return IMPOSSIBLE_MATCH;
}
 
Example 6
Source File: BasicSearchEngine.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
boolean match(char patternTypeSuffix, char[] patternPkg, int matchRulePkg, char[] patternTypeName, int matchRuleType, int typeKind, char[] pkg, char[] typeName) {
	switch(patternTypeSuffix) {
		case IIndexConstants.CLASS_SUFFIX :
			if (typeKind != TypeDeclaration.CLASS_DECL) return false;
			break;
		case IIndexConstants.CLASS_AND_INTERFACE_SUFFIX:
			if (typeKind != TypeDeclaration.CLASS_DECL && typeKind != TypeDeclaration.INTERFACE_DECL) return false;
			break;
		case IIndexConstants.CLASS_AND_ENUM_SUFFIX:
			if (typeKind != TypeDeclaration.CLASS_DECL && typeKind != TypeDeclaration.ENUM_DECL) return false;
			break;
		case IIndexConstants.INTERFACE_SUFFIX :
			if (typeKind != TypeDeclaration.INTERFACE_DECL) return false;
			break;
		case IIndexConstants.INTERFACE_AND_ANNOTATION_SUFFIX:
			if (typeKind != TypeDeclaration.INTERFACE_DECL && typeKind != TypeDeclaration.ANNOTATION_TYPE_DECL) return false;
			break;
		case IIndexConstants.ENUM_SUFFIX :
			if (typeKind != TypeDeclaration.ENUM_DECL) return false;
			break;
		case IIndexConstants.ANNOTATION_TYPE_SUFFIX :
			if (typeKind != TypeDeclaration.ANNOTATION_TYPE_DECL) return false;
			break;
		case IIndexConstants.TYPE_SUFFIX : // nothing
	}

	boolean isPkgCaseSensitive = (matchRulePkg & SearchPattern.R_CASE_SENSITIVE) != 0;
	if (patternPkg != null && !CharOperation.equals(patternPkg, pkg, isPkgCaseSensitive))
		return false;
	
	boolean isCaseSensitive = (matchRuleType & SearchPattern.R_CASE_SENSITIVE) != 0;
	if (patternTypeName != null) {
		boolean isCamelCase = (matchRuleType & (SearchPattern.R_CAMELCASE_MATCH | SearchPattern.R_CAMELCASE_SAME_PART_COUNT_MATCH)) != 0;
		int matchMode = matchRuleType & JavaSearchPattern.MATCH_MODE_MASK;
		if (!isCaseSensitive && !isCamelCase) {
			patternTypeName = CharOperation.toLowerCase(patternTypeName);
		}
		boolean matchFirstChar = !isCaseSensitive || patternTypeName[0] == typeName[0];
		switch(matchMode) {
			case SearchPattern.R_EXACT_MATCH :
				return matchFirstChar && CharOperation.equals(patternTypeName, typeName, isCaseSensitive);
			case SearchPattern.R_PREFIX_MATCH :
				return matchFirstChar && CharOperation.prefixEquals(patternTypeName, typeName, isCaseSensitive);
			case SearchPattern.R_PATTERN_MATCH :
				return CharOperation.match(patternTypeName, typeName, isCaseSensitive);
			case SearchPattern.R_REGEXP_MATCH :
				// TODO (frederic) implement regular expression match
				break;
			case SearchPattern.R_CAMELCASE_MATCH:
				if (matchFirstChar && CharOperation.camelCaseMatch(patternTypeName, typeName, false)) {
					return true;
				}
				return !isCaseSensitive && matchFirstChar && CharOperation.prefixEquals(patternTypeName, typeName, false);
			case SearchPattern.R_CAMELCASE_SAME_PART_COUNT_MATCH:
				return matchFirstChar && CharOperation.camelCaseMatch(patternTypeName, typeName, true);
		}
	}
	return true;

}
 
Example 7
Source File: AbstractJavaCompletionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Case insensitive comparison of <code>prefix</code> with the start of <code>string</code>.
 *
 * @param prefix the prefix
 * @param string  the string to look for the prefix
 * @return <code>true</code> if the string begins with the given prefix and
 *			<code>false</code> if <code>prefix</code> is longer than <code>string</code>
 *			or the string doesn't start with the given prefix
 * @since 3.2
 */
protected boolean isPrefix(String prefix, String string) {
	if (prefix == null || string ==null || prefix.length() > string.length())
		return false;
	String start= string.substring(0, prefix.length());
	return start.equalsIgnoreCase(prefix) || isCamelCaseMatching() && CharOperation.camelCaseMatch(prefix.toCharArray(), string.toCharArray());
}