Unnamed repository; edit this file 'description' to name the repository.
remove span related API from proc-macro-srv/cli/api and remove span from procmacrosrvcli
bit-aloo 4 months ago
parent 89831ff · commit 49c15b2
-rw-r--r--Cargo.lock1
-rw-r--r--crates/load-cargo/src/lib.rs36
-rw-r--r--crates/proc-macro-api/src/bidirectional_protocol/msg.rs7
-rw-r--r--crates/proc-macro-srv-cli/Cargo.toml1
-rw-r--r--crates/proc-macro-srv-cli/src/main_loop.rs14
-rw-r--r--crates/proc-macro-srv/src/lib.rs6
-rw-r--r--crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs19
7 files changed, 49 insertions, 35 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 453c35a574..fbaeff1eb5 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1881,7 +1881,6 @@ dependencies = [
"postcard",
"proc-macro-api",
"proc-macro-srv",
- "span",
]
[[package]]
diff --git a/crates/load-cargo/src/lib.rs b/crates/load-cargo/src/lib.rs
index 474046ee2f..c302e266fe 100644
--- a/crates/load-cargo/src/lib.rs
+++ b/crates/load-cargo/src/lib.rs
@@ -34,7 +34,7 @@ use proc_macro_api::{
use project_model::{CargoConfig, PackageRoot, ProjectManifest, ProjectWorkspace};
use span::Span;
use vfs::{
- AbsPath, AbsPathBuf, VfsPath,
+ AbsPath, AbsPathBuf, FileId, VfsPath,
file_set::FileSetConfig,
loader::{Handle, LoadingProgress},
};
@@ -541,8 +541,8 @@ impl ProcMacroExpander for Expander {
current_dir: String,
) -> Result<tt::TopSubtree, ProcMacroExpansionError> {
let mut cb = |req| match req {
- SubRequest::LocalFilePath { span } => {
- let file_id = span.anchor.file_id.file_id();
+ SubRequest::LocalFilePath { file_id } => {
+ let file_id = FileId::from_raw(file_id);
let source_root_id = db.file_source_root(file_id).source_root_id(db);
let source_root = db.source_root(source_root_id).source_root(db);
let name = source_root
@@ -552,22 +552,26 @@ impl ProcMacroExpander for Expander {
Ok(SubResponse::LocalFilePathResult { name })
}
- SubRequest::SourceText { span } => {
- let anchor = span.anchor;
- let file_id = EditionedFileId::from_span_guess_origin(db, anchor.file_id);
- let range = db
- .ast_id_map(hir_expand::HirFileId::FileId(file_id))
- .get_erased(anchor.ast_id)
- .text_range();
- let source = db.file_text(anchor.file_id.file_id()).text(db);
- let text = source
- .get(usize::from(range.start())..usize::from(range.end()))
- .map(ToOwned::to_owned);
+ SubRequest::SourceText { file_id, ast_id, start, end } => {
+ let raw_file_id = FileId::from_raw(file_id);
+ let editioned_file_id = span::EditionedFileId::from_raw(file_id);
+ let ast_id = span::ErasedFileAstId::from_raw(ast_id);
+ let hir_file_id = EditionedFileId::from_span_guess_origin(db, editioned_file_id);
+ let anchor_offset = db
+ .ast_id_map(hir_expand::HirFileId::FileId(hir_file_id))
+ .get_erased(ast_id)
+ .text_range()
+ .start();
+ let anchor_offset = u32::from(anchor_offset);
+ let abs_start = start + anchor_offset;
+ let abs_end = end + anchor_offset;
+ let source = db.file_text(raw_file_id).text(db);
+ let text = source.get(abs_start as usize..abs_end as usize).map(ToOwned::to_owned);
Ok(SubResponse::SourceTextResult { text })
}
- SubRequest::FilePath { span } => {
- let file_id = span.anchor.file_id.file_id();
+ SubRequest::FilePath { file_id } => {
+ let file_id = FileId::from_raw(file_id);
let source_root_id = db.file_source_root(file_id).source_root_id(db);
let source_root = db.source_root(source_root_id).source_root(db);
let name = source_root
diff --git a/crates/proc-macro-api/src/bidirectional_protocol/msg.rs b/crates/proc-macro-api/src/bidirectional_protocol/msg.rs
index 73cf58bd9e..e41f8a5d7d 100644
--- a/crates/proc-macro-api/src/bidirectional_protocol/msg.rs
+++ b/crates/proc-macro-api/src/bidirectional_protocol/msg.rs
@@ -2,7 +2,6 @@
use paths::Utf8PathBuf;
use serde::{Deserialize, Serialize};
-use tt::Span;
use crate::{
ProcMacroKind,
@@ -11,9 +10,9 @@ use crate::{
#[derive(Debug, Serialize, Deserialize)]
pub enum SubRequest {
- FilePath { span: Span },
- SourceText { span: Span },
- LocalFilePath { span: Span },
+ FilePath { file_id: u32 },
+ SourceText { file_id: u32, ast_id: u32, start: u32, end: u32 },
+ LocalFilePath { file_id: u32 },
}
#[derive(Debug, Serialize, Deserialize)]
diff --git a/crates/proc-macro-srv-cli/Cargo.toml b/crates/proc-macro-srv-cli/Cargo.toml
index 875d9f6801..2c6e5a16ee 100644
--- a/crates/proc-macro-srv-cli/Cargo.toml
+++ b/crates/proc-macro-srv-cli/Cargo.toml
@@ -14,7 +14,6 @@ publish = false
proc-macro-srv.workspace = true
proc-macro-api.workspace = true
postcard.workspace = true
-span.workspace = true
clap = {version = "4.5.42", default-features = false, features = ["std"]}
[features]
diff --git a/crates/proc-macro-srv-cli/src/main_loop.rs b/crates/proc-macro-srv-cli/src/main_loop.rs
index a5bf84df04..4891e07314 100644
--- a/crates/proc-macro-srv-cli/src/main_loop.rs
+++ b/crates/proc-macro-srv-cli/src/main_loop.rs
@@ -6,7 +6,6 @@ use proc_macro_api::{
transport::codec::{json::JsonProtocol, postcard::PostcardProtocol},
version::CURRENT_API_VERSION,
};
-use span::Span;
use std::io;
use legacy::Message;
@@ -186,8 +185,8 @@ impl<'a, C: Codec> ProcMacroClientHandle<'a, C> {
}
impl<C: Codec> proc_macro_srv::ProcMacroClientInterface for ProcMacroClientHandle<'_, C> {
- fn file(&mut self, span: Span) -> String {
- match self.roundtrip(bidirectional::SubRequest::FilePath { span }) {
+ fn file(&mut self, file_id: u32) -> String {
+ match self.roundtrip(bidirectional::SubRequest::FilePath { file_id }) {
Some(bidirectional::BidirectionalMessage::SubResponse(
bidirectional::SubResponse::FilePathResult { name },
)) => name,
@@ -195,8 +194,9 @@ impl<C: Codec> proc_macro_srv::ProcMacroClientInterface for ProcMacroClientHandl
}
}
- fn source_text(&mut self, span: Span) -> Option<String> {
- match self.roundtrip(bidirectional::SubRequest::SourceText { span }) {
+ fn source_text(&mut self, file_id: u32, ast_id: u32, start: u32, end: u32) -> Option<String> {
+ match self.roundtrip(bidirectional::SubRequest::SourceText { file_id, ast_id, start, end })
+ {
Some(bidirectional::BidirectionalMessage::SubResponse(
bidirectional::SubResponse::SourceTextResult { text },
)) => text,
@@ -204,8 +204,8 @@ impl<C: Codec> proc_macro_srv::ProcMacroClientInterface for ProcMacroClientHandl
}
}
- fn local_file(&mut self, span: Span) -> Option<String> {
- match self.roundtrip(bidirectional::SubRequest::LocalFilePath { span }) {
+ fn local_file(&mut self, file_id: u32) -> Option<String> {
+ match self.roundtrip(bidirectional::SubRequest::LocalFilePath { file_id }) {
Some(bidirectional::BidirectionalMessage::SubResponse(
bidirectional::SubResponse::LocalFilePathResult { name },
)) => name,
diff --git a/crates/proc-macro-srv/src/lib.rs b/crates/proc-macro-srv/src/lib.rs
index 6620800779..d63aea947c 100644
--- a/crates/proc-macro-srv/src/lib.rs
+++ b/crates/proc-macro-srv/src/lib.rs
@@ -94,9 +94,9 @@ impl<'env> ProcMacroSrv<'env> {
pub type ProcMacroClientHandle<'a> = &'a mut (dyn ProcMacroClientInterface + Sync + Send);
pub trait ProcMacroClientInterface {
- fn file(&mut self, span: Span) -> String;
- fn source_text(&mut self, span: Span) -> Option<String>;
- fn local_file(&mut self, span: Span) -> Option<String>;
+ fn file(&mut self, file_id: u32) -> String;
+ fn source_text(&mut self, file_id: u32, ast_id: u32, start: u32, end: u32) -> Option<String>;
+ fn local_file(&mut self, file_id: u32) -> Option<String>;
}
const EXPANDER_STACK_SIZE: usize = 8 * 1024 * 1024;
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 c4392a9ae1..2ce3b717cb 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
@@ -128,10 +128,13 @@ impl server::Span for RaSpanServer<'_> {
format!("{:?}", span)
}
fn file(&mut self, span: Self::Span) -> String {
- self.callback.as_mut().map(|cb| cb.file(span)).unwrap_or_default()
+ 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))
+ 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
@@ -150,7 +153,17 @@ impl server::Span for RaSpanServer<'_> {
/// See PR:
/// https://github.com/rust-lang/rust/pull/55780
fn source_text(&mut self, span: Self::Span) -> Option<String> {
- self.callback.as_mut()?.source_text(span)
+ let file_id = span.anchor.file_id;
+ let ast_id = span.anchor.ast_id;
+ let start: u32 = span.range.start().into();
+ let end: u32 = span.range.end().into();
+
+ self.callback.as_mut()?.source_text(
+ file_id.file_id().index(),
+ ast_id.into_raw(),
+ start,
+ end,
+ )
}
fn parent(&mut self, _span: Self::Span) -> Option<Self::Span> {