Skip to content

Commit 9689398

Browse files
Fetch the language servers
1 parent bbb1355 commit 9689398

File tree

2 files changed

+52
-10
lines changed

2 files changed

+52
-10
lines changed

crates/language_tools/src/lsp_tool.rs

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,75 @@
1+
use collections::HashSet;
2+
use editor::Editor;
3+
use gpui::{Entity, WeakEntity};
4+
use project::LspStore;
15
use ui::{IconButtonShape, Tooltip, prelude::*};
2-
use workspace::StatusItemView;
6+
use workspace::{StatusItemView, Workspace};
37

4-
pub struct LspTool {}
8+
pub struct LspTool {
9+
active_editor: Option<WeakEntity<Editor>>,
10+
lsp_store: Entity<LspStore>,
11+
}
512

613
impl LspTool {
7-
pub fn new() -> Self {
8-
Self {}
14+
pub fn new(workspace: &Workspace, cx: &App) -> Self {
15+
let lsp_store = workspace.project().read(cx).lsp_store();
16+
Self {
17+
active_editor: None,
18+
lsp_store,
19+
}
920
}
1021
}
1122

1223
impl StatusItemView for LspTool {
1324
fn set_active_pane_item(
1425
&mut self,
15-
_active_pane_item: Option<&dyn workspace::ItemHandle>,
26+
active_pane_item: Option<&dyn workspace::ItemHandle>,
1627
_window: &mut ui::Window,
17-
_cx: &mut ui::Context<Self>,
28+
cx: &mut ui::Context<Self>,
1829
) {
19-
// TODO kb see inline_completion_button.rs
30+
self.active_editor = active_pane_item
31+
.and_then(|item| item.downcast::<Editor>().map(|editor| editor.downgrade()));
32+
cx.notify();
2033
}
2134
}
2235

2336
impl Render for LspTool {
24-
// TODO kb won't work for remote clients, disable for now
37+
// TODO kb won't work for remote clients for now
2538
// TODO kb add a setting to remove this button out of the status bar
2639
fn render(
2740
&mut self,
2841
_window: &mut ui::Window,
2942
cx: &mut ui::Context<Self>,
3043
) -> impl ui::IntoElement {
44+
let Some(editor) = self
45+
.active_editor
46+
.as_ref()
47+
.and_then(|editor| editor.upgrade())
48+
else {
49+
return div();
50+
};
51+
52+
let buffers = editor.read(cx).buffer().read(cx).all_buffers();
53+
let mut server_ids = HashSet::default();
54+
let applicable_language_servers = self.lsp_store.update(cx, |lsp_store, cx| {
55+
buffers
56+
.iter()
57+
.flat_map(|buffer| {
58+
buffer.update(cx, |buffer, cx| {
59+
lsp_store
60+
.language_servers_for_local_buffer(buffer, cx)
61+
.filter(|(_, server)| server_ids.insert(server.server_id()))
62+
.map(|(adapter, server)| (adapter.clone(), server.clone()))
63+
.collect::<Vec<_>>()
64+
})
65+
})
66+
.collect::<Vec<_>>()
67+
});
68+
dbg!(applicable_language_servers.len());
69+
if applicable_language_servers.is_empty() {
70+
return div();
71+
}
72+
3173
div().child(
3274
IconButton::new("zed-lsp-tool-button", IconName::Bolt)
3375
.shape(IconButtonShape::Square)

crates/zed/src/zed.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ pub fn initialize_workspace(
237237
cx.new(|cx| toolchain_selector::ActiveToolchain::new(workspace, window, cx));
238238
let vim_mode_indicator = cx.new(|cx| vim::ModeIndicator::new(window, cx));
239239
let image_info = cx.new(|_cx| ImageInfo::new(workspace));
240-
let lsp_tool = cx.new(|_| LspTool::new());
240+
let lsp_tool = cx.new(|cx| LspTool::new(workspace, cx));
241241

242242
let cursor_position =
243243
cx.new(|_| go_to_line::cursor_position::CursorPosition::new(workspace));
@@ -246,8 +246,8 @@ pub fn initialize_workspace(
246246
status_bar.add_left_item(diagnostic_summary, window, cx);
247247
status_bar.add_left_item(activity_indicator, window, cx);
248248
status_bar.add_right_item(edit_prediction_button, window, cx);
249-
status_bar.add_right_item(lsp_tool, window, cx);
250249
status_bar.add_right_item(active_buffer_language, window, cx);
250+
status_bar.add_right_item(lsp_tool, window, cx);
251251
status_bar.add_right_item(active_toolchain_language, window, cx);
252252
status_bar.add_right_item(vim_mode_indicator, window, cx);
253253
status_bar.add_right_item(cursor_position, window, cx);

0 commit comments

Comments
 (0)