How to get a PsiElement by it's text?

I want to get a PsiElement from a PyFile. I have the text or name of the element. I tried using PyFIle.multiResolveName(text) but didn't get anything, since it looks at the __all__ file. Thank you.

0
正式评论

What if there are several elements with the same text?

Which problem are you trying to solve?

Avatar
Permanently deleted user

I have the line number that has an error and i know what PsiElement or keyword is causing the error and if the error is inside a function i will also have the function name. I want to get the range of PsiElement to highlight the error. Also i would know the PyFile. It seems like if there is multiple elements with same text, will have trouble. Any solution you would suggest. Thanks.

0
Avatar
Permanently deleted user

I can do something with the line number and make an approximations.

0

You can get the file's document (PsiFile#getViewProvider#getDocument), find line range (Document#getLineStartOffset/getLineEndOffset), then find your text's offset inside that range (Document#getImmutableCharSequence + StringUtil#indexOf), and finally find PSI at that offset/range (PsiFile#findElementAt or PsiTreeUtil#findElementOfClassAtRange).

1
Avatar
Permanently deleted user

Thank you so much for the help.

0

请先登录再写评论。