I have PyFunction, Caret position, PsiFile. How to get other things from here?
已回答
I have a PyFunction object. I want to get the datatype of every parameter and if the datatype is defined in the project scope, I want to get the text of it. For example, if Class A datatype is used in the function, then I want to get the text of the class. I also want to get the text of the return type of the function if the datatype is defined in project scope. If the parameter has no datatype or is trivial like int I want to ignore it.
请先登录再写评论。
Hello, does anyone know how to get it?
Hi,
You can get parameters of a PyFunction with getParameterList(). Then if it's a named parameter, you can ask it its type with TypeEvalContext#getType. Depending on whether you need to do it in an inspection or in some action triggered by a user, e.g. intention/refactoring/completion contributor, you will need either TypeEvalContext#codeAnalysis or TypeEvalContext#userInitiated/TypeEvalContext#codeCompletion. As for the return value of PyFunction, there is TypeEvalContext#getReturnType. If an inferred type is an instance of PyClassType, you can retrieve the corresponding class definition with PyClassType#getPyClass. It's a normal PSI element, so you can access its text with getText() as usual. You can also check if a particular PSI element belongs to project sources with PsiManagerImpl#isInProject or ProjectFileIndex#isInSource.