String (@NotNull Editor editor, @NotNull PsiFile file, @NotNull PsiElement leaf, @Nullable PsiReference ref) { if (isAfterError(file, leaf)) { return null; } String leafText = ref != null ? ref.getRangeInElement().substring(ref.getElement().getText()) : leaf.getText(); if (leafText.isEmpty() || !Character.isLetter(leafText.charAt(0)) || leaf instanceof PsiWhiteSpace || PsiTreeUtil.getNonStrictParentOfType(leaf, PsiComment.class) != null) { return null; } IElementType leafType = PsiUtilCore.getElementType(leaf); if ("org.intellij.lang.regexp.RegExpElementType".equals(leafType.getClass().getName())) { if (leafText.length() == 1) { return null; // regexp has a token for each character: not interesting (and no completion) } if ("NAME".equals(leafType.toString())) { return null; // group name, no completion expected } } if (isDeclarationName(editor, file, leaf, ref)) return null; if (ref != null) { PsiElement target = getValidResolveResult(ref); if (target == null || !shouldSuggestReferenceText(ref, target)) return null; if (ref instanceof PsiMultiReference) { for (PsiReference ref1 : ((PsiMultiReference)ref).getReferences()) { if (target.getClass().isInstance(ref1.resolve()) && !shouldSuggestReferenceText(ref1, target)) return null; } } } else { if (!SyntaxTraverser.psiTraverser(file).filter(PsiErrorElement.class).isEmpty()) { return null; } if (LanguageWordCompletion.INSTANCE.isEnabledIn(leafType)) { // Looks like plain text. And the word under caret is excluded from word completion anyway. return null; } if (!shouldSuggestNonReferenceLeafText(leaf)) return null; } return leafText; }