LSP
LSP copied to clipboard
Make DocumentSyncListener more efficient if no server is running
Currently if LSP (this package) is installed, it runs quite some code in the background whenever the callbacks from DocumentSyncListener, which is a subclass of sublime.ViewEventListener, are triggered. This happens for all views, not only when a server is active. For example it creates an empty (if no server is running) sublime.CompletionList and schedules to run more code on the async thread in https://github.com/sublimelsp/LSP/blob/a6b17a4aa4d10d8ddc225413d221c4241b877f8b/plugin/documents.py#L555-L560
or it runs the debouncing in https://github.com/sublimelsp/LSP/blob/a6b17a4aa4d10d8ddc225413d221c4241b877f8b/plugin/documents.py#L384 via sublime.set_timeout_async each time when the cursor position changes.
All of this runs on the async thread, so you won't immediately experience any lag from this. But still it feels very inefficient and I wondered how it could be prevented or improved. We can't use the is_applicable classmethod for DocumentSyncListener, because if the view's syntax is changed later, an LSP session might still get attached to this view. So my idea would be to add a check in each of the ST callback methods and return early if there is no session running for this view. Does it make sense or is there a better solution?