aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax')
-rw-r--r--crates/ra_syntax/src/ast/edit.rs6
-rw-r--r--crates/ra_syntax/src/ast/generated/nodes.rs36
-rw-r--r--crates/ra_syntax/src/fuzz.rs6
-rw-r--r--crates/ra_syntax/src/lib.rs15
-rw-r--r--crates/ra_syntax/src/parsing/reparsing.rs28
5 files changed, 48 insertions, 43 deletions
diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs
index c507dc683..3e6dd6061 100644
--- a/crates/ra_syntax/src/ast/edit.rs
+++ b/crates/ra_syntax/src/ast/edit.rs
@@ -79,7 +79,7 @@ where
79 79
80impl ast::ItemList { 80impl ast::ItemList {
81 #[must_use] 81 #[must_use]
82 pub fn append_items(&self, items: impl IntoIterator<Item = ast::ImplItem>) -> ast::ItemList { 82 pub fn append_items(&self, items: impl IntoIterator<Item = ast::AssocItem>) -> ast::ItemList {
83 let mut res = self.clone(); 83 let mut res = self.clone();
84 if !self.syntax().text().contains_char('\n') { 84 if !self.syntax().text().contains_char('\n') {
85 res = make_multiline(res); 85 res = make_multiline(res);
@@ -89,8 +89,8 @@ impl ast::ItemList {
89 } 89 }
90 90
91 #[must_use] 91 #[must_use]
92 pub fn append_item(&self, item: ast::ImplItem) -> ast::ItemList { 92 pub fn append_item(&self, item: ast::AssocItem) -> ast::ItemList {
93 let (indent, position) = match self.impl_items().last() { 93 let (indent, position) = match self.assoc_items().last() {
94 Some(it) => ( 94 Some(it) => (
95 leading_indent(it.syntax()).unwrap_or_default().to_string(), 95 leading_indent(it.syntax()).unwrap_or_default().to_string(),
96 InsertPosition::After(it.syntax().clone().into()), 96 InsertPosition::After(it.syntax().clone().into()),
diff --git a/crates/ra_syntax/src/ast/generated/nodes.rs b/crates/ra_syntax/src/ast/generated/nodes.rs
index c2cc25958..b00c15608 100644
--- a/crates/ra_syntax/src/ast/generated/nodes.rs
+++ b/crates/ra_syntax/src/ast/generated/nodes.rs
@@ -196,7 +196,7 @@ pub struct ItemList {
196impl ast::ModuleItemOwner for ItemList {} 196impl ast::ModuleItemOwner for ItemList {}
197impl ItemList { 197impl ItemList {
198 pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['{']) } 198 pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['{']) }
199 pub fn impl_items(&self) -> AstChildren<ImplItem> { support::children(&self.syntax) } 199 pub fn assoc_items(&self) -> AstChildren<AssocItem> { support::children(&self.syntax) }
200 pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['}']) } 200 pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['}']) }
201} 201}
202 202
@@ -1429,13 +1429,13 @@ impl ast::AttrsOwner for ModuleItem {}
1429impl ast::VisibilityOwner for ModuleItem {} 1429impl ast::VisibilityOwner for ModuleItem {}
1430 1430
1431#[derive(Debug, Clone, PartialEq, Eq, Hash)] 1431#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1432pub enum ImplItem { 1432pub enum AssocItem {
1433 FnDef(FnDef), 1433 FnDef(FnDef),
1434 TypeAliasDef(TypeAliasDef), 1434 TypeAliasDef(TypeAliasDef),
1435 ConstDef(ConstDef), 1435 ConstDef(ConstDef),
1436} 1436}
1437impl ast::NameOwner for ImplItem {} 1437impl ast::NameOwner for AssocItem {}
1438impl ast::AttrsOwner for ImplItem {} 1438impl ast::AttrsOwner for AssocItem {}
1439 1439
1440#[derive(Debug, Clone, PartialEq, Eq, Hash)] 1440#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1441pub enum ExternItem { 1441pub enum ExternItem {
@@ -3167,16 +3167,16 @@ impl AstNode for ModuleItem {
3167 } 3167 }
3168 } 3168 }
3169} 3169}
3170impl From<FnDef> for ImplItem { 3170impl From<FnDef> for AssocItem {
3171 fn from(node: FnDef) -> ImplItem { ImplItem::FnDef(node) } 3171 fn from(node: FnDef) -> AssocItem { AssocItem::FnDef(node) }
3172} 3172}
3173impl From<TypeAliasDef> for ImplItem { 3173impl From<TypeAliasDef> for AssocItem {
3174 fn from(node: TypeAliasDef) -> ImplItem { ImplItem::TypeAliasDef(node) } 3174 fn from(node: TypeAliasDef) -> AssocItem { AssocItem::TypeAliasDef(node) }
3175} 3175}
3176impl From<ConstDef> for ImplItem { 3176impl From<ConstDef> for AssocItem {
3177 fn from(node: ConstDef) -> ImplItem { ImplItem::ConstDef(node) } 3177 fn from(node: ConstDef) -> AssocItem { AssocItem::ConstDef(node) }
3178} 3178}
3179impl AstNode for ImplItem { 3179impl AstNode for AssocItem {
3180 fn can_cast(kind: SyntaxKind) -> bool { 3180 fn can_cast(kind: SyntaxKind) -> bool {
3181 match kind { 3181 match kind {
3182 FN_DEF | TYPE_ALIAS_DEF | CONST_DEF => true, 3182 FN_DEF | TYPE_ALIAS_DEF | CONST_DEF => true,
@@ -3185,18 +3185,18 @@ impl AstNode for ImplItem {
3185 } 3185 }
3186 fn cast(syntax: SyntaxNode) -> Option<Self> { 3186 fn cast(syntax: SyntaxNode) -> Option<Self> {
3187 let res = match syntax.kind() { 3187 let res = match syntax.kind() {
3188 FN_DEF => ImplItem::FnDef(FnDef { syntax }), 3188 FN_DEF => AssocItem::FnDef(FnDef { syntax }),
3189 TYPE_ALIAS_DEF => ImplItem::TypeAliasDef(TypeAliasDef { syntax }), 3189 TYPE_ALIAS_DEF => AssocItem::TypeAliasDef(TypeAliasDef { syntax }),
3190 CONST_DEF => ImplItem::ConstDef(ConstDef { syntax }), 3190 CONST_DEF => AssocItem::ConstDef(ConstDef { syntax }),
3191 _ => return None, 3191 _ => return None,
3192 }; 3192 };
3193 Some(res) 3193 Some(res)
3194 } 3194 }
3195 fn syntax(&self) -> &SyntaxNode { 3195 fn syntax(&self) -> &SyntaxNode {
3196 match self { 3196 match self {
3197 ImplItem::FnDef(it) => &it.syntax, 3197 AssocItem::FnDef(it) => &it.syntax,
3198 ImplItem::TypeAliasDef(it) => &it.syntax, 3198 AssocItem::TypeAliasDef(it) => &it.syntax,
3199 ImplItem::ConstDef(it) => &it.syntax, 3199 AssocItem::ConstDef(it) => &it.syntax,
3200 } 3200 }
3201 } 3201 }
3202} 3202}
@@ -3641,7 +3641,7 @@ impl std::fmt::Display for ModuleItem {
3641 std::fmt::Display::fmt(self.syntax(), f) 3641 std::fmt::Display::fmt(self.syntax(), f)
3642 } 3642 }
3643} 3643}
3644impl std::fmt::Display for ImplItem { 3644impl std::fmt::Display for AssocItem {
3645 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { 3645 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
3646 std::fmt::Display::fmt(self.syntax(), f) 3646 std::fmt::Display::fmt(self.syntax(), f)
3647 } 3647 }
diff --git a/crates/ra_syntax/src/fuzz.rs b/crates/ra_syntax/src/fuzz.rs
index 10fbe3176..39f9b12ab 100644
--- a/crates/ra_syntax/src/fuzz.rs
+++ b/crates/ra_syntax/src/fuzz.rs
@@ -5,7 +5,7 @@ use std::{
5 str::{self, FromStr}, 5 str::{self, FromStr},
6}; 6};
7 7
8use ra_text_edit::AtomTextEdit; 8use ra_text_edit::Indel;
9 9
10use crate::{validation, AstNode, SourceFile, TextRange}; 10use crate::{validation, AstNode, SourceFile, TextRange};
11 11
@@ -22,7 +22,7 @@ pub fn check_parser(text: &str) {
22#[derive(Debug, Clone)] 22#[derive(Debug, Clone)]
23pub struct CheckReparse { 23pub struct CheckReparse {
24 text: String, 24 text: String,
25 edit: AtomTextEdit, 25 edit: Indel,
26 edited_text: String, 26 edited_text: String,
27} 27}
28 28
@@ -43,7 +43,7 @@ impl CheckReparse {
43 TextRange::at(delete_start.try_into().unwrap(), delete_len.try_into().unwrap()); 43 TextRange::at(delete_start.try_into().unwrap(), delete_len.try_into().unwrap());
44 let edited_text = 44 let edited_text =
45 format!("{}{}{}", &text[..delete_start], &insert, &text[delete_start + delete_len..]); 45 format!("{}{}{}", &text[..delete_start], &insert, &text[delete_start + delete_len..]);
46 let edit = AtomTextEdit { delete, insert }; 46 let edit = Indel { delete, insert };
47 Some(CheckReparse { text, edit, edited_text }) 47 Some(CheckReparse { text, edit, edited_text })
48 } 48 }
49 49
diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs
index d0234cada..61e686da5 100644
--- a/crates/ra_syntax/src/lib.rs
+++ b/crates/ra_syntax/src/lib.rs
@@ -39,7 +39,7 @@ pub mod fuzz;
39 39
40use std::{marker::PhantomData, sync::Arc}; 40use std::{marker::PhantomData, sync::Arc};
41 41
42use ra_text_edit::AtomTextEdit; 42use ra_text_edit::Indel;
43use stdx::format_to; 43use stdx::format_to;
44 44
45use crate::syntax_node::GreenNode; 45use crate::syntax_node::GreenNode;
@@ -126,13 +126,13 @@ impl Parse<SourceFile> {
126 buf 126 buf
127 } 127 }
128 128
129 pub fn reparse(&self, edit: &AtomTextEdit) -> Parse<SourceFile> { 129 pub fn reparse(&self, indel: &Indel) -> Parse<SourceFile> {
130 self.incremental_reparse(edit).unwrap_or_else(|| self.full_reparse(edit)) 130 self.incremental_reparse(indel).unwrap_or_else(|| self.full_reparse(indel))
131 } 131 }
132 132
133 fn incremental_reparse(&self, edit: &AtomTextEdit) -> Option<Parse<SourceFile>> { 133 fn incremental_reparse(&self, indel: &Indel) -> Option<Parse<SourceFile>> {
134 // FIXME: validation errors are not handled here 134 // FIXME: validation errors are not handled here
135 parsing::incremental_reparse(self.tree().syntax(), edit, self.errors.to_vec()).map( 135 parsing::incremental_reparse(self.tree().syntax(), indel, self.errors.to_vec()).map(
136 |(green_node, errors, _reparsed_range)| Parse { 136 |(green_node, errors, _reparsed_range)| Parse {
137 green: green_node, 137 green: green_node,
138 errors: Arc::new(errors), 138 errors: Arc::new(errors),
@@ -141,8 +141,9 @@ impl Parse<SourceFile> {
141 ) 141 )
142 } 142 }
143 143
144 fn full_reparse(&self, edit: &AtomTextEdit) -> Parse<SourceFile> { 144 fn full_reparse(&self, indel: &Indel) -> Parse<SourceFile> {
145 let text = edit.apply(self.tree().syntax().text().to_string()); 145 let mut text = self.tree().syntax().text().to_string();
146 indel.apply(&mut text);
146 SourceFile::parse(&text) 147 SourceFile::parse(&text)
147 } 148 }
148} 149}
diff --git a/crates/ra_syntax/src/parsing/reparsing.rs b/crates/ra_syntax/src/parsing/reparsing.rs
index ffff0a7b2..edbc190f8 100644
--- a/crates/ra_syntax/src/parsing/reparsing.rs
+++ b/crates/ra_syntax/src/parsing/reparsing.rs
@@ -7,7 +7,7 @@
7//! and try to parse only this block. 7//! and try to parse only this block.
8 8
9use ra_parser::Reparser; 9use ra_parser::Reparser;
10use ra_text_edit::AtomTextEdit; 10use ra_text_edit::Indel;
11 11
12use crate::{ 12use crate::{
13 algo, 13 algo,
@@ -24,7 +24,7 @@ use crate::{
24 24
25pub(crate) fn incremental_reparse( 25pub(crate) fn incremental_reparse(
26 node: &SyntaxNode, 26 node: &SyntaxNode,
27 edit: &AtomTextEdit, 27 edit: &Indel,
28 errors: Vec<SyntaxError>, 28 errors: Vec<SyntaxError>,
29) -> Option<(GreenNode, Vec<SyntaxError>, TextRange)> { 29) -> Option<(GreenNode, Vec<SyntaxError>, TextRange)> {
30 if let Some((green, new_errors, old_range)) = reparse_token(node, &edit) { 30 if let Some((green, new_errors, old_range)) = reparse_token(node, &edit) {
@@ -39,7 +39,7 @@ pub(crate) fn incremental_reparse(
39 39
40fn reparse_token<'node>( 40fn reparse_token<'node>(
41 root: &'node SyntaxNode, 41 root: &'node SyntaxNode,
42 edit: &AtomTextEdit, 42 edit: &Indel,
43) -> Option<(GreenNode, Vec<SyntaxError>, TextRange)> { 43) -> Option<(GreenNode, Vec<SyntaxError>, TextRange)> {
44 let prev_token = algo::find_covering_element(root, edit.delete).as_token()?.clone(); 44 let prev_token = algo::find_covering_element(root, edit.delete).as_token()?.clone();
45 let prev_token_kind = prev_token.kind(); 45 let prev_token_kind = prev_token.kind();
@@ -88,7 +88,7 @@ fn reparse_token<'node>(
88 88
89fn reparse_block<'node>( 89fn reparse_block<'node>(
90 root: &'node SyntaxNode, 90 root: &'node SyntaxNode,
91 edit: &AtomTextEdit, 91 edit: &Indel,
92) -> Option<(GreenNode, Vec<SyntaxError>, TextRange)> { 92) -> Option<(GreenNode, Vec<SyntaxError>, TextRange)> {
93 let (node, reparser) = find_reparsable_node(root, edit.delete)?; 93 let (node, reparser) = find_reparsable_node(root, edit.delete)?;
94 let text = get_text_after_edit(node.clone().into(), edit); 94 let text = get_text_after_edit(node.clone().into(), edit);
@@ -108,15 +108,15 @@ fn reparse_block<'node>(
108 Some((node.replace_with(green), new_parser_errors, node.text_range())) 108 Some((node.replace_with(green), new_parser_errors, node.text_range()))
109} 109}
110 110
111fn get_text_after_edit(element: SyntaxElement, edit: &AtomTextEdit) -> String { 111fn get_text_after_edit(element: SyntaxElement, edit: &Indel) -> String {
112 let edit = 112 let edit = Indel::replace(edit.delete - element.text_range().start(), edit.insert.clone());
113 AtomTextEdit::replace(edit.delete - element.text_range().start(), edit.insert.clone());
114 113
115 let text = match element { 114 let mut text = match element {
116 NodeOrToken::Token(token) => token.text().to_string(), 115 NodeOrToken::Token(token) => token.text().to_string(),
117 NodeOrToken::Node(node) => node.text().to_string(), 116 NodeOrToken::Node(node) => node.text().to_string(),
118 }; 117 };
119 edit.apply(text) 118 edit.apply(&mut text);
119 text
120} 120}
121 121
122fn is_contextual_kw(text: &str) -> bool { 122fn is_contextual_kw(text: &str) -> bool {
@@ -167,7 +167,7 @@ fn merge_errors(
167 old_errors: Vec<SyntaxError>, 167 old_errors: Vec<SyntaxError>,
168 new_errors: Vec<SyntaxError>, 168 new_errors: Vec<SyntaxError>,
169 range_before_reparse: TextRange, 169 range_before_reparse: TextRange,
170 edit: &AtomTextEdit, 170 edit: &Indel,
171) -> Vec<SyntaxError> { 171) -> Vec<SyntaxError> {
172 let mut res = Vec::new(); 172 let mut res = Vec::new();
173 173
@@ -198,8 +198,12 @@ mod tests {
198 198
199 fn do_check(before: &str, replace_with: &str, reparsed_len: u32) { 199 fn do_check(before: &str, replace_with: &str, reparsed_len: u32) {
200 let (range, before) = extract_range(before); 200 let (range, before) = extract_range(before);
201 let edit = AtomTextEdit::replace(range, replace_with.to_owned()); 201 let edit = Indel::replace(range, replace_with.to_owned());
202 let after = edit.apply(before.clone()); 202 let after = {
203 let mut after = before.clone();
204 edit.apply(&mut after);
205 after
206 };
203 207
204 let fully_reparsed = SourceFile::parse(&after); 208 let fully_reparsed = SourceFile::parse(&after);
205 let incrementally_reparsed: Parse<SourceFile> = { 209 let incrementally_reparsed: Parse<SourceFile> = {