-
Notifications
You must be signed in to change notification settings - Fork 765
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generalize file-based RPC command client beyond VSCode (#956)
# What is this Split command-client so that other application can reuse the logic for of command server for RPC using existing VS code Command-Client. ## Reasoning In slack advised by aegis, when discussing RPC mechanism for Visual Studio. " if you replicate the vscode file rpc, please try to port the existing code as precisely as possible, without changing the sequence of file reads/writes or anything, because we spent a while making that work well " Initially copied and pasted existing code, wrote the Visual Studio side of things, then thought that best port is re-use of existing code ## Discussion * If this PR goes ahead is Command-Client in the correct location? Probably not, but was not sure were to place it and wanted to float this idea before continuing with anymore work. * Should the methods not be renamed so that they are not so VSCode specific? Yes probably, for the moment I'm happy with the way things work and there is existing code which would need updating (Cursorless) which I did not want to touch as part of this PR. ## Apologies I've installed pre-commit but have not gotten it working correctly as of yet, but wanted to get your opinion on, whether this is this a valid direction of travel. Co-authored-by: johneffo <johneffo@gmailcom.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Michael Arntzenius <daekharel@gmail.com> Co-authored-by: Pokey Rule <755842+pokey@users.noreply.github.com>
- Loading branch information
1 parent
1bc4b0d
commit 9e664a1
Showing
4 changed files
with
165 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from talon import Module | ||
|
||
mod = Module() | ||
|
||
mod.tag( | ||
"command_client", desc="For applications which implement file-based RPC with Talon" | ||
) | ||
|
||
|
||
@mod.action_class | ||
class Actions: | ||
def command_server_directory() -> str: | ||
""" | ||
The dirctory which contains the files required for communication between | ||
the application and Talon. This is the only function which absolutely | ||
must be implemented for any application using the command-client. Each | ||
application that supports file-based RPC should use its own directory | ||
name. Note that this action should only return a name; the parent | ||
directory is determined by the core command client code. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from talon import Context | ||
|
||
ctx = Context() | ||
|
||
ctx.matches = r""" | ||
app: visual_studio | ||
""" | ||
|
||
ctx.tags = ["user.command_client"] | ||
|
||
|
||
@ctx.action_class("user") | ||
class VisualStudioActions: | ||
def command_server_directory() -> str: | ||
return "visual-studio-command-server" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
from pickle import FALSE | ||
from typing import Any | ||
|
||
from talon import Context, Module, actions | ||
|
||
from .command_client import NoFileServerException, NotSet, run_command | ||
|
||
ctx = Context() | ||
|
||
ctx.matches = r""" | ||
app: vscode | ||
""" | ||
ctx.tags = ["user.command_client"] | ||
mod = Module() | ||
|
||
|
||
def command_server_or_client_fallback(command_id: str, wait: bool): | ||
"""Execute command via command server, falling back to command palette if directory not present.""" | ||
try: | ||
run_command(command_id, wait_for_finish=wait) | ||
except NoFileServerException: | ||
actions.user.command_palette() | ||
actions.user.paste(command_id) | ||
actions.key("enter") | ||
print( | ||
"Command server directory not found; falling back to command palette. For better performance, install the VSCode extension for Talon: https://marketplace.visualstudio.com/items?itemName=pokey.talon" | ||
) | ||
|
||
|
||
@ctx.action_class("user") | ||
class VsCodeAction: | ||
def command_server_directory() -> str: | ||
return "vscode-command-server" | ||
|
||
|
||
@mod.action_class | ||
class Actions: | ||
def vscode(command_id: str): | ||
"""Execute command via vscode command server, if available, or fallback | ||
to command palette.""" | ||
command_server_or_client_fallback(command_id, False) | ||
|
||
def vscode_and_wait(command_id: str): | ||
"""Execute command via vscode command server, if available, and wait | ||
for command to finish. If command server not available, uses command | ||
palette and doesn't guarantee that it will wait for command to | ||
finish.""" | ||
command_server_or_client_fallback(command_id, True) | ||
|
||
# These commands are shims, to provide backwards compatibility, they may be removed in the fuuture. | ||
# Prefer the run_command... version in command_client. | ||
def vscode_with_plugin( | ||
command_id: str, | ||
arg1: Any = NotSet, | ||
arg2: Any = NotSet, | ||
arg3: Any = NotSet, | ||
arg4: Any = NotSet, | ||
arg5: Any = NotSet, | ||
): | ||
"""Execute command via vscode command server.""" | ||
actions.user.run_rpc_command( | ||
command_id, | ||
arg1, | ||
arg2, | ||
arg3, | ||
arg4, | ||
arg5, | ||
) | ||
|
||
def vscode_with_plugin_and_wait( | ||
command_id: str, | ||
arg1: Any = NotSet, | ||
arg2: Any = NotSet, | ||
arg3: Any = NotSet, | ||
arg4: Any = NotSet, | ||
arg5: Any = NotSet, | ||
): | ||
"""Execute command via vscode command server and wait for command to finish.""" | ||
actions.user.run_rpc_command_and_wait(command_id, arg1, arg2, arg3, arg4, arg5) | ||
|
||
def vscode_get( | ||
command_id: str, | ||
arg1: Any = NotSet, | ||
arg2: Any = NotSet, | ||
arg3: Any = NotSet, | ||
arg4: Any = NotSet, | ||
arg5: Any = NotSet, | ||
) -> Any: | ||
"""Execute command via vscode command server and return command output.""" | ||
return actions.user.run_rpc_command_get( | ||
command_id, arg1, arg2, arg3, arg4, arg5 | ||
) |