下面列出了com.intellij.psi.PsiInvalidElementAccessException#com.intellij.psi.PsiNameIdentifierOwner 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
static void addError(
AnnotationHolder holder, SpecModelValidationError error, List<IntentionAction> fixes) {
PsiElement errorElement = (PsiElement) error.element;
Annotation errorAnnotation =
holder.createErrorAnnotation(
Optional.of(errorElement)
.filter(element -> element instanceof PsiClass || element instanceof PsiMethod)
.map(PsiNameIdentifierOwner.class::cast)
.map(PsiNameIdentifierOwner::getNameIdentifier)
.orElse(errorElement),
error.message);
if (!fixes.isEmpty()) {
for (IntentionAction fix : fixes) {
errorAnnotation.registerFix(fix);
}
}
}
@NotNull
public static Collection<PsiNameIdentifierOwner> getExpressions(@NotNull PsiFile file, @Nullable String name) {
Collection<PsiNameIdentifierOwner> result = new ArrayList<>();
if (name != null) {
PsiElement element = file.getFirstChild();
while (element != null) {
if (element instanceof PsiNameIdentifierOwner && name.equals(((PsiNameIdentifierOwner) element).getName())) {
result.add((PsiNameIdentifierOwner) element);
}
element = element.getNextSibling();
}
}
return result;
}
private void extractRelatedExpressions(@Nullable PsiElement element, @NotNull Collection<? super RelatedItemLineMarkerInfo<?>> result,
@NotNull FileBase containingFile) {
if (element == null) {
return;
}
FileBase psiRelatedFile = PsiFinder.getInstance(containingFile.getProject()).findRelatedFile(containingFile);
if (psiRelatedFile != null) {
Collection<PsiNameIdentifierOwner> expressions = psiRelatedFile.getExpressions(element.getText());
if (expressions.size() == 1) {
PsiNameIdentifierOwner relatedElement = expressions.iterator().next();
PsiElement nameIdentifier = relatedElement.getNameIdentifier();
if (nameIdentifier != null) {
String tooltip = GutterIconTooltipHelper
.composeText(new PsiElement[]{psiRelatedFile}, "", "Implements method <b>" + nameIdentifier.getText() + "</b> in <b>{0}</b>");
result.add(NavigationGutterIconBuilder.
create(containingFile.isInterface() ? ORIcons.IMPLEMENTED : ORIcons.IMPLEMENTING).
setTooltipText(tooltip).
setAlignment(GutterIconRenderer.Alignment.RIGHT).
setTargets(Collections.singleton(nameIdentifier instanceof PsiLowerSymbol ? nameIdentifier.getFirstChild() : nameIdentifier)).
createLineMarkerInfo(element));
}
}
}
}
public void testWithConstraints() {
Collection<PsiNameIdentifierOwner> expressions = expressions(
parseCode("module Make = (M: Input) : (S with type t('a) = M.t('a) and type b = M.b) => {};"));
assertEquals(1, expressions.size());
PsiFunctor f = (PsiFunctor) first(expressions);
assertEquals("M: Input", first(f.getParameters()).getText());
assertEquals("S", f.getReturnType().getText());
List<PsiConstraint> constraints = new ArrayList<>(f.getConstraints());
assertEquals(2, constraints.size());
assertEquals("type t('a) = M.t('a)", constraints.get(0).getText());
assertEquals("type b = M.b", constraints.get(1).getText());
assertEquals("{}", f.getBinding().getText());
}
@Nullable
@Override
public String getElementDescription(PsiElement element, ElementDescriptionLocation location) {
if (!(element instanceof BuildElement)) {
return null;
}
if (location instanceof UsageViewLongNameLocation) {
return ((BuildElement) element).getPresentableText();
}
if (location instanceof UsageViewShortNameLocation) {
if (element instanceof PsiNameIdentifierOwner) {
// this is used by rename operations, so needs to be accurate
return ((PsiNameIdentifierOwner) element).getName();
}
if (element instanceof PsiFile) {
return ((PsiFile) element).getName();
}
return ((BuildElement) element).getPresentableText();
}
return null;
}
@NotNull
@Override
public Tokenizer getTokenizer(PsiElement element) {
if (element instanceof PsiWhiteSpace) {
return EMPTY_TOKENIZER;
}
if (element instanceof PsiNameIdentifierOwner) {
return new PsiIdentifierOwnerTokenizer();
}
if (element.getParent() instanceof PsiNameIdentifierOwner) {
return EMPTY_TOKENIZER;
}
if (element.getNode().getElementType() == JSGraphQLEndpointTokenTypes.IDENTIFIER) {
return IDENTIFIER_TOKENIZER;
}
if (element instanceof PsiComment) {
if (SuppressionUtil.isSuppressionComment(element)) {
return EMPTY_TOKENIZER;
}
return myCommentTokenizer;
}
return EMPTY_TOKENIZER;
}
@Nullable
@Override
public String getPresentableText() {
if(element instanceof JSGraphQLEndpointImportDeclaration) {
return element.getText();
}
final PsiNameIdentifierOwner identifier = PsiTreeUtil.getChildOfType(element, PsiNameIdentifierOwner.class);
if (identifier != null) {
return identifier.getText();
}
final ASTNode astIdentifier = element.getNode().getFirstChildNode();
if (astIdentifier != null && astIdentifier.getElementType() == JSGraphQLEndpointTokenTypes.IDENTIFIER) {
return astIdentifier.getText();
}
if (element instanceof PsiFile) {
return null;
}
return element.getText();
}
@Nullable
@RequiredReadAction
public static String getNameWithAt(@Nonnull PsiNameIdentifierOwner element)
{
PsiElement nameIdentifier = element.getNameIdentifier();
if(nameIdentifier == null)
{
return null;
}
if(!(nameIdentifier instanceof CSharpIdentifier))
{
LOGGER.error("NameIdentifier is not 'CSharpIdentifier' element. Owner: " + element.getClass().getName());
return nameIdentifier.getText();
}
String value = ((CSharpIdentifier) nameIdentifier).getValue();
if(value == null)
{
return null;
}
return value;
}
@Nullable
@Override
@RequiredReadAction
public PsiElement getAnchor(@Nonnull PsiElement element)
{
if(element instanceof MsilElementWrapper || !element.isPhysical())
{
return null;
}
if(element instanceof CSharpTypeDeclaration ||
element instanceof CSharpFieldDeclaration ||
element instanceof CSharpMethodDeclaration ||
element instanceof CSharpConstructorDeclaration ||
element instanceof CSharpIndexMethodDeclaration ||
//element instanceof CSharpConversionMethodDeclaration ||
element instanceof CSharpPropertyDeclaration ||
element instanceof CSharpEventDeclaration)
{
return ((PsiNameIdentifierOwner) element).getNameIdentifier();
}
return null;
}
@RequiredReadAction
@Nullable
@Override
public CompilerCheckBuilder checkImpl(@Nonnull CSharpLanguageVersion languageVersion, @Nonnull CSharpHighlightContext highlightContext, @Nonnull DotNetVirtualImplementOwner element)
{
PsiElement nameIdentifier = ((PsiNameIdentifierOwner) element).getNameIdentifier();
if(nameIdentifier == null)
{
return null;
}
final Pair<CSharpLikeMethodDeclarationImplUtil.ResolveVirtualImplementResult, PsiElement> resultPair = CSharpLikeMethodDeclarationImplUtil.resolveVirtualImplementation(element, element);
switch(resultPair.getFirst())
{
case CANT_HAVE:
case FOUND:
default:
return null;
case NOT_FOUND:
return newBuilder(nameIdentifier, formatElement(element));
}
}
@RequiredReadAction
@Nullable
@Override
public HighlightInfoFactory checkImpl(@Nonnull CSharpLanguageVersion languageVersion, @Nonnull CSharpHighlightContext highlightContext, @Nonnull DotNetQualifiedElement element)
{
PsiElement parent = element.getParent();
if(!(parent instanceof CSharpTypeDeclaration) | !(element instanceof PsiNameIdentifierOwner) || element instanceof CSharpConstructorDeclaration || element instanceof
CSharpConversionMethodDeclaration || element instanceof CSharpEnumConstantDeclaration)
{
return null;
}
if(Comparing.equal(element.getName(), ((CSharpTypeDeclaration) parent).getName()))
{
PsiElement nameIdentifier = ((PsiNameIdentifierOwner) element).getNameIdentifier();
if(nameIdentifier == null)
{
return null;
}
return newBuilder(nameIdentifier, element.getName());
}
return null;
}
@RequiredReadAction
@Nullable
@Override
public HighlightInfoFactory checkImpl(@Nonnull CSharpLanguageVersion languageVersion, @Nonnull CSharpHighlightContext highlightContext, @Nonnull DotNetModifierListOwner element)
{
PsiElement parent = element.getParent();
if(parent instanceof DotNetTypeDeclaration && ((DotNetTypeDeclaration) parent).hasModifier(DotNetModifier.STATIC))
{
if(CSharpPsiUtilImpl.isTypeLikeElement(element))
{
return null;
}
if(!element.hasModifier(DotNetModifier.STATIC))
{
PsiElement nameIdentifier = ((PsiNameIdentifierOwner) element).getNameIdentifier();
return newBuilder(ObjectUtil.notNull(nameIdentifier, element), formatElement(element)).addQuickFix(new AddModifierFix
(DotNetModifier.STATIC, element));
}
}
return null;
}
@javax.annotation.Nullable
@RequiredReadAction
public static PsiElement getNameIdentifier(@Nonnull PsiElement element) {
if (element instanceof PsiNameIdentifierOwner) {
return ((PsiNameIdentifierOwner)element).getNameIdentifier();
}
if (element.isPhysical() &&
element instanceof PsiNamedElement &&
element.getContainingFile() != null &&
element.getTextRange() != null) {
// Quite hacky way to get name identifier. Depends on getTextOffset overriden properly.
final PsiElement potentialIdentifier = element.findElementAt(element.getTextOffset() - element.getTextRange().getStartOffset());
if (potentialIdentifier != null && Comparing.equal(potentialIdentifier.getText(), ((PsiNamedElement)element).getName(), false)) {
return potentialIdentifier;
}
}
return null;
}
@Nullable
private static PsiElement findNameIdentifier(Editor editor, PsiFile file, TextRange toDuplicate) {
int nonWs = CharArrayUtil.shiftForward(editor.getDocument().getCharsSequence(), toDuplicate.getStartOffset(), "\n\t ");
PsiElement psi = file.findElementAt(nonWs);
PsiElement named = null;
while (psi != null) {
TextRange range = psi.getTextRange();
if (range == null || psi instanceof PsiFile || !toDuplicate.contains(psi.getTextRange())) {
break;
}
if (psi instanceof PsiNameIdentifierOwner) {
named = ((PsiNameIdentifierOwner)psi).getNameIdentifier();
}
psi = psi.getParent();
}
return named;
}
@Nullable
private static PsiElement findTargetMember(@Nullable PsiElement element) {
if (element == null) return null;
final ChangeSignatureHandler fileHandler = getChangeSignatureHandler(element.getLanguage());
if (fileHandler != null) {
final PsiElement targetMember = fileHandler.findTargetMember(element);
if (targetMember != null) return targetMember;
}
PsiReference reference = element.getReference();
if (reference == null && element instanceof PsiNameIdentifierOwner) {
return element;
}
if (reference != null) {
return reference.resolve();
}
return null;
}
private InplaceRefactoring doRename(PsiElement elementToRename, Editor editor) {
if (elementToRename instanceof PsiNameIdentifierOwner) {
RenamePsiElementProcessor processor = RenamePsiElementProcessor.forElement(elementToRename);
if (processor.isInplaceRenameSupported()) {
StartMarkAction startMarkAction = StartMarkAction.canStart(elementToRename.getProject());
if (startMarkAction == null || (processor.substituteElementToRename(elementToRename, editor)
== elementToRename)) {
processor.substituteElementToRename(elementToRename, editor, new Pass<PsiElement>() {
@Override
public void pass(PsiElement element) {
MemberInplaceRenamer renamer = createMemberRenamer(element,
(PsiNameIdentifierOwner) elementToRename, editor);
boolean startedRename = renamer.performInplaceRename();
if (!startedRename) {
performDialogRename(editor);
}
}
});
return null;
}
} else {
InplaceRefactoring inplaceRefactoring = editor.getUserData(InplaceRefactoring.INPLACE_RENAMER);
if ((inplaceRefactoring instanceof MemberInplaceRenamer)) {
TemplateState templateState = TemplateManagerImpl
.getTemplateState(InjectedLanguageUtil.getTopLevelEditor(editor));
if (templateState != null) {
templateState.gotoEnd(true);
}
}
}
}
performDialogRename(editor);
return null;
}
@NotNull
public static String getQualifiedPath(@NotNull PsiNameIdentifierOwner element) {
String path = "";
PsiElement parent = element.getParent();
while (parent != null) {
if (parent instanceof PsiQualifiedElement) {
if (parent instanceof PsiNameIdentifierOwner && ((PsiNameIdentifierOwner) parent).getNameIdentifier() == element) {
return ((PsiQualifiedElement) parent).getPath();
}
return ((PsiQualifiedElement) parent).getQualifiedName() + (path.isEmpty() ? "" : "." + path);
} else {
if (parent instanceof PsiNameIdentifierOwner) {
String parentName = ((PsiNamedElement) parent).getName();
if (parentName != null && !parentName.isEmpty()) {
path = parentName + (path.isEmpty() ? "" : "." + path);
}
}
parent = parent.getParent();
}
}
try {
PsiFile containingFile = element.getContainingFile(); // Fail in 2019.2... ?
return ((FileBase) containingFile).getModuleName() + (path.isEmpty() ? "" : "." + path);
} catch (PsiInvalidElementAccessException e) {
return path;
}
}
@NotNull
private <T extends PsiNameIdentifierOwner> List<T> getExpressions(@Nullable String name, @NotNull Class<T> clazz) {
List<T> result = new ArrayList<>();
if (name != null) {
Collection<T> children = PsiTreeUtil.findChildrenOfType(getContainingFile(), clazz);
for (T child : children) {
if (name.equals(child.getName())) {
result.add(child);
}
}
}
return result;
}
@Nullable
@Override
public PsiType getTypeExpression(@Nullable String name) {
PsiElement body = name == null ? null : getBody();
if (body != null) {
ExpressionFilter expressionFilter = element -> element instanceof PsiType && name.equals(element.getName());
Collection<PsiNameIdentifierOwner> expressions = getExpressions(ExpressionScope.all, expressionFilter);
if (!expressions.isEmpty()) {
return (PsiType) expressions.iterator().next();
}
}
return null;
}
@Nullable
@Override
public PsiLet getLetExpression(@Nullable String name) {
PsiElement body = name == null ? null : getBody();
if (body != null) {
ExpressionFilter expressionFilter = element -> element instanceof PsiLet && name.equals(element.getName());
Collection<PsiNameIdentifierOwner> expressions = getExpressions(ExpressionScope.all, expressionFilter);
if (!expressions.isEmpty()) {
return (PsiLet) expressions.iterator().next();
}
}
return null;
}
@Nullable
@Override
public PsiVal getValExpression(@Nullable String name) {
PsiElement body = name == null ? null : getBody();
if (body != null) {
ExpressionFilter expressionFilter = element -> element instanceof PsiVal && name.equals(element.getName());
Collection<PsiNameIdentifierOwner> expressions = getExpressions(ExpressionScope.all, expressionFilter);
if (!expressions.isEmpty()) {
return (PsiVal) expressions.iterator().next();
}
}
return null;
}
@Nullable
@Override
public PsiType getTypeExpression(@Nullable String name) {
if (name != null) {
ExpressionFilter expressionFilter = element -> element instanceof PsiType && name.equals(element.getName());
Collection<PsiNameIdentifierOwner> expressions = getExpressions(ExpressionScope.all, expressionFilter);
if (!expressions.isEmpty()) {
return (PsiType) expressions.iterator().next();
}
}
return null;
}
@Nullable
@Override
public PsiLet getLetExpression(@Nullable String name) {
if (name != null) {
ExpressionFilter expressionFilter = element -> element instanceof PsiLet && name.equals(element.getName());
Collection<PsiNameIdentifierOwner> expressions = getExpressions(ExpressionScope.all, expressionFilter);
if (!expressions.isEmpty()) {
return (PsiLet) expressions.iterator().next();
}
}
return null;
}
@Nullable
@Override
public PsiVal getValExpression(@Nullable String name) {
if (name != null) {
ExpressionFilter expressionFilter = element -> element instanceof PsiVal && name.equals(element.getName());
Collection<PsiNameIdentifierOwner> expressions = getExpressions(ExpressionScope.all, expressionFilter);
if (!expressions.isEmpty()) {
return (PsiVal) expressions.iterator().next();
}
}
return null;
}
@NotNull
public static Collection<PsiNameIdentifierOwner> getExpressions(@Nullable PsiFile file, @NotNull ExpressionScope eScope,
@Nullable ExpressionFilter filter) {
ArrayList<PsiNameIdentifierOwner> result = new ArrayList<>();
if (file != null) {
PsiFinder psiFinder = PsiFinder.getInstance(file.getProject());
QNameFinder qnameFinder = file.getLanguage() == RmlLanguage.INSTANCE ? RmlQNameFinder.INSTANCE : OclQNameFinder.INSTANCE;
processSiblingExpressions(psiFinder, qnameFinder, file.getFirstChild(), eScope, result, filter);
}
return result;
}
@Override
protected void collectNavigationMarkers(@NotNull PsiElement element, @NotNull Collection<? super RelatedItemLineMarkerInfo> result) {
PsiElement parent = element.getParent();
FileBase containingFile = (FileBase) element.getContainingFile();
if (element instanceof PsiTypeConstrName) {
FileBase psiRelatedFile = PsiFinder.getInstance(containingFile.getProject()).findRelatedFile(containingFile);
if (psiRelatedFile != null) {
Collection<PsiType> expressions = psiRelatedFile.getExpressions(element.getText(), PsiType.class);
if (expressions.size() == 1) {
PsiType relatedType = expressions.iterator().next();
PsiElement symbol = PsiTreeUtil.findChildOfType(element, PsiLowerSymbol.class);
PsiElement relatedSymbol = PsiTreeUtil.findChildOfType(relatedType, PsiLowerSymbol.class);
if (symbol != null && relatedSymbol != null) {
result.add(NavigationGutterIconBuilder.
create(containingFile.isInterface() ? ORIcons.IMPLEMENTED : ORIcons.IMPLEMENTING).
setAlignment(GutterIconRenderer.Alignment.RIGHT).
setTargets(Collections.singleton(relatedSymbol.getFirstChild())).
createLineMarkerInfo(symbol.getFirstChild()));
}
}
}
} else if (element instanceof PsiLowerSymbol && parent instanceof PsiLet && ((PsiNameIdentifierOwner) parent).getNameIdentifier() == element) {
extractRelatedExpressions(element.getFirstChild(), result, containingFile);
} else if (element instanceof PsiLowerSymbol && parent instanceof PsiVal && ((PsiNameIdentifierOwner) parent).getNameIdentifier() == element) {
extractRelatedExpressions(element.getFirstChild(), result, containingFile);
} else if (element instanceof PsiLowerSymbol && parent instanceof PsiExternal && ((PsiNameIdentifierOwner) parent).getNameIdentifier() == element) {
extractRelatedExpressions(element.getFirstChild(), result, containingFile);
} else if (element instanceof PsiUpperSymbol && parent instanceof PsiInnerModule && ((PsiNameIdentifierOwner) parent).getNameIdentifier() == element) {
extractRelatedExpressions(element.getFirstChild(), result, containingFile);
} else if (element instanceof PsiUpperSymbol && parent instanceof PsiException && ((PsiNameIdentifierOwner) parent).getNameIdentifier() == element) {
extractRelatedExpressions(element.getFirstChild(), result, containingFile);
}
}
private static void addExpressions(@NotNull CompletionResultSet resultSet, @NotNull Collection<PsiNameIdentifierOwner> expressions,
@NotNull Language language) {
for (PsiNameIdentifierOwner expression : expressions) {
if (!(expression instanceof PsiOpen) && !(expression instanceof PsiInclude) && !(expression instanceof PsiAnnotation)) {
// TODO: if include => include
String name = expression.getName();
if (name != null) {
String signature = PsiSignatureUtil.getSignature(expression, language);
resultSet.addElement(LookupElementBuilder.
create(name).
withTypeText(signature).
withIcon(PsiIconUtil.getProvidersIcon(expression, 0)));
}
if (expression instanceof PsiType) {
PsiType eType = (PsiType) expression;
Collection<PsiVariantDeclaration> variants = eType.getVariants();
if (!variants.isEmpty()) {
for (PsiVariantDeclaration variant : variants) {
String variantName = variant.getName();
if (variantName != null) {
resultSet.addElement(LookupElementBuilder.
create(variantName).
withTypeText(eType.getName()).
withIcon(PsiIconUtil.getProvidersIcon(variant, 0)));
}
}
}
}
}
}
}
@NotNull
public <T extends PsiNameIdentifierOwner> List<T> getExpressions(@Nullable String name, @NotNull Class<T> clazz) {
List<T> result = new ArrayList<>();
if (name != null) {
Collection<T> children = PsiTreeUtil.findChildrenOfType(this, clazz);
for (T child : children) {
if (name.equals(child.getName())) {
result.add(child);
}
}
}
return result;
}
public void testPatternMatchConstant() {
PsiFile file = parseCode("let unwrapValue = fun " +
" | `String(s) => toJsUnsafe(s) " +
" | `bool(b) => toJsUnsafe(Js.Boolean.to_js_boolean(b));");
Collection<PsiNameIdentifierOwner> expressions = expressions(file);
assertEquals(1, expressions.size());
Collection<PsiPatternMatch> matches = PsiTreeUtil.findChildrenOfType(first(expressions), PsiPatternMatch.class);
assertEquals(2, matches.size());
}
public void testBasic() {
PsiNameIdentifierOwner e = first(expressions(parseCode("module Make = (M: Def) : S => {};")));
PsiFunctor f = (PsiFunctor) e;
assertEquals("{}", f.getBinding().getText());
assertEquals("S", f.getReturnType().getText());
}