Skip to content

Commit 49c15b2

Browse files
committed
remove span related API from proc-macro-srv/cli/api and remove span from procmacrosrvcli
1 parent 89831ff commit 49c15b2

7 files changed

Lines changed: 49 additions & 35 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/load-cargo/src/lib.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use proc_macro_api::{
3434
use project_model::{CargoConfig, PackageRoot, ProjectManifest, ProjectWorkspace};
3535
use span::Span;
3636
use vfs::{
37-
AbsPath, AbsPathBuf, VfsPath,
37+
AbsPath, AbsPathBuf, FileId, VfsPath,
3838
file_set::FileSetConfig,
3939
loader::{Handle, LoadingProgress},
4040
};
@@ -541,8 +541,8 @@ impl ProcMacroExpander for Expander {
541541
current_dir: String,
542542
) -> Result<tt::TopSubtree, ProcMacroExpansionError> {
543543
let mut cb = |req| match req {
544-
SubRequest::LocalFilePath { span } => {
545-
let file_id = span.anchor.file_id.file_id();
544+
SubRequest::LocalFilePath { file_id } => {
545+
let file_id = FileId::from_raw(file_id);
546546
let source_root_id = db.file_source_root(file_id).source_root_id(db);
547547
let source_root = db.source_root(source_root_id).source_root(db);
548548
let name = source_root
@@ -552,22 +552,26 @@ impl ProcMacroExpander for Expander {
552552

553553
Ok(SubResponse::LocalFilePathResult { name })
554554
}
555-
SubRequest::SourceText { span } => {
556-
let anchor = span.anchor;
557-
let file_id = EditionedFileId::from_span_guess_origin(db, anchor.file_id);
558-
let range = db
559-
.ast_id_map(hir_expand::HirFileId::FileId(file_id))
560-
.get_erased(anchor.ast_id)
561-
.text_range();
562-
let source = db.file_text(anchor.file_id.file_id()).text(db);
563-
let text = source
564-
.get(usize::from(range.start())..usize::from(range.end()))
565-
.map(ToOwned::to_owned);
555+
SubRequest::SourceText { file_id, ast_id, start, end } => {
556+
let raw_file_id = FileId::from_raw(file_id);
557+
let editioned_file_id = span::EditionedFileId::from_raw(file_id);
558+
let ast_id = span::ErasedFileAstId::from_raw(ast_id);
559+
let hir_file_id = EditionedFileId::from_span_guess_origin(db, editioned_file_id);
560+
let anchor_offset = db
561+
.ast_id_map(hir_expand::HirFileId::FileId(hir_file_id))
562+
.get_erased(ast_id)
563+
.text_range()
564+
.start();
565+
let anchor_offset = u32::from(anchor_offset);
566+
let abs_start = start + anchor_offset;
567+
let abs_end = end + anchor_offset;
568+
let source = db.file_text(raw_file_id).text(db);
569+
let text = source.get(abs_start as usize..abs_end as usize).map(ToOwned::to_owned);
566570

567571
Ok(SubResponse::SourceTextResult { text })
568572
}
569-
SubRequest::FilePath { span } => {
570-
let file_id = span.anchor.file_id.file_id();
573+
SubRequest::FilePath { file_id } => {
574+
let file_id = FileId::from_raw(file_id);
571575
let source_root_id = db.file_source_root(file_id).source_root_id(db);
572576
let source_root = db.source_root(source_root_id).source_root(db);
573577
let name = source_root

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
33
use paths::Utf8PathBuf;
44
use serde::{Deserialize, Serialize};
5-
use tt::Span;
65

76
use crate::{
87
ProcMacroKind,
@@ -11,9 +10,9 @@ use crate::{
1110

1211
#[derive(Debug, Serialize, Deserialize)]
1312
pub enum SubRequest {
14-
FilePath { span: Span },
15-
SourceText { span: Span },
16-
LocalFilePath { span: Span },
13+
FilePath { file_id: u32 },
14+
SourceText { file_id: u32, ast_id: u32, start: u32, end: u32 },
15+
LocalFilePath { file_id: u32 },
1716
}
1817

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

crates/proc-macro-srv-cli/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ publish = false
1414
proc-macro-srv.workspace = true
1515
proc-macro-api.workspace = true
1616
postcard.workspace = true
17-
span.workspace = true
1817
clap = {version = "4.5.42", default-features = false, features = ["std"]}
1918

2019
[features]

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use proc_macro_api::{
66
transport::codec::{json::JsonProtocol, postcard::PostcardProtocol},
77
version::CURRENT_API_VERSION,
88
};
9-
use span::Span;
109
use std::io;
1110

1211
use legacy::Message;
@@ -186,26 +185,27 @@ impl<'a, C: Codec> ProcMacroClientHandle<'a, C> {
186185
}
187186

188187
impl<C: Codec> proc_macro_srv::ProcMacroClientInterface for ProcMacroClientHandle<'_, C> {
189-
fn file(&mut self, span: Span) -> String {
190-
match self.roundtrip(bidirectional::SubRequest::FilePath { span }) {
188+
fn file(&mut self, file_id: u32) -> String {
189+
match self.roundtrip(bidirectional::SubRequest::FilePath { file_id }) {
191190
Some(bidirectional::BidirectionalMessage::SubResponse(
192191
bidirectional::SubResponse::FilePathResult { name },
193192
)) => name,
194193
_ => String::new(),
195194
}
196195
}
197196

198-
fn source_text(&mut self, span: Span) -> Option<String> {
199-
match self.roundtrip(bidirectional::SubRequest::SourceText { span }) {
197+
fn source_text(&mut self, file_id: u32, ast_id: u32, start: u32, end: u32) -> Option<String> {
198+
match self.roundtrip(bidirectional::SubRequest::SourceText { file_id, ast_id, start, end })
199+
{
200200
Some(bidirectional::BidirectionalMessage::SubResponse(
201201
bidirectional::SubResponse::SourceTextResult { text },
202202
)) => text,
203203
_ => None,
204204
}
205205
}
206206

207-
fn local_file(&mut self, span: Span) -> Option<String> {
208-
match self.roundtrip(bidirectional::SubRequest::LocalFilePath { span }) {
207+
fn local_file(&mut self, file_id: u32) -> Option<String> {
208+
match self.roundtrip(bidirectional::SubRequest::LocalFilePath { file_id }) {
209209
Some(bidirectional::BidirectionalMessage::SubResponse(
210210
bidirectional::SubResponse::LocalFilePathResult { name },
211211
)) => name,

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +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, span: Span) -> String;
98-
fn source_text(&mut self, span: Span) -> Option<String>;
99-
fn local_file(&mut self, span: Span) -> Option<String>;
97+
fn file(&mut self, file_id: u32) -> String;
98+
fn source_text(&mut self, file_id: u32, ast_id: u32, start: u32, end: u32) -> Option<String>;
99+
fn local_file(&mut self, file_id: u32) -> Option<String>;
100100
}
101101

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

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,13 @@ impl server::Span for RaSpanServer<'_> {
128128
format!("{:?}", span)
129129
}
130130
fn file(&mut self, span: Self::Span) -> String {
131-
self.callback.as_mut().map(|cb| cb.file(span)).unwrap_or_default()
131+
self.callback
132+
.as_mut()
133+
.map(|cb| cb.file(span.anchor.file_id.file_id().index()))
134+
.unwrap_or_default()
132135
}
133136
fn local_file(&mut self, span: Self::Span) -> Option<String> {
134-
self.callback.as_mut().and_then(|cb| cb.local_file(span))
137+
self.callback.as_mut().and_then(|cb| cb.local_file(span.anchor.file_id.file_id().index()))
135138
}
136139
fn save_span(&mut self, _span: Self::Span) -> usize {
137140
// FIXME, quote is incompatible with third-party tools
@@ -150,7 +153,17 @@ impl server::Span for RaSpanServer<'_> {
150153
/// See PR:
151154
/// https://github.com/rust-lang/rust/pull/55780
152155
fn source_text(&mut self, span: Self::Span) -> Option<String> {
153-
self.callback.as_mut()?.source_text(span)
156+
let file_id = span.anchor.file_id;
157+
let ast_id = span.anchor.ast_id;
158+
let start: u32 = span.range.start().into();
159+
let end: u32 = span.range.end().into();
160+
161+
self.callback.as_mut()?.source_text(
162+
file_id.file_id().index(),
163+
ast_id.into_raw(),
164+
start,
165+
end,
166+
)
154167
}
155168

156169
fn parent(&mut self, _span: Self::Span) -> Option<Self::Span> {

0 commit comments

Comments
 (0)