Skip to content
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 GlobalLspListener #2523

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft

feat: add GlobalLspListener #2523

wants to merge 9 commits into from

Conversation

rchl
Copy link
Member

@rchl rchl commented Sep 29, 2024

I've been thinking a bit on how to support support sublimelsp/LSP-volar#221

My latest idea is to not do any magic to make LSP-typescript initialize for Vue files and instead when LSP-volar is initialized on a vue file, and LSP-typescript is not running, add a Phantom at the top of the file that tells the user to initialize LSP-typescript to get the full experience. Clicking on the phantom triggers a window command that modifies project settings that enables LSP-typescript for Vue files and also enables the vue plugin by referencing it from the LSP-volar storage path. Would look something like:

Screenshot 2024-09-29 at 22 02 23

To implement that idea I need some global listener that will tell me whenever a session initializes or shuts down on a view thus this API. It would be used like this:

from LSP.plugin import GlobalLspListener
from LSP.plugin import Session
from LSP.plugin import windows

class SessionListener(GlobalLspListener):

    def __init__(self) -> None:
        self._phantoms: dict[int, sublime.PhantomSet] = dict()

    def on_session_initialized_async(self, view_listener: AbstractViewListener, session: Session) -> None:
        pass

    def on_session_shutdown_async(self, view_listener: AbstractViewListener, session: Session) -> None:
        pass


session_listener = SessionListener()


def plugin_loaded():
    LspVolarPlugin.setup()
    windows.add_global_listener(session_listener)


def plugin_unloaded():
    windows.remove_global_listener(session_listener)
    LspVolarPlugin.cleanup()

Let me know if you have any better idea on how this could be handled.

rchl and others added 9 commits September 29, 2024 21:57
* origin/main:
  Use orjson to de-/serialize json-rpc messages (#2513)
* fix/remove-generators:
  Use orjson to de-/serialize json-rpc messages (#2513)
  remove unnecessary use of generators for session retrieval
* origin/main:
  feat: make DebouncerNonThreadSafe public (#2525)
* main:
  style change
  Don't restart servers when userprefs change (#2448)
@jwortmann
Copy link
Member

I think LspSessionListener could be a better name than GlobalLspListener.

I'm not a big fan of exposing windows and doing the add/remove manually. Would it be possible to handle it automatically, like the event listeners from the ST API, maybe similar to this?

LSP/boot.py

Lines 175 to 184 in d71e11a

def _register_all_plugins() -> None:
plugin_classes: list[type[AbstractPlugin]] = []
_get_final_subclasses(AbstractPlugin.__subclasses__(), plugin_classes)
for plugin_class in plugin_classes:
try:
if not plugin_class.name():
continue
except NotImplementedError:
continue
register_plugin(plugin_class, notify_listener=False)

Alternatively I would define two new public functions register_listener / unregister_listener instead of using class methods on windows.

But in general, if the use case is only the interaction between LSP-volar and LSP-typescript, shouldn't this whole logic better be handled in LSP-volar? The __init__ / on_post_start / on_pre_send_request_async methods of AbstractPlugin should be enough to know when a session initializes or is about to shutdown.

@rchl
Copy link
Member Author

rchl commented Oct 11, 2024

But in general, if the use case is only the interaction between LSP-volar and LSP-typescript, shouldn't this whole logic better be handled in LSP-volar? The __init__ / on_post_start / on_pre_send_request_async methods of AbstractPlugin should be enough to know when a session initializes or is about to shutdown.

In this case LSP-volar needs to know when LSP-typescript initializes or shuts down.

I'm still thinking about this and experimenting with different approaches though.

@rchl rchl marked this pull request as draft October 26, 2024 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants