Merge #8061
8061: Auto-magical whitespace r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
2f9ce4f663
@ -8,7 +8,7 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
ast,
|
ast,
|
||||||
ted::{self, Position},
|
ted::{self, Position},
|
||||||
AstNode, Direction, SyntaxKind,
|
AstNode, Direction, SyntaxElement,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::NameOwner;
|
use super::NameOwner;
|
||||||
@ -27,7 +27,7 @@ fn get_or_create_where_clause(&self) -> WhereClause {
|
|||||||
} else {
|
} else {
|
||||||
Position::last_child_of(self.syntax().clone())
|
Position::last_child_of(self.syntax().clone())
|
||||||
};
|
};
|
||||||
create_where_clause(position, true)
|
create_where_clause(position)
|
||||||
}
|
}
|
||||||
self.where_clause().unwrap()
|
self.where_clause().unwrap()
|
||||||
}
|
}
|
||||||
@ -41,7 +41,7 @@ fn get_or_create_where_clause(&self) -> WhereClause {
|
|||||||
} else {
|
} else {
|
||||||
Position::last_child_of(self.syntax().clone())
|
Position::last_child_of(self.syntax().clone())
|
||||||
};
|
};
|
||||||
create_where_clause(position, false)
|
create_where_clause(position)
|
||||||
}
|
}
|
||||||
self.where_clause().unwrap()
|
self.where_clause().unwrap()
|
||||||
}
|
}
|
||||||
@ -55,7 +55,7 @@ fn get_or_create_where_clause(&self) -> WhereClause {
|
|||||||
} else {
|
} else {
|
||||||
Position::last_child_of(self.syntax().clone())
|
Position::last_child_of(self.syntax().clone())
|
||||||
};
|
};
|
||||||
create_where_clause(position, false)
|
create_where_clause(position)
|
||||||
}
|
}
|
||||||
self.where_clause().unwrap()
|
self.where_clause().unwrap()
|
||||||
}
|
}
|
||||||
@ -77,7 +77,7 @@ fn get_or_create_where_clause(&self) -> WhereClause {
|
|||||||
} else {
|
} else {
|
||||||
Position::last_child_of(self.syntax().clone())
|
Position::last_child_of(self.syntax().clone())
|
||||||
};
|
};
|
||||||
create_where_clause(position, true)
|
create_where_clause(position)
|
||||||
}
|
}
|
||||||
self.where_clause().unwrap()
|
self.where_clause().unwrap()
|
||||||
}
|
}
|
||||||
@ -93,21 +93,16 @@ fn get_or_create_where_clause(&self) -> WhereClause {
|
|||||||
} else {
|
} else {
|
||||||
Position::last_child_of(self.syntax().clone())
|
Position::last_child_of(self.syntax().clone())
|
||||||
};
|
};
|
||||||
create_where_clause(position, true)
|
create_where_clause(position)
|
||||||
}
|
}
|
||||||
self.where_clause().unwrap()
|
self.where_clause().unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_where_clause(position: Position, after: bool) {
|
fn create_where_clause(position: Position) {
|
||||||
let mut elements = vec![make::where_clause(empty()).clone_for_update().syntax().clone().into()];
|
let where_clause: SyntaxElement =
|
||||||
let ws = make::tokens::single_space().into();
|
make::where_clause(empty()).clone_for_update().syntax().clone().into();
|
||||||
if after {
|
ted::insert_ws(position, where_clause);
|
||||||
elements.insert(0, ws)
|
|
||||||
} else {
|
|
||||||
elements.push(ws)
|
|
||||||
}
|
|
||||||
ted::insert_all(position, elements);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ast::WhereClause {
|
impl ast::WhereClause {
|
||||||
@ -117,12 +112,7 @@ pub fn add_predicate(&self, predicate: ast::WherePred) {
|
|||||||
ted::append_child(self.syntax().clone(), make::token(T![,]));
|
ted::append_child(self.syntax().clone(), make::token(T![,]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if self.syntax().children_with_tokens().last().map(|it| it.kind())
|
ted::append_child_ws(self.syntax().clone(), predicate.syntax().clone())
|
||||||
!= Some(SyntaxKind::WHITESPACE)
|
|
||||||
{
|
|
||||||
ted::append_child(self.syntax().clone(), make::tokens::single_space());
|
|
||||||
}
|
|
||||||
ted::append_child(self.syntax().clone(), predicate.syntax().clone())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
//! Primitive tree editor, ed for trees
|
//! Primitive tree editor, ed for trees
|
||||||
#![allow(unused)]
|
|
||||||
use std::ops::RangeInclusive;
|
use std::ops::RangeInclusive;
|
||||||
|
|
||||||
use crate::{SyntaxElement, SyntaxNode};
|
use parser::T;
|
||||||
|
|
||||||
|
use crate::{ast::make, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Position {
|
pub struct Position {
|
||||||
@ -42,9 +43,25 @@ pub fn last_child_of(node: impl Into<SyntaxNode>) -> 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>) {
|
||||||
|
if let Some(first) = elements.first() {
|
||||||
|
if let Some(ws) = ws_before(&position, first) {
|
||||||
|
elements.insert(0, ws.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let Some(last) = elements.last() {
|
||||||
|
if let Some(ws) = ws_after(&position, last) {
|
||||||
|
elements.push(ws.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
insert_all(position, elements)
|
||||||
|
}
|
||||||
pub fn insert_all(position: Position, elements: Vec<SyntaxElement>) {
|
pub fn insert_all(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),
|
||||||
@ -72,7 +89,35 @@ 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn ws_before(position: &Position, new: &SyntaxElement) -> Option<SyntaxToken> {
|
||||||
|
let prev = match &position.repr {
|
||||||
|
PositionRepr::FirstChild(_) => return None,
|
||||||
|
PositionRepr::After(it) => it,
|
||||||
|
};
|
||||||
|
ws_between(prev, new)
|
||||||
|
}
|
||||||
|
fn ws_after(position: &Position, new: &SyntaxElement) -> Option<SyntaxToken> {
|
||||||
|
let next = match &position.repr {
|
||||||
|
PositionRepr::FirstChild(parent) => parent.first_child_or_token()?,
|
||||||
|
PositionRepr::After(sibling) => sibling.next_sibling_or_token()?,
|
||||||
|
};
|
||||||
|
ws_between(new, &next)
|
||||||
|
}
|
||||||
|
fn ws_between(left: &SyntaxElement, right: &SyntaxElement) -> Option<SyntaxToken> {
|
||||||
|
if left.kind() == SyntaxKind::WHITESPACE || right.kind() == SyntaxKind::WHITESPACE {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
if right.kind() == T![;] || right.kind() == T![,] {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
Some(make::tokens::single_space().into())
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user