mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 12:33:33 +00:00
Move atom edit to libsyntax2
This commit is contained in:
parent
87cd57d56a
commit
32c8ea9307
3 changed files with 35 additions and 27 deletions
|
@ -1,16 +1,11 @@
|
|||
use {TextRange, TextUnit};
|
||||
use libsyntax2::AtomEdit;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Edit {
|
||||
atoms: Vec<AtomEdit>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AtomEdit {
|
||||
pub delete: TextRange,
|
||||
pub insert: String,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct EditBuilder {
|
||||
atoms: Vec<AtomEdit>
|
||||
|
@ -21,23 +16,23 @@ impl EditBuilder {
|
|||
EditBuilder { atoms: Vec::new() }
|
||||
}
|
||||
|
||||
pub fn replace(&mut self, range: TextRange, replacement: String) {
|
||||
self.atoms.push(AtomEdit { delete: range, insert: replacement })
|
||||
pub fn replace(&mut self, range: TextRange, replace_with: String) {
|
||||
self.atoms.push(AtomEdit::replace(range, replace_with))
|
||||
}
|
||||
|
||||
pub fn delete(&mut self, range: TextRange) {
|
||||
self.replace(range, String::new());
|
||||
self.atoms.push(AtomEdit::delete(range))
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, offset: TextUnit, text: String) {
|
||||
self.replace(TextRange::offset_len(offset, 0.into()), text)
|
||||
self.atoms.push(AtomEdit::insert(offset, text))
|
||||
}
|
||||
|
||||
pub fn finish(self) -> Edit {
|
||||
let mut atoms = self.atoms;
|
||||
atoms.sort_by_key(|a| a.delete.start());
|
||||
for (a1, a2) in atoms.iter().zip(atoms.iter().skip(1)) {
|
||||
assert!(a1.end() <= a2.start())
|
||||
assert!(a1.delete.end() <= a2.delete.start())
|
||||
}
|
||||
Edit { atoms }
|
||||
}
|
||||
|
@ -52,16 +47,18 @@ impl Edit {
|
|||
let mut total_len = text.len();
|
||||
for atom in self.atoms.iter() {
|
||||
total_len += atom.insert.len();
|
||||
total_len -= atom.end() - atom.start();
|
||||
total_len -= u32::from(atom.delete.end() - atom.delete.start()) as usize;
|
||||
}
|
||||
let mut buf = String::with_capacity(total_len);
|
||||
let mut prev = 0;
|
||||
for atom in self.atoms.iter() {
|
||||
if atom.start() > prev {
|
||||
buf.push_str(&text[prev..atom.start()]);
|
||||
let start = u32::from(atom.delete.start()) as usize;
|
||||
let end = u32::from(atom.delete.end()) as usize;
|
||||
if start > prev {
|
||||
buf.push_str(&text[prev..start]);
|
||||
}
|
||||
buf.push_str(&atom.insert);
|
||||
prev = atom.end();
|
||||
prev = end;
|
||||
}
|
||||
buf.push_str(&text[prev..text.len()]);
|
||||
assert_eq!(buf.len(), total_len);
|
||||
|
@ -83,13 +80,3 @@ impl Edit {
|
|||
Some(res)
|
||||
}
|
||||
}
|
||||
|
||||
impl AtomEdit {
|
||||
fn start(&self) -> usize {
|
||||
u32::from(self.delete.start()) as usize
|
||||
}
|
||||
|
||||
fn end(&self) -> usize {
|
||||
u32::from(self.delete.end()) as usize
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,11 +15,12 @@ use libsyntax2::{
|
|||
algo::{walk, find_leaf_at_offset},
|
||||
SyntaxKind::{self, *},
|
||||
};
|
||||
pub use libsyntax2::AtomEdit;
|
||||
pub use self::{
|
||||
line_index::{LineIndex, LineCol},
|
||||
extend_selection::extend_selection,
|
||||
symbols::{StructureNode, file_structure, FileSymbol, file_symbols},
|
||||
edit::{EditBuilder, Edit, AtomEdit},
|
||||
edit::{EditBuilder, Edit},
|
||||
code_actions::{
|
||||
ActionResult, find_node,
|
||||
flip_comma, add_derive, add_impl,
|
||||
|
|
|
@ -64,7 +64,7 @@ impl File {
|
|||
validate_block_structure(root.borrowed());
|
||||
File { root }
|
||||
}
|
||||
pub fn parse(text: &str) -> Self {
|
||||
pub fn parse(text: &str) -> File {
|
||||
let tokens = tokenize(&text);
|
||||
let (root, errors) = parser_impl::parse::<yellow::GreenBuilder>(text, &tokens);
|
||||
File::new(root, errors)
|
||||
|
@ -112,3 +112,23 @@ fn validate_block_structure(root: SyntaxNodeRef) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AtomEdit {
|
||||
pub delete: TextRange,
|
||||
pub insert: String,
|
||||
}
|
||||
|
||||
impl AtomEdit {
|
||||
pub fn replace(range: TextRange, replace_with: String) -> AtomEdit {
|
||||
AtomEdit { delete: range, insert: replace_with }
|
||||
}
|
||||
|
||||
pub fn delete(range: TextRange) -> AtomEdit {
|
||||
AtomEdit::replace(range, String::new())
|
||||
}
|
||||
|
||||
pub fn insert(offset: TextUnit, text: String) -> AtomEdit {
|
||||
AtomEdit::replace(TextRange::offset_len(offset, 0.into()), text)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue