Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EXPERIMENT: port to rowan#35 #2182

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ incremental = true
debug = 1 # only line info

[patch.'crates-io']
rowan = { git = "https://github.com/cad97/rowan", branch = "custom-green" }
41 changes: 18 additions & 23 deletions crates/ra_syntax/src/algo.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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::<Box<[_]>>()
}
InsertPosition::Last => old_children.iter().cloned().chain(to_insert).collect::<Box<[_]>>(),
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::<Box<[_]>>()
before.iter().cloned().chain(to_insert).chain(after.iter().cloned()).collect()
}
};

Expand All @@ -175,7 +168,7 @@ pub fn replace_children(
.cloned()
.chain(to_insert.map(to_green_element))
.chain(old_children[end + 1..].iter().cloned())
.collect::<Box<[_]>>();
.collect::<Vec<_>>();
with_children(parent, new_children)
}

Expand All @@ -188,31 +181,31 @@ pub fn replace_descendants(
map: &FxHashMap<SyntaxElement, SyntaxElement>,
) -> SyntaxNode {
// FIXME: this could be made much faster.
let new_children = parent.children_with_tokens().map(|it| go(map, it)).collect::<Box<[_]>>();
let new_children = parent.children_with_tokens().map(|it| go(map, it)).collect::<Vec<_>>();
return with_children(parent, new_children);

fn go(
map: &FxHashMap<SyntaxElement, SyntaxElement>,
element: SyntaxElement,
) -> NodeOrToken<rowan::GreenNode, rowan::GreenToken> {
) -> rowan::GreenElement {
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) => it.green().to_owned().into(),
NodeOrToken::Token(it) => it.green().clone().into(),
};
}
match element {
NodeOrToken::Token(it) => NodeOrToken::Token(it.green().clone()),
NodeOrToken::Token(it) => it.green().clone().into(),
NodeOrToken::Node(it) => {
NodeOrToken::Node(replace_descendants(&it, map).green().clone())
replace_descendants(&it, map).green().to_owned().into()
}
}
}
}

fn with_children(
parent: &SyntaxNode,
new_children: Box<[NodeOrToken<rowan::GreenNode, rowan::GreenToken>]>,
new_children: Vec<rowan::GreenElement>,
) -> SyntaxNode {
let len = new_children.iter().map(|it| it.text_len()).sum::<TextUnit>();
let new_node =
Expand All @@ -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<rowan::GreenNode, rowan::GreenToken> {
fn to_green_element(
element: SyntaxElement,
) -> rowan::GreenElement {
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(),
}
}
6 changes: 3 additions & 3 deletions crates/ra_syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -58,7 +58,7 @@ pub use rowan::{SmolStr, SyntaxText, TextRange, TextUnit, TokenAtOffset, WalkEve
/// files.
#[derive(Debug, PartialEq, Eq)]
pub struct Parse<T> {
green: GreenNode,
green: ArcGreenNode,
errors: Arc<Vec<SyntaxError>>,
_ty: PhantomData<fn() -> T>,
}
Expand All @@ -70,7 +70,7 @@ impl<T> Clone for Parse<T> {
}

impl<T> Parse<T> {
fn new(green: GreenNode, errors: Vec<SyntaxError>) -> Parse<T> {
fn new(green: ArcGreenNode, errors: Vec<SyntaxError>) -> Parse<T> {
Parse { green, errors: Arc::new(errors), _ty: PhantomData }
}

Expand Down
4 changes: 2 additions & 2 deletions crates/ra_syntax/src/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ mod text_token_source;
mod text_tree_sink;
mod reparsing;

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) -> (GreenNode, Vec<SyntaxError>) {
pub(crate) fn parse_text(text: &str) -> (ArcGreenNode, Vec<SyntaxError>) {
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);
Expand Down
14 changes: 8 additions & 6 deletions crates/ra_syntax/src/parsing/reparsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,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,
Expand All @@ -26,7 +26,7 @@ pub(crate) fn incremental_reparse(
node: &SyntaxNode,
edit: &AtomTextEdit,
errors: Vec<SyntaxError>,
) -> Option<(GreenNode, Vec<SyntaxError>, TextRange)> {
) -> Option<(ArcGreenNode, Vec<SyntaxError>, TextRange)> {
if let Some((green, old_range)) = reparse_token(node, &edit) {
return Some((green, merge_errors(errors, Vec::new(), old_range, edit), old_range));
}
Expand All @@ -40,7 +40,7 @@ pub(crate) fn incremental_reparse(
fn reparse_token<'node>(
root: &'node SyntaxNode,
edit: &AtomTextEdit,
) -> Option<(GreenNode, 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 => {
Expand Down Expand Up @@ -70,8 +70,10 @@ fn reparse_token<'node>(
}
}

let new_token =
GreenToken::new(rowan::cursor::SyntaxKind(token.kind().into()), text.into());
let new_token = GreenToken::new(
rowan::cursor::SyntaxKind(token.kind().into()),
text.into(),
);
Some((token.replace_with(new_token), token.text_range()))
}
_ => None,
Expand All @@ -81,7 +83,7 @@ fn reparse_token<'node>(
fn reparse_block<'node>(
root: &'node SyntaxNode,
edit: &AtomTextEdit,
) -> Option<(GreenNode, Vec<SyntaxError>, TextRange)> {
) -> Option<(ArcGreenNode, Vec<SyntaxError>, TextRange)> {
let (node, reparser) = find_reparsable_node(root, edit.delete)?;
let text = get_text_after_edit(node.clone().into(), &edit);
let tokens = tokenize(&text);
Expand Down
4 changes: 2 additions & 2 deletions crates/ra_syntax/src/parsing/text_tree_sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ra_parser::{ParseError, TreeSink};

use crate::{
parsing::Token,
syntax_node::GreenNode,
syntax_node::ArcGreenNode,
SmolStr, SyntaxError,
SyntaxKind::{self, *},
SyntaxTreeBuilder, TextRange, TextUnit,
Expand Down Expand Up @@ -103,7 +103,7 @@ impl<'a> TextTreeSink<'a> {
}
}

pub(super) fn finish(mut self) -> (GreenNode, Vec<SyntaxError>) {
pub(super) fn finish(mut self) -> (ArcGreenNode, Vec<SyntaxError>) {
match mem::replace(&mut self.state, State::Normal) {
State::PendingFinish => {
self.eat_trivias();
Expand Down
8 changes: 4 additions & 4 deletions crates/ra_syntax/src/syntax_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,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 {}
Expand Down Expand Up @@ -50,9 +50,9 @@ impl Default for SyntaxTreeBuilder {
}

impl SyntaxTreeBuilder {
pub(crate) fn finish_raw(self) -> (GreenNode, Vec<SyntaxError>) {
pub(crate) fn finish_raw(self) -> (ArcGreenNode, Vec<SyntaxError>) {
let green = self.inner.finish();
(green, self.errors)
(green.unwrap_node(), self.errors)
}

pub fn finish(self) -> Parse<SyntaxNode> {
Expand All @@ -61,7 +61,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) {
Expand Down