Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions crates/load-cargo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)));
Expand All @@ -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(),
Expand Down
6 changes: 6 additions & 0 deletions crates/proc-macro-api/src/bidirectional_protocol/msg.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Bidirectional protocol messages

use std::ops::Range;

use paths::Utf8PathBuf;
use serde::{Deserialize, Serialize};

Expand All @@ -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)]
Expand All @@ -32,6 +35,9 @@ pub enum SubResponse {
line: u32,
column: u32,
},
ByteRangeResult {
range: Range<usize>,
},
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down
22 changes: 21 additions & 1 deletion crates/proc-macro-srv-cli/src/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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>(
Expand Down
3 changes: 3 additions & 0 deletions crates/proc-macro-srv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use std::{
env,
ffi::OsString,
fs,
ops::Range,
path::{Path, PathBuf},
sync::{Arc, Mutex, PoisonError},
thread,
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
5 changes: 5 additions & 0 deletions crates/proc-macro-srv/src/tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down