rust-analyzer/crates/ra_ide_db/src/source_change.rs

69 lines
2 KiB
Rust
Raw Normal View History

2019-10-25 08:26:53 +00: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`.
use ra_db::{FileId, RelativePathBuf, SourceRootId};
2020-05-20 22:46:08 +00:00
use ra_text_edit::TextEdit;
2019-10-25 08:26:53 +00:00
2020-05-06 13:26:40 +00:00
#[derive(Debug, Clone)]
2019-10-25 08:26:53 +00:00
pub struct SourceChange {
pub source_file_edits: Vec<SourceFileEdit>,
pub file_system_edits: Vec<FileSystemEdit>,
2020-05-17 10:09:53 +00:00
pub is_snippet: bool,
2019-10-25 08:26:53 +00:00
}
impl SourceChange {
/// Creates a new SourceChange with the given label
/// from the edits.
pub fn from_edits(
2019-10-25 08:26:53 +00:00
source_file_edits: Vec<SourceFileEdit>,
file_system_edits: Vec<FileSystemEdit>,
) -> Self {
SourceChange { source_file_edits, file_system_edits, is_snippet: false }
2019-10-25 08:26:53 +00:00
}
/// Creates a new SourceChange with the given label,
/// containing only the given `SourceFileEdits`.
pub fn source_file_edits(edits: Vec<SourceFileEdit>) -> Self {
SourceChange { source_file_edits: edits, file_system_edits: vec![], is_snippet: false }
2019-10-25 08:26:53 +00:00
}
/// Creates a new SourceChange with the given label
/// from the given `FileId` and `TextEdit`
pub fn source_file_edit_from(file_id: FileId, edit: TextEdit) -> Self {
SourceFileEdit { file_id, edit }.into()
2019-10-25 08:26:53 +00:00
}
}
2020-05-06 13:26:40 +00:00
#[derive(Debug, Clone)]
2019-10-25 08:26:53 +00:00
pub struct SourceFileEdit {
pub file_id: FileId,
pub edit: TextEdit,
}
impl From<SourceFileEdit> for SourceChange {
fn from(edit: SourceFileEdit) -> SourceChange {
SourceChange {
source_file_edits: vec![edit],
file_system_edits: Vec::new(),
is_snippet: false,
}
}
}
2020-05-06 13:26:40 +00:00
#[derive(Debug, Clone)]
2019-10-25 08:26:53 +00:00
pub enum FileSystemEdit {
CreateFile { source_root: SourceRootId, path: RelativePathBuf },
MoveFile { src: FileId, dst_source_root: SourceRootId, dst_path: RelativePathBuf },
}
2019-10-25 08:49:38 +00:00
impl From<FileSystemEdit> for SourceChange {
fn from(edit: FileSystemEdit) -> SourceChange {
2019-10-25 08:49:38 +00:00
SourceChange {
source_file_edits: Vec::new(),
file_system_edits: vec![edit],
2020-05-17 10:09:53 +00:00
is_snippet: false,
2019-10-25 08:49:38 +00:00
}
}
}