diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/libsyntax2/src/grammar/expressions/mod.rs | 2 | ||||
-rw-r--r-- | crates/libsyntax2/src/grammar/items/mod.rs | 4 | ||||
-rw-r--r-- | crates/libsyntax2/src/grammar/items/structs.rs | 2 | ||||
-rw-r--r-- | crates/libsyntax2/src/grammar/mod.rs | 4 | ||||
-rw-r--r-- | crates/libsyntax2/src/lib.rs | 38 |
5 files changed, 46 insertions, 4 deletions
diff --git a/crates/libsyntax2/src/grammar/expressions/mod.rs b/crates/libsyntax2/src/grammar/expressions/mod.rs index 59a0564d9..bd6c84886 100644 --- a/crates/libsyntax2/src/grammar/expressions/mod.rs +++ b/crates/libsyntax2/src/grammar/expressions/mod.rs | |||
@@ -25,7 +25,7 @@ fn expr_no_struct(p: &mut Parser) { | |||
25 | // fn b() { let _ = 1; } | 25 | // fn b() { let _ = 1; } |
26 | // fn c() { 1; 2; } | 26 | // fn c() { 1; 2; } |
27 | // fn d() { 1; 2 } | 27 | // fn d() { 1; 2 } |
28 | pub(super) fn block(p: &mut Parser) { | 28 | pub(crate) fn block(p: &mut Parser) { |
29 | assert!(p.at(L_CURLY)); | 29 | assert!(p.at(L_CURLY)); |
30 | let m = p.start(); | 30 | let m = p.start(); |
31 | p.bump(); | 31 | p.bump(); |
diff --git a/crates/libsyntax2/src/grammar/items/mod.rs b/crates/libsyntax2/src/grammar/items/mod.rs index 206c85280..44ab92c63 100644 --- a/crates/libsyntax2/src/grammar/items/mod.rs +++ b/crates/libsyntax2/src/grammar/items/mod.rs | |||
@@ -1,10 +1,12 @@ | |||
1 | use super::*; | ||
2 | 1 | ||
3 | mod consts; | 2 | mod consts; |
4 | mod structs; | 3 | mod structs; |
5 | mod traits; | 4 | mod traits; |
6 | mod use_item; | 5 | mod use_item; |
7 | 6 | ||
7 | use super::*; | ||
8 | pub(crate) use self::structs::named_field_def_list; | ||
9 | |||
8 | // test mod_contents | 10 | // test mod_contents |
9 | // fn foo() {} | 11 | // fn foo() {} |
10 | // macro_rules! foo {} | 12 | // macro_rules! foo {} |
diff --git a/crates/libsyntax2/src/grammar/items/structs.rs b/crates/libsyntax2/src/grammar/items/structs.rs index ca027d718..93d3381f8 100644 --- a/crates/libsyntax2/src/grammar/items/structs.rs +++ b/crates/libsyntax2/src/grammar/items/structs.rs | |||
@@ -82,7 +82,7 @@ fn enum_variant_list(p: &mut Parser) { | |||
82 | m.complete(p, ENUM_VARIANT_LIST); | 82 | m.complete(p, ENUM_VARIANT_LIST); |
83 | } | 83 | } |
84 | 84 | ||
85 | fn named_field_def_list(p: &mut Parser) { | 85 | pub(crate) fn named_field_def_list(p: &mut Parser) { |
86 | assert!(p.at(L_CURLY)); | 86 | assert!(p.at(L_CURLY)); |
87 | let m = p.start(); | 87 | let m = p.start(); |
88 | p.bump(); | 88 | p.bump(); |
diff --git a/crates/libsyntax2/src/grammar/mod.rs b/crates/libsyntax2/src/grammar/mod.rs index e3ca2714c..46ba8a89a 100644 --- a/crates/libsyntax2/src/grammar/mod.rs +++ b/crates/libsyntax2/src/grammar/mod.rs | |||
@@ -35,6 +35,10 @@ use { | |||
35 | parser_api::{Marker, CompletedMarker, Parser, TokenSet}, | 35 | parser_api::{Marker, CompletedMarker, Parser, TokenSet}, |
36 | SyntaxKind::{self, *}, | 36 | SyntaxKind::{self, *}, |
37 | }; | 37 | }; |
38 | pub(crate) use self::{ | ||
39 | expressions::block, | ||
40 | items::named_field_def_list, | ||
41 | }; | ||
38 | 42 | ||
39 | pub(crate) fn file(p: &mut Parser) { | 43 | pub(crate) fn file(p: &mut Parser) { |
40 | let file = p.start(); | 44 | let file = p.start(); |
diff --git a/crates/libsyntax2/src/lib.rs b/crates/libsyntax2/src/lib.rs index 86fdbd23f..3a36a57b1 100644 --- a/crates/libsyntax2/src/lib.rs +++ b/crates/libsyntax2/src/lib.rs | |||
@@ -50,7 +50,11 @@ pub use { | |||
50 | yellow::{SyntaxNode, SyntaxNodeRef, OwnedRoot, RefRoot, TreeRoot, SyntaxError}, | 50 | yellow::{SyntaxNode, SyntaxNodeRef, OwnedRoot, RefRoot, TreeRoot, SyntaxError}, |
51 | }; | 51 | }; |
52 | 52 | ||
53 | use yellow::{GreenNode, SyntaxRoot}; | 53 | use { |
54 | SyntaxKind::*, | ||
55 | yellow::{GreenNode, SyntaxRoot}, | ||
56 | parser_api::Parser, | ||
57 | }; | ||
54 | 58 | ||
55 | #[derive(Clone, Debug)] | 59 | #[derive(Clone, Debug)] |
56 | pub struct File { | 60 | pub struct File { |
@@ -69,6 +73,22 @@ impl File { | |||
69 | let (root, errors) = parser_impl::parse::<yellow::GreenBuilder>(text, &tokens); | 73 | let (root, errors) = parser_impl::parse::<yellow::GreenBuilder>(text, &tokens); |
70 | File::new(root, errors) | 74 | File::new(root, errors) |
71 | } | 75 | } |
76 | pub fn reparse(&self, edit: &AtomEdit) -> File { | ||
77 | self.incremental_reparse(edit).unwrap_or_else(|| { | ||
78 | self.full_reparse(edit) | ||
79 | }) | ||
80 | } | ||
81 | fn incremental_reparse(&self, edit: &AtomEdit) -> Option<File> { | ||
82 | let (node, reparser) = find_reparsable_node(self.syntax(), edit.delete)?; | ||
83 | None | ||
84 | } | ||
85 | fn full_reparse(&self, edit: &AtomEdit) -> File { | ||
86 | let start = u32::from(edit.delete.start()) as usize; | ||
87 | let end = u32::from(edit.delete.end()) as usize; | ||
88 | let mut text = self.syntax().text(); | ||
89 | text.replace_range(start..end, &edit.insert); | ||
90 | File::parse(&text) | ||
91 | } | ||
72 | pub fn ast(&self) -> ast::Root { | 92 | pub fn ast(&self) -> ast::Root { |
73 | ast::Root::cast(self.syntax()).unwrap() | 93 | ast::Root::cast(self.syntax()).unwrap() |
74 | } | 94 | } |
@@ -132,3 +152,19 @@ impl AtomEdit { | |||
132 | AtomEdit::replace(TextRange::offset_len(offset, 0.into()), text) | 152 | AtomEdit::replace(TextRange::offset_len(offset, 0.into()), text) |
133 | } | 153 | } |
134 | } | 154 | } |
155 | |||
156 | fn find_reparsable_node(node: SyntaxNodeRef, range: TextRange) -> Option<(SyntaxNodeRef, fn(&mut Parser))> { | ||
157 | let node = algo::find_covering_node(node, range); | ||
158 | return algo::ancestors(node) | ||
159 | .filter_map(|node| reparser(node).map(|r| (node, r))) | ||
160 | .next(); | ||
161 | |||
162 | fn reparser(node: SyntaxNodeRef) -> Option<fn(&mut Parser)> { | ||
163 | let res = match node.kind() { | ||
164 | BLOCK => grammar::block, | ||
165 | NAMED_FIELD_DEF_LIST => grammar::named_field_def_list, | ||
166 | _ => return None, | ||
167 | }; | ||
168 | Some(res) | ||
169 | } | ||
170 | } | ||