1use std::path::PathBuf;
2
3use anyhow::Context as _;
4use gpui::{View, ViewContext, WindowContext};
5use language::Language;
6use url::Url;
7
8use crate::lsp_ext::find_specific_language_server_in_selection;
9
10use crate::{element::register_action, Editor, SwitchSourceHeader};
11
12const CLANGD_SERVER_NAME: &str = "clangd";
13
14fn is_c_language(language: &Language) -> bool {
15 return language.name() == "C++".into() || language.name() == "C".into();
16}
17
18pub fn switch_source_header(
19 editor: &mut Editor,
20 _: &SwitchSourceHeader,
21 cx: &mut ViewContext<'_, Editor>,
22) {
23 let Some(project) = &editor.project else {
24 return;
25 };
26 let Some(workspace) = editor.workspace() else {
27 return;
28 };
29
30 let Some((_, _, server_to_query, buffer)) =
31 find_specific_language_server_in_selection(editor, cx, is_c_language, CLANGD_SERVER_NAME)
32 else {
33 return;
34 };
35
36 let project = project.clone();
37 let buffer_snapshot = buffer.read(cx).snapshot();
38 let source_file = buffer_snapshot
39 .file()
40 .unwrap()
41 .file_name(cx)
42 .to_str()
43 .unwrap()
44 .to_owned();
45
46 let switch_source_header_task = project.update(cx, |project, cx| {
47 project.request_lsp(
48 buffer,
49 project::LanguageServerToQuery::Other(server_to_query),
50 project::lsp_ext_command::SwitchSourceHeader,
51 cx,
52 )
53 });
54 cx.spawn(|_editor, mut cx| async move {
55 let switch_source_header = switch_source_header_task
56 .await
57 .with_context(|| format!("Switch source/header LSP request for path \"{}\" failed", source_file))?;
58 if switch_source_header.0.is_empty() {
59 log::info!("Clangd returned an empty string when requesting to switch source/header from \"{}\"", source_file);
60 return Ok(());
61 }
62
63 let goto = Url::parse(&switch_source_header.0).with_context(|| {
64 format!(
65 "Parsing URL \"{}\" returned from switch source/header failed",
66 switch_source_header.0
67 )
68 })?;
69
70 workspace
71 .update(&mut cx, |workspace, view_cx| {
72 workspace.open_abs_path(PathBuf::from(goto.path()), false, view_cx)
73 })
74 .with_context(|| {
75 format!(
76 "Switch source/header could not open \"{}\" in workspace",
77 goto.path()
78 )
79 })?
80 .await
81 .map(|_| ())
82 })
83 .detach_and_log_err(cx);
84}
85
86pub fn apply_related_actions(editor: &View<Editor>, cx: &mut WindowContext) {
87 if editor.update(cx, |e, cx| {
88 find_specific_language_server_in_selection(e, cx, is_c_language, CLANGD_SERVER_NAME)
89 .is_some()
90 }) {
91 register_action(editor, cx, switch_source_header);
92 }
93}