-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add decorators for api handlers #38
feat: add decorators for api handlers #38
Conversation
d8efbb3
to
97da089
Compare
Interesting Pros:
Cons:
Also, I wonder whether it is types-friendly (haven't tried it myself)? Will it complain if the decorated function doesn't match the expected signature? |
You would have to import the |
Ah, yes, the if lsp_version >= (1, 0, 0): # type: ignore
# ST 4
from .vs_marketplace_client_handler_v2 import ApiWrapper # <-----------------------------
from .vs_marketplace_client_handler_v2 import VsMarketplaceClientHandler
else:
# ST 3
from .vs_marketplace_client_handler import ApiWrapper # <--------------------------------
from .vs_marketplace_client_handler import VsMarketplaceClientHandler
__all__ = [
"ApiWrapper", # <------------------------------------------------------------------------
"ApiWrapperInterface",
"ServerVsMarketplaceResource",
"VsMarketplaceClientHandler",
] And then you can use it like from lsp_utils import ApiWrapper
@ApiWrapper.as_notification_handler("eslint/status")
def handle_status(self, params) -> None:
pass |
It resolves nothing in practice. Somehow feel it can be useful in some ways (not necessary in this PR). |
Conceptually that would be wrong as it's an implementation-specific class that is not supposed to be exposed. |
I think yes. On my side, LSP-py* can complain after I make the function signature clear. T_NOTIFICATION_HANDLER = Callable[["VsMarketplaceClientHandler", Dict[str, Any]], None]
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the first arg is always `self` and @staticmethod
def as_notification_handler(event_name: str) -> Callable[[T_NOTIFICATION_HANDLER], T_NOTIFICATION_HANDLER]:
""" Marks the decorated function as a handler for the notification event. """
def decorator(func: T_NOTIFICATION_HANDLER) -> T_NOTIFICATION_HANDLER:
setattr(func, ApiWrapper.HANDLER_MARK_NOTIFICATION, event_name)
return func
return decorator |
Oh no, Or put them in something like |
1d1179e
to
e15f076
Compare
Signed-off-by: Jack Cherng <jfcherng@gmail.com>
e15f076
to
6f9caf4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should avoid force-push when there are reviewers involved since that makes it harder to see what has changed.
In the end, when ready to merge, there is an option to squash and merge so the history will get cleaned up.
Signed-off-by: Jack Cherng <jfcherng@gmail.com>
Signed-off-by: Jack Cherng <jfcherng@gmail.com>
Signed-off-by: Jack Cherng <jfcherng@gmail.com>
d42c1e4
to
35e57d5
Compare
Signed-off-by: Jack Cherng <jfcherng@gmail.com>
Signed-off-by: Jack Cherng <jfcherng@gmail.com>
Signed-off-by: Jack Cherng <jfcherng@gmail.com>
Signed-off-by: Jack Cherng <jfcherng@gmail.com>
Signed-off-by: Jack Cherng <jfcherng@gmail.com>
Signed-off-by: Jack Cherng <jfcherng@gmail.com>
Signed-off-by: Jack Cherng <jfcherng@gmail.com>
…handlers() Signed-off-by: Jack Cherng <jfcherng@gmail.com>
Signed-off-by: Jack Cherng <jfcherng@gmail.com>
Signed-off-by: Jack Cherng <jfcherng@gmail.com>
Signed-off-by: Jack Cherng <jfcherng@gmail.com>
I'd also like this to be documented in new docs. |
MUCH appreciated for PR reviewing (many lessons learned) and documenting. And that lsp_utils refactoring ❤️ |
The link to documents in the README is wrong btw. |
What do you think of a shorter name for the decorators?
|
I feel that the existing naming is more in line with conventions I'm familiar with where decorator describes what it turns the function into (like |
When you put it on that way it makes sense to name it Here is the reason on why I suggested the name change.
the @on_notification decorator for me would suggest that the function it decorates will be executed when the notification is received. |
I said:
and I think the example you made:
is also in line with that statement. In the case of a central |
Can't wait for the next release. So many improvements for LSP-* devs :D |
Currently the decorator methods can't handle some special notifications like |
I feel like it was really only meant to handle custom requests/notifications. I think that it's consistent in that case? It would be a bit ambiguous to specify what should happen if both LSP and a plugin handle given request at least. |
If the client wants to make it into a mess, that's its own issue. I can't prevent that from happening if that's what the plugin author wants, but I need that ability to do something good. |
Just sharing an idea I got during playing with a
mysteriousPython language server +vs_marketplace_client_handler
.With this PR, you can register message handlers with decorators.
For example,
can be written as