1use zed_extension_api::{
2 self as zed, SlashCommand, SlashCommandArgumentCompletion, SlashCommandOutput,
3 SlashCommandOutputSection, Worktree,
4};
5
6struct SlashCommandsExampleExtension;
7
8impl zed::Extension for SlashCommandsExampleExtension {
9 fn new() -> Self {
10 SlashCommandsExampleExtension
11 }
12
13 fn complete_slash_command_argument(
14 &self,
15 command: SlashCommand,
16 _args: Vec<String>,
17 ) -> Result<Vec<zed_extension_api::SlashCommandArgumentCompletion>, String> {
18 match command.name.as_str() {
19 "echo" => Ok(vec![]),
20 "pick-one" => Ok(vec![
21 SlashCommandArgumentCompletion {
22 label: "Option One".to_string(),
23 new_text: "option-1".to_string(),
24 run_command: true,
25 },
26 SlashCommandArgumentCompletion {
27 label: "Option Two".to_string(),
28 new_text: "option-2".to_string(),
29 run_command: true,
30 },
31 SlashCommandArgumentCompletion {
32 label: "Option Three".to_string(),
33 new_text: "option-3".to_string(),
34 run_command: true,
35 },
36 ]),
37 command => Err(format!("unknown slash command: \"{command}\"")),
38 }
39 }
40
41 fn run_slash_command(
42 &self,
43 command: SlashCommand,
44 args: Vec<String>,
45 _worktree: Option<&Worktree>,
46 ) -> Result<SlashCommandOutput, String> {
47 match command.name.as_str() {
48 "echo" => {
49 if args.is_empty() {
50 return Err("nothing to echo".to_string());
51 }
52
53 let text = args.join(" ");
54
55 Ok(SlashCommandOutput {
56 sections: vec![SlashCommandOutputSection {
57 range: (0..text.len()).into(),
58 label: "Echo".to_string(),
59 }],
60 text,
61 })
62 }
63 "pick-one" => {
64 let Some(selection) = args.first() else {
65 return Err("no option selected".to_string());
66 };
67
68 match selection.as_str() {
69 "option-1" | "option-2" | "option-3" => {}
70 invalid_option => {
71 return Err(format!("{invalid_option} is not a valid option"));
72 }
73 }
74
75 let text = format!("You chose {selection}.");
76
77 Ok(SlashCommandOutput {
78 sections: vec![SlashCommandOutputSection {
79 range: (0..text.len()).into(),
80 label: format!("Pick One: {selection}"),
81 }],
82 text,
83 })
84 }
85 command => Err(format!("unknown slash command: \"{command}\"")),
86 }
87 }
88}
89
90zed::register_extension!(SlashCommandsExampleExtension);