8062: pit-of-successify tree editor r=matklad a=matklad

bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2021-03-16 20:01:20 +00:00 committed by GitHub
commit 83e6940efb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 14 deletions

View File

@ -102,17 +102,17 @@ impl GenericParamsOwnerEdit for ast::Enum {
fn create_where_clause(position: Position) { fn create_where_clause(position: Position) {
let where_clause: SyntaxElement = let where_clause: SyntaxElement =
make::where_clause(empty()).clone_for_update().syntax().clone().into(); make::where_clause(empty()).clone_for_update().syntax().clone().into();
ted::insert_ws(position, where_clause); ted::insert(position, where_clause);
} }
impl ast::WhereClause { impl ast::WhereClause {
pub fn add_predicate(&self, predicate: ast::WherePred) { pub fn add_predicate(&self, predicate: ast::WherePred) {
if let Some(pred) = self.predicates().last() { if let Some(pred) = self.predicates().last() {
if !pred.syntax().siblings_with_tokens(Direction::Next).any(|it| it.kind() == T![,]) { if !pred.syntax().siblings_with_tokens(Direction::Next).any(|it| it.kind() == T![,]) {
ted::append_child(self.syntax().clone(), make::token(T![,])); ted::append_child_raw(self.syntax().clone(), make::token(T![,]));
} }
} }
ted::append_child_ws(self.syntax().clone(), predicate.syntax().clone()) ted::append_child(self.syntax().clone(), predicate.syntax().clone())
} }
} }

View File

@ -1,4 +1,7 @@
//! Primitive tree editor, ed for trees //! Primitive tree editor, ed for trees.
//!
//! The `_raw`-suffixed functions insert elements as is, unsuffixed versions fix
//! up elements around the edges.
use std::ops::RangeInclusive; use std::ops::RangeInclusive;
use parser::T; use parser::T;
@ -43,13 +46,13 @@ impl Position {
} }
} }
pub fn insert_ws(position: Position, elem: impl Into<SyntaxElement>) {
insert_all_ws(position, vec![elem.into()])
}
pub fn insert(position: Position, elem: impl Into<SyntaxElement>) { pub fn insert(position: Position, elem: impl Into<SyntaxElement>) {
insert_all(position, vec![elem.into()]) insert_all(position, vec![elem.into()])
} }
pub fn insert_all_ws(position: Position, mut elements: Vec<SyntaxElement>) { pub fn insert_raw(position: Position, elem: impl Into<SyntaxElement>) {
insert_all_raw(position, vec![elem.into()])
}
pub fn insert_all(position: Position, mut elements: Vec<SyntaxElement>) {
if let Some(first) = elements.first() { if let Some(first) = elements.first() {
if let Some(ws) = ws_before(&position, first) { if let Some(ws) = ws_before(&position, first) {
elements.insert(0, ws.into()) elements.insert(0, ws.into())
@ -60,9 +63,9 @@ pub fn insert_all_ws(position: Position, mut elements: Vec<SyntaxElement>) {
elements.push(ws.into()) elements.push(ws.into())
} }
} }
insert_all(position, elements) insert_all_raw(position, elements)
} }
pub fn insert_all(position: Position, elements: Vec<SyntaxElement>) { pub fn insert_all_raw(position: Position, elements: Vec<SyntaxElement>) {
let (parent, index) = match position.repr { let (parent, index) = match position.repr {
PositionRepr::FirstChild(parent) => (parent, 0), PositionRepr::FirstChild(parent) => (parent, 0),
PositionRepr::After(child) => (child.parent().unwrap(), child.index() + 1), PositionRepr::After(child) => (child.parent().unwrap(), child.index() + 1),
@ -89,14 +92,14 @@ pub fn replace_all(range: RangeInclusive<SyntaxElement>, new: Vec<SyntaxElement>
parent.splice_children(start..end + 1, new) parent.splice_children(start..end + 1, new)
} }
pub fn append_child_ws(node: impl Into<SyntaxNode>, child: impl Into<SyntaxElement>) {
let position = Position::last_child_of(node);
insert_ws(position, child)
}
pub fn append_child(node: impl Into<SyntaxNode>, child: impl Into<SyntaxElement>) { pub fn append_child(node: impl Into<SyntaxNode>, child: impl Into<SyntaxElement>) {
let position = Position::last_child_of(node); let position = Position::last_child_of(node);
insert(position, child) insert(position, child)
} }
pub fn append_child_raw(node: impl Into<SyntaxNode>, child: impl Into<SyntaxElement>) {
let position = Position::last_child_of(node);
insert_raw(position, child)
}
fn ws_before(position: &Position, new: &SyntaxElement) -> Option<SyntaxToken> { fn ws_before(position: &Position, new: &SyntaxElement) -> Option<SyntaxToken> {
let prev = match &position.repr { let prev = match &position.repr {