rename Edit to TextEdit and AtomEdit to AtomTextEdit

This commit is contained in:
Bernardo 2018-12-11 19:07:17 +01:00
parent 7344d28768
commit 0527e3b283
11 changed files with 92 additions and 87 deletions

View file

@ -1,7 +1,7 @@
mod reference_completion; mod reference_completion;
use ra_editor::find_node_at_offset; use ra_editor::find_node_at_offset;
use ra_text_edit::AtomEdit; use ra_text_edit::AtomTextEdit;
use ra_syntax::{ use ra_syntax::{
algo::visit::{visitor_ctx, VisitorCtx}, algo::visit::{visitor_ctx, VisitorCtx},
ast, ast,
@ -34,7 +34,7 @@ pub(crate) fn completions(
let original_file = db.source_file(position.file_id); let original_file = db.source_file(position.file_id);
// Insert a fake ident to get a valid parse tree // Insert a fake ident to get a valid parse tree
let file = { let file = {
let edit = AtomEdit::insert(position.offset, "intellijRulezz".to_string()); let edit = AtomTextEdit::insert(position.offset, "intellijRulezz".to_string());
original_file.reparse(&edit) original_file.reparse(&edit)
}; };

View file

@ -19,7 +19,7 @@ pub mod mock_analysis;
use std::{fmt, sync::Arc}; use std::{fmt, sync::Arc};
use ra_syntax::{SourceFileNode, TextRange, TextUnit}; use ra_syntax::{SourceFileNode, TextRange, TextUnit};
use ra_text_edit::AtomEdit; use ra_text_edit::AtomTextEdit;
use ra_db::FileResolverImp; use ra_db::FileResolverImp;
use rayon::prelude::*; use rayon::prelude::*;
use relative_path::RelativePathBuf; use relative_path::RelativePathBuf;
@ -121,7 +121,7 @@ pub struct SourceChange {
#[derive(Debug)] #[derive(Debug)]
pub struct SourceFileNodeEdit { pub struct SourceFileNodeEdit {
pub file_id: FileId, pub file_id: FileId,
pub edits: Vec<AtomEdit>, pub edits: Vec<AtomTextEdit>,
} }
#[derive(Debug)] #[derive(Debug)]

View file

@ -8,11 +8,11 @@ use ra_syntax::{
SyntaxNodeRef, TextRange, TextUnit, SyntaxNodeRef, TextRange, TextUnit,
}; };
use crate::{find_node_at_offset, Edit, EditBuilder}; use crate::{find_node_at_offset, TextEdit, TextEditBuilder};
#[derive(Debug)] #[derive(Debug)]
pub struct LocalEdit { pub struct LocalEdit {
pub edit: Edit, pub edit: TextEdit,
pub cursor_position: Option<TextUnit>, pub cursor_position: Option<TextUnit>,
} }
@ -26,7 +26,7 @@ pub fn flip_comma<'a>(
let prev = non_trivia_sibling(comma, Direction::Prev)?; let prev = non_trivia_sibling(comma, Direction::Prev)?;
let next = non_trivia_sibling(comma, Direction::Next)?; let next = non_trivia_sibling(comma, Direction::Next)?;
Some(move || { Some(move || {
let mut edit = EditBuilder::new(); let mut edit = TextEditBuilder::new();
edit.replace(prev.range(), next.text().to_string()); edit.replace(prev.range(), next.text().to_string());
edit.replace(next.range(), prev.text().to_string()); edit.replace(next.range(), prev.text().to_string());
LocalEdit { LocalEdit {
@ -49,7 +49,7 @@ pub fn add_derive<'a>(
.filter(|(name, _arg)| name == "derive") .filter(|(name, _arg)| name == "derive")
.map(|(_name, arg)| arg) .map(|(_name, arg)| arg)
.next(); .next();
let mut edit = EditBuilder::new(); let mut edit = TextEditBuilder::new();
let offset = match derive_attr { let offset = match derive_attr {
None => { None => {
edit.insert(node_start, "#[derive()]\n".to_string()); edit.insert(node_start, "#[derive()]\n".to_string());
@ -82,7 +82,7 @@ pub fn add_impl<'a>(
Some(move || { Some(move || {
let type_params = nominal.type_param_list(); let type_params = nominal.type_param_list();
let mut edit = EditBuilder::new(); let mut edit = TextEditBuilder::new();
let start_offset = nominal.syntax().range().end(); let start_offset = nominal.syntax().range().end();
let mut buf = String::new(); let mut buf = String::new();
buf.push_str("\n\nimpl"); buf.push_str("\n\nimpl");
@ -129,7 +129,7 @@ pub fn introduce_variable<'a>(
} }
return Some(move || { return Some(move || {
let mut buf = String::new(); let mut buf = String::new();
let mut edit = EditBuilder::new(); let mut edit = TextEditBuilder::new();
buf.push_str("let var_name = "); buf.push_str("let var_name = ");
expr.syntax().text().push_to(&mut buf); expr.syntax().text().push_to(&mut buf);

View file

@ -15,7 +15,7 @@ pub use self::{
symbols::{file_structure, file_symbols, FileSymbol, StructureNode}, symbols::{file_structure, file_symbols, FileSymbol, StructureNode},
typing::{join_lines, on_enter, on_eq_typed}, typing::{join_lines, on_enter, on_eq_typed},
}; };
use ra_text_edit::{Edit, EditBuilder}; use ra_text_edit::{TextEdit, TextEditBuilder};
use ra_syntax::{ use ra_syntax::{
algo::find_leaf_at_offset, algo::find_leaf_at_offset,
ast::{self, AstNode, NameOwner}, ast::{self, AstNode, NameOwner},

View file

@ -10,7 +10,7 @@ use ra_syntax::{
}; };
use ra_text_edit::text_utils::contains_offset_nonstrict; use ra_text_edit::text_utils::contains_offset_nonstrict;
use crate::{find_node_at_offset, EditBuilder, LocalEdit}; use crate::{find_node_at_offset, TextEditBuilder, LocalEdit};
pub fn join_lines(file: &SourceFileNode, range: TextRange) -> LocalEdit { pub fn join_lines(file: &SourceFileNode, range: TextRange) -> LocalEdit {
let range = if range.is_empty() { let range = if range.is_empty() {
@ -19,7 +19,7 @@ pub fn join_lines(file: &SourceFileNode, range: TextRange) -> LocalEdit {
let pos = match text.find('\n') { let pos = match text.find('\n') {
None => { None => {
return LocalEdit { return LocalEdit {
edit: EditBuilder::new().finish(), edit: TextEditBuilder::new().finish(),
cursor_position: None, cursor_position: None,
}; };
} }
@ -31,7 +31,7 @@ pub fn join_lines(file: &SourceFileNode, range: TextRange) -> LocalEdit {
}; };
let node = find_covering_node(file.syntax(), range); let node = find_covering_node(file.syntax(), range);
let mut edit = EditBuilder::new(); let mut edit = TextEditBuilder::new();
for node in node.descendants() { for node in node.descendants() {
let text = match node.leaf_text() { let text = match node.leaf_text() {
Some(text) => text, Some(text) => text,
@ -73,7 +73,7 @@ pub fn on_enter(file: &SourceFileNode, offset: TextUnit) -> Option<LocalEdit> {
let indent = node_indent(file, comment.syntax())?; let indent = node_indent(file, comment.syntax())?;
let inserted = format!("\n{}{} ", indent, prefix); let inserted = format!("\n{}{} ", indent, prefix);
let cursor_position = offset + TextUnit::of_str(&inserted); let cursor_position = offset + TextUnit::of_str(&inserted);
let mut edit = EditBuilder::new(); let mut edit = TextEditBuilder::new();
edit.insert(offset, inserted); edit.insert(offset, inserted);
Some(LocalEdit { Some(LocalEdit {
edit: edit.finish(), edit: edit.finish(),
@ -123,7 +123,7 @@ pub fn on_eq_typed(file: &SourceFileNode, offset: TextUnit) -> Option<LocalEdit>
return None; return None;
} }
let offset = let_stmt.syntax().range().end(); let offset = let_stmt.syntax().range().end();
let mut edit = EditBuilder::new(); let mut edit = TextEditBuilder::new();
edit.insert(offset, ";".to_string()); edit.insert(offset, ";".to_string());
Some(LocalEdit { Some(LocalEdit {
edit: edit.finish(), edit: edit.finish(),
@ -131,7 +131,12 @@ pub fn on_eq_typed(file: &SourceFileNode, offset: TextUnit) -> Option<LocalEdit>
}) })
} }
fn remove_newline(edit: &mut EditBuilder, node: SyntaxNodeRef, node_text: &str, offset: TextUnit) { fn remove_newline(
edit: &mut TextEditBuilder,
node: SyntaxNodeRef,
node_text: &str,
offset: TextUnit,
) {
if node.kind() != WHITESPACE || node_text.bytes().filter(|&b| b == b'\n').count() != 1 { if node.kind() != WHITESPACE || node_text.bytes().filter(|&b| b == b'\n').count() != 1 {
// The node is either the first or the last in the file // The node is either the first or the last in the file
let suff = &node_text[TextRange::from_to( let suff = &node_text[TextRange::from_to(
@ -192,7 +197,7 @@ fn is_trailing_comma(left: SyntaxKind, right: SyntaxKind) -> bool {
} }
} }
fn join_single_expr_block(edit: &mut EditBuilder, node: SyntaxNodeRef) -> Option<()> { fn join_single_expr_block(edit: &mut TextEditBuilder, node: SyntaxNodeRef) -> Option<()> {
let block = ast::Block::cast(node.parent()?)?; let block = ast::Block::cast(node.parent()?)?;
let block_expr = ast::BlockExpr::cast(block.syntax().parent()?)?; let block_expr = ast::BlockExpr::cast(block.syntax().parent()?)?;
let expr = single_expr(block)?; let expr = single_expr(block)?;
@ -270,14 +275,14 @@ fn foo() {
fn test_join_lines_lambda_block() { fn test_join_lines_lambda_block() {
check_join_lines( check_join_lines(
r" r"
pub fn reparse(&self, edit: &AtomEdit) -> File { pub fn reparse(&self, edit: &AtomTextEdit) -> File {
<|>self.incremental_reparse(edit).unwrap_or_else(|| { <|>self.incremental_reparse(edit).unwrap_or_else(|| {
self.full_reparse(edit) self.full_reparse(edit)
}) })
} }
", ",
r" r"
pub fn reparse(&self, edit: &AtomEdit) -> File { pub fn reparse(&self, edit: &AtomTextEdit) -> File {
<|>self.incremental_reparse(edit).unwrap_or_else(|| self.full_reparse(edit)) <|>self.incremental_reparse(edit).unwrap_or_else(|| self.full_reparse(edit))
} }
", ",

View file

@ -1,10 +1,10 @@
use languageserver_types::{ use languageserver_types::{
Location, Position, Range, SymbolKind, TextDocumentEdit, TextDocumentIdentifier, self, Location, Position, Range, SymbolKind, TextDocumentEdit, TextDocumentIdentifier,
TextDocumentItem, TextDocumentPositionParams, TextEdit, Url, VersionedTextDocumentIdentifier, TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier,
}; };
use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileNodeEdit, FilePosition}; use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileNodeEdit, FilePosition};
use ra_editor::{LineCol, LineIndex}; use ra_editor::{LineCol, LineIndex};
use ra_text_edit::{AtomEdit, Edit}; use ra_text_edit::{AtomTextEdit, TextEdit};
use ra_syntax::{SyntaxKind, TextRange, TextUnit}; use ra_syntax::{SyntaxKind, TextRange, TextUnit};
use crate::{req, server_world::ServerWorld, Result}; use crate::{req, server_world::ServerWorld, Result};
@ -92,11 +92,11 @@ impl ConvWith for Range {
} }
} }
impl ConvWith for Edit { impl ConvWith for TextEdit {
type Ctx = LineIndex; type Ctx = LineIndex;
type Output = Vec<TextEdit>; type Output = Vec<languageserver_types::TextEdit>;
fn conv_with(self, line_index: &LineIndex) -> Vec<TextEdit> { fn conv_with(self, line_index: &LineIndex) -> Vec<languageserver_types::TextEdit> {
self.into_atoms() self.into_atoms()
.into_iter() .into_iter()
.map_conv_with(line_index) .map_conv_with(line_index)
@ -104,12 +104,12 @@ impl ConvWith for Edit {
} }
} }
impl ConvWith for AtomEdit { impl ConvWith for AtomTextEdit {
type Ctx = LineIndex; type Ctx = LineIndex;
type Output = TextEdit; type Output = languageserver_types::TextEdit;
fn conv_with(self, line_index: &LineIndex) -> TextEdit { fn conv_with(self, line_index: &LineIndex) -> languageserver_types::TextEdit {
TextEdit { languageserver_types::TextEdit {
range: self.delete.conv_with(line_index), range: self.delete.conv_with(line_index),
new_text: self.insert, new_text: self.insert,
} }
@ -229,7 +229,7 @@ impl TryConvWith for SourceChange {
fn translate_offset_with_edit( fn translate_offset_with_edit(
pre_edit_index: &LineIndex, pre_edit_index: &LineIndex,
offset: TextUnit, offset: TextUnit,
edits: &[AtomEdit], edits: &[AtomTextEdit],
) -> LineCol { ) -> LineCol {
let fallback = pre_edit_index.line_col(offset); let fallback = pre_edit_index.line_col(offset);
let edit = match edits.first() { let edit = match edits.first() {

View file

@ -47,7 +47,7 @@ pub use crate::{
}, },
}; };
use ra_text_edit::AtomEdit; use ra_text_edit::AtomTextEdit;
use crate::yellow::GreenNode; use crate::yellow::GreenNode;
/// `SourceFileNode` represents a parse tree for a single Rust file. /// `SourceFileNode` represents a parse tree for a single Rust file.
@ -68,15 +68,15 @@ impl SourceFileNode {
parser_impl::parse_with(yellow::GreenBuilder::new(), text, &tokens, grammar::root); parser_impl::parse_with(yellow::GreenBuilder::new(), text, &tokens, grammar::root);
SourceFileNode::new(green, errors) SourceFileNode::new(green, errors)
} }
pub fn reparse(&self, edit: &AtomEdit) -> SourceFileNode { pub fn reparse(&self, edit: &AtomTextEdit) -> SourceFileNode {
self.incremental_reparse(edit) self.incremental_reparse(edit)
.unwrap_or_else(|| self.full_reparse(edit)) .unwrap_or_else(|| self.full_reparse(edit))
} }
pub fn incremental_reparse(&self, edit: &AtomEdit) -> Option<SourceFileNode> { pub fn incremental_reparse(&self, edit: &AtomTextEdit) -> Option<SourceFileNode> {
reparsing::incremental_reparse(self.syntax(), edit, self.errors()) reparsing::incremental_reparse(self.syntax(), edit, self.errors())
.map(|(green_node, errors)| SourceFileNode::new(green_node, errors)) .map(|(green_node, errors)| SourceFileNode::new(green_node, errors))
} }
fn full_reparse(&self, edit: &AtomEdit) -> SourceFileNode { fn full_reparse(&self, edit: &AtomTextEdit) -> SourceFileNode {
let text = let text =
text_utils::replace_range(self.syntax().text().to_string(), edit.delete, &edit.insert); text_utils::replace_range(self.syntax().text().to_string(), edit.delete, &edit.insert);
SourceFileNode::parse(&text) SourceFileNode::parse(&text)

View file

@ -6,11 +6,11 @@ use crate::parser_impl;
use crate::text_utils::replace_range; use crate::text_utils::replace_range;
use crate::yellow::{self, GreenNode, SyntaxError, SyntaxNodeRef}; use crate::yellow::{self, GreenNode, SyntaxError, SyntaxNodeRef};
use crate::{SyntaxKind::*, TextRange, TextUnit}; use crate::{SyntaxKind::*, TextRange, TextUnit};
use ra_text_edit::AtomEdit; use ra_text_edit::AtomTextEdit;
pub(crate) fn incremental_reparse( pub(crate) fn incremental_reparse(
node: SyntaxNodeRef, node: SyntaxNodeRef,
edit: &AtomEdit, edit: &AtomTextEdit,
errors: Vec<SyntaxError>, errors: Vec<SyntaxError>,
) -> Option<(GreenNode, Vec<SyntaxError>)> { ) -> Option<(GreenNode, Vec<SyntaxError>)> {
let (node, green, new_errors) = let (node, green, new_errors) =
@ -22,7 +22,7 @@ pub(crate) fn incremental_reparse(
fn reparse_leaf<'node>( fn reparse_leaf<'node>(
node: SyntaxNodeRef<'node>, node: SyntaxNodeRef<'node>,
edit: &AtomEdit, edit: &AtomTextEdit,
) -> Option<(SyntaxNodeRef<'node>, GreenNode, Vec<SyntaxError>)> { ) -> Option<(SyntaxNodeRef<'node>, GreenNode, Vec<SyntaxError>)> {
let node = algo::find_covering_node(node, edit.delete); let node = algo::find_covering_node(node, edit.delete);
match node.kind() { match node.kind() {
@ -48,7 +48,7 @@ fn reparse_leaf<'node>(
fn reparse_block<'node>( fn reparse_block<'node>(
node: SyntaxNodeRef<'node>, node: SyntaxNodeRef<'node>,
edit: &AtomEdit, edit: &AtomTextEdit,
) -> Option<(SyntaxNodeRef<'node>, GreenNode, Vec<SyntaxError>)> { ) -> Option<(SyntaxNodeRef<'node>, GreenNode, Vec<SyntaxError>)> {
let (node, reparser) = find_reparsable_node(node, edit.delete)?; let (node, reparser) = find_reparsable_node(node, edit.delete)?;
let text = get_text_after_edit(node, &edit); let text = get_text_after_edit(node, &edit);
@ -61,7 +61,7 @@ fn reparse_block<'node>(
Some((node, green, new_errors)) Some((node, green, new_errors))
} }
fn get_text_after_edit(node: SyntaxNodeRef, edit: &AtomEdit) -> String { fn get_text_after_edit(node: SyntaxNodeRef, edit: &AtomTextEdit) -> String {
replace_range( replace_range(
node.text().to_string(), node.text().to_string(),
edit.delete - node.range().start(), edit.delete - node.range().start(),
@ -139,7 +139,7 @@ fn merge_errors(
old_errors: Vec<SyntaxError>, old_errors: Vec<SyntaxError>,
new_errors: Vec<SyntaxError>, new_errors: Vec<SyntaxError>,
old_node: SyntaxNodeRef, old_node: SyntaxNodeRef,
edit: &AtomEdit, edit: &AtomTextEdit,
) -> Vec<SyntaxError> { ) -> Vec<SyntaxError> {
let mut res = Vec::new(); let mut res = Vec::new();
for e in old_errors { for e in old_errors {
@ -166,7 +166,7 @@ mod tests {
where where
for<'a> F: Fn( for<'a> F: Fn(
SyntaxNodeRef<'a>, SyntaxNodeRef<'a>,
&AtomEdit, &AtomTextEdit,
) -> Option<(SyntaxNodeRef<'a>, GreenNode, Vec<SyntaxError>)>, ) -> Option<(SyntaxNodeRef<'a>, GreenNode, Vec<SyntaxError>)>,
{ {
let (range, before) = extract_range(before); let (range, before) = extract_range(before);
@ -175,7 +175,7 @@ mod tests {
let fully_reparsed = SourceFileNode::parse(&after); let fully_reparsed = SourceFileNode::parse(&after);
let incrementally_reparsed = { let incrementally_reparsed = {
let f = SourceFileNode::parse(&before); let f = SourceFileNode::parse(&before);
let edit = AtomEdit { let edit = AtomTextEdit {
delete: range, delete: range,
insert: replace_with.to_string(), insert: replace_with.to_string(),
}; };

View file

@ -10,10 +10,10 @@
); );
File::new(green, errors) File::new(green, errors)
} }
pub fn reparse(&self, edit: &AtomEdit) -> File { pub fn reparse(&self, edit: &AtomTextEdit) -> File {
self.incremental_reparse(edit).unwrap_or_else(|| self.full_reparse(edit)) self.incremental_reparse(edit).unwrap_or_else(|| self.full_reparse(edit))
} }
pub fn incremental_reparse(&self, edit: &AtomEdit) -> Option<File> { pub fn incremental_reparse(&self, edit: &AtomTextEdit) -> Option<File> {
let (node, reparser) = find_reparsable_node(self.syntax(), edit.delete)?; let (node, reparser) = find_reparsable_node(self.syntax(), edit.delete)?;
let text = replace_range( let text = replace_range(
node.text().to_string(), node.text().to_string(),
@ -31,7 +31,7 @@
let errors = merge_errors(self.errors(), new_errors, node, edit); let errors = merge_errors(self.errors(), new_errors, node, edit);
Some(File::new(green_root, errors)) Some(File::new(green_root, errors))
} }
fn full_reparse(&self, edit: &AtomEdit) -> File { fn full_reparse(&self, edit: &AtomTextEdit) -> File {
let text = replace_range(self.syntax().text().to_string(), edit.delete, &edit.insert); let text = replace_range(self.syntax().text().to_string(), edit.delete, &edit.insert);
File::parse(&text) File::parse(&text)
} }
@ -58,22 +58,22 @@
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct AtomEdit { pub struct AtomTextEdit {
pub delete: TextRange, pub delete: TextRange,
pub insert: String, pub insert: String,
} }
impl AtomEdit { impl AtomTextEdit {
pub fn replace(range: TextRange, replace_with: String) -> AtomEdit { pub fn replace(range: TextRange, replace_with: String) -> AtomTextEdit {
AtomEdit { delete: range, insert: replace_with } AtomTextEdit { delete: range, insert: replace_with }
} }
pub fn delete(range: TextRange) -> AtomEdit { pub fn delete(range: TextRange) -> AtomTextEdit {
AtomEdit::replace(range, String::new()) AtomTextEdit::replace(range, String::new())
} }
pub fn insert(offset: TextUnit, text: String) -> AtomEdit { pub fn insert(offset: TextUnit, text: String) -> AtomTextEdit {
AtomEdit::replace(TextRange::offset_len(offset, 0.into()), text) AtomTextEdit::replace(TextRange::offset_len(offset, 0.into()), text)
} }
} }
@ -114,17 +114,17 @@ fn is_balanced(tokens: &[Token]) -> bool {
pub insert: String, pub insert: String,
} }
impl AtomEdit { impl AtomTextEdit {
pub fn replace(range: TextRange, replace_with: String) -> AtomEdit { pub fn replace(range: TextRange, replace_with: String) -> AtomTextEdit {
AtomEdit { delete: range, insert: replace_with } AtomTextEdit { delete: range, insert: replace_with }
} }
pub fn delete(range: TextRange) -> AtomEdit { pub fn delete(range: TextRange) -> AtomTextEdit {
AtomEdit::replace(range, String::new()) AtomTextEdit::replace(range, String::new())
} }
pub fn insert(offset: TextUnit, text: String) -> AtomEdit { pub fn insert(offset: TextUnit, text: String) -> AtomTextEdit {
AtomEdit::replace(TextRange::offset_len(offset, 0.into()), text) AtomTextEdit::replace(TextRange::offset_len(offset, 0.into()), text)
} }
} }
@ -176,7 +176,7 @@ fn merge_errors(
old_errors: Vec<SyntaxError>, old_errors: Vec<SyntaxError>,
new_errors: Vec<SyntaxError>, new_errors: Vec<SyntaxError>,
old_node: SyntaxNodeRef, old_node: SyntaxNodeRef,
edit: &AtomEdit, edit: &AtomTextEdit,
) -> Vec<SyntaxError> { ) -> Vec<SyntaxError> {
let mut res = Vec::new(); let mut res = Vec::new();
for e in old_errors { for e in old_errors {

View file

@ -1,29 +1,29 @@
mod edit; mod text_edit;
pub mod text_utils; pub mod text_utils;
pub use crate::edit::{Edit, EditBuilder}; pub use crate::text_edit::{TextEdit, TextEditBuilder};
use text_unit::{TextRange, TextUnit}; use text_unit::{TextRange, TextUnit};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct AtomEdit { pub struct AtomTextEdit {
pub delete: TextRange, pub delete: TextRange,
pub insert: String, pub insert: String,
} }
impl AtomEdit { impl AtomTextEdit {
pub fn replace(range: TextRange, replace_with: String) -> AtomEdit { pub fn replace(range: TextRange, replace_with: String) -> AtomTextEdit {
AtomEdit { AtomTextEdit {
delete: range, delete: range,
insert: replace_with, insert: replace_with,
} }
} }
pub fn delete(range: TextRange) -> AtomEdit { pub fn delete(range: TextRange) -> AtomTextEdit {
AtomEdit::replace(range, String::new()) AtomTextEdit::replace(range, String::new())
} }
pub fn insert(offset: TextUnit, text: String) -> AtomEdit { pub fn insert(offset: TextUnit, text: String) -> AtomTextEdit {
AtomEdit::replace(TextRange::offset_len(offset, 0.into()), text) AtomTextEdit::replace(TextRange::offset_len(offset, 0.into()), text)
} }
} }

View file

@ -1,37 +1,37 @@
use crate::AtomEdit; use crate::AtomTextEdit;
use crate::text_utils::contains_offset_nonstrict; use crate::text_utils::contains_offset_nonstrict;
use text_unit::{TextRange, TextUnit}; use text_unit::{TextRange, TextUnit};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Edit { pub struct TextEdit {
atoms: Vec<AtomEdit>, atoms: Vec<AtomTextEdit>,
} }
#[derive(Debug)] #[derive(Debug)]
pub struct EditBuilder { pub struct TextEditBuilder {
atoms: Vec<AtomEdit>, atoms: Vec<AtomTextEdit>,
} }
impl EditBuilder { impl TextEditBuilder {
pub fn new() -> EditBuilder { pub fn new() -> TextEditBuilder {
EditBuilder { atoms: Vec::new() } TextEditBuilder { atoms: Vec::new() }
} }
pub fn replace(&mut self, range: TextRange, replace_with: String) { pub fn replace(&mut self, range: TextRange, replace_with: String) {
self.atoms.push(AtomEdit::replace(range, replace_with)) self.atoms.push(AtomTextEdit::replace(range, replace_with))
} }
pub fn delete(&mut self, range: TextRange) { pub fn delete(&mut self, range: TextRange) {
self.atoms.push(AtomEdit::delete(range)) self.atoms.push(AtomTextEdit::delete(range))
} }
pub fn insert(&mut self, offset: TextUnit, text: String) { pub fn insert(&mut self, offset: TextUnit, text: String) {
self.atoms.push(AtomEdit::insert(offset, text)) self.atoms.push(AtomTextEdit::insert(offset, text))
} }
pub fn finish(self) -> Edit { pub fn finish(self) -> TextEdit {
let mut atoms = self.atoms; let mut atoms = self.atoms;
atoms.sort_by_key(|a| (a.delete.start(), a.delete.end())); atoms.sort_by_key(|a| (a.delete.start(), a.delete.end()));
for (a1, a2) in atoms.iter().zip(atoms.iter().skip(1)) { for (a1, a2) in atoms.iter().zip(atoms.iter().skip(1)) {
assert!(a1.delete.end() <= a2.delete.start()) assert!(a1.delete.end() <= a2.delete.start())
} }
Edit { atoms } TextEdit { atoms }
} }
pub fn invalidates_offset(&self, offset: TextUnit) -> bool { pub fn invalidates_offset(&self, offset: TextUnit) -> bool {
self.atoms self.atoms
@ -40,8 +40,8 @@ impl EditBuilder {
} }
} }
impl Edit { impl TextEdit {
pub fn into_atoms(self) -> Vec<AtomEdit> { pub fn into_atoms(self) -> Vec<AtomTextEdit> {
self.atoms self.atoms
} }