Unnamed repository; edit this file 'description' to name the repository.
-rw-r--r--crates/load-cargo/src/lib.rs14
-rw-r--r--crates/proc-macro-api/src/bidirectional_protocol/msg.rs2
-rw-r--r--crates/proc-macro-srv-cli/src/main_loop.rs26
-rw-r--r--crates/proc-macro-srv/src/lib.rs1
-rw-r--r--crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs15
5 files changed, 51 insertions, 7 deletions
diff --git a/crates/load-cargo/src/lib.rs b/crates/load-cargo/src/lib.rs
index 5122051fb3..c8a157e88e 100644
--- a/crates/load-cargo/src/lib.rs
+++ b/crates/load-cargo/src/lib.rs
@@ -546,6 +546,20 @@ impl ProcMacroExpander for Expander {
let slice = text.get(start as usize..end as usize).map(ToOwned::to_owned);
Ok(SubResponse::SourceTextResult { text: slice })
}
+ SubRequest::FileName { file_id } => {
+ let file = FileId::from_raw(file_id);
+ let source_root_id = db.file_source_root(file).source_root_id(db);
+ let source_root = db.source_root(source_root_id).source_root(db);
+
+ let name = source_root.path_for_file(&file).and_then(|path| {
+ path.name_and_extension().map(|(name, ext)| match ext {
+ Some(ext) => format!("{name}.{ext}"),
+ None => name.to_owned(),
+ })
+ });
+
+ Ok(SubResponse::FileNameResult { name: name.unwrap_or_default() })
+ }
};
match self.0.expand(
subtree.view(),
diff --git a/crates/proc-macro-api/src/bidirectional_protocol/msg.rs b/crates/proc-macro-api/src/bidirectional_protocol/msg.rs
index cf8becd922..7edd9062d9 100644
--- a/crates/proc-macro-api/src/bidirectional_protocol/msg.rs
+++ b/crates/proc-macro-api/src/bidirectional_protocol/msg.rs
@@ -10,11 +10,13 @@ use crate::{
#[derive(Debug, Serialize, Deserialize)]
pub enum SubRequest {
+ FileName { file_id: u32 },
SourceText { file_id: u32, start: u32, end: u32 },
}
#[derive(Debug, Serialize, Deserialize)]
pub enum SubResponse {
+ FileNameResult { name: String },
SourceTextResult { text: Option<String> },
}
diff --git a/crates/proc-macro-srv-cli/src/main_loop.rs b/crates/proc-macro-srv-cli/src/main_loop.rs
index 1a929a02ac..279eab06c0 100644
--- a/crates/proc-macro-srv-cli/src/main_loop.rs
+++ b/crates/proc-macro-srv-cli/src/main_loop.rs
@@ -167,6 +167,32 @@ struct ProcMacroClientHandle<'a, C: Codec> {
}
impl<C: Codec> proc_macro_srv::ProcMacroClientInterface for ProcMacroClientHandle<'_, C> {
+ fn file(&mut self, file_id: u32) -> String {
+ let req =
+ bidirectional::BidirectionalMessage::SubRequest(bidirectional::SubRequest::FileName {
+ file_id,
+ });
+
+ if req.write::<_, C>(&mut self.stdout.lock()).is_err() {
+ return String::new();
+ }
+
+ let msg = match bidirectional::BidirectionalMessage::read::<_, C>(
+ &mut self.stdin.lock(),
+ self.buf,
+ ) {
+ Ok(Some(msg)) => msg,
+ _ => return String::new(),
+ };
+
+ match msg {
+ bidirectional::BidirectionalMessage::SubResponse(
+ bidirectional::SubResponse::FileNameResult { name },
+ ) => name,
+ _ => String::new(),
+ }
+ }
+
fn source_text(&mut self, file_id: u32, start: u32, end: u32) -> Option<String> {
let req = bidirectional::BidirectionalMessage::SubRequest(
bidirectional::SubRequest::SourceText { file_id, start, end },
diff --git a/crates/proc-macro-srv/src/lib.rs b/crates/proc-macro-srv/src/lib.rs
index 17ffa29ce1..1804238a32 100644
--- a/crates/proc-macro-srv/src/lib.rs
+++ b/crates/proc-macro-srv/src/lib.rs
@@ -94,6 +94,7 @@ impl<'env> ProcMacroSrv<'env> {
pub type ProcMacroClientHandle<'a> = &'a mut (dyn ProcMacroClientInterface + Sync + Send);
pub trait ProcMacroClientInterface {
+ fn file(&mut self, file_id: u32) -> String;
fn source_text(&mut self, file_id: u32, start: u32, end: u32) -> Option<String>;
}
diff --git a/crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs b/crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs
index 5d9090c060..7a9d655431 100644
--- a/crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs
+++ b/crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs
@@ -127,13 +127,14 @@ impl server::Span for RaSpanServer<'_> {
fn debug(&mut self, span: Self::Span) -> String {
format!("{:?}", span)
}
- fn file(&mut self, _: Self::Span) -> String {
- // FIXME
- String::new()
- }
- fn local_file(&mut self, _: Self::Span) -> Option<String> {
- // FIXME
- None
+ fn file(&mut self, span: Self::Span) -> String {
+ self.callback
+ .as_mut()
+ .map(|cb| cb.file(span.anchor.file_id.file_id().index()))
+ .unwrap_or_default()
+ }
+ fn local_file(&mut self, span: Self::Span) -> Option<String> {
+ self.callback.as_mut().and_then(|cb| cb.local_file(span.anchor.file_id.file_id().index()))
}
fn save_span(&mut self, _span: Self::Span) -> usize {
// FIXME, quote is incompatible with third-party tools