Unnamed repository; edit this file 'description' to name the repository.
Merge pull request #21416 from Shourya742/2026-01-07-add-byte-range
Implement Span::ByteRange for proc-macro-srv
Lukas Wirth 3 months ago
parent e42e8ff · parent 5956c5a · commit dd48777
-rw-r--r--crates/load-cargo/src/lib.rs13
-rw-r--r--crates/proc-macro-api/src/bidirectional_protocol/msg.rs6
-rw-r--r--crates/proc-macro-srv-cli/src/main_loop.rs22
-rw-r--r--crates/proc-macro-srv/src/lib.rs3
-rw-r--r--crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs5
-rw-r--r--crates/proc-macro-srv/src/tests/utils.rs5
6 files changed, 52 insertions, 2 deletions
diff --git a/crates/load-cargo/src/lib.rs b/crates/load-cargo/src/lib.rs
index 33468a5003..8342492a33 100644
--- a/crates/load-cargo/src/lib.rs
+++ b/crates/load-cargo/src/lib.rs
@@ -553,6 +553,7 @@ impl ProcMacroExpander for Expander {
Ok(SubResponse::LocalFilePathResult { name })
}
+ // Not incremental: requires full file text.
SubRequest::SourceText { file_id, ast_id, start, end } => {
let range = resolve_sub_span(
db,
@@ -567,6 +568,7 @@ impl ProcMacroExpander for Expander {
Ok(SubResponse::SourceTextResult { text })
}
+ // Not incremental: requires building line index.
SubRequest::LineColumn { file_id, ast_id, offset } => {
let range =
resolve_sub_span(db, file_id, ast_id, TextRange::empty(TextSize::from(offset)));
@@ -591,6 +593,17 @@ impl ProcMacroExpander for Expander {
Ok(SubResponse::FilePathResult { name })
}
+ // Not incremental: requires global span resolution.
+ SubRequest::ByteRange { file_id, ast_id, start, end } => {
+ let range = resolve_sub_span(
+ db,
+ file_id,
+ ast_id,
+ TextRange::new(TextSize::from(start), TextSize::from(end)),
+ );
+
+ Ok(SubResponse::ByteRangeResult { range: range.range.into() })
+ }
};
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 0e3b700dcc..57e7b1ee8f 100644
--- a/crates/proc-macro-api/src/bidirectional_protocol/msg.rs
+++ b/crates/proc-macro-api/src/bidirectional_protocol/msg.rs
@@ -1,5 +1,7 @@
//! Bidirectional protocol messages
+use std::ops::Range;
+
use paths::Utf8PathBuf;
use serde::{Deserialize, Serialize};
@@ -14,6 +16,7 @@ pub enum SubRequest {
SourceText { file_id: u32, ast_id: u32, start: u32, end: u32 },
LocalFilePath { file_id: u32 },
LineColumn { file_id: u32, ast_id: u32, offset: u32 },
+ ByteRange { file_id: u32, ast_id: u32, start: u32, end: u32 },
}
#[derive(Debug, Serialize, Deserialize)]
@@ -32,6 +35,9 @@ pub enum SubResponse {
line: u32,
column: u32,
},
+ ByteRangeResult {
+ range: Range<usize>,
+ },
}
#[derive(Debug, Serialize, Deserialize)]
diff --git a/crates/proc-macro-srv-cli/src/main_loop.rs b/crates/proc-macro-srv-cli/src/main_loop.rs
index 0c651d22b4..3beaeb0697 100644
--- a/crates/proc-macro-srv-cli/src/main_loop.rs
+++ b/crates/proc-macro-srv-cli/src/main_loop.rs
@@ -6,7 +6,10 @@ use proc_macro_api::{
transport::codec::{json::JsonProtocol, postcard::PostcardProtocol},
version::CURRENT_API_VERSION,
};
-use std::io::{self, BufRead, Write};
+use std::{
+ io::{self, BufRead, Write},
+ ops::Range,
+};
use legacy::Message;
@@ -240,6 +243,23 @@ impl<C: Codec> proc_macro_srv::ProcMacroClientInterface for ProcMacroClientHandl
_ => None,
}
}
+
+ fn byte_range(
+ &mut self,
+ proc_macro_srv::span::Span { range, anchor, ctx: _ }: proc_macro_srv::span::Span,
+ ) -> Range<usize> {
+ match self.roundtrip(bidirectional::SubRequest::ByteRange {
+ file_id: anchor.file_id.as_u32(),
+ ast_id: anchor.ast_id.into_raw(),
+ start: range.start().into(),
+ end: range.end().into(),
+ }) {
+ Some(bidirectional::BidirectionalMessage::SubResponse(
+ bidirectional::SubResponse::ByteRangeResult { range },
+ )) => range,
+ _ => Range { start: range.start().into(), end: range.end().into() },
+ }
+ }
}
fn handle_expand_ra<C: Codec>(
diff --git a/crates/proc-macro-srv/src/lib.rs b/crates/proc-macro-srv/src/lib.rs
index c1ef49a717..ac9f89352c 100644
--- a/crates/proc-macro-srv/src/lib.rs
+++ b/crates/proc-macro-srv/src/lib.rs
@@ -41,6 +41,7 @@ use std::{
env,
ffi::OsString,
fs,
+ ops::Range,
path::{Path, PathBuf},
sync::{Arc, Mutex, PoisonError},
thread,
@@ -100,6 +101,8 @@ pub trait ProcMacroClientInterface {
fn local_file(&mut self, file_id: span::FileId) -> Option<String>;
/// Line and column are 1-based.
fn line_column(&mut self, span: Span) -> Option<(u32, u32)>;
+
+ fn byte_range(&mut self, span: Span) -> Range<usize>;
}
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 3a25391b57..9946608247 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
@@ -162,7 +162,10 @@ impl server::Span for RaSpanServer<'_> {
span
}
fn byte_range(&mut self, span: Self::Span) -> Range<usize> {
- // FIXME requires db to resolve the ast id, THIS IS NOT INCREMENTAL
+ if let Some(cb) = self.callback.as_mut() {
+ return cb.byte_range(span);
+ }
+
Range { start: span.range.start().into(), end: span.range.end().into() }
}
fn join(&mut self, first: Self::Span, second: Self::Span) -> Option<Self::Span> {
diff --git a/crates/proc-macro-srv/src/tests/utils.rs b/crates/proc-macro-srv/src/tests/utils.rs
index 81ff1965d6..b7c5c4fdd2 100644
--- a/crates/proc-macro-srv/src/tests/utils.rs
+++ b/crates/proc-macro-srv/src/tests/utils.rs
@@ -4,6 +4,7 @@ use expect_test::Expect;
use span::{
EditionedFileId, FileId, ROOT_ERASED_FILE_AST_ID, Span, SpanAnchor, SyntaxContext, TextRange,
};
+use std::ops::Range;
use crate::{
EnvSnapshot, ProcMacroClientInterface, ProcMacroSrv, SpanId, dylib, proc_macro_test_dylib_path,
@@ -137,6 +138,10 @@ impl ProcMacroClientInterface for MockCallback<'_> {
// proc_macro uses 1-based line/column
Some((line_col.line as u32 + 1, line_col.col as u32 + 1))
}
+
+ fn byte_range(&mut self, span: Span) -> Range<usize> {
+ Range { start: span.range.start().into(), end: span.range.end().into() }
+ }
}
pub fn assert_expand_with_callback(