Skip to content

Commit 201dbbe

Browse files
bors[bot]matklad
andauthored
Merge #8854
8854: internal: use mutable syntax trees when filling fields r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 9df0a23 + 1859df3 commit 201dbbe

File tree

4 files changed

+49
-104
lines changed

4 files changed

+49
-104
lines changed

crates/ide/src/diagnostics.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ fn test_fn() {
579579
struct TestStruct { one: i32, two: i64 }
580580
581581
fn test_fn() {
582-
let s = TestStruct { one: (), two: ()};
582+
let s = TestStruct { one: (), two: () };
583583
}
584584
"#,
585585
);
@@ -599,7 +599,7 @@ impl TestStruct {
599599
struct TestStruct { one: i32 }
600600
601601
impl TestStruct {
602-
fn test_fn() { let s = Self { one: ()}; }
602+
fn test_fn() { let s = Self { one: () }; }
603603
}
604604
"#,
605605
);
@@ -792,7 +792,7 @@ fn main() {
792792
pub struct Foo { pub a: i32, pub b: i32 }
793793
"#,
794794
r#"
795-
fn some(, b: ()) {}
795+
fn some(, b: () ) {}
796796
fn items() {}
797797
fn here() {}
798798

crates/ide/src/diagnostics/fixes.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,12 @@ impl DiagnosticWithFix for MissingFields {
100100
let root = sema.db.parse_or_expand(self.file)?;
101101
let field_list_parent = self.field_list_parent.to_node(&root);
102102
let old_field_list = field_list_parent.record_expr_field_list()?;
103-
let mut new_field_list = old_field_list.clone();
103+
let new_field_list = old_field_list.clone_for_update();
104104
for f in self.missed_fields.iter() {
105105
let field =
106-
make::record_expr_field(make::name_ref(&f.to_string()), Some(make::expr_unit()));
107-
new_field_list = new_field_list.append_field(&field);
106+
make::record_expr_field(make::name_ref(&f.to_string()), Some(make::expr_unit()))
107+
.clone_for_update();
108+
new_field_list.add_field(field);
108109
}
109110

110111
let edit = {

crates/syntax/src/ast/edit.rs

Lines changed: 2 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,8 @@ use arrayvec::ArrayVec;
1010

1111
use crate::{
1212
algo,
13-
ast::{
14-
self,
15-
make::{self, tokens},
16-
AstNode,
17-
},
18-
ted, AstToken, Direction, InsertPosition, NodeOrToken, SmolStr, SyntaxElement, SyntaxKind,
13+
ast::{self, make, AstNode},
14+
ted, AstToken, InsertPosition, NodeOrToken, SyntaxElement, SyntaxKind,
1915
SyntaxKind::{ATTR, COMMENT, WHITESPACE},
2016
SyntaxNode, SyntaxToken, T,
2117
};
@@ -29,82 +25,6 @@ impl ast::BinExpr {
2925
}
3026
}
3127

32-
impl ast::RecordExprFieldList {
33-
#[must_use]
34-
pub fn append_field(&self, field: &ast::RecordExprField) -> ast::RecordExprFieldList {
35-
self.insert_field(InsertPosition::Last, field)
36-
}
37-
38-
#[must_use]
39-
pub fn insert_field(
40-
&self,
41-
position: InsertPosition<&'_ ast::RecordExprField>,
42-
field: &ast::RecordExprField,
43-
) -> ast::RecordExprFieldList {
44-
let is_multiline = self.syntax().text().contains_char('\n');
45-
let ws;
46-
let space = if is_multiline {
47-
ws = tokens::WsBuilder::new(&format!(
48-
"\n{} ",
49-
leading_indent(self.syntax()).unwrap_or_default()
50-
));
51-
ws.ws()
52-
} else {
53-
tokens::single_space()
54-
};
55-
56-
let mut to_insert: ArrayVec<SyntaxElement, 4> = ArrayVec::new();
57-
to_insert.push(space.into());
58-
to_insert.push(field.syntax().clone().into());
59-
to_insert.push(make::token(T![,]).into());
60-
61-
macro_rules! after_l_curly {
62-
() => {{
63-
let anchor = match self.l_curly_token() {
64-
Some(it) => it.into(),
65-
None => return self.clone(),
66-
};
67-
InsertPosition::After(anchor)
68-
}};
69-
}
70-
71-
macro_rules! after_field {
72-
($anchor:expr) => {
73-
if let Some(comma) = $anchor
74-
.syntax()
75-
.siblings_with_tokens(Direction::Next)
76-
.find(|it| it.kind() == T![,])
77-
{
78-
InsertPosition::After(comma)
79-
} else {
80-
to_insert.insert(0, make::token(T![,]).into());
81-
InsertPosition::After($anchor.syntax().clone().into())
82-
}
83-
};
84-
}
85-
86-
let position = match position {
87-
InsertPosition::First => after_l_curly!(),
88-
InsertPosition::Last => {
89-
if !is_multiline {
90-
// don't insert comma before curly
91-
to_insert.pop();
92-
}
93-
match self.fields().last() {
94-
Some(it) => after_field!(it),
95-
None => after_l_curly!(),
96-
}
97-
}
98-
InsertPosition::Before(anchor) => {
99-
InsertPosition::Before(anchor.syntax().clone().into())
100-
}
101-
InsertPosition::After(anchor) => after_field!(anchor),
102-
};
103-
104-
self.insert_children(position, to_insert)
105-
}
106-
}
107-
10828
impl ast::Path {
10929
#[must_use]
11030
pub fn with_segment(&self, segment: ast::PathSegment) -> ast::Path {
@@ -308,22 +228,6 @@ impl IndentLevel {
308228
}
309229
}
310230

311-
// FIXME: replace usages with IndentLevel above
312-
fn leading_indent(node: &SyntaxNode) -> Option<SmolStr> {
313-
for token in prev_tokens(node.first_token()?) {
314-
if let Some(ws) = ast::Whitespace::cast(token.clone()) {
315-
let ws_text = ws.text();
316-
if let Some(pos) = ws_text.rfind('\n') {
317-
return Some(ws_text[pos + 1..].into());
318-
}
319-
}
320-
if token.text().contains('\n') {
321-
break;
322-
}
323-
}
324-
None
325-
}
326-
327231
fn prev_tokens(token: SyntaxToken) -> impl Iterator<Item = SyntaxToken> {
328232
iter::successors(Some(token), |token| token.prev_token())
329233
}

crates/syntax/src/ast/edit_in_place.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,46 @@ impl ast::MatchArmList {
368368
}
369369
}
370370

371+
impl ast::RecordExprFieldList {
372+
pub fn add_field(&self, field: ast::RecordExprField) {
373+
let is_multiline = self.syntax().text().contains_char('\n');
374+
let whitespace = if is_multiline {
375+
let indent = IndentLevel::from_node(self.syntax()) + 1;
376+
make::tokens::whitespace(&format!("\n{}", indent))
377+
} else {
378+
make::tokens::single_space()
379+
};
380+
381+
let position = match self.fields().last() {
382+
Some(last_field) => {
383+
let comma = match last_field
384+
.syntax()
385+
.siblings_with_tokens(Direction::Next)
386+
.filter_map(|it| it.into_token())
387+
.find(|it| it.kind() == T![,])
388+
{
389+
Some(it) => it,
390+
None => {
391+
let comma = ast::make::token(T![,]);
392+
ted::insert(Position::after(last_field.syntax()), &comma);
393+
comma
394+
}
395+
};
396+
Position::after(comma)
397+
}
398+
None => match self.l_curly_token() {
399+
Some(it) => Position::after(it),
400+
None => Position::last_child_of(self.syntax()),
401+
},
402+
};
403+
404+
ted::insert_all(position, vec![whitespace.into(), field.syntax().clone().into()]);
405+
if is_multiline {
406+
ted::insert(Position::after(field.syntax()), ast::make::token(T![,]));
407+
}
408+
}
409+
}
410+
371411
fn normalize_ws_between_braces(node: &SyntaxNode) -> Option<()> {
372412
let l = node
373413
.children_with_tokens()

0 commit comments

Comments
 (0)