Skip to content

Commit 036c71a

Browse files
authored
Merge pull request #21377 from Shourya742/2025-12-31-add-more-proc-macro-bidirectional-flow
proc-macro-srv: support file and local_file via bidirectional callbacks
2 parents e3268f7 + 26b3c82 commit 036c71a

5 files changed

Lines changed: 75 additions & 24 deletions

File tree

crates/load-cargo/src/lib.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,12 +540,37 @@ impl ProcMacroExpander for Expander {
540540
current_dir: String,
541541
) -> Result<tt::TopSubtree, ProcMacroExpansionError> {
542542
let mut cb = |req| match req {
543+
SubRequest::LocalFilePath { file_id } => {
544+
let file = FileId::from_raw(file_id);
545+
let source_root_id = db.file_source_root(file).source_root_id(db);
546+
let source_root = db.source_root(source_root_id).source_root(db);
547+
548+
let name = source_root
549+
.path_for_file(&file)
550+
.and_then(|path| path.as_path())
551+
.map(|path| path.to_string());
552+
553+
Ok(SubResponse::LocalFilePathResult { name })
554+
}
543555
SubRequest::SourceText { file_id, start, end } => {
544556
let file = FileId::from_raw(file_id);
545557
let text = db.file_text(file).text(db);
546558
let slice = text.get(start as usize..end as usize).map(ToOwned::to_owned);
547559
Ok(SubResponse::SourceTextResult { text: slice })
548560
}
561+
SubRequest::FilePath { file_id } => {
562+
let file = FileId::from_raw(file_id);
563+
let source_root_id = db.file_source_root(file).source_root_id(db);
564+
let source_root = db.source_root(source_root_id).source_root(db);
565+
566+
let name = source_root
567+
.path_for_file(&file)
568+
.and_then(|path| path.as_path())
569+
.map(|path| path.to_string())
570+
.unwrap_or_default();
571+
572+
Ok(SubResponse::FilePathResult { name })
573+
}
549574
};
550575
match self.0.expand(
551576
subtree.view(),

crates/proc-macro-api/src/bidirectional_protocol/msg.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ use crate::{
1010

1111
#[derive(Debug, Serialize, Deserialize)]
1212
pub enum SubRequest {
13+
FilePath { file_id: u32 },
1314
SourceText { file_id: u32, start: u32, end: u32 },
15+
LocalFilePath { file_id: u32 },
1416
}
1517

1618
#[derive(Debug, Serialize, Deserialize)]
1719
pub enum SubResponse {
20+
FilePathResult { name: String },
1821
SourceTextResult { text: Option<String> },
22+
LocalFilePathResult { name: Option<String> },
1923
}
2024

2125
#[derive(Debug, Serialize, Deserialize)]

crates/proc-macro-srv-cli/src/main_loop.rs

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ fn run_new<C: Codec>() -> io::Result<()> {
8181
}
8282

8383
bidirectional::Request::ApiVersionCheck {} => {
84-
// bidirectional::Response::ApiVersionCheck(CURRENT_API_VERSION).write::<_, C>(stdout)
8584
send_response::<C>(
8685
&stdout,
8786
bidirectional::Response::ApiVersionCheck(CURRENT_API_VERSION),
@@ -167,28 +166,48 @@ struct ProcMacroClientHandle<'a, C: Codec> {
167166
buf: &'a mut C::Buf,
168167
}
169168

170-
impl<C: Codec> proc_macro_srv::ProcMacroClientInterface for ProcMacroClientHandle<'_, C> {
171-
fn source_text(&mut self, file_id: u32, start: u32, end: u32) -> Option<String> {
172-
let req = bidirectional::BidirectionalMessage::SubRequest(
173-
bidirectional::SubRequest::SourceText { file_id, start, end },
174-
);
169+
impl<'a, C: Codec> ProcMacroClientHandle<'a, C> {
170+
fn roundtrip(
171+
&mut self,
172+
req: bidirectional::SubRequest,
173+
) -> Option<bidirectional::BidirectionalMessage> {
174+
let msg = bidirectional::BidirectionalMessage::SubRequest(req);
175175

176-
if req.write::<_, C>(&mut self.stdout.lock()).is_err() {
176+
if msg.write::<_, C>(&mut self.stdout.lock()).is_err() {
177177
return None;
178178
}
179179

180-
let msg = match bidirectional::BidirectionalMessage::read::<_, C>(
181-
&mut self.stdin.lock(),
182-
self.buf,
183-
) {
184-
Ok(Some(msg)) => msg,
185-
_ => return None,
186-
};
180+
match bidirectional::BidirectionalMessage::read::<_, C>(&mut self.stdin.lock(), self.buf) {
181+
Ok(Some(msg)) => Some(msg),
182+
_ => None,
183+
}
184+
}
185+
}
186+
187+
impl<C: Codec> proc_macro_srv::ProcMacroClientInterface for ProcMacroClientHandle<'_, C> {
188+
fn file(&mut self, file_id: u32) -> String {
189+
match self.roundtrip(bidirectional::SubRequest::FilePath { file_id }) {
190+
Some(bidirectional::BidirectionalMessage::SubResponse(
191+
bidirectional::SubResponse::FilePathResult { name },
192+
)) => name,
193+
_ => String::new(),
194+
}
195+
}
187196

188-
match msg {
189-
bidirectional::BidirectionalMessage::SubResponse(
197+
fn source_text(&mut self, file_id: u32, start: u32, end: u32) -> Option<String> {
198+
match self.roundtrip(bidirectional::SubRequest::SourceText { file_id, start, end }) {
199+
Some(bidirectional::BidirectionalMessage::SubResponse(
190200
bidirectional::SubResponse::SourceTextResult { text },
191-
) => text,
201+
)) => text,
202+
_ => None,
203+
}
204+
}
205+
206+
fn local_file(&mut self, file_id: u32) -> Option<String> {
207+
match self.roundtrip(bidirectional::SubRequest::LocalFilePath { file_id }) {
208+
Some(bidirectional::BidirectionalMessage::SubResponse(
209+
bidirectional::SubResponse::LocalFilePathResult { name },
210+
)) => name,
192211
_ => None,
193212
}
194213
}

crates/proc-macro-srv/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ impl<'env> ProcMacroSrv<'env> {
9494
pub type ProcMacroClientHandle<'a> = &'a mut (dyn ProcMacroClientInterface + Sync + Send);
9595

9696
pub trait ProcMacroClientInterface {
97+
fn file(&mut self, file_id: u32) -> String;
9798
fn source_text(&mut self, file_id: u32, start: u32, end: u32) -> Option<String>;
99+
fn local_file(&mut self, file_id: u32) -> Option<String>;
98100
}
99101

100102
const EXPANDER_STACK_SIZE: usize = 8 * 1024 * 1024;

crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,14 @@ impl server::Span for RaSpanServer<'_> {
127127
fn debug(&mut self, span: Self::Span) -> String {
128128
format!("{:?}", span)
129129
}
130-
fn file(&mut self, _: Self::Span) -> String {
131-
// FIXME
132-
String::new()
133-
}
134-
fn local_file(&mut self, _: Self::Span) -> Option<String> {
135-
// FIXME
136-
None
130+
fn file(&mut self, span: Self::Span) -> String {
131+
self.callback
132+
.as_mut()
133+
.map(|cb| cb.file(span.anchor.file_id.file_id().index()))
134+
.unwrap_or_default()
135+
}
136+
fn local_file(&mut self, span: Self::Span) -> Option<String> {
137+
self.callback.as_mut().and_then(|cb| cb.local_file(span.anchor.file_id.file_id().index()))
137138
}
138139
fn save_span(&mut self, _span: Self::Span) -> usize {
139140
// FIXME, quote is incompatible with third-party tools

0 commit comments

Comments
 (0)