1use crate::sign_in::CopilotCodeVerification;
2use anyhow::Result;
3use copilot::{Copilot, SignOut, Status};
4use editor::{scroll::Autoscroll, Editor};
5use fs::Fs;
6use gpui::{
7 div, Action, AnchorCorner, AppContext, AsyncWindowContext, Entity, IntoElement, ParentElement,
8 Render, Subscription, View, ViewContext, WeakView, WindowContext,
9};
10use language::{
11 language_settings::{self, all_language_settings, AllLanguageSettings},
12 File, Language,
13};
14use settings::{update_settings_file, Settings, SettingsStore};
15use std::{path::Path, sync::Arc};
16use util::{paths, ResultExt};
17use workspace::{
18 create_and_open_local_file,
19 item::ItemHandle,
20 ui::{
21 popover_menu, ButtonCommon, Clickable, ContextMenu, IconButton, IconName, IconSize, Tooltip,
22 },
23 StatusItemView, Toast, Workspace,
24};
25use zed_actions::OpenBrowser;
26
27const COPILOT_SETTINGS_URL: &str = "https://github.com/settings/copilot";
28const COPILOT_STARTING_TOAST_ID: usize = 1337;
29const COPILOT_ERROR_TOAST_ID: usize = 1338;
30
31pub struct CopilotButton {
32 editor_subscription: Option<(Subscription, usize)>,
33 editor_enabled: Option<bool>,
34 language: Option<Arc<Language>>,
35 file: Option<Arc<dyn File>>,
36 fs: Arc<dyn Fs>,
37}
38
39impl Render for CopilotButton {
40 fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
41 let all_language_settings = all_language_settings(None, cx);
42 if !all_language_settings.copilot.feature_enabled {
43 return div();
44 }
45
46 let Some(copilot) = Copilot::global(cx) else {
47 return div();
48 };
49 let status = copilot.read(cx).status();
50
51 let enabled = self
52 .editor_enabled
53 .unwrap_or_else(|| all_language_settings.copilot_enabled(None, None));
54
55 let icon = match status {
56 Status::Error(_) => IconName::CopilotError,
57 Status::Authorized => {
58 if enabled {
59 IconName::Copilot
60 } else {
61 IconName::CopilotDisabled
62 }
63 }
64 _ => IconName::CopilotInit,
65 };
66
67 if let Status::Error(e) = status {
68 return div().child(
69 IconButton::new("copilot-error", icon)
70 .icon_size(IconSize::Small)
71 .on_click(cx.listener(move |_, _, cx| {
72 if let Some(workspace) = cx.window_handle().downcast::<Workspace>() {
73 workspace
74 .update(cx, |workspace, cx| {
75 workspace.show_toast(
76 Toast::new(
77 COPILOT_ERROR_TOAST_ID,
78 format!("Copilot can't be started: {}", e),
79 )
80 .on_click(
81 "Reinstall Copilot",
82 |cx| {
83 if let Some(copilot) = Copilot::global(cx) {
84 copilot
85 .update(cx, |copilot, cx| {
86 copilot.reinstall(cx)
87 })
88 .detach();
89 }
90 },
91 ),
92 cx,
93 );
94 })
95 .ok();
96 }
97 }))
98 .tooltip(|cx| Tooltip::text("GitHub Copilot", cx)),
99 );
100 }
101 let this = cx.view().clone();
102
103 div().child(
104 popover_menu("copilot")
105 .menu(move |cx| match status {
106 Status::Authorized => {
107 Some(this.update(cx, |this, cx| this.build_copilot_menu(cx)))
108 }
109 _ => Some(this.update(cx, |this, cx| this.build_copilot_start_menu(cx))),
110 })
111 .anchor(AnchorCorner::BottomRight)
112 .trigger(
113 IconButton::new("copilot-icon", icon)
114 .tooltip(|cx| Tooltip::text("GitHub Copilot", cx)),
115 ),
116 )
117 }
118}
119
120impl CopilotButton {
121 pub fn new(fs: Arc<dyn Fs>, cx: &mut ViewContext<Self>) -> Self {
122 if let Some(copilot) = Copilot::global(cx) {
123 cx.observe(&copilot, |_, _, cx| cx.notify()).detach()
124 }
125
126 cx.observe_global::<SettingsStore>(move |_, cx| cx.notify())
127 .detach();
128
129 Self {
130 editor_subscription: None,
131 editor_enabled: None,
132 language: None,
133 file: None,
134 fs,
135 }
136 }
137
138 pub fn build_copilot_start_menu(&mut self, cx: &mut ViewContext<Self>) -> View<ContextMenu> {
139 let fs = self.fs.clone();
140 ContextMenu::build(cx, |menu, _| {
141 menu.entry("Sign In", None, initiate_sign_in).entry(
142 "Disable Copilot",
143 None,
144 move |cx| hide_copilot(fs.clone(), cx),
145 )
146 })
147 }
148
149 pub fn build_copilot_menu(&mut self, cx: &mut ViewContext<Self>) -> View<ContextMenu> {
150 let fs = self.fs.clone();
151
152 ContextMenu::build(cx, move |mut menu, cx| {
153 if let Some(language) = self.language.clone() {
154 let fs = fs.clone();
155 let language_enabled =
156 language_settings::language_settings(Some(&language), None, cx)
157 .show_copilot_suggestions;
158
159 menu = menu.entry(
160 format!(
161 "{} Suggestions for {}",
162 if language_enabled { "Hide" } else { "Show" },
163 language.name()
164 ),
165 None,
166 move |cx| toggle_copilot_for_language(language.clone(), fs.clone(), cx),
167 );
168 }
169
170 let settings = AllLanguageSettings::get_global(cx);
171
172 if let Some(file) = &self.file {
173 let path = file.path().clone();
174 let path_enabled = settings.copilot_enabled_for_path(&path);
175
176 menu = menu.entry(
177 format!(
178 "{} Suggestions for This Path",
179 if path_enabled { "Hide" } else { "Show" }
180 ),
181 None,
182 move |cx| {
183 if let Some(workspace) = cx.window_handle().downcast::<Workspace>() {
184 if let Ok(workspace) = workspace.root_view(cx) {
185 let workspace = workspace.downgrade();
186 cx.spawn(|cx| {
187 configure_disabled_globs(
188 workspace,
189 path_enabled.then_some(path.clone()),
190 cx,
191 )
192 })
193 .detach_and_log_err(cx);
194 }
195 }
196 },
197 );
198 }
199
200 let globally_enabled = settings.copilot_enabled(None, None);
201 menu.entry(
202 if globally_enabled {
203 "Hide Suggestions for All Files"
204 } else {
205 "Show Suggestions for All Files"
206 },
207 None,
208 move |cx| toggle_copilot_globally(fs.clone(), cx),
209 )
210 .separator()
211 .link(
212 "Copilot Settings",
213 OpenBrowser {
214 url: COPILOT_SETTINGS_URL.to_string(),
215 }
216 .boxed_clone(),
217 )
218 .action("Sign Out", SignOut.boxed_clone())
219 })
220 }
221
222 pub fn update_enabled(&mut self, editor: View<Editor>, cx: &mut ViewContext<Self>) {
223 let editor = editor.read(cx);
224 let snapshot = editor.buffer().read(cx).snapshot(cx);
225 let suggestion_anchor = editor.selections.newest_anchor().start;
226 let language = snapshot.language_at(suggestion_anchor);
227 let file = snapshot.file_at(suggestion_anchor).cloned();
228
229 self.editor_enabled = Some(
230 file.as_ref().map(|file| !file.is_private()).unwrap_or(true)
231 && all_language_settings(self.file.as_ref(), cx)
232 .copilot_enabled(language, file.as_ref().map(|file| file.path().as_ref())),
233 );
234 self.language = language.cloned();
235 self.file = file;
236
237 cx.notify()
238 }
239}
240
241impl StatusItemView for CopilotButton {
242 fn set_active_pane_item(&mut self, item: Option<&dyn ItemHandle>, cx: &mut ViewContext<Self>) {
243 if let Some(editor) = item.and_then(|item| item.act_as::<Editor>(cx)) {
244 self.editor_subscription = Some((
245 cx.observe(&editor, Self::update_enabled),
246 editor.entity_id().as_u64() as usize,
247 ));
248 self.update_enabled(editor, cx);
249 } else {
250 self.language = None;
251 self.editor_subscription = None;
252 self.editor_enabled = None;
253 }
254 cx.notify();
255 }
256}
257
258async fn configure_disabled_globs(
259 workspace: WeakView<Workspace>,
260 path_to_disable: Option<Arc<Path>>,
261 mut cx: AsyncWindowContext,
262) -> Result<()> {
263 let settings_editor = workspace
264 .update(&mut cx, |_, cx| {
265 create_and_open_local_file(&paths::SETTINGS, cx, || {
266 settings::initial_user_settings_content().as_ref().into()
267 })
268 })?
269 .await?
270 .downcast::<Editor>()
271 .unwrap();
272
273 settings_editor.downgrade().update(&mut cx, |item, cx| {
274 let text = item.buffer().read(cx).snapshot(cx).text();
275
276 let settings = cx.global::<SettingsStore>();
277 let edits = settings.edits_for_update::<AllLanguageSettings>(&text, |file| {
278 let copilot = file.copilot.get_or_insert_with(Default::default);
279 let globs = copilot.disabled_globs.get_or_insert_with(|| {
280 settings
281 .get::<AllLanguageSettings>(None)
282 .copilot
283 .disabled_globs
284 .iter()
285 .map(|glob| glob.glob().to_string())
286 .collect()
287 });
288
289 if let Some(path_to_disable) = &path_to_disable {
290 globs.push(path_to_disable.to_string_lossy().into_owned());
291 } else {
292 globs.clear();
293 }
294 });
295
296 if !edits.is_empty() {
297 item.change_selections(Some(Autoscroll::newest()), cx, |selections| {
298 selections.select_ranges(edits.iter().map(|e| e.0.clone()));
299 });
300
301 // When *enabling* a path, don't actually perform an edit, just select the range.
302 if path_to_disable.is_some() {
303 item.edit(edits.iter().cloned(), cx);
304 }
305 }
306 })?;
307
308 anyhow::Ok(())
309}
310
311fn toggle_copilot_globally(fs: Arc<dyn Fs>, cx: &mut AppContext) {
312 let show_copilot_suggestions = all_language_settings(None, cx).copilot_enabled(None, None);
313 update_settings_file::<AllLanguageSettings>(fs, cx, move |file| {
314 file.defaults.show_copilot_suggestions = Some(!show_copilot_suggestions)
315 });
316}
317
318fn toggle_copilot_for_language(language: Arc<Language>, fs: Arc<dyn Fs>, cx: &mut AppContext) {
319 let show_copilot_suggestions =
320 all_language_settings(None, cx).copilot_enabled(Some(&language), None);
321 update_settings_file::<AllLanguageSettings>(fs, cx, move |file| {
322 file.languages
323 .entry(language.name())
324 .or_default()
325 .show_copilot_suggestions = Some(!show_copilot_suggestions);
326 });
327}
328
329fn hide_copilot(fs: Arc<dyn Fs>, cx: &mut AppContext) {
330 update_settings_file::<AllLanguageSettings>(fs, cx, move |file| {
331 file.features.get_or_insert(Default::default()).copilot = Some(false);
332 });
333}
334
335pub fn initiate_sign_in(cx: &mut WindowContext) {
336 let Some(copilot) = Copilot::global(cx) else {
337 return;
338 };
339 let status = copilot.read(cx).status();
340 let Some(workspace) = cx.window_handle().downcast::<Workspace>() else {
341 return;
342 };
343 match status {
344 Status::Starting { task } => {
345 let Some(workspace) = cx.window_handle().downcast::<Workspace>() else {
346 return;
347 };
348
349 let Ok(workspace) = workspace.update(cx, |workspace, cx| {
350 workspace.show_toast(
351 Toast::new(COPILOT_STARTING_TOAST_ID, "Copilot is starting..."),
352 cx,
353 );
354 workspace.weak_handle()
355 }) else {
356 return;
357 };
358
359 cx.spawn(|mut cx| async move {
360 task.await;
361 if let Some(copilot) = cx.update(|cx| Copilot::global(cx)).ok().flatten() {
362 workspace
363 .update(&mut cx, |workspace, cx| match copilot.read(cx).status() {
364 Status::Authorized => workspace.show_toast(
365 Toast::new(COPILOT_STARTING_TOAST_ID, "Copilot has started!"),
366 cx,
367 ),
368 _ => {
369 workspace.dismiss_toast(COPILOT_STARTING_TOAST_ID, cx);
370 copilot
371 .update(cx, |copilot, cx| copilot.sign_in(cx))
372 .detach_and_log_err(cx);
373 }
374 })
375 .log_err();
376 }
377 })
378 .detach();
379 }
380 _ => {
381 copilot.update(cx, |this, cx| this.sign_in(cx)).detach();
382 workspace
383 .update(cx, |this, cx| {
384 this.toggle_modal(cx, |cx| CopilotCodeVerification::new(&copilot, cx));
385 })
386 .ok();
387 }
388 }
389}