2019-10-25 03:26:53 -05:00
|
|
|
//! This modules defines type to represent changes to the source code, that flow
|
|
|
|
//! from the server to the client.
|
|
|
|
//!
|
2021-02-28 05:57:41 -06:00
|
|
|
//! It can be viewed as a dual for `Change`.
|
2019-10-25 03:26:53 -05:00
|
|
|
|
2021-01-14 11:35:22 -06:00
|
|
|
use std::{
|
|
|
|
collections::hash_map::Entry,
|
|
|
|
iter::{self, FromIterator},
|
|
|
|
};
|
|
|
|
|
2020-12-09 10:01:15 -06:00
|
|
|
use base_db::{AnchoredPathBuf, FileId};
|
2021-01-14 11:35:22 -06:00
|
|
|
use rustc_hash::FxHashMap;
|
2021-01-26 13:11:12 -06:00
|
|
|
use stdx::never;
|
2020-08-12 10:03:06 -05:00
|
|
|
use text_edit::TextEdit;
|
2019-10-25 03:26:53 -05:00
|
|
|
|
2020-07-07 05:10:18 -05:00
|
|
|
#[derive(Default, Debug, Clone)]
|
2019-10-25 03:26:53 -05:00
|
|
|
pub struct SourceChange {
|
2021-01-14 15:43:36 -06:00
|
|
|
pub source_file_edits: FxHashMap<FileId, TextEdit>,
|
2019-10-25 03:26:53 -05:00
|
|
|
pub file_system_edits: Vec<FileSystemEdit>,
|
2020-05-17 05:09:53 -05:00
|
|
|
pub is_snippet: bool,
|
2019-10-25 03:26:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SourceChange {
|
|
|
|
/// Creates a new SourceChange with the given label
|
|
|
|
/// from the edits.
|
2020-05-22 11:03:08 -05:00
|
|
|
pub fn from_edits(
|
2021-01-14 15:43:36 -06:00
|
|
|
source_file_edits: FxHashMap<FileId, TextEdit>,
|
2019-10-25 03:26:53 -05:00
|
|
|
file_system_edits: Vec<FileSystemEdit>,
|
|
|
|
) -> Self {
|
2020-05-22 11:03:08 -05:00
|
|
|
SourceChange { source_file_edits, file_system_edits, is_snippet: false }
|
2019-10-25 03:26:53 -05:00
|
|
|
}
|
2021-01-14 11:35:22 -06:00
|
|
|
|
|
|
|
pub fn from_text_edit(file_id: FileId, edit: TextEdit) -> Self {
|
2021-01-14 15:43:36 -06:00
|
|
|
SourceChange {
|
|
|
|
source_file_edits: FxHashMap::from_iter(iter::once((file_id, edit))),
|
|
|
|
..Default::default()
|
|
|
|
}
|
2021-01-14 11:35:22 -06:00
|
|
|
}
|
|
|
|
|
2021-01-14 15:43:36 -06:00
|
|
|
pub fn insert_source_edit(&mut self, file_id: FileId, edit: TextEdit) {
|
|
|
|
match self.source_file_edits.entry(file_id) {
|
2021-01-14 11:35:22 -06:00
|
|
|
Entry::Occupied(mut entry) => {
|
2021-01-26 13:11:12 -06:00
|
|
|
never!(entry.get_mut().union(edit).is_err(), "overlapping edits for same file");
|
2021-01-14 11:35:22 -06:00
|
|
|
}
|
|
|
|
Entry::Vacant(entry) => {
|
|
|
|
entry.insert(edit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-14 15:43:36 -06:00
|
|
|
|
|
|
|
pub fn push_file_system_edit(&mut self, edit: FileSystemEdit) {
|
|
|
|
self.file_system_edits.push(edit);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_source_edit(&self, file_id: FileId) -> Option<&TextEdit> {
|
|
|
|
self.source_file_edits.get(&file_id)
|
|
|
|
}
|
2019-10-25 03:26:53 -05:00
|
|
|
}
|
|
|
|
|
2021-01-14 15:43:36 -06:00
|
|
|
impl Extend<(FileId, TextEdit)> for SourceChange {
|
2021-01-14 11:35:22 -06:00
|
|
|
fn extend<T: IntoIterator<Item = (FileId, TextEdit)>>(&mut self, iter: T) {
|
2021-01-14 15:43:36 -06:00
|
|
|
iter.into_iter().for_each(|(file_id, edit)| self.insert_source_edit(file_id, edit));
|
2020-06-08 14:44:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 15:43:36 -06:00
|
|
|
impl From<FxHashMap<FileId, TextEdit>> for SourceChange {
|
|
|
|
fn from(source_file_edits: FxHashMap<FileId, TextEdit>) -> SourceChange {
|
2020-06-08 14:44:42 -05:00
|
|
|
SourceChange { source_file_edits, file_system_edits: Vec::new(), is_snippet: false }
|
2020-05-22 11:03:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 08:26:40 -05:00
|
|
|
#[derive(Debug, Clone)]
|
2019-10-25 03:26:53 -05:00
|
|
|
pub enum FileSystemEdit {
|
2020-12-17 07:09:55 -06:00
|
|
|
CreateFile { dst: AnchoredPathBuf, initial_contents: String },
|
2020-12-09 10:01:15 -06:00
|
|
|
MoveFile { src: FileId, dst: AnchoredPathBuf },
|
2019-10-25 03:26:53 -05:00
|
|
|
}
|
2019-10-25 03:49:38 -05:00
|
|
|
|
2020-05-22 11:03:08 -05:00
|
|
|
impl From<FileSystemEdit> for SourceChange {
|
|
|
|
fn from(edit: FileSystemEdit) -> SourceChange {
|
2019-10-25 03:49:38 -05:00
|
|
|
SourceChange {
|
2021-01-14 11:35:22 -06:00
|
|
|
source_file_edits: Default::default(),
|
2020-05-22 11:03:08 -05:00
|
|
|
file_system_edits: vec![edit],
|
2020-05-17 05:09:53 -05:00
|
|
|
is_snippet: false,
|
2019-10-25 03:49:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|