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.
|
|
|
|
//!
|
|
|
|
//! It can be viewed as a dual for `AnalysisChange`.
|
|
|
|
|
2020-05-06 04:31:26 -05:00
|
|
|
use ra_db::{FileId, FilePosition, RelativePathBuf, SourceRootId};
|
|
|
|
use ra_text_edit::{TextEdit, TextSize};
|
2019-10-25 03:26:53 -05:00
|
|
|
|
2020-05-06 08:26:40 -05:00
|
|
|
#[derive(Debug, Clone)]
|
2019-10-25 03:26:53 -05:00
|
|
|
pub struct SourceChange {
|
2020-05-06 04:31:26 -05:00
|
|
|
/// For display in the undo log in the editor
|
2019-10-25 03:26:53 -05:00
|
|
|
pub label: String,
|
|
|
|
pub source_file_edits: Vec<SourceFileEdit>,
|
|
|
|
pub file_system_edits: Vec<FileSystemEdit>,
|
|
|
|
pub cursor_position: Option<FilePosition>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SourceChange {
|
|
|
|
/// Creates a new SourceChange with the given label
|
|
|
|
/// from the edits.
|
2020-05-06 04:31:26 -05:00
|
|
|
pub fn from_edits<L: Into<String>>(
|
2019-10-25 03:26:53 -05:00
|
|
|
label: L,
|
|
|
|
source_file_edits: Vec<SourceFileEdit>,
|
|
|
|
file_system_edits: Vec<FileSystemEdit>,
|
|
|
|
) -> Self {
|
|
|
|
SourceChange {
|
|
|
|
label: label.into(),
|
|
|
|
source_file_edits,
|
|
|
|
file_system_edits,
|
|
|
|
cursor_position: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new SourceChange with the given label,
|
|
|
|
/// containing only the given `SourceFileEdits`.
|
2020-05-06 04:31:26 -05:00
|
|
|
pub fn source_file_edits<L: Into<String>>(label: L, edits: Vec<SourceFileEdit>) -> Self {
|
2020-05-05 13:55:12 -05:00
|
|
|
let label = label.into();
|
|
|
|
assert!(label.starts_with(char::is_uppercase));
|
2019-10-25 03:26:53 -05:00
|
|
|
SourceChange {
|
2020-05-05 13:55:12 -05:00
|
|
|
label: label,
|
2019-10-25 03:26:53 -05:00
|
|
|
source_file_edits: edits,
|
|
|
|
file_system_edits: vec![],
|
|
|
|
cursor_position: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new SourceChange with the given label,
|
|
|
|
/// containing only the given `FileSystemEdits`.
|
|
|
|
pub(crate) fn file_system_edits<L: Into<String>>(label: L, edits: Vec<FileSystemEdit>) -> Self {
|
|
|
|
SourceChange {
|
|
|
|
label: label.into(),
|
|
|
|
source_file_edits: vec![],
|
|
|
|
file_system_edits: edits,
|
|
|
|
cursor_position: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new SourceChange with the given label,
|
|
|
|
/// containing only a single `SourceFileEdit`.
|
2020-05-06 04:31:26 -05:00
|
|
|
pub fn source_file_edit<L: Into<String>>(label: L, edit: SourceFileEdit) -> Self {
|
2019-10-25 03:26:53 -05:00
|
|
|
SourceChange::source_file_edits(label, vec![edit])
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new SourceChange with the given label
|
|
|
|
/// from the given `FileId` and `TextEdit`
|
2020-05-06 04:31:26 -05:00
|
|
|
pub fn source_file_edit_from<L: Into<String>>(
|
2019-10-25 03:26:53 -05:00
|
|
|
label: L,
|
|
|
|
file_id: FileId,
|
|
|
|
edit: TextEdit,
|
|
|
|
) -> Self {
|
|
|
|
SourceChange::source_file_edit(label, SourceFileEdit { file_id, edit })
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new SourceChange with the given label
|
|
|
|
/// from the given `FileId` and `TextEdit`
|
2020-05-06 04:31:26 -05:00
|
|
|
pub fn file_system_edit<L: Into<String>>(label: L, edit: FileSystemEdit) -> Self {
|
2019-10-25 03:26:53 -05:00
|
|
|
SourceChange::file_system_edits(label, vec![edit])
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the cursor position to the given `FilePosition`
|
2020-05-06 04:31:26 -05:00
|
|
|
pub fn with_cursor(mut self, cursor_position: FilePosition) -> Self {
|
2019-10-25 03:26:53 -05:00
|
|
|
self.cursor_position = Some(cursor_position);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the cursor position to the given `FilePosition`
|
2020-05-06 04:31:26 -05:00
|
|
|
pub fn with_cursor_opt(mut self, cursor_position: Option<FilePosition>) -> Self {
|
2019-10-25 03:26:53 -05:00
|
|
|
self.cursor_position = cursor_position;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 08:26:40 -05:00
|
|
|
#[derive(Debug, Clone)]
|
2019-10-25 03:26:53 -05:00
|
|
|
pub struct SourceFileEdit {
|
|
|
|
pub file_id: FileId,
|
|
|
|
pub edit: TextEdit,
|
|
|
|
}
|
|
|
|
|
2020-05-06 08:26:40 -05:00
|
|
|
#[derive(Debug, Clone)]
|
2019-10-25 03:26:53 -05:00
|
|
|
pub enum FileSystemEdit {
|
|
|
|
CreateFile { source_root: SourceRootId, path: RelativePathBuf },
|
|
|
|
MoveFile { src: FileId, dst_source_root: SourceRootId, dst_path: RelativePathBuf },
|
|
|
|
}
|
2019-10-25 03:49:38 -05:00
|
|
|
|
2020-05-06 04:31:26 -05:00
|
|
|
pub struct SingleFileChange {
|
2019-10-25 03:49:38 -05:00
|
|
|
pub label: String,
|
|
|
|
pub edit: TextEdit,
|
2020-04-24 16:40:41 -05:00
|
|
|
pub cursor_position: Option<TextSize>,
|
2019-10-25 03:49:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SingleFileChange {
|
2020-05-06 04:31:26 -05:00
|
|
|
pub fn into_source_change(self, file_id: FileId) -> SourceChange {
|
2019-10-25 03:49:38 -05:00
|
|
|
SourceChange {
|
|
|
|
label: self.label,
|
|
|
|
source_file_edits: vec![SourceFileEdit { file_id, edit: self.edit }],
|
|
|
|
file_system_edits: Vec::new(),
|
|
|
|
cursor_position: self.cursor_position.map(|offset| FilePosition { file_id, offset }),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|