Skip to content

Commit f3b5b86

Browse files
committed
Make WorkspaceEdit compatible with protocol 3.0.
Note: the SerdeUrl/Serde<Url> can be replaced by Url once servo/rust-url#304 is merged in.
1 parent 2b92496 commit f3b5b86

File tree

1 file changed

+36
-58
lines changed

1 file changed

+36
-58
lines changed

src/lib.rs

Lines changed: 36 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -264,73 +264,51 @@ impl TextEdit {
264264
}
265265
}
266266

267+
/**
268+
Describes textual changes on a single text document. The text document is referred to as a
269+
`VersionedTextDocumentIdentifier` to allow clients to check the text document version before an
270+
edit is applied. A `TextDocumentEdit` describes all changes on a version Si and after they are
271+
applied move the document to version Si+1. So the creator of a `TextDocumentEdit` doesn't need to
272+
sort the array or do any kind of ordering. However the edits must be non overlapping.
273+
*/
274+
#[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
275+
pub struct TextDocumentEdit {
276+
/**
277+
* The text document to change.
278+
*/
279+
#[serde(rename = "textDocument")]
280+
pub text_document: VersionedTextDocumentIdentifier,
281+
282+
/**
283+
* The edits to be applied.
284+
*/
285+
pub edits: Vec<TextEdit>,
286+
}
287+
267288
/// A workspace edit represents changes to many resources managed in the workspace.
268289
#[derive(Debug, Eq, PartialEq, Clone, Default, Deserialize, Serialize)]
269290
pub struct WorkspaceEdit {
270291
/// Holds changes to existing resources.
271-
#[serde(with = "url_map")]
272-
pub changes: HashMap<Url, Vec<TextEdit>>, // changes: { [uri: string]: TextEdit[]; };
273-
}
274-
275-
mod url_map {
276-
use super::*;
277-
278-
use std::fmt;
279-
280-
pub fn deserialize<'de, D>(deserializer: D) -> Result<HashMap<Url, Vec<TextEdit>>, D::Error>
281-
where
282-
D: serde::Deserializer<'de>,
283-
{
284-
struct UrlMapVisitor;
285-
286-
impl<'de> de::Visitor<'de> for UrlMapVisitor {
287-
type Value = HashMap<Url, Vec<TextEdit>>;
288-
289-
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
290-
formatter.write_str("map")
291-
}
292-
293-
fn visit_map<M>(self, mut visitor: M) -> Result<Self::Value, M::Error>
294-
where
295-
M: de::MapAccess<'de>,
296-
{
297-
let mut values = HashMap::with_capacity(visitor.size_hint().unwrap_or(0));
298-
299-
// While there are entries remaining in the input, add them
300-
// into our map.
301-
while let Some((key, value)) = visitor.next_entry::<url_serde::De<Url>, _>()? {
302-
values.insert(key.into_inner(), value);
303-
}
304-
305-
Ok(values)
306-
}
307-
}
308-
309-
// Instantiate our Visitor and ask the Deserializer to drive
310-
// it over the input data, resulting in an instance of MyMap.
311-
deserializer.deserialize_map(UrlMapVisitor)
312-
}
313-
314-
pub fn serialize<S>(
315-
changes: &HashMap<Url, Vec<TextEdit>>,
316-
serializer: S,
317-
) -> Result<S::Ok, S::Error>
318-
where
319-
S: serde::Serializer,
320-
{
321-
use serde::ser::SerializeMap;
292+
pub changes: Option<HashMap<url_serde::SerdeUrl, Vec<TextEdit>>>,
322293

323-
let mut map = serializer.serialize_map(Some(changes.len()))?;
324-
for (k, v) in changes {
325-
map.serialize_entry(k.as_str(), v)?;
326-
}
327-
map.end()
328-
}
294+
/**
295+
* An array of `TextDocumentEdit`s to express changes to n different text documents
296+
* where each text document edit addresses a specific version of a text document.
297+
* Whether a client supports versioned document edits is expressed via
298+
* `WorkspaceClientCapabilities.workspaceEdit.documentChanges`.
299+
*/
300+
#[serde(rename = "documentChanges")]
301+
pub document_changes: Option<Vec<TextDocumentEdit>>,
329302
}
330303

331304
impl WorkspaceEdit {
332305
pub fn new(changes: HashMap<Url, Vec<TextEdit>>) -> WorkspaceEdit {
333-
WorkspaceEdit { changes: changes }
306+
let mut map = HashMap::new();
307+
for (k, v) in changes {
308+
map.insert(url_serde::Serde(k), v);
309+
}
310+
311+
WorkspaceEdit { changes: Some(map), document_changes: None, }
334312
}
335313
}
336314

0 commit comments

Comments
 (0)