1use crate::context_editor::ContextEditor;
2use anyhow::Result;
3pub use assistant_slash_command::SlashCommand;
4use assistant_slash_command::{AfterCompletion, SlashCommandLine, SlashCommandWorkingSet};
5use editor::{CompletionProvider, Editor};
6use fuzzy::{match_strings, StringMatchCandidate};
7use gpui::{App, AppContext as _, Context, Entity, Task, WeakEntity, Window};
8use language::{Anchor, Buffer, CompletionDocumentation, LanguageServerId, ToPoint};
9use parking_lot::Mutex;
10use project::CompletionIntent;
11use rope::Point;
12use std::{
13 cell::RefCell,
14 ops::Range,
15 rc::Rc,
16 sync::{
17 atomic::{AtomicBool, Ordering::SeqCst},
18 Arc,
19 },
20};
21use workspace::Workspace;
22
23pub struct SlashCommandCompletionProvider {
24 cancel_flag: Mutex<Arc<AtomicBool>>,
25 slash_commands: Arc<SlashCommandWorkingSet>,
26 editor: Option<WeakEntity<ContextEditor>>,
27 workspace: Option<WeakEntity<Workspace>>,
28}
29
30impl SlashCommandCompletionProvider {
31 pub fn new(
32 slash_commands: Arc<SlashCommandWorkingSet>,
33 editor: Option<WeakEntity<ContextEditor>>,
34 workspace: Option<WeakEntity<Workspace>>,
35 ) -> Self {
36 Self {
37 cancel_flag: Mutex::new(Arc::new(AtomicBool::new(false))),
38 slash_commands,
39 editor,
40 workspace,
41 }
42 }
43
44 fn complete_command_name(
45 &self,
46 command_name: &str,
47 command_range: Range<Anchor>,
48 name_range: Range<Anchor>,
49 window: &mut Window,
50 cx: &mut App,
51 ) -> Task<Result<Vec<project::Completion>>> {
52 let slash_commands = self.slash_commands.clone();
53 let candidates = slash_commands
54 .command_names(cx)
55 .into_iter()
56 .enumerate()
57 .map(|(ix, def)| StringMatchCandidate::new(ix, &def))
58 .collect::<Vec<_>>();
59 let command_name = command_name.to_string();
60 let editor = self.editor.clone();
61 let workspace = self.workspace.clone();
62 window.spawn(cx, |mut cx| async move {
63 let matches = match_strings(
64 &candidates,
65 &command_name,
66 true,
67 usize::MAX,
68 &Default::default(),
69 cx.background_executor().clone(),
70 )
71 .await;
72
73 cx.update(|_, cx| {
74 matches
75 .into_iter()
76 .filter_map(|mat| {
77 let command = slash_commands.command(&mat.string, cx)?;
78 let mut new_text = mat.string.clone();
79 let requires_argument = command.requires_argument();
80 let accepts_arguments = command.accepts_arguments();
81 if requires_argument || accepts_arguments {
82 new_text.push(' ');
83 }
84
85 let confirm =
86 editor
87 .clone()
88 .zip(workspace.clone())
89 .map(|(editor, workspace)| {
90 let command_name = mat.string.clone();
91 let command_range = command_range.clone();
92 let editor = editor.clone();
93 let workspace = workspace.clone();
94 Arc::new(
95 move |intent: CompletionIntent,
96 window: &mut Window,
97 cx: &mut App| {
98 if !requires_argument
99 && (!accepts_arguments || intent.is_complete())
100 {
101 editor
102 .update(cx, |editor, cx| {
103 editor.run_command(
104 command_range.clone(),
105 &command_name,
106 &[],
107 true,
108 workspace.clone(),
109 window,
110 cx,
111 );
112 })
113 .ok();
114 false
115 } else {
116 requires_argument || accepts_arguments
117 }
118 },
119 ) as Arc<_>
120 });
121 Some(project::Completion {
122 old_range: name_range.clone(),
123 documentation: Some(CompletionDocumentation::SingleLine(
124 command.description(),
125 )),
126 new_text,
127 label: command.label(cx),
128 server_id: LanguageServerId(0),
129 lsp_completion: Default::default(),
130 confirm,
131 resolved: true,
132 })
133 })
134 .collect()
135 })
136 })
137 }
138
139 fn complete_command_argument(
140 &self,
141 command_name: &str,
142 arguments: &[String],
143 command_range: Range<Anchor>,
144 argument_range: Range<Anchor>,
145 last_argument_range: Range<Anchor>,
146 window: &mut Window,
147 cx: &mut App,
148 ) -> Task<Result<Vec<project::Completion>>> {
149 let new_cancel_flag = Arc::new(AtomicBool::new(false));
150 let mut flag = self.cancel_flag.lock();
151 flag.store(true, SeqCst);
152 *flag = new_cancel_flag.clone();
153 if let Some(command) = self.slash_commands.command(command_name, cx) {
154 let completions = command.complete_argument(
155 arguments,
156 new_cancel_flag.clone(),
157 self.workspace.clone(),
158 window,
159 cx,
160 );
161 let command_name: Arc<str> = command_name.into();
162 let editor = self.editor.clone();
163 let workspace = self.workspace.clone();
164 let arguments = arguments.to_vec();
165 cx.background_spawn(async move {
166 Ok(completions
167 .await?
168 .into_iter()
169 .map(|new_argument| {
170 let confirm =
171 editor
172 .clone()
173 .zip(workspace.clone())
174 .map(|(editor, workspace)| {
175 Arc::new({
176 let mut completed_arguments = arguments.clone();
177 if new_argument.replace_previous_arguments {
178 completed_arguments.clear();
179 } else {
180 completed_arguments.pop();
181 }
182 completed_arguments.push(new_argument.new_text.clone());
183
184 let command_range = command_range.clone();
185 let command_name = command_name.clone();
186 move |intent: CompletionIntent,
187 window: &mut Window,
188 cx: &mut App| {
189 if new_argument.after_completion.run()
190 || intent.is_complete()
191 {
192 editor
193 .update(cx, |editor, cx| {
194 editor.run_command(
195 command_range.clone(),
196 &command_name,
197 &completed_arguments,
198 true,
199 workspace.clone(),
200 window,
201 cx,
202 );
203 })
204 .ok();
205 false
206 } else {
207 !new_argument.after_completion.run()
208 }
209 }
210 }) as Arc<_>
211 });
212
213 let mut new_text = new_argument.new_text.clone();
214 if new_argument.after_completion == AfterCompletion::Continue {
215 new_text.push(' ');
216 }
217
218 project::Completion {
219 old_range: if new_argument.replace_previous_arguments {
220 argument_range.clone()
221 } else {
222 last_argument_range.clone()
223 },
224 label: new_argument.label,
225 new_text,
226 documentation: None,
227 server_id: LanguageServerId(0),
228 lsp_completion: Default::default(),
229 confirm,
230 resolved: true,
231 }
232 })
233 .collect())
234 })
235 } else {
236 Task::ready(Ok(Vec::new()))
237 }
238 }
239}
240
241impl CompletionProvider for SlashCommandCompletionProvider {
242 fn completions(
243 &self,
244 buffer: &Entity<Buffer>,
245 buffer_position: Anchor,
246 _: editor::CompletionContext,
247 window: &mut Window,
248 cx: &mut Context<Editor>,
249 ) -> Task<Result<Vec<project::Completion>>> {
250 let Some((name, arguments, command_range, last_argument_range)) =
251 buffer.update(cx, |buffer, _cx| {
252 let position = buffer_position.to_point(buffer);
253 let line_start = Point::new(position.row, 0);
254 let mut lines = buffer.text_for_range(line_start..position).lines();
255 let line = lines.next()?;
256 let call = SlashCommandLine::parse(line)?;
257
258 let command_range_start = Point::new(position.row, call.name.start as u32 - 1);
259 let command_range_end = Point::new(
260 position.row,
261 call.arguments.last().map_or(call.name.end, |arg| arg.end) as u32,
262 );
263 let command_range = buffer.anchor_after(command_range_start)
264 ..buffer.anchor_after(command_range_end);
265
266 let name = line[call.name.clone()].to_string();
267 let (arguments, last_argument_range) = if let Some(argument) = call.arguments.last()
268 {
269 let last_arg_start =
270 buffer.anchor_after(Point::new(position.row, argument.start as u32));
271 let first_arg_start = call.arguments.first().expect("we have the last element");
272 let first_arg_start =
273 buffer.anchor_after(Point::new(position.row, first_arg_start.start as u32));
274 let arguments = call
275 .arguments
276 .iter()
277 .filter_map(|argument| Some(line.get(argument.clone())?.to_string()))
278 .collect::<Vec<_>>();
279 let argument_range = first_arg_start..buffer_position;
280 (
281 Some((arguments, argument_range)),
282 last_arg_start..buffer_position,
283 )
284 } else {
285 let start =
286 buffer.anchor_after(Point::new(position.row, call.name.start as u32));
287 (None, start..buffer_position)
288 };
289
290 Some((name, arguments, command_range, last_argument_range))
291 })
292 else {
293 return Task::ready(Ok(Vec::new()));
294 };
295
296 if let Some((arguments, argument_range)) = arguments {
297 self.complete_command_argument(
298 &name,
299 &arguments,
300 command_range,
301 argument_range,
302 last_argument_range,
303 window,
304 cx,
305 )
306 } else {
307 self.complete_command_name(&name, command_range, last_argument_range, window, cx)
308 }
309 }
310
311 fn resolve_completions(
312 &self,
313 _: Entity<Buffer>,
314 _: Vec<usize>,
315 _: Rc<RefCell<Box<[project::Completion]>>>,
316 _: &mut Context<Editor>,
317 ) -> Task<Result<bool>> {
318 Task::ready(Ok(true))
319 }
320
321 fn is_completion_trigger(
322 &self,
323 buffer: &Entity<Buffer>,
324 position: language::Anchor,
325 _text: &str,
326 _trigger_in_words: bool,
327 cx: &mut Context<Editor>,
328 ) -> bool {
329 let buffer = buffer.read(cx);
330 let position = position.to_point(buffer);
331 let line_start = Point::new(position.row, 0);
332 let mut lines = buffer.text_for_range(line_start..position).lines();
333 if let Some(line) = lines.next() {
334 SlashCommandLine::parse(line).is_some()
335 } else {
336 false
337 }
338 }
339
340 fn sort_completions(&self) -> bool {
341 false
342 }
343}