From a20823b3f3ae453eb7ae741685dbc149c65b1c73 Mon Sep 17 00:00:00 2001 From: cad97 Date: Sat, 2 Nov 2019 15:42:39 -0400 Subject: [PATCH 1/2] EXPERIMENT: port to rowan#34 --- Cargo.lock | 19 ++++++--- Cargo.toml | 1 + crates/ra_syntax/src/algo.rs | 41 ++++++++----------- crates/ra_syntax/src/lib.rs | 4 +- crates/ra_syntax/src/parsing.rs | 4 +- crates/ra_syntax/src/parsing/reparsing.rs | 14 ++++--- .../ra_syntax/src/parsing/text_tree_sink.rs | 4 +- crates/ra_syntax/src/syntax_node.rs | 6 ++- 8 files changed, 52 insertions(+), 41 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 10f51403a380..34810505ed03 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -896,7 +896,7 @@ dependencies = [ [[package]] name = "psm" -version = "0.1.5" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "cc 1.0.46 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1165,7 +1165,7 @@ dependencies = [ "once_cell 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "ra_parser 0.1.0", "ra_text_edit 0.1.0", - "rowan 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rowan 0.6.2 (git+https://github.com/cad97/rowan?branch=boxed-tokens)", "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "rustc_lexer 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "smol_str 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1431,10 +1431,11 @@ dependencies = [ [[package]] name = "rowan" version = "0.6.2" -source = "registry+https://github.com/rust-lang/crates.io-index" +source = "git+https://github.com/cad97/rowan?branch=boxed-tokens#c03a3bb4e41583f485234da055643a8b8fd84b02" dependencies = [ "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "smol_str 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", + "static_assertions 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "text_unit 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1601,10 +1602,15 @@ dependencies = [ "cc 1.0.46 (registry+https://github.com/rust-lang/crates.io-index)", "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", - "psm 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "psm 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "static_assertions" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "superslice" version = "1.0.0" @@ -1939,7 +1945,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum proc-macro-hack 0.5.11 (registry+https://github.com/rust-lang/crates.io-index)" = "ecd45702f76d6d3c75a80564378ae228a85f0b59d2f3ed43c91b4a69eb2ebfc5" "checksum proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "9c9e470a8dc4aeae2dee2f335e8f533e2d4b347e1434e5671afc49b054592f27" "checksum proptest 0.9.4 (registry+https://github.com/rust-lang/crates.io-index)" = "cf147e022eacf0c8a054ab864914a7602618adba841d800a9a9868a5237a529f" -"checksum psm 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "54639efee26275dfadc49644039c31c73673edde0ef2815acf25a8db526ee193" +"checksum psm 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "b14fc68b454f875abc8354c2555e1d56596f74833ddc0f77f87f4871ed6a30e0" "checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0" "checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe" "checksum ra_vfs 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc8f508bc7a9871b81b6ee75e15d0bbc9f81a96698364a090a8d10b0f8b970b7" @@ -1967,7 +1973,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum relative-path 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bedde000f40f2921ce439ea165c9c53fd629bfa115140c72e22aceacb4a21954" "checksum remove_dir_all 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4a83fa3702a688b9359eccba92d153ac33fd2e8462f9e0e3fdf155239ea7792e" "checksum ron 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2ece421e0c4129b90e4a35b6f625e472e96c552136f5093a2f4fa2bbb75a62d5" -"checksum rowan 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dc2b79612dedc9004083a61448eb669d336d56690aab29fbd7249e8c8ab41d8c" +"checksum rowan 0.6.2 (git+https://github.com/cad97/rowan?branch=boxed-tokens)" = "" "checksum rustc-demangle 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783" "checksum rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7540fc8b0c49f096ee9c961cda096467dce8084bec6bdca2fc83895fd9b28cb8" "checksum rustc_lexer 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c86aae0c77166108c01305ee1a36a1e77289d7dc6ca0a3cd91ff4992de2d16a5" @@ -1988,6 +1994,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum smallvec 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)" = "533e29e15d0748f28afbaf4ff7cab44d73e483a8e50b38c40bd13b7f3d48f542" "checksum smol_str 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)" = "34836c9a295c62c2ce3514471117c5cb269891e8421b2aafdd910050576c4d8b" "checksum stacker 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "d96fc4f13a0ac088e9a3cd9af1cc8c5cc1ab5deb2145cef661267dfc9c542f8a" +"checksum static_assertions 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0fa13613355688665b68639b1c378a62dbedea78aff0fc59a4fa656cbbdec657" "checksum superslice 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ab16ced94dbd8a46c82fd81e3ed9a8727dac2977ea869d217bcc4ea1f122e81f" "checksum syn 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "0e7bedb3320d0f3035594b0b723c8a28d7d336a3eda3881db79e61d676fb644c" "checksum tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" diff --git a/Cargo.toml b/Cargo.toml index 97c02b40f4fa..456f38c83455 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,4 @@ incremental = true debug = 1 # only line info [patch.'crates-io'] +rowan = { git = "https://github.com/cad97/rowan", branch = "boxed-tokens" } diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs index 7cfea70f9791..6bb8b828fb35 100644 --- a/crates/ra_syntax/src/algo.rs +++ b/crates/ra_syntax/src/algo.rs @@ -1,6 +1,6 @@ //! FIXME: write short doc here -use std::ops::RangeInclusive; +use std::{ops::RangeInclusive, sync::Arc}; use itertools::Itertools; use ra_text_edit::TextEditBuilder; @@ -136,21 +136,14 @@ pub fn insert_children( let old_children = parent.green().children(); - let new_children = match &position { - InsertPosition::First => { - to_insert.chain(old_children.iter().cloned()).collect::>() - } - InsertPosition::Last => old_children.iter().cloned().chain(to_insert).collect::>(), + let new_children: Vec<_> = match &position { + InsertPosition::First => to_insert.chain(old_children.iter().cloned()).collect(), + InsertPosition::Last => old_children.iter().cloned().chain(to_insert).collect(), InsertPosition::Before(anchor) | InsertPosition::After(anchor) => { let take_anchor = if let InsertPosition::After(_) = position { 1 } else { 0 }; let split_at = position_of_child(parent, anchor.clone()) + take_anchor; let (before, after) = old_children.split_at(split_at); - before - .iter() - .cloned() - .chain(to_insert) - .chain(after.iter().cloned()) - .collect::>() + before.iter().cloned().chain(to_insert).chain(after.iter().cloned()).collect() } }; @@ -175,7 +168,7 @@ pub fn replace_children( .cloned() .chain(to_insert.map(to_green_element)) .chain(old_children[end + 1..].iter().cloned()) - .collect::>(); + .collect::>(); with_children(parent, new_children) } @@ -188,23 +181,23 @@ pub fn replace_descendants( map: &FxHashMap, ) -> SyntaxNode { // FIXME: this could be made much faster. - let new_children = parent.children_with_tokens().map(|it| go(map, it)).collect::>(); + let new_children = parent.children_with_tokens().map(|it| go(map, it)).collect::>(); return with_children(parent, new_children); fn go( map: &FxHashMap, element: SyntaxElement, - ) -> NodeOrToken { + ) -> NodeOrToken, Arc> { if let Some(replacement) = map.get(&element) { return match replacement { - NodeOrToken::Node(it) => NodeOrToken::Node(it.green().clone()), - NodeOrToken::Token(it) => NodeOrToken::Token(it.green().clone()), + NodeOrToken::Node(it) => NodeOrToken::Node(it.green().to_owned()), + NodeOrToken::Token(it) => NodeOrToken::Token(Arc::new(it.green().clone())), }; } match element { - NodeOrToken::Token(it) => NodeOrToken::Token(it.green().clone()), + NodeOrToken::Token(it) => NodeOrToken::Token(Arc::new(it.green().clone())), NodeOrToken::Node(it) => { - NodeOrToken::Node(replace_descendants(&it, map).green().clone()) + NodeOrToken::Node(replace_descendants(&it, map).green().to_owned()) } } } @@ -212,7 +205,7 @@ pub fn replace_descendants( fn with_children( parent: &SyntaxNode, - new_children: Box<[NodeOrToken]>, + new_children: Vec, Arc>>, ) -> SyntaxNode { let len = new_children.iter().map(|it| it.text_len()).sum::(); let new_node = @@ -234,9 +227,11 @@ fn position_of_child(parent: &SyntaxNode, child: SyntaxElement) -> usize { .expect("element is not a child of current element") } -fn to_green_element(element: SyntaxElement) -> NodeOrToken { +fn to_green_element( + element: SyntaxElement, +) -> NodeOrToken, Arc> { match element { - NodeOrToken::Node(it) => it.green().clone().into(), - NodeOrToken::Token(it) => it.green().clone().into(), + NodeOrToken::Node(it) => it.green().to_owned().into(), + NodeOrToken::Token(it) => Arc::new(it.green().clone()).into(), } } diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs index 5dcb6a95a663..633642d18f93 100644 --- a/crates/ra_syntax/src/lib.rs +++ b/crates/ra_syntax/src/lib.rs @@ -58,7 +58,7 @@ pub use rowan::{SmolStr, SyntaxText, TextRange, TextUnit, TokenAtOffset, WalkEve /// files. #[derive(Debug, PartialEq, Eq)] pub struct Parse { - green: GreenNode, + green: Arc, errors: Arc>, _ty: PhantomData T>, } @@ -70,7 +70,7 @@ impl Clone for Parse { } impl Parse { - fn new(green: GreenNode, errors: Vec) -> Parse { + fn new(green: Arc, errors: Vec) -> Parse { Parse { green, errors: Arc::new(errors), _ty: PhantomData } } diff --git a/crates/ra_syntax/src/parsing.rs b/crates/ra_syntax/src/parsing.rs index 0387f0378061..605628eea49d 100644 --- a/crates/ra_syntax/src/parsing.rs +++ b/crates/ra_syntax/src/parsing.rs @@ -6,13 +6,15 @@ mod text_token_source; mod text_tree_sink; mod reparsing; +use std::sync::Arc; + use crate::{syntax_node::GreenNode, SyntaxError}; pub use self::lexer::{classify_literal, tokenize, Token}; pub(crate) use self::reparsing::incremental_reparse; -pub(crate) fn parse_text(text: &str) -> (GreenNode, Vec) { +pub(crate) fn parse_text(text: &str) -> (Arc, Vec) { let tokens = tokenize(&text); let mut token_source = text_token_source::TextTokenSource::new(text, &tokens); let mut tree_sink = text_tree_sink::TextTreeSink::new(text, &tokens); diff --git a/crates/ra_syntax/src/parsing/reparsing.rs b/crates/ra_syntax/src/parsing/reparsing.rs index 65b8aa10d0f1..ae6f71249088 100644 --- a/crates/ra_syntax/src/parsing/reparsing.rs +++ b/crates/ra_syntax/src/parsing/reparsing.rs @@ -6,6 +6,8 @@ //! - otherwise, we search for the nearest `{}` block which contains the edit //! and try to parse only this block. +use std::sync::Arc; + use ra_parser::Reparser; use ra_text_edit::AtomTextEdit; @@ -26,7 +28,7 @@ pub(crate) fn incremental_reparse( node: &SyntaxNode, edit: &AtomTextEdit, errors: Vec, -) -> Option<(GreenNode, Vec, TextRange)> { +) -> Option<(Arc, Vec, TextRange)> { if let Some((green, old_range)) = reparse_token(node, &edit) { return Some((green, merge_errors(errors, Vec::new(), old_range, edit), old_range)); } @@ -40,7 +42,7 @@ pub(crate) fn incremental_reparse( fn reparse_token<'node>( root: &'node SyntaxNode, edit: &AtomTextEdit, -) -> Option<(GreenNode, TextRange)> { +) -> Option<(Arc, TextRange)> { let token = algo::find_covering_element(root, edit.delete).as_token()?.clone(); match token.kind() { WHITESPACE | COMMENT | IDENT | STRING | RAW_STRING => { @@ -70,8 +72,10 @@ fn reparse_token<'node>( } } - let new_token = - GreenToken::new(rowan::cursor::SyntaxKind(token.kind().into()), text.into()); + let new_token = Arc::new(GreenToken::new( + rowan::cursor::SyntaxKind(token.kind().into()), + text.into(), + )); Some((token.replace_with(new_token), token.text_range())) } _ => None, @@ -81,7 +85,7 @@ fn reparse_token<'node>( fn reparse_block<'node>( root: &'node SyntaxNode, edit: &AtomTextEdit, -) -> Option<(GreenNode, Vec, TextRange)> { +) -> Option<(Arc, Vec, TextRange)> { let (node, reparser) = find_reparsable_node(root, edit.delete)?; let text = get_text_after_edit(node.clone().into(), &edit); let tokens = tokenize(&text); diff --git a/crates/ra_syntax/src/parsing/text_tree_sink.rs b/crates/ra_syntax/src/parsing/text_tree_sink.rs index 142164316360..49654c8b6461 100644 --- a/crates/ra_syntax/src/parsing/text_tree_sink.rs +++ b/crates/ra_syntax/src/parsing/text_tree_sink.rs @@ -1,6 +1,6 @@ //! FIXME: write short doc here -use std::mem; +use std::{mem, sync::Arc}; use ra_parser::{ParseError, TreeSink}; @@ -103,7 +103,7 @@ impl<'a> TextTreeSink<'a> { } } - pub(super) fn finish(mut self) -> (GreenNode, Vec) { + pub(super) fn finish(mut self) -> (Arc, Vec) { match mem::replace(&mut self.state, State::Normal) { State::PendingFinish => { self.eat_trivias(); diff --git a/crates/ra_syntax/src/syntax_node.rs b/crates/ra_syntax/src/syntax_node.rs index b2f5b8c6469a..d102e8cf64c5 100644 --- a/crates/ra_syntax/src/syntax_node.rs +++ b/crates/ra_syntax/src/syntax_node.rs @@ -6,6 +6,8 @@ //! The *real* implementation is in the (language-agnostic) `rowan` crate, this //! modules just wraps its API. +use std::sync::Arc; + use ra_parser::ParseError; use rowan::{GreenNodeBuilder, Language}; @@ -50,7 +52,7 @@ impl Default for SyntaxTreeBuilder { } impl SyntaxTreeBuilder { - pub(crate) fn finish_raw(self) -> (GreenNode, Vec) { + pub(crate) fn finish_raw(self) -> (Arc, Vec) { let green = self.inner.finish(); (green, self.errors) } @@ -61,7 +63,7 @@ impl SyntaxTreeBuilder { if cfg!(debug_assertions) { crate::validation::validate_block_structure(&node); } - Parse::new(node.green().clone(), errors) + Parse::new(node.green().to_owned(), errors) } pub fn token(&mut self, kind: SyntaxKind, text: SmolStr) { From 1e188687720ce24aba1b836b963e0cf8c9c29874 Mon Sep 17 00:00:00 2001 From: cad97 Date: Mon, 4 Nov 2019 22:40:41 -0500 Subject: [PATCH 2/2] EXPERIMENT: port to rowan#35 --- Cargo.lock | 6 +++--- Cargo.toml | 2 +- crates/ra_syntax/src/algo.rs | 14 +++++++------- crates/ra_syntax/src/lib.rs | 6 +++--- crates/ra_syntax/src/parsing.rs | 6 ++---- crates/ra_syntax/src/parsing/reparsing.rs | 14 ++++++-------- crates/ra_syntax/src/parsing/text_tree_sink.rs | 6 +++--- crates/ra_syntax/src/syntax_node.rs | 8 +++----- 8 files changed, 28 insertions(+), 34 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 34810505ed03..b768145e31ba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1165,7 +1165,7 @@ dependencies = [ "once_cell 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "ra_parser 0.1.0", "ra_text_edit 0.1.0", - "rowan 0.6.2 (git+https://github.com/cad97/rowan?branch=boxed-tokens)", + "rowan 0.6.2 (git+https://github.com/cad97/rowan?branch=custom-green)", "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "rustc_lexer 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "smol_str 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1431,7 +1431,7 @@ dependencies = [ [[package]] name = "rowan" version = "0.6.2" -source = "git+https://github.com/cad97/rowan?branch=boxed-tokens#c03a3bb4e41583f485234da055643a8b8fd84b02" +source = "git+https://github.com/cad97/rowan?branch=custom-green#a392206f3dafc757dec3661b5a980120dce188bb" dependencies = [ "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "smol_str 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1973,7 +1973,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum relative-path 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bedde000f40f2921ce439ea165c9c53fd629bfa115140c72e22aceacb4a21954" "checksum remove_dir_all 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4a83fa3702a688b9359eccba92d153ac33fd2e8462f9e0e3fdf155239ea7792e" "checksum ron 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2ece421e0c4129b90e4a35b6f625e472e96c552136f5093a2f4fa2bbb75a62d5" -"checksum rowan 0.6.2 (git+https://github.com/cad97/rowan?branch=boxed-tokens)" = "" +"checksum rowan 0.6.2 (git+https://github.com/cad97/rowan?branch=custom-green)" = "" "checksum rustc-demangle 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783" "checksum rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7540fc8b0c49f096ee9c961cda096467dce8084bec6bdca2fc83895fd9b28cb8" "checksum rustc_lexer 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c86aae0c77166108c01305ee1a36a1e77289d7dc6ca0a3cd91ff4992de2d16a5" diff --git a/Cargo.toml b/Cargo.toml index 456f38c83455..4e9599ebf2a5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,4 +9,4 @@ incremental = true debug = 1 # only line info [patch.'crates-io'] -rowan = { git = "https://github.com/cad97/rowan", branch = "boxed-tokens" } +rowan = { git = "https://github.com/cad97/rowan", branch = "custom-green" } diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs index 6bb8b828fb35..2dbf34b5c51b 100644 --- a/crates/ra_syntax/src/algo.rs +++ b/crates/ra_syntax/src/algo.rs @@ -187,17 +187,17 @@ pub fn replace_descendants( fn go( map: &FxHashMap, element: SyntaxElement, - ) -> NodeOrToken, Arc> { + ) -> rowan::GreenElement { if let Some(replacement) = map.get(&element) { return match replacement { - NodeOrToken::Node(it) => NodeOrToken::Node(it.green().to_owned()), - NodeOrToken::Token(it) => NodeOrToken::Token(Arc::new(it.green().clone())), + NodeOrToken::Node(it) => it.green().to_owned().into(), + NodeOrToken::Token(it) => it.green().clone().into(), }; } match element { - NodeOrToken::Token(it) => NodeOrToken::Token(Arc::new(it.green().clone())), + NodeOrToken::Token(it) => it.green().clone().into(), NodeOrToken::Node(it) => { - NodeOrToken::Node(replace_descendants(&it, map).green().to_owned()) + replace_descendants(&it, map).green().to_owned().into() } } } @@ -205,7 +205,7 @@ pub fn replace_descendants( fn with_children( parent: &SyntaxNode, - new_children: Vec, Arc>>, + new_children: Vec, ) -> SyntaxNode { let len = new_children.iter().map(|it| it.text_len()).sum::(); let new_node = @@ -229,7 +229,7 @@ fn position_of_child(parent: &SyntaxNode, child: SyntaxElement) -> usize { fn to_green_element( element: SyntaxElement, -) -> NodeOrToken, Arc> { +) -> rowan::GreenElement { match element { NodeOrToken::Node(it) => it.green().to_owned().into(), NodeOrToken::Token(it) => Arc::new(it.green().clone()).into(), diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs index 633642d18f93..6d3882d42bf4 100644 --- a/crates/ra_syntax/src/lib.rs +++ b/crates/ra_syntax/src/lib.rs @@ -36,7 +36,7 @@ use std::{fmt::Write, marker::PhantomData, sync::Arc}; use ra_text_edit::AtomTextEdit; -use crate::syntax_node::GreenNode; +use crate::syntax_node::ArcGreenNode; pub use crate::{ algo::InsertPosition, @@ -58,7 +58,7 @@ pub use rowan::{SmolStr, SyntaxText, TextRange, TextUnit, TokenAtOffset, WalkEve /// files. #[derive(Debug, PartialEq, Eq)] pub struct Parse { - green: Arc, + green: ArcGreenNode, errors: Arc>, _ty: PhantomData T>, } @@ -70,7 +70,7 @@ impl Clone for Parse { } impl Parse { - fn new(green: Arc, errors: Vec) -> Parse { + fn new(green: ArcGreenNode, errors: Vec) -> Parse { Parse { green, errors: Arc::new(errors), _ty: PhantomData } } diff --git a/crates/ra_syntax/src/parsing.rs b/crates/ra_syntax/src/parsing.rs index 605628eea49d..ae921febea70 100644 --- a/crates/ra_syntax/src/parsing.rs +++ b/crates/ra_syntax/src/parsing.rs @@ -6,15 +6,13 @@ mod text_token_source; mod text_tree_sink; mod reparsing; -use std::sync::Arc; - -use crate::{syntax_node::GreenNode, SyntaxError}; +use crate::{syntax_node::ArcGreenNode, SyntaxError}; pub use self::lexer::{classify_literal, tokenize, Token}; pub(crate) use self::reparsing::incremental_reparse; -pub(crate) fn parse_text(text: &str) -> (Arc, Vec) { +pub(crate) fn parse_text(text: &str) -> (ArcGreenNode, Vec) { let tokens = tokenize(&text); let mut token_source = text_token_source::TextTokenSource::new(text, &tokens); let mut tree_sink = text_tree_sink::TextTreeSink::new(text, &tokens); diff --git a/crates/ra_syntax/src/parsing/reparsing.rs b/crates/ra_syntax/src/parsing/reparsing.rs index ae6f71249088..592134e4cfac 100644 --- a/crates/ra_syntax/src/parsing/reparsing.rs +++ b/crates/ra_syntax/src/parsing/reparsing.rs @@ -6,8 +6,6 @@ //! - otherwise, we search for the nearest `{}` block which contains the edit //! and try to parse only this block. -use std::sync::Arc; - use ra_parser::Reparser; use ra_text_edit::AtomTextEdit; @@ -18,7 +16,7 @@ use crate::{ text_token_source::TextTokenSource, text_tree_sink::TextTreeSink, }, - syntax_node::{GreenNode, GreenToken, NodeOrToken, SyntaxElement, SyntaxNode}, + syntax_node::{ArcGreenNode, GreenToken, NodeOrToken, SyntaxElement, SyntaxNode}, SyntaxError, SyntaxKind::*, TextRange, TextUnit, T, @@ -28,7 +26,7 @@ pub(crate) fn incremental_reparse( node: &SyntaxNode, edit: &AtomTextEdit, errors: Vec, -) -> Option<(Arc, Vec, TextRange)> { +) -> Option<(ArcGreenNode, Vec, TextRange)> { if let Some((green, old_range)) = reparse_token(node, &edit) { return Some((green, merge_errors(errors, Vec::new(), old_range, edit), old_range)); } @@ -42,7 +40,7 @@ pub(crate) fn incremental_reparse( fn reparse_token<'node>( root: &'node SyntaxNode, edit: &AtomTextEdit, -) -> Option<(Arc, TextRange)> { +) -> Option<(ArcGreenNode, TextRange)> { let token = algo::find_covering_element(root, edit.delete).as_token()?.clone(); match token.kind() { WHITESPACE | COMMENT | IDENT | STRING | RAW_STRING => { @@ -72,10 +70,10 @@ fn reparse_token<'node>( } } - let new_token = Arc::new(GreenToken::new( + let new_token = GreenToken::new( rowan::cursor::SyntaxKind(token.kind().into()), text.into(), - )); + ); Some((token.replace_with(new_token), token.text_range())) } _ => None, @@ -85,7 +83,7 @@ fn reparse_token<'node>( fn reparse_block<'node>( root: &'node SyntaxNode, edit: &AtomTextEdit, -) -> Option<(Arc, Vec, TextRange)> { +) -> Option<(ArcGreenNode, Vec, TextRange)> { let (node, reparser) = find_reparsable_node(root, edit.delete)?; let text = get_text_after_edit(node.clone().into(), &edit); let tokens = tokenize(&text); diff --git a/crates/ra_syntax/src/parsing/text_tree_sink.rs b/crates/ra_syntax/src/parsing/text_tree_sink.rs index 49654c8b6461..534407cfe79b 100644 --- a/crates/ra_syntax/src/parsing/text_tree_sink.rs +++ b/crates/ra_syntax/src/parsing/text_tree_sink.rs @@ -1,12 +1,12 @@ //! FIXME: write short doc here -use std::{mem, sync::Arc}; +use std::mem; use ra_parser::{ParseError, TreeSink}; use crate::{ parsing::Token, - syntax_node::GreenNode, + syntax_node::ArcGreenNode, SmolStr, SyntaxError, SyntaxKind::{self, *}, SyntaxTreeBuilder, TextRange, TextUnit, @@ -103,7 +103,7 @@ impl<'a> TextTreeSink<'a> { } } - pub(super) fn finish(mut self) -> (Arc, Vec) { + pub(super) fn finish(mut self) -> (ArcGreenNode, Vec) { match mem::replace(&mut self.state, State::Normal) { State::PendingFinish => { self.eat_trivias(); diff --git a/crates/ra_syntax/src/syntax_node.rs b/crates/ra_syntax/src/syntax_node.rs index d102e8cf64c5..098113390651 100644 --- a/crates/ra_syntax/src/syntax_node.rs +++ b/crates/ra_syntax/src/syntax_node.rs @@ -6,8 +6,6 @@ //! The *real* implementation is in the (language-agnostic) `rowan` crate, this //! modules just wraps its API. -use std::sync::Arc; - use ra_parser::ParseError; use rowan::{GreenNodeBuilder, Language}; @@ -16,7 +14,7 @@ use crate::{ Parse, SmolStr, SyntaxKind, TextUnit, }; -pub(crate) use rowan::{GreenNode, GreenToken}; +pub(crate) use rowan::{ArcGreenNode, GreenToken}; #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum RustLanguage {} @@ -52,9 +50,9 @@ impl Default for SyntaxTreeBuilder { } impl SyntaxTreeBuilder { - pub(crate) fn finish_raw(self) -> (Arc, Vec) { + pub(crate) fn finish_raw(self) -> (ArcGreenNode, Vec) { let green = self.inner.finish(); - (green, self.errors) + (green.unwrap_node(), self.errors) } pub fn finish(self) -> Parse {