Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/proc-macro-api/src/bidirectional_protocol/msg.rs')
| -rw-r--r-- | crates/proc-macro-api/src/bidirectional_protocol/msg.rs | 55 |
1 files changed, 40 insertions, 15 deletions
diff --git a/crates/proc-macro-api/src/bidirectional_protocol/msg.rs b/crates/proc-macro-api/src/bidirectional_protocol/msg.rs index e41f8a5d7d..3f0422dc5b 100644 --- a/crates/proc-macro-api/src/bidirectional_protocol/msg.rs +++ b/crates/proc-macro-api/src/bidirectional_protocol/msg.rs @@ -1,11 +1,17 @@ //! Bidirectional protocol messages +use std::{ + io::{self, BufRead, Write}, + ops::Range, +}; + use paths::Utf8PathBuf; use serde::{Deserialize, Serialize}; use crate::{ ProcMacroKind, legacy_protocol::msg::{FlatTree, Message, PanicMessage, ServerConfig}, + transport::postcard, }; #[derive(Debug, Serialize, Deserialize)] @@ -13,13 +19,32 @@ pub enum SubRequest { FilePath { file_id: u32 }, 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)] pub enum SubResponse { - FilePathResult { name: String }, - SourceTextResult { text: Option<String> }, - LocalFilePathResult { name: Option<String> }, + FilePathResult { + name: String, + }, + SourceTextResult { + text: Option<String>, + }, + LocalFilePathResult { + name: Option<String>, + }, + /// Line and column are 1-based. + LineColumnResult { + line: u32, + column: u32, + }, + ByteRangeResult { + range: Range<usize>, + }, + Cancel { + reason: String, + }, } #[derive(Debug, Serialize, Deserialize)] @@ -52,7 +77,6 @@ pub struct ExpandMacro { pub lib: Utf8PathBuf, pub env: Vec<(String, String)>, pub current_dir: Option<String>, - #[serde(flatten)] pub data: ExpandMacroData, } @@ -67,29 +91,30 @@ pub struct ExpandMacroData { pub macro_body: FlatTree, pub macro_name: String, pub attributes: Option<FlatTree>, - #[serde(skip_serializing_if = "ExpnGlobals::skip_serializing_if")] #[serde(default)] pub has_global_spans: ExpnGlobals, - - #[serde(skip_serializing_if = "Vec::is_empty")] #[serde(default)] pub span_data_table: Vec<u32>, } #[derive(Clone, Copy, Default, Debug, Serialize, Deserialize)] pub struct ExpnGlobals { - #[serde(skip_serializing)] - #[serde(default)] - 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 +impl Message for BidirectionalMessage { + type Buf = Vec<u8>; + + fn read(inp: &mut dyn BufRead, buf: &mut Self::Buf) -> io::Result<Option<Self>> { + Ok(match postcard::read(inp, buf)? { + None => None, + Some(buf) => Some(postcard::decode(buf)?), + }) + } + fn write(self, out: &mut dyn Write) -> io::Result<()> { + let value = postcard::encode(&self)?; + postcard::write(out, &value) } } - -impl Message for BidirectionalMessage {} |