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.
Please sign in to leave a comment.
What if there are several elements with the same text?
Which problem are you trying to solve?
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.
I can do something with the line number and make an approximations.
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).
Thank you so much for the help.