Skip to content
Open
Show file tree
Hide file tree
Changes from 91 commits
Commits
Show all changes
138 commits
Select commit Hold shift + click to select a range
699dad3
Test both TCP modes
rwols Apr 13, 2026
908c849
Use enum for 'Mode' in server.py
rwols Apr 18, 2026
b4f0420
Make init_options optional
rwols Apr 18, 2026
76f3c3f
WIP add asyncio to transports
rwols Apr 18, 2026
45d7ea6
Test both TCP modes
rwols Apr 13, 2026
e8fae30
Use enum for 'Mode' in server.py
rwols Apr 18, 2026
c511cec
Make init_options optional
rwols Apr 18, 2026
7438d4a
Use StrEnum
rwols Apr 18, 2026
6a104b0
Merge branch 'chore/add-tests' into feat/asyncio
rwols Apr 19, 2026
d59bb1c
More work on adding asyncio
rwols Apr 20, 2026
c0bea09
Merge branch 'main' into feat/asyncio
rwols Apr 20, 2026
c1fc59c
More work towards asyncio
rwols Apr 22, 2026
e35b611
Remove accidental merge conflict changes
rwols Apr 22, 2026
6cad098
Remove code in server.py, I really screwed up the merge but will reba…
rwols Apr 22, 2026
1597782
refactor Promise.__await__
rwols Apr 22, 2026
cc642f4
Add more comments to _SetTimeoutAsyncExecutor
rwols Apr 22, 2026
cea0e99
Rename Session._invoke_views -> Session._invoke_views_async
rwols Apr 22, 2026
3d30ad4
More work towards asyncio
rwols Apr 28, 2026
472705b
debugging sublime_aio.ViewEventListener
rwols Apr 28, 2026
ea34714
Merge branch 'main' into feat/asyncio
rwols May 2, 2026
069ed50
Fix calls
rwols May 2, 2026
776749a
More fix calls
rwols May 2, 2026
0227877
More work. Some diagnostics show intermittently. Request logic is not…
rwols May 3, 2026
2649a6f
Tweaks to pull diagnostics handling
rwols May 3, 2026
54af917
Restore method names
rwols May 5, 2026
1a4589c
Forgot import
rwols May 5, 2026
900f721
Restore more method names
rwols May 5, 2026
35621cf
ResponseException is not needed, we already have Error
rwols May 8, 2026
dbf4d7d
Merge branch 'main' into feat/asyncio
rwols May 8, 2026
f3b2f84
More rename reverts
rwols May 8, 2026
a9c1692
Add more tracing for understanding why requests are not resolving
rwols May 8, 2026
7004df5
Start fixing up 'goto' functionality
rwols May 9, 2026
a876949
Fix:
rwols May 10, 2026
d27aa93
debugging
rwols May 10, 2026
341ff99
We have something working
rwols May 10, 2026
4ed4e3f
Start fixing diagnostic errors because diagnostics work
rwols May 11, 2026
7aba65e
Remove trace() calls
rwols May 12, 2026
e3ccba2
Remove trace() calls
rwols May 12, 2026
c2c2a8c
Remove trace() calls
rwols May 12, 2026
af561a8
Fixups
rwols May 12, 2026
7260c90
Remove trace() calls and fixup type hints
rwols May 12, 2026
a60ddd2
Reintroduce request_code_actions_async for SessionBuffer
rwols May 12, 2026
23210e3
Consolidate sublime_aio & asyncio functions/classes in plugin/core/ai…
rwols May 12, 2026
73ed644
get_session_buffer_for_uri -> get_session_buffer_for_uri_async
rwols May 12, 2026
6d6e772
Session.session_buffers -> Session.session_buffers_async
rwols May 12, 2026
a84f3dd
_invoke_views_async -> _invoke_views
rwols May 12, 2026
f7702c2
Fix notifications being logged twice
rwols May 12, 2026
bb51929
Fix LspCheckApplicableCommand... I think
rwols May 12, 2026
7d58862
Fix errors in WindowManager.start, and allow async version of LspPlug…
rwols May 12, 2026
e2119a1
Print exceptions from coroutines started from `run_coroutine_threadsafe`
rwols May 13, 2026
ce27231
Ensure plugin_unloaded works as expected
rwols May 13, 2026
a4aba53
Merge branch 'main' into feat/asyncio
rwols May 13, 2026
c215234
Fixup incorrect (old) usage of Session.send_request_async
rwols May 13, 2026
ae081e3
Remove trace() calls from sessions.py
rwols May 13, 2026
e4d3324
Merge branch 'main' into feat/asyncio
rwols May 14, 2026
6e5ca85
Fix type errors in sessions.py
rwols May 14, 2026
4866feb
Review all sublime.set_timeout_async call sites
rwols May 14, 2026
73a658a
Merge branch 'main' into feat/asyncio
rwols May 14, 2026
8a6de63
asyncio.Future, async functions, and Promises are all just Awaitables
rwols May 14, 2026
fcb8717
Fix process args
rwols May 14, 2026
8997d51
Fix most type errors, except for tooling.py
rwols May 14, 2026
5943571
Fix: document link was requested before didOpen
rwols May 14, 2026
7485b90
Rename Files: open files sequentially, as it as before
rwols May 14, 2026
edd3ccd
apply_text_edits: wait at least one UI frame
rwols May 14, 2026
91af0e0
LspPlugin.prefer_async_on_pre_start -> LspPlugin.use_asyncio
rwols May 14, 2026
da1fc1d
Invoke LspPlugin.on_initialize after the `initialized` notification
rwols May 14, 2026
34697be
Update tooling.py for asyncio
rwols May 15, 2026
aa37898
Remove unused imports
rwols May 15, 2026
9335c7d
Fix 'TCP client' mode
rwols May 15, 2026
afde9fb
Compatibility with python 3.8
rwols May 15, 2026
0795ee4
Fix missing import for type checking
rwols May 15, 2026
4659e8d
Add stubs/sublime_aio.pyi
rwols May 15, 2026
76aeba1
Fix formatting
rwols May 15, 2026
66bd43a
Fix all remaining lint errors
rwols May 15, 2026
a143bdf
Fix interface method (why isn't this reported as an error by either p…
rwols May 15, 2026
69fd44d
Fixup interface method of `Manager` (why isn't this reported by eithe…
rwols May 15, 2026
c2a9e30
Fixup wm.handle_show_message: it's not async
rwols May 15, 2026
8835214
Add @override to all methods in WindowManager that implement an inter…
rwols May 15, 2026
668846c
Turn off @deprecation warnings
rwols May 15, 2026
c6a89b1
Fix lint warnings
rwols May 15, 2026
9d8c1b3
The return type of the `Window.handle_show_message` interface method …
rwols May 15, 2026
d271624
Fix reference to task object
rwols May 16, 2026
68b8945
Add function: exceptions_log
rwols May 16, 2026
a3c88ec
Catch possible exception when draining the stream writer
rwols May 16, 2026
3915ca6
Add functions aclosing, gather_and_flatten_exceptions, TaskContainer.…
rwols May 16, 2026
82973a5
Fix for python 3.8 runtime regarding opening files lock
rwols May 16, 2026
1ae40fe
Fixes for python 3.8 runtime, better CancellableInflightStreamingRequ…
rwols May 16, 2026
45bbb05
Fix LSP: Rename
rwols May 16, 2026
998ac1c
Comment out the debug print in the tranports.py because I'm feeling c…
rwols May 16, 2026
9c761d1
Merge branch 'main' into feat/asyncio
rwols May 16, 2026
056359b
LspPlugin.use_asyncio() -> LspPlugin.use_asyncio
rwols May 16, 2026
aca3b34
Fixup incorrect merge resolution in api.py
rwols May 16, 2026
1a6448b
I don't know how this got here.
rwols May 16, 2026
dd2b791
Odds and ends in sessions.py
rwols May 16, 2026
ff9cfd7
Odds and ends: make `@requires_session` compatible with coroutine fun…
rwols May 16, 2026
5e3af9c
WIP refactor tests
rwols May 17, 2026
d91eeda
Convert unit/integration tests to asyncio
rwols May 18, 2026
8676dc4
Fix bugs revealed by tests
rwols May 18, 2026
c6efba4
Merge branch 'main' into feat/asyncio
rwols May 18, 2026
2c2e7b3
Merge branch 'main' into feat/asyncio
rwols May 18, 2026
119fa99
Add runtime check for accidental coroutine continuations
rwols May 18, 2026
9e35cc4
In the process of fixing bugs due to tests revealing bugs
rwols May 19, 2026
8c83c96
Re-introduce classSetUp and classTearDown
rwols May 20, 2026
691d8ea
Fixes for macOS
rwols May 21, 2026
0aa553b
Merge branch 'main' into feat/asyncio
rwols May 21, 2026
e9d65dd
Fixes after merge
rwols May 22, 2026
a8f6ba1
Merge branch 'main' into feat/asyncio
rwols May 23, 2026
a5d67f5
Session.open_scratch_buffer always returns a View
rwols May 23, 2026
09b3d3d
Fix disabling and then enabling configs
rwols May 23, 2026
bc70dee
Simplify executors, and encode/decode JSON on the ST async thread
rwols May 23, 2026
7005ac0
Maybe fix LSP.tests.test_single_document.SingleDocumentTestCase.test_…
rwols May 23, 2026
750b995
Remove debug prints from test code
rwols May 23, 2026
e3fc6e9
Fix remaining tests... hopefully?
rwols May 23, 2026
6817036
Fix lint errors
rwols May 23, 2026
3c90ff9
Revert "Maybe fix LSP.tests.test_single_document.SingleDocumentTestCa…
rwols May 23, 2026
6e7f252
Fix purge_changes_async in rename.py not being called from the asynci…
rwols May 23, 2026
a5e3a6d
Rename run_coroutine_threadsafe -> run_coroutine
rwols May 23, 2026
135aa60
Fix bug in the rename flow when first making a preview and then click…
rwols May 23, 2026
9506bd5
Rename call_soon_threadsafe -> run_in_asyncio_thread
rwols May 23, 2026
0e19738
Remove trace() calls from code_actions.py and save_command.py
rwols May 23, 2026
45ee4dd
Remove duplicated imports from api.py
rwols May 23, 2026
ca61cc6
Fix toggling inlay hints
rwols May 23, 2026
bb41f71
Fix hardcoded value for on-save task timeout
rwols May 23, 2026
1529494
Catch the asyncio.CancelledError that's going to be thrown when cance…
rwols May 23, 2026
1f14cee
Mark SessionBuffer.request_code_actions_async as deprecated
rwols May 23, 2026
baec759
There's no need for that opening_files_lock to be used in boot.py
rwols May 23, 2026
81e3ab8
Merge branch 'main' into feat/asyncio
rwols May 23, 2026
89d8ae9
Simplify types in statement
rwols May 23, 2026
ff28a4e
Fix running code actions with multiple language servers
rwols May 23, 2026
0ab6df7
Prefer apply_workspace_edit over apply_workspace_edit_async
rwols May 24, 2026
4801cd6
Merge branch 'main' into feat/asyncio
rwols May 24, 2026
c7af523
Fix code actions on save
rwols May 24, 2026
f3d0684
Rename: run_in_asyncio_thread -> run_on_asyncio_thread
rwols May 24, 2026
9d2283d
Rename: _run_in_st_thread -> _run_on_st_thread
rwols May 24, 2026
fbfba1c
Rename: run_in_main_thread -> run_on_main_thread
rwols May 24, 2026
e39ccf2
Rename: run_in_async_thread -> run_on_async_thread
rwols May 24, 2026
4081b04
Rename: wait_until_st_state -> wait_until
rwols May 24, 2026
42111fb
Merge branch 'main' into feat/asyncio
rchl May 24, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 28 additions & 18 deletions boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
from .plugin.configuration import LspDisableLanguageServerInProjectCommand
from .plugin.configuration import LspEnableLanguageServerGloballyCommand
from .plugin.configuration import LspEnableLanguageServerInProjectCommand
from .plugin.core.aio import run_coroutine_threadsafe
from .plugin.core.constants import ST_VERSION
from .plugin.core.css import load as load_css
from .plugin.core.open import g_opening_files
from .plugin.core.open import get_opening_files_lock
from .plugin.core.panels import PanelName
from .plugin.core.registry import LspCheckApplicableCommand
from .plugin.core.registry import LspNextDiagnosticCommand
Expand Down Expand Up @@ -88,10 +90,18 @@
from .plugin.tooling import LspParseVscodePackageJson
from .plugin.tooling import LspTroubleshootServerCommand
from typing import Any
from typing import TYPE_CHECKING
import os
import sublime
import sublime_aio
import sublime_plugin

if TYPE_CHECKING:
import asyncio

# Uncomment to see all invocations that are marked @deprecated in the Console.
# warnings.simplefilter('always', DeprecationWarning)

__all__ = (
"DocumentSyncListener",
"Listener",
Expand Down Expand Up @@ -219,14 +229,14 @@ def show_warning() -> None:

def plugin_unloaded() -> None:
_unregister_all_plugins()
windows.disable()
run_coroutine_threadsafe(windows.disable())
unload_settings()


class Listener(sublime_plugin.EventListener):
class Listener(sublime_aio.EventListener):

def on_exit(self) -> None:
kill_all_subprocesses()
async def on_exit(self) -> None:
await kill_all_subprocesses()

def on_load_project_async(self, window: sublime.Window) -> None:
if manager := windows.lookup(window):
Expand Down Expand Up @@ -255,27 +265,27 @@ def on_pre_move(self, view: sublime.View) -> None:
sublime.set_timeout_async(listener.on_post_move_window_async, 1)
return

def on_load(self, view: sublime.View) -> None:
async def on_load(self, view: sublime.View) -> None:
file_name = view.file_name()
if not file_name:
return
for fn in g_opening_files:
if fn == file_name or os.path.samefile(fn, file_name):
# Remove it from the pending opening files, and resolve the promise.
g_opening_files.pop(fn)[1](view)
break
if future := await self._find_opening_file_future(file_name):
future.set_result(view)

def on_pre_close(self, view: sublime.View) -> None:
async def on_pre_close(self, view: sublime.View) -> None:
file_name = view.file_name()
if not file_name:
return
for fn in g_opening_files:
if fn == file_name or os.path.samefile(fn, file_name):
tup = g_opening_files.pop(fn, None) # noqa: B909
if tup:
# The view got closed before it finished loading. This can happen.
tup[1](None)
break
if future := await self._find_opening_file_future(file_name):
# The view got closed before it finished loading. This can happen.
future.set_result(None)

async def _find_opening_file_future(self, file_name: str) -> asyncio.Future[sublime.View | None] | None:
async with get_opening_files_lock():
for fn in g_opening_files:
if fn == file_name or os.path.samefile(fn, file_name): # noqa: ASYNC240
return g_opening_files.pop(fn, None)
return None

def on_post_window_command(self, window: sublime.Window, command_name: str, args: dict[str, Any] | None) -> None:
if command_name == "show_panel":
Expand Down
1 change: 1 addition & 0 deletions dependencies.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"bracex",
"mdpopups",
"orjson",
"sublime_aio",
"typing_extensions",
"wcmatch"
]
Expand Down
51 changes: 38 additions & 13 deletions plugin/api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from __future__ import annotations

from ..protocol import ConfigurationItem
from ..protocol import DocumentUri
from ..protocol import ExecuteCommandParams
from ..protocol import LSPAny
from .core.constants import ST_STORAGE_PATH
from .core.logging import exception_log
from .core.protocol import Notification
from .core.protocol import Request
from .core.protocol import Response
from .core.settings import client_configs
from .core.types import method2attr
Expand All @@ -14,6 +19,7 @@
from functools import wraps
from pathlib import Path
from typing import Any
from typing import Awaitable
from typing import Callable
from typing import Final
from typing import final
Expand Down Expand Up @@ -221,31 +227,33 @@ def decorator(func: Callable[[Any, P], None]) -> Callable[[Any, P], None]:

def request_handler(
method: str
) -> Callable[[Callable[[Any, P], Promise[R]]], Callable[[Any, P, int], Promise[Response[R]]]]:
) -> Callable[[Callable[[Any, P], Awaitable[R]]], Callable[[Any, P, int], Awaitable[Response[R]]]]:
"""
Decorator to mark a method as a handler for a specific LSP request.
Decorator to mark a coroutine method as a handler for a specific LSP request.

Usage:
```py
@request_handler('eslint/openDoc')
def on_open_doc(self, params: TextDocumentIdentifier) -> Promise[bool]:
async def on_open_doc(self, params: TextDocumentIdentifier) -> bool:
Comment thread
rchl marked this conversation as resolved.
...
```

The decorated method will be called with the request parameters whenever the specified
request is received from the language server. The method must return a Promise that resolves
to the response value. The framework will automatically send it back to the server.
The decorated coroutine method will be called with the request parameters whenever the specified
request is received from the language server. The coroutine method must return a response value.
The framework will automatically send it back to the server.

An older, but backwards-compatible way to define a request handler is by defining a function that returns a Promise.
While that works, the advice is to define a coroutine function.

:param method: The LSP request method name (e.g., 'eslint/openDoc').
:returns: A decorator that registers the function as a request handler.
:returns: A decorator that registers the coroutine function as a request handler.
"""

def decorator(func: Callable[[Any, P], Promise[R]]) -> Callable[[Any, P, int], Promise[Response[R]]]:
def decorator(func: Callable[[Any, P], Awaitable[R]]) -> Callable[[Any, P, int], Awaitable[Response[R]]]:

@wraps(func)
def wrapper(self: Any, params: P, request_id: int) -> Promise[Response[Any]]:
promise = func(self, params)
return promise.then(lambda result: Response(request_id, result))
async def wrapper(self: Any, params: P, request_id: int) -> Response[Any]:
return Response(request_id, await func(self, params))

setattr(wrapper, HANDLER_MARKER, method)
return wrapper
Expand Down Expand Up @@ -390,6 +398,9 @@ def plugin_unloaded() -> None:
Use this as your directory to install server files. Its path is `$DATA/Package Storage/<Package Name>`.
"""

use_asyncio: bool = False
"""Set to `true` to make LSP use `async def` variants."""

@classmethod
@final
def register(cls) -> None:
Expand Down Expand Up @@ -451,8 +462,8 @@ def on_pre_start_async(cls, context: OnPreStartContext) -> None:
Override to perform any preparation needed before startup - for example installing or updating server binaries,
resolving the working directory, or injecting extra template variables into `context.variables`.

This method runs on a worker thread so perform any blocking I/O (e.g. downloading a binary, running
`npm install`) directly here without spawning additional threads.
Attempt to use non-blocking functionality for downloading binaries and running subprocesses in order to not
block the asyncio thread.

Mutations to `context.working_directory` and `context.variables` are picked up and used when launching the
server process.
Expand All @@ -464,6 +475,16 @@ def on_pre_start_async(cls, context: OnPreStartContext) -> None:
"""
pass

@classmethod
async def on_pre_start(cls, context: OnPreStartContext) -> None:
"""
Async version of on_pre_start_async.

:param context: The startup context. `context.configuration`, `context.variables` and
`context.working_directory` can be mutated to influence how the server is launched.
"""
pass

def __init__(self, weaksession: ref[Session]) -> None:
"""
Constructs a new instance.
Expand Down Expand Up @@ -491,6 +512,10 @@ def on_initialized_async(self) -> None:
"""
pass

async def on_initialized(self) -> None:
"""Async version of `on_initialize_async`."""
pass

def on_pre_send_request_async(self, request: ClientRequest, view: sublime.View | None) -> None:
"""
Notifies about a request that is about to be sent to the language server.
Expand Down
30 changes: 16 additions & 14 deletions plugin/code_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from ..protocol import CodeActionParams
from ..protocol import Command
from ..protocol import Diagnostic
from .core.aio import call_soon_threadsafe
from .core.aio import run_coroutine_threadsafe
from .core.promise import Promise
from .core.protocol import Error
from .core.protocol import Request
Expand Down Expand Up @@ -65,9 +67,9 @@ def is_quickfix(action: Command | CodeAction) -> bool:


def filter_quickfix_actions(
only_with_diagnostics: bool, response: list[Command | CodeAction] | Error | None
only_with_diagnostics: bool, response: list[Command | CodeAction] | BaseException | None
Comment on lines -68 to +70
Copy link
Copy Markdown
Member

@rchl rchl May 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting that we don't receive Error anymore. It can carry some additional info about the error which we don't have anymore.

Does it make a difference in practice? Probably not if you've managed to convert the code without using Error but still I would think it would be better to preserve maybe.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you mean here. Error inherits from Exception, which inherits from BaseException. So response could still be of type Error?

The underlying reason why some of these response / error handlers now take a BaseException instead of an Error is due to asyncio.Future handling cancellation and general exceptions. The Promise class did not. So, when wrapping a coroutine into a Promise using the Promise.wrap_task factory function, the type that that factory function returns is Promise[T | BaseException] instead of Promise[T], in order to be able to deal with the possible exception carried by asyncio.Future.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error has two additional properties - code and data.

Also one can't just use Error in place of BaseException in this particular example because then call site will raise type issues.

I'm not saying that all futures should return Error but errors during handling requests ideally should since the server can trigger error response which should be converted to Error type.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also one can't just use Error in place of BaseException in this particular example because then call site will raise type issues.

Yes... one can isinstance(ex, Error) to check that... I still don't see the problem you're trying to highlight here?

Once again the reason why BaseException is used is because asyncio.Future takes care of any exception being raised and response handlers have to deal with that. Whereas Promise previously let exceptions just be swallowed into the ether.

Copy link
Copy Markdown
Member

@rchl rchl May 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm saying that in places like

return await self.session.request(Request.codeAction(params, view))
the return value should include Error because it's one of the possible return types that server can trigger.

With the old code (even though it used promises) it did work like that. The new code doesn't include Error (not BaseException either but it really should be Error or eventually both).

EDIT: Actually, don't think it should be both. It should be Error to indicate that it's expected error value from the server. The BaseException can be thrown instead if it's triggered from normal code where we don't expect errors.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exception flow is something inherent to the await keyword. Any await suspension point may resume by raising an exception instead of returning a result from the suspension point.

I made JSON-RPC errors be part of this exception flow, which you are now questioning whether is suitable.

I do believe it’s “natural” to let this be part of the regular exception flow (which is why I wrote it like this).

A consequence of this is that one does have to write a bunch of try-except blocks in places where this wasn’t the case before. I think this enhances understanding of what may go wrong.

Copy link
Copy Markdown
Member

@rchl rchl May 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's one of the points of review - to question things. Maybe the approach to treat JSON-RPC errors as exceptions isn't really correct?

Disclaimer: I haven't even started properly reviewing the code yet. I'm just picking on things that stick out. So I don't have the full picture yet. But I'm really not sure if handling LSP Errors as exceptions is a good idea. I would think that any unexpected errors during RPC processing should be an exception but expected Error responses shouldn't. Technically any response can result in error so now every handler should be in a try/except blocks (instead of just checking if response is of Error type)?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't really followed and also haven't reviewed this PR yet, but there should be 1 or 2 places in the current code (on main) where we have special handling for certain error responses (iirc for pull diagnostics to retrigger requests when server is busy), and we might want to add some more handling of error responses for more requests in the future. There is also a ContentModified response error that a server could return for any request, and we don't have special handling for that yet (although I think it should work in a more or less correct way automatically anyway) - see

"retryOnContentModified": []
and https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#staleRequestSupportOptions

) -> list[Command | CodeAction]:
if isinstance(response, Error) or not response:
if isinstance(response, BaseException) or not response:
return []
if only_with_diagnostics:
# If there are multiple diagnostics for the region, in the hover popup we can only use those code actions which
Expand Down Expand Up @@ -302,7 +304,7 @@ def _handle_response_async(
if self._cancelled:
return
view = self._task_runner.view
tasks: list[Promise[None]] = []
tasks: list[Promise[BaseException | None]] = []
config_name, code_actions = response
session = self._task_runner.session_by_name(config_name, 'codeActionProvider')
if session and code_actions:
Expand Down Expand Up @@ -386,9 +388,9 @@ def run(
if code_actions_by_config:
self._handle_code_actions(code_actions_by_config, run_first=True)
return
self._run_async(only_kinds)
run_coroutine_threadsafe(self._run(only_kinds))

def _run_async(self, only_kinds: list[str | CodeActionKind] | None = None) -> None:
async def _run(self, only_kinds: list[str | CodeActionKind] | None = None) -> None:
view = self.view
region = first_selection_region(view)
if region is None:
Expand Down Expand Up @@ -427,13 +429,13 @@ def _handle_select(self, index: int, actions: list[tuple[ConfigName, CodeActionO
if index == -1:
return

def run_async() -> None:
async def run() -> None:
config_name, action = actions[index]
if session := self.session_by_name(config_name):
session.run_code_action_async(action, progress=True, view=self.view) \
.then(lambda response: self._handle_response_async(config_name, response))
response = await session.run_code_action(action, progress=True, view=self.view)
self._handle_response_async(config_name, response)

sublime.set_timeout_async(run_async)
run_coroutine_threadsafe(run())

def _handle_response_async(self, session_name: str, response: Any) -> None:
if isinstance(response, Error):
Expand Down Expand Up @@ -463,7 +465,7 @@ def is_enabled(self, index: int, event: dict | None = None) -> bool:
def is_visible(self, index: int, event: dict | None = None) -> bool:
if index == -1:
if self._has_session(event):
sublime.set_timeout_async(partial(self._request_menu_actions_async, event))
call_soon_threadsafe(partial(self._request_menu_actions_async, event))
return False
return index < len(self.actions_cache) and self._is_cache_valid(event)

Expand All @@ -488,14 +490,14 @@ def want_event(self) -> bool:
return True

def run(self, index: int, event: dict | None = None) -> None:
sublime.set_timeout_async(partial(self.run_async, index, event))
run_coroutine_threadsafe(self._run(index, event))

def run_async(self, index: int, event: dict | None) -> None:
async def _run(self, index: int, event: dict | None) -> None:
if self._is_cache_valid(event):
config_name, action = self.actions_cache[index]
if session := self.session_by_name(config_name):
session.run_code_action_async(action, progress=True, view=self.view) \
.then(lambda response: self._handle_response_async(config_name, response))
response = await session.run_code_action(action, progress=True, view=self.view)
self._handle_response_async(config_name, response)

def _handle_response_async(self, session_name: str, response: Any) -> None:
if isinstance(response, Error):
Expand Down
4 changes: 2 additions & 2 deletions plugin/code_lens.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from __future__ import annotations

from .core.aio import call_soon_threadsafe
from .core.constants import CODE_LENS_ENABLED_KEY
from .core.protocol import Error
from .core.protocol import ResolvedCodeLens
from .core.registry import LspTextCommand
from .core.registry import LspWindowCommand
from .core.registry import windows
from .core.views import range_to_region
from functools import partial
from typing import cast
from typing import TYPE_CHECKING
from typing_extensions import TypeGuard
Expand Down Expand Up @@ -128,7 +128,7 @@ def is_checked(self) -> bool:
def run(self) -> None:
enable = not self.is_checked()
self.window.settings().set(CODE_LENS_ENABLED_KEY, enable)
sublime.set_timeout_async(partial(self._update_views_async, enable))
call_soon_threadsafe(self._update_views_async, enable)

def _update_views_async(self, enable: bool) -> None:
window_manager = windows.lookup(self.window)
Expand Down
9 changes: 7 additions & 2 deletions plugin/color.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from .core.aio import run_coroutine_threadsafe
from .core.edit import apply_text_edits
from .core.protocol import Request
from .core.registry import LspTextCommand
Expand Down Expand Up @@ -27,7 +28,7 @@ def run(self, edit: sublime.Edit, color_information: ColorInformation) -> None:
'color': color_information['color'],
'range': self._range
}
session.send_request_async(Request.colorPresentation(params, self.view), self._handle_response_async)
session.send_request(Request.colorPresentation(params, self.view), self._handle_response_async)

def want_event(self) -> bool:
return False
Expand Down Expand Up @@ -60,4 +61,8 @@ def _on_select(self, index: int) -> None:
if index > -1:
color_pres = self._filtered_response[index]
text_edit = color_pres.get('textEdit') or {'range': self._range, 'newText': color_pres['label']}
apply_text_edits(self.view, [text_edit], label="Change Color Format", required_view_version=self._version)
run_coroutine_threadsafe(
apply_text_edits(
self.view, [text_edit], label="Change Color Format", required_view_version=self._version
)
)
Loading
Loading