mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 04:53:34 +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 {TextRange, TextUnit};
|
||||||
|
use libsyntax2::AtomEdit;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Edit {
|
pub struct Edit {
|
||||||
atoms: Vec<AtomEdit>,
|
atoms: Vec<AtomEdit>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct AtomEdit {
|
|
||||||
pub delete: TextRange,
|
|
||||||
pub insert: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct EditBuilder {
|
pub struct EditBuilder {
|
||||||
atoms: Vec<AtomEdit>
|
atoms: Vec<AtomEdit>
|
||||||
|
@ -21,23 +16,23 @@ impl EditBuilder {
|
||||||
EditBuilder { atoms: Vec::new() }
|
EditBuilder { atoms: Vec::new() }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn replace(&mut self, range: TextRange, replacement: String) {
|
pub fn replace(&mut self, range: TextRange, replace_with: String) {
|
||||||
self.atoms.push(AtomEdit { delete: range, insert: replacement })
|
self.atoms.push(AtomEdit::replace(range, replace_with))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delete(&mut self, range: TextRange) {
|
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) {
|
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 {
|
pub fn finish(self) -> Edit {
|
||||||
let mut atoms = self.atoms;
|
let mut atoms = self.atoms;
|
||||||
atoms.sort_by_key(|a| a.delete.start());
|
atoms.sort_by_key(|a| a.delete.start());
|
||||||
for (a1, a2) in atoms.iter().zip(atoms.iter().skip(1)) {
|
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 }
|
Edit { atoms }
|
||||||
}
|
}
|
||||||
|
@ -52,16 +47,18 @@ impl Edit {
|
||||||
let mut total_len = text.len();
|
let mut total_len = text.len();
|
||||||
for atom in self.atoms.iter() {
|
for atom in self.atoms.iter() {
|
||||||
total_len += atom.insert.len();
|
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 buf = String::with_capacity(total_len);
|
||||||
let mut prev = 0;
|
let mut prev = 0;
|
||||||
for atom in self.atoms.iter() {
|
for atom in self.atoms.iter() {
|
||||||
if atom.start() > prev {
|
let start = u32::from(atom.delete.start()) as usize;
|
||||||
buf.push_str(&text[prev..atom.start()]);
|
let end = u32::from(atom.delete.end()) as usize;
|
||||||
|
if start > prev {
|
||||||
|
buf.push_str(&text[prev..start]);
|
||||||
}
|
}
|
||||||
buf.push_str(&atom.insert);
|
buf.push_str(&atom.insert);
|
||||||
prev = atom.end();
|
prev = end;
|
||||||
}
|
}
|
||||||
buf.push_str(&text[prev..text.len()]);
|
buf.push_str(&text[prev..text.len()]);
|
||||||
assert_eq!(buf.len(), total_len);
|
assert_eq!(buf.len(), total_len);
|
||||||
|
@ -83,13 +80,3 @@ impl Edit {
|
||||||
Some(res)
|
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},
|
algo::{walk, find_leaf_at_offset},
|
||||||
SyntaxKind::{self, *},
|
SyntaxKind::{self, *},
|
||||||
};
|
};
|
||||||
|
pub use libsyntax2::AtomEdit;
|
||||||
pub use self::{
|
pub use self::{
|
||||||
line_index::{LineIndex, LineCol},
|
line_index::{LineIndex, LineCol},
|
||||||
extend_selection::extend_selection,
|
extend_selection::extend_selection,
|
||||||
symbols::{StructureNode, file_structure, FileSymbol, file_symbols},
|
symbols::{StructureNode, file_structure, FileSymbol, file_symbols},
|
||||||
edit::{EditBuilder, Edit, AtomEdit},
|
edit::{EditBuilder, Edit},
|
||||||
code_actions::{
|
code_actions::{
|
||||||
ActionResult, find_node,
|
ActionResult, find_node,
|
||||||
flip_comma, add_derive, add_impl,
|
flip_comma, add_derive, add_impl,
|
||||||
|
|
|
@ -64,7 +64,7 @@ impl File {
|
||||||
validate_block_structure(root.borrowed());
|
validate_block_structure(root.borrowed());
|
||||||
File { root }
|
File { root }
|
||||||
}
|
}
|
||||||
pub fn parse(text: &str) -> Self {
|
pub fn parse(text: &str) -> File {
|
||||||
let tokens = tokenize(&text);
|
let tokens = tokenize(&text);
|
||||||
let (root, errors) = parser_impl::parse::<yellow::GreenBuilder>(text, &tokens);
|
let (root, errors) = parser_impl::parse::<yellow::GreenBuilder>(text, &tokens);
|
||||||
File::new(root, errors)
|
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