extension: Make worktree argument to run_slash_command optional (#15658)

This PR updates the extension API to make the `worktree` argument to
`run_slash_command` optional.

We may not always have a worktree, and not all slash commands need them,
so by making it optional we can allow individual slash commands to
decide what to do when there is no worktree.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2024-08-01 17:34:44 -04:00 committed by GitHub
parent 88f29c8355
commit 771424e4f9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 12 additions and 9 deletions

View file

@ -91,10 +91,11 @@ impl SlashCommand for ExtensionSlashCommand {
let this = self.clone(); let this = self.clone();
move |extension, store| { move |extension, store| {
async move { async move {
let delegate = delegate.ok_or_else(|| { let resource = if let Some(delegate) = delegate {
anyhow!("no worktree for extension slash command") Some(store.data_mut().table().push(delegate)?)
})?; } else {
let resource = store.data_mut().table().push(delegate)?; None
};
let output = extension let output = extension
.call_run_slash_command( .call_run_slash_command(
store, store,

View file

@ -277,7 +277,7 @@ impl Extension {
store: &mut Store<WasmState>, store: &mut Store<WasmState>,
command: &SlashCommand, command: &SlashCommand,
argument: Option<&str>, argument: Option<&str>,
resource: Resource<Arc<dyn LspAdapterDelegate>>, resource: Option<Resource<Arc<dyn LspAdapterDelegate>>>,
) -> Result<Result<SlashCommandOutput, String>> { ) -> Result<Result<SlashCommandOutput, String>> {
match self { match self {
Extension::V007(ext) => { Extension::V007(ext) => {

View file

@ -125,7 +125,7 @@ pub trait Extension: Send + Sync {
&self, &self,
_command: SlashCommand, _command: SlashCommand,
_argument: Option<String>, _argument: Option<String>,
_worktree: &Worktree, _worktree: Option<&Worktree>,
) -> Result<SlashCommandOutput, String> { ) -> Result<SlashCommandOutput, String> {
Err("`run_slash_command` not implemented".to_string()) Err("`run_slash_command` not implemented".to_string())
} }
@ -256,7 +256,7 @@ impl wit::Guest for Component {
fn run_slash_command( fn run_slash_command(
command: SlashCommand, command: SlashCommand,
argument: Option<String>, argument: Option<String>,
worktree: &Worktree, worktree: Option<&Worktree>,
) -> Result<SlashCommandOutput, String> { ) -> Result<SlashCommandOutput, String> {
extension().run_slash_command(command, argument, worktree) extension().run_slash_command(command, argument, worktree)
} }

View file

@ -133,7 +133,7 @@ world extension {
export complete-slash-command-argument: func(command: slash-command, query: string) -> result<list<slash-command-argument-completion>, string>; export complete-slash-command-argument: func(command: slash-command, query: string) -> result<list<slash-command-argument-completion>, string>;
/// Returns the output from running the provided slash command. /// Returns the output from running the provided slash command.
export run-slash-command: func(command: slash-command, argument: option<string>, worktree: borrow<worktree>) -> result<slash-command-output, string>; export run-slash-command: func(command: slash-command, argument: option<string>, worktree: option<borrow<worktree>>) -> result<slash-command-output, string>;
/// Indexes the docs for the specified package. /// Indexes the docs for the specified package.
export index-docs: func(provider-name: string, package-name: string, database: borrow<key-value-store>) -> result<_, string>; export index-docs: func(provider-name: string, package-name: string, database: borrow<key-value-store>) -> result<_, string>;

View file

@ -181,7 +181,7 @@ impl zed::Extension for GleamExtension {
&self, &self,
command: SlashCommand, command: SlashCommand,
argument: Option<String>, argument: Option<String>,
worktree: &zed::Worktree, worktree: Option<&zed::Worktree>,
) -> Result<SlashCommandOutput, String> { ) -> Result<SlashCommandOutput, String> {
match command.name.as_str() { match command.name.as_str() {
"gleam-docs" => { "gleam-docs" => {
@ -218,6 +218,8 @@ impl zed::Extension for GleamExtension {
}) })
} }
"gleam-project" => { "gleam-project" => {
let worktree = worktree.ok_or_else(|| "no worktree")?;
let mut text = String::new(); let mut text = String::new();
text.push_str("You are in a Gleam project.\n"); text.push_str("You are in a Gleam project.\n");