1use db::kvp::Dismissable;
2use editor::Editor;
3use gpui::{Context, EventEmitter, Subscription};
4use ui::{Banner, FluentBuilder as _, prelude::*};
5use workspace::{ToolbarItemEvent, ToolbarItemLocation, ToolbarItemView, Workspace};
6
7pub struct BasedPyrightBanner {
8 dismissed: bool,
9 have_basedpyright: bool,
10 _subscriptions: [Subscription; 1],
11}
12
13impl Dismissable for BasedPyrightBanner {
14 const KEY: &str = "basedpyright-banner";
15}
16
17impl BasedPyrightBanner {
18 pub fn new(workspace: &Workspace, cx: &mut Context<Self>) -> Self {
19 let subscription = cx.subscribe(workspace.project(), |this, _, event, _| {
20 if let project::Event::LanguageServerAdded(_, name, _) = event
21 && name == "basedpyright"
22 {
23 this.have_basedpyright = true;
24 }
25 });
26 let dismissed = Self::dismissed();
27 Self {
28 dismissed,
29 have_basedpyright: false,
30 _subscriptions: [subscription],
31 }
32 }
33}
34
35impl EventEmitter<ToolbarItemEvent> for BasedPyrightBanner {}
36
37impl Render for BasedPyrightBanner {
38 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
39 div()
40 .id("basedpyright-banner")
41 .when(!self.dismissed && self.have_basedpyright, |el| {
42 el.child(
43 Banner::new()
44 .child(
45 v_flex()
46 .gap_0p5()
47 .child(Label::new("Basedpyright is now the only default language server for Python").mt_0p5())
48 .child(Label::new("We have disabled PyRight and pylsp by default. They can be re-enabled in your settings.").size(LabelSize::Small).color(Color::Muted))
49 )
50 .action_slot(
51 h_flex()
52 .gap_0p5()
53 .child(
54 Button::new("learn-more", "Learn More")
55 .icon(IconName::ArrowUpRight)
56 .label_size(LabelSize::Small)
57 .icon_size(IconSize::XSmall)
58 .icon_color(Color::Muted)
59 .on_click(|_, _, cx| {
60 cx.open_url("https://zed.dev/docs/languages/python")
61 }),
62 )
63 .child(IconButton::new("dismiss", IconName::Close).icon_size(IconSize::Small).on_click(
64 cx.listener(|this, _, _, cx| {
65 this.dismissed = true;
66 Self::set_dismissed(true, cx);
67 cx.notify();
68 }),
69 ))
70 )
71 .into_any_element(),
72 )
73 })
74 }
75}
76
77impl ToolbarItemView for BasedPyrightBanner {
78 fn set_active_pane_item(
79 &mut self,
80 active_pane_item: Option<&dyn workspace::ItemHandle>,
81 _window: &mut ui::Window,
82 cx: &mut Context<Self>,
83 ) -> ToolbarItemLocation {
84 if let Some(item) = active_pane_item
85 && let Some(editor) = item.act_as::<Editor>(cx)
86 && let Some(path) = editor.update(cx, |editor, cx| editor.target_file_abs_path(cx))
87 && let Some(file_name) = path.file_name()
88 && file_name.as_encoded_bytes().ends_with(".py".as_bytes())
89 {
90 return ToolbarItemLocation::Secondary;
91 }
92
93 ToolbarItemLocation::Hidden
94 }
95}