下面列出了怎么用 com.intellij.codeInsight.completion.impl.CamelHumpMatcher 的API类实例代码及写法,或者点击链接到github查看源代码。
public void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet resultSet) {
if(!Symfony2ProjectComponent.isEnabled(parameters.getPosition())) {
return;
}
PhpIndex phpIndex = PhpIndex.getInstance(parameters.getOriginalFile().getProject());
Map<String, String> entityNamespaces = ServiceXmlParserFactory.getInstance(parameters.getOriginalFile().getProject(), EntityNamesServiceParser.class).getEntityNameMap();
// copied from PhpCompletionUtil::addClassesInNamespace looks the official way to find classes in namespaces
// its a really performance nightmare
Collection<String> names = phpIndex.getAllClassNames(new CamelHumpMatcher(resultSet.getPrefixMatcher().getPrefix()));
for (String name : names) {
Collection<PhpClass> classes = phpIndex.getClassesByName(name);
for(Map.Entry<String, String> entry: entityNamespaces.entrySet()) {
String namespaceFqn = PhpLangUtil.toFQN(entry.getValue());
Collection<PhpClass> filtered = PhpCompletionUtil.filterByNamespace(classes, namespaceFqn);
for (PhpClass phpClass : filtered) {
resultSet.addElement(new PhpClassLookupElement(phpClass, true, PhpClassReferenceInsertHandler.getInstance()));
}
}
}
}
public static CompletionResultSet forValue(
final @NotNull CompletionParameters parameters, final @NotNull CompletionResultSet result) {
final boolean caseSensitive = false;
return getPrefix(parameters.getOffset() - 1, parameters.getOriginalFile().getText())
.map(prefix -> result.withPrefixMatcher(new CamelHumpMatcher(prefix, caseSensitive)))
.orElse(result);
}
public boolean prefixMatches(@NotNull String prefix, @NotNull String variant) {
boolean matches = new CamelHumpMatcher(prefix, false).prefixMatches(variant.replace(' ', '_'));
if (matches && StringUtil.isWhiteSpace(prefix.charAt(prefix.length() - 1))) {
return StringUtil.startsWithIgnoreCase(variant, prefix);
}
return matches;
}
public boolean prefixMatches(@NotNull String prefix, @NotNull String variant) {
boolean matches = new CamelHumpMatcher(prefix, false).prefixMatches(variant.replace(' ', '_'));
if (matches && isWhiteSpace(prefix.charAt(prefix.length() - 1))) {
return startsWithIgnoreCase(variant, prefix);
}
return matches;
}
@RequiredReadAction
@Override
public void fillCompletionVariants(CompletionParameters parameters, CompletionResultSet result) {
PsiFile file = parameters.getOriginalFile();
Document document = PsiDocumentManager.getInstance(file.getProject()).getDocument(file);
if (document != null) {
DataContext dataContext = document.getUserData(CommitMessage.DATA_CONTEXT_KEY);
if (dataContext != null) {
result.stopHere();
if (parameters.getInvocationCount() > 0) {
ChangeList[] lists = dataContext.getData(VcsDataKeys.CHANGE_LISTS);
if (lists != null) {
String prefix = TextFieldWithAutoCompletionListProvider.getCompletionPrefix(parameters);
CompletionResultSet insensitive = result.caseInsensitive().withPrefixMatcher(new CamelHumpMatcher(prefix));
for (ChangeList list : lists) {
for (Change change : list.getChanges()) {
VirtualFile virtualFile = change.getVirtualFile();
if (virtualFile != null) {
LookupElementBuilder element = LookupElementBuilder.create(virtualFile.getName()).
withIcon(VirtualFilePresentation.getAWTIcon(virtualFile));
insensitive.addElement(element);
}
}
}
}
}
}
}
}
@RequiredUIAccess
@Override
public void invoke(@Nonnull Project project, @Nonnull final Editor editor, @Nonnull PsiFile file) {
if (!FileModificationService.getInstance().prepareFileForWrite(file)) return;
LookupManager.getInstance(project).hideActiveLookup();
final CharSequence charsSequence = editor.getDocument().getCharsSequence();
final CompletionData data = computeData(editor, charsSequence);
String currentPrefix = data.myPrefix;
final CompletionState completionState = getCompletionState(editor);
String oldPrefix = completionState.oldPrefix;
CompletionVariant lastProposedVariant = completionState.lastProposedVariant;
if (lastProposedVariant == null || oldPrefix == null || !new CamelHumpMatcher(oldPrefix).isStartMatch(currentPrefix) ||
!currentPrefix.equals(lastProposedVariant.variant)) {
//we are starting over
oldPrefix = currentPrefix;
completionState.oldPrefix = oldPrefix;
lastProposedVariant = null;
}
CompletionVariant nextVariant = computeNextVariant(editor, oldPrefix, lastProposedVariant, data, file);
if (nextVariant == null) return;
int replacementEnd = data.startOffset + data.myWordUnderCursor.length();
editor.getDocument().replaceString(data.startOffset, replacementEnd, nextVariant.variant);
editor.getCaretModel().moveToOffset(data.startOffset + nextVariant.variant.length());
completionState.lastProposedVariant = nextVariant;
highlightWord(nextVariant, project, data);
}
private static void addWordsForEditor(final EditorEx editor,
final CamelHumpMatcher matcher,
final CharSequence chars,
final List<CompletionVariant> words,
final List<CompletionVariant> afterWords, final int caretOffset) {
int startOffset = 0;
TokenProcessor processor = new TokenProcessor() {
@Override
public boolean processToken(int start, int end) {
if ((start > caretOffset || end < caretOffset) && //skip prefix itself
end - start > matcher.getPrefix().length()) {
final String word = chars.subSequence(start, end).toString();
if (matcher.isStartMatch(word)) {
CompletionVariant v = new CompletionVariant(editor, word, start);
if (end > caretOffset) {
afterWords.add(v);
}
else {
words.add(v);
}
}
}
return true;
}
};
processWords(editor, startOffset, processor);
}
@Override
@Nonnull
public PrefixMatcher itemMatcher(@Nonnull LookupElement item) {
if (item instanceof EmptyLookupItem) {
return new CamelHumpMatcher("");
}
return myPresentableArranger.itemMatcher(item);
}
public boolean prefixMatches(@Nonnull String prefix, @Nonnull String variant) {
boolean matches = new CamelHumpMatcher(prefix, false).prefixMatches(variant.replace(' ', '_'));
if (matches && isWhiteSpace(prefix.charAt(prefix.length() - 1))) {
return startsWithIgnoreCase(variant, prefix);
}
return matches;
}