reflex
reflex copied to clipboard
Allow language servers to pick up definitions of shorthands
We are doing this to produce all the shorthands to create components: https://github.com/pynecone-io/pynecone/blob/c690b2a824370c242d94bd9515792ff3d2d2589b/pynecone/components/init.py#L19-L26
This is really good yet most python language servers aren't smart enough to pick the definition for these shorthands.
This would definitely be nice - anyone have any ideas how to do this cleanly? Or will we have to manually write out each create function...
This would definitely be nice - anyone have any ideas how to do this cleanly? Or will we have to manually write out each create function...
Maybe we can make a create
class method for every conponent classes instead of define all the lower-case functions dynamicly? That should let the langurage server know and provide suggestions correctly, but it will be a breaking change.
@FHU-yezi we already have a create
class method. For example, instead of pc.text("hello")
you can already do pc.Text.create("hello")
and it has the same result. The former is just a shorthand to be less verbose.
So can the langurage server give suggestions to pc.Text.create()
?
@FHU-yezi Yes for pc.Text.create()
but not for the lower case functions. You can test it easily in most of the IDEs. e.g. having python language plugins in vscode
@FHU-yezi Yes for
pc.Text.create()
but not for the lower case functions. You can test it easily in most of the IDEs. e.g. having python language plugins in vscode
Yes i have already tested it in VS Code with Pylance as langurage server, I think all langurage servers can not detect functions which is dynamic created, so we need to define all the functions, maybe we can write a simple code generator to do this? It will be easy, just parse all the asts of python files and iter each class which based on Compnent class.
tbh, i have no idea how to implement this π please go ahead with the solution in your mind!
@FHU-yezi Yes for
pc.Text.create()
but not for the lower case functions. You can test it easily in most of the IDEs. e.g. having python language plugins in vscodeYes i have already tested it in VS Code with Pylance as langurage server, I think all langurage servers can not detect functions which is dynamic created, so we need to define all the functions, maybe we can write a simple code generator to do this? It will be easy, just parse all the asts of python files and iter each class which based on Compnent class.
It doesn't even need to parse the AST or anything, it just needs to call the Component.get_props
to know what props the create function should take. Still will be challenging to do the codegen part tho
While it is not easy to write a code generator, can we write all the create logic in __init__
magic method of every component? And user can just create a object from the class, is there any magic has been done in create method?
Edit: just checked some code, seems like all the component class is based on Component class, and just use it's init function? Maybe we can use super().__init__()
first, and then run the same logic as create method did, does it make sense?
We can't use the __init__
method because it's takes different arguments than the create
method. The __init__
calls the Pydantic constructor, while create
takes in props as the arguments to create the component (it calls __init__
under the hood).
There's also a type issue even if we use Component.create
: link
VSCode, using Pylance as language server, got the same issue.