rust-analyzer/crates/proc-macro-api/src/msg.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

299 lines
9.9 KiB
Rust
Raw Normal View History

2020-04-20 18:26:10 +00:00
//! Defines messages for cross-process message passing based on `ndjson` wire protocol
pub(crate) mod flat;
2020-03-26 20:26:34 +00:00
2024-03-19 12:05:50 +00:00
use std::io::{self, BufRead, Write};
2020-03-26 20:26:34 +00:00
2024-03-19 12:05:50 +00:00
use paths::Utf8PathBuf;
2020-08-12 14:46:20 +00:00
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use crate::ProcMacroKind;
pub use crate::msg::flat::{
deserialize_span_data_index_map, serialize_span_data_index_map, FlatTree, SpanDataIndexMap,
TokenId,
};
2020-03-26 20:26:34 +00:00
// The versions of the server protocol
2023-02-01 10:04:20 +00:00
pub const NO_VERSION_CHECK_VERSION: u32 = 0;
pub const VERSION_CHECK_VERSION: u32 = 1;
pub const ENCODE_CLOSE_SPAN_VERSION: u32 = 2;
2023-11-28 15:28:51 +00:00
pub const HAS_GLOBAL_SPANS: u32 = 3;
pub const RUST_ANALYZER_SPAN_SUPPORT: u32 = 4;
/// Whether literals encode their kind as an additional u32 field and idents their rawness as a u32 field
pub const EXTENDED_LEAF_DATA: u32 = 5;
pub const CURRENT_API_VERSION: u32 = EXTENDED_LEAF_DATA;
2023-02-01 10:04:20 +00:00
#[derive(Debug, Serialize, Deserialize)]
2020-03-28 10:12:51 +00:00
pub enum Request {
2023-12-15 17:25:47 +00:00
/// Since [`NO_VERSION_CHECK_VERSION`]
2024-03-19 12:05:50 +00:00
ListMacros { dylib_path: Utf8PathBuf },
2023-12-15 17:25:47 +00:00
/// Since [`NO_VERSION_CHECK_VERSION`]
2024-01-22 01:43:28 +00:00
ExpandMacro(Box<ExpandMacro>),
2023-12-15 17:25:47 +00:00
/// Since [`VERSION_CHECK_VERSION`]
2023-02-01 10:04:20 +00:00
ApiVersionCheck {},
2023-12-15 17:25:47 +00:00
/// Since [`RUST_ANALYZER_SPAN_SUPPORT`]
SetConfig(ServerConfig),
2020-03-26 20:26:34 +00:00
}
#[derive(Copy, Clone, Default, Debug, Serialize, Deserialize)]
pub enum SpanMode {
#[default]
Id,
RustAnalyzer,
}
#[derive(Debug, Serialize, Deserialize)]
2020-03-28 10:12:51 +00:00
pub enum Response {
2023-12-15 17:25:47 +00:00
/// Since [`NO_VERSION_CHECK_VERSION`]
ListMacros(Result<Vec<(String, ProcMacroKind)>, String>),
2023-12-15 17:25:47 +00:00
/// Since [`NO_VERSION_CHECK_VERSION`]
ExpandMacro(Result<FlatTree, PanicMessage>),
2023-12-15 17:25:47 +00:00
/// Since [`NO_VERSION_CHECK_VERSION`]
2023-02-01 10:04:20 +00:00
ApiVersionCheck(u32),
2023-12-15 17:25:47 +00:00
/// Since [`RUST_ANALYZER_SPAN_SUPPORT`]
SetConfig(ServerConfig),
2023-12-15 17:25:47 +00:00
/// Since [`RUST_ANALYZER_SPAN_SUPPORT`]
ExpandMacroExtended(Result<ExpandMacroExtended, PanicMessage>),
}
#[derive(Debug, Serialize, Deserialize, Default)]
#[serde(default)]
pub struct ServerConfig {
pub span_mode: SpanMode,
}
2023-12-15 17:25:47 +00:00
#[derive(Debug, Serialize, Deserialize)]
pub struct ExpandMacroExtended {
pub tree: FlatTree,
pub span_data_table: Vec<u32>,
2020-03-26 20:26:34 +00:00
}
#[derive(Debug, Serialize, Deserialize)]
pub struct PanicMessage(pub String);
2020-03-26 20:26:34 +00:00
#[derive(Debug, Serialize, Deserialize)]
pub struct ExpandMacro {
pub lib: Utf8PathBuf,
/// Environment variables to set during macro expansion.
pub env: Vec<(String, String)>,
pub current_dir: Option<String>,
#[serde(flatten)]
pub data: ExpandMacroData,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ExpandMacroData {
/// Argument of macro call.
///
/// In custom derive this will be a struct or enum; in attribute-like macro - underlying
/// item; in function-like macro - the macro body.
pub macro_body: FlatTree,
/// Name of macro to expand.
///
/// In custom derive this is the name of the derived trait (`Serialize`, `Getters`, etc.).
/// In attribute-like and function-like macros - single name of macro itself (`show_streams`).
pub macro_name: String,
/// Possible attributes for the attribute-like macros.
pub attributes: Option<FlatTree>,
2023-11-28 15:28:51 +00:00
/// marker for serde skip stuff
#[serde(skip_serializing_if = "ExpnGlobals::skip_serializing_if")]
#[serde(default)]
2023-11-28 15:28:51 +00:00
pub has_global_spans: ExpnGlobals,
#[serde(skip_serializing_if = "Vec::is_empty")]
#[serde(default)]
pub span_data_table: Vec<u32>,
2023-11-28 15:28:51 +00:00
}
#[derive(Copy, Clone, Default, Debug, Serialize, Deserialize)]
2023-11-28 15:28:51 +00:00
pub struct ExpnGlobals {
#[serde(skip_serializing)]
#[serde(default)]
2023-11-28 15:28:51 +00:00
pub serialize: bool,
pub def_site: usize,
pub call_site: usize,
pub mixed_site: usize,
}
impl ExpnGlobals {
fn skip_serializing_if(&self) -> bool {
!self.serialize
}
2020-03-26 20:26:34 +00:00
}
2020-04-20 18:26:10 +00:00
pub trait Message: Serialize + DeserializeOwned {
fn read<R: BufRead>(
from_proto: ProtocolRead<R>,
inp: &mut R,
buf: &mut String,
) -> io::Result<Option<Self>> {
Ok(match from_proto(inp, buf)? {
2020-04-20 18:26:10 +00:00
None => None,
Some(text) => {
2021-06-13 03:54:16 +00:00
let mut deserializer = serde_json::Deserializer::from_str(text);
// Note that some proc-macro generate very deep syntax tree
// We have to disable the current limit of serde here
deserializer.disable_recursion_limit();
Some(Self::deserialize(&mut deserializer)?)
}
2020-04-20 18:26:10 +00:00
})
2020-03-26 20:26:34 +00:00
}
fn write<W: Write>(self, to_proto: ProtocolWrite<W>, out: &mut W) -> io::Result<()> {
2020-03-28 10:12:51 +00:00
let text = serde_json::to_string(&self)?;
to_proto(out, &text)
2020-03-26 20:26:34 +00:00
}
}
2020-03-28 10:12:51 +00:00
impl Message for Request {}
impl Message for Response {}
2020-03-26 20:26:34 +00:00
#[allow(type_alias_bounds)]
type ProtocolRead<R: BufRead> =
for<'i, 'buf> fn(inp: &'i mut R, buf: &'buf mut String) -> io::Result<Option<&'buf String>>;
#[allow(type_alias_bounds)]
type ProtocolWrite<W: Write> = for<'o, 'msg> fn(out: &'o mut W, msg: &'msg str) -> io::Result<()>;
#[cfg(test)]
mod tests {
2024-07-16 07:59:39 +00:00
use intern::{sym, Symbol};
2024-08-03 17:16:56 +00:00
use span::{ErasedFileAstId, Span, SpanAnchor, SyntaxContextId, TextRange, TextSize};
2023-11-28 15:28:51 +00:00
use tt::{Delimiter, DelimiterKind, Ident, Leaf, Literal, Punct, Spacing, Subtree, TokenTree};
use super::*;
2023-12-18 12:30:41 +00:00
fn fixture_token_tree() -> Subtree<Span> {
let anchor = SpanAnchor {
file_id: span::EditionedFileId::new(
span::FileId::from_raw(0xe4e4e),
span::Edition::CURRENT,
),
2024-08-03 17:16:56 +00:00
ast_id: ErasedFileAstId::from_raw(0),
};
let token_trees = Box::new([
TokenTree::Leaf(
Ident {
2024-07-16 07:59:39 +00:00
sym: Symbol::intern("struct"),
span: Span {
range: TextRange::at(TextSize::new(0), TextSize::of("struct")),
anchor,
ctx: SyntaxContextId::ROOT,
},
is_raw: tt::IdentIsRaw::No,
}
.into(),
),
TokenTree::Leaf(
Ident {
2024-07-16 07:59:39 +00:00
sym: Symbol::intern("Foo"),
span: Span {
range: TextRange::at(TextSize::new(5), TextSize::of("r#Foo")),
anchor,
ctx: SyntaxContextId::ROOT,
},
is_raw: tt::IdentIsRaw::Yes,
}
.into(),
),
TokenTree::Leaf(Leaf::Literal(Literal {
2024-07-16 07:59:39 +00:00
symbol: Symbol::intern("Foo"),
2023-12-18 12:30:41 +00:00
span: Span {
range: TextRange::at(TextSize::new(10), TextSize::of("\"Foo\"")),
2023-11-28 15:28:51 +00:00
anchor,
ctx: SyntaxContextId::ROOT,
},
kind: tt::LitKind::Str,
suffix: None,
})),
TokenTree::Leaf(Leaf::Punct(Punct {
char: '@',
2023-12-18 12:30:41 +00:00
span: Span {
range: TextRange::at(TextSize::new(13), TextSize::of('@')),
2023-11-28 15:28:51 +00:00
anchor,
ctx: SyntaxContextId::ROOT,
},
spacing: Spacing::Joint,
})),
TokenTree::Subtree(Subtree {
delimiter: Delimiter {
open: Span {
range: TextRange::at(TextSize::new(14), TextSize::of('{')),
anchor,
ctx: SyntaxContextId::ROOT,
},
close: Span {
range: TextRange::at(TextSize::new(19), TextSize::of('}')),
anchor,
ctx: SyntaxContextId::ROOT,
},
kind: DelimiterKind::Brace,
},
token_trees: Box::new([TokenTree::Leaf(Leaf::Literal(Literal {
2024-07-16 07:59:39 +00:00
symbol: sym::INTEGER_0.clone(),
span: Span {
range: TextRange::at(TextSize::new(15), TextSize::of("0u32")),
anchor,
ctx: SyntaxContextId::ROOT,
},
kind: tt::LitKind::Integer,
2024-07-16 07:59:39 +00:00
suffix: Some(sym::u32.clone()),
}))]),
}),
]);
Subtree {
2023-01-31 10:49:49 +00:00
delimiter: Delimiter {
2023-12-18 12:30:41 +00:00
open: Span {
range: TextRange::empty(TextSize::new(0)),
2023-11-28 15:28:51 +00:00
anchor,
ctx: SyntaxContextId::ROOT,
},
2023-12-18 12:30:41 +00:00
close: Span {
range: TextRange::empty(TextSize::new(19)),
2023-11-28 15:28:51 +00:00
anchor,
ctx: SyntaxContextId::ROOT,
},
kind: DelimiterKind::Invisible,
2023-01-31 10:49:49 +00:00
},
token_trees,
}
}
#[test]
fn test_proc_macro_rpc_works() {
let tt = fixture_token_tree();
for v in RUST_ANALYZER_SPAN_SUPPORT..=CURRENT_API_VERSION {
let mut span_data_table = Default::default();
let task = ExpandMacro {
data: ExpandMacroData {
macro_body: FlatTree::new(&tt, v, &mut span_data_table),
macro_name: Default::default(),
attributes: None,
has_global_spans: ExpnGlobals {
serialize: true,
def_site: 0,
call_site: 0,
mixed_site: 0,
},
span_data_table: Vec::new(),
},
lib: Utf8PathBuf::from_path_buf(std::env::current_dir().unwrap()).unwrap(),
env: Default::default(),
current_dir: Default::default(),
};
let json = serde_json::to_string(&task).unwrap();
// println!("{}", json);
let back: ExpandMacro = serde_json::from_str(&json).unwrap();
assert_eq!(
tt,
back.data.macro_body.to_subtree_resolved(v, &span_data_table),
"version: {v}"
);
}
}
}