reflex icon indicating copy to clipboard operation
reflex copied to clipboard

Allow language servers to pick up definitions of shorthands

Open TommyDew42 opened this issue 2 years ago β€’ 12 comments

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.

TommyDew42 avatar Jan 27 '23 09:01 TommyDew42

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...

picklelo avatar Jan 31 '23 17:01 picklelo

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 avatar Jan 31 '23 23:01 FHU-yezi

@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.

picklelo avatar Jan 31 '23 23:01 picklelo

So can the langurage server give suggestions to pc.Text.create()?

FHU-yezi avatar Jan 31 '23 23:01 FHU-yezi

@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

TommyDew42 avatar Feb 01 '23 14:02 TommyDew42

@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.

FHU-yezi avatar Feb 01 '23 14:02 FHU-yezi

tbh, i have no idea how to implement this πŸ˜‚ please go ahead with the solution in your mind!

TommyDew42 avatar Feb 01 '23 15:02 TommyDew42

@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.

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

picklelo avatar Feb 04 '23 23:02 picklelo

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?

FHU-yezi avatar Feb 05 '23 00:02 FHU-yezi

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).

picklelo avatar Feb 06 '23 02:02 picklelo

There's also a type issue even if we use Component.create: link

TommyDew42 avatar Feb 11 '23 16:02 TommyDew42

VSCode, using Pylance as language server, got the same issue.

RuofengX avatar Mar 20 '23 07:03 RuofengX