Simplify a bit

This commit is contained in:
Florian Diebold 2022-02-07 18:17:28 +01:00
parent cff209f152
commit b9c5d23f69

View file

@ -484,12 +484,12 @@ impl Convertor {
fn new( fn new(
node: &SyntaxNode, node: &SyntaxNode,
global_offset: TextSize, global_offset: TextSize,
replace: FxHashMap<SyntaxNode, Vec<SyntheticToken>>, mut replace: FxHashMap<SyntaxNode, Vec<SyntheticToken>>,
append: FxHashMap<SyntaxNode, Vec<SyntheticToken>>, mut append: FxHashMap<SyntaxNode, Vec<SyntheticToken>>,
) -> Convertor { ) -> Convertor {
let range = node.text_range(); let range = node.text_range();
let mut preorder = node.preorder_with_tokens(); let mut preorder = node.preorder_with_tokens();
let (first, synthetic) = Self::next_token(&mut preorder, &replace, &append); let (first, synthetic) = Self::next_token(&mut preorder, &mut replace, &mut append);
Convertor { Convertor {
id_alloc: { TokenIdAlloc { map: TokenMap::default(), global_offset, next_id: 0 } }, id_alloc: { TokenIdAlloc { map: TokenMap::default(), global_offset, next_id: 0 } },
current: first, current: first,
@ -504,19 +504,18 @@ impl Convertor {
fn next_token( fn next_token(
preorder: &mut PreorderWithTokens, preorder: &mut PreorderWithTokens,
replace: &FxHashMap<SyntaxNode, Vec<SyntheticToken>>, replace: &mut FxHashMap<SyntaxNode, Vec<SyntheticToken>>,
append: &FxHashMap<SyntaxNode, Vec<SyntheticToken>>, append: &mut FxHashMap<SyntaxNode, Vec<SyntheticToken>>,
) -> (Option<SyntaxToken>, Vec<SyntheticToken>) { ) -> (Option<SyntaxToken>, Vec<SyntheticToken>) {
while let Some(ev) = preorder.next() { while let Some(ev) = preorder.next() {
let ele = match ev { let ele = match ev {
WalkEvent::Enter(ele) => ele, WalkEvent::Enter(ele) => ele,
WalkEvent::Leave(SyntaxElement::Node(node)) => { WalkEvent::Leave(SyntaxElement::Node(node)) => {
if let Some(v) = append.get(&node) { if let Some(mut v) = append.remove(&node) {
eprintln!("after {:?}, appending {:?}", node, v); eprintln!("after {:?}, appending {:?}", node, v);
if !v.is_empty() { if !v.is_empty() {
let mut reversed = v.clone(); v.reverse();
reversed.reverse(); return (None, v);
return (None, reversed);
} }
} }
continue; continue;
@ -526,13 +525,12 @@ impl Convertor {
match ele { match ele {
SyntaxElement::Token(t) => return (Some(t), Vec::new()), SyntaxElement::Token(t) => return (Some(t), Vec::new()),
SyntaxElement::Node(node) => { SyntaxElement::Node(node) => {
if let Some(v) = replace.get(&node) { if let Some(mut v) = replace.remove(&node) {
preorder.skip_subtree(); preorder.skip_subtree();
eprintln!("replacing {:?} by {:?}", node, v); eprintln!("replacing {:?} by {:?}", node, v);
if !v.is_empty() { if !v.is_empty() {
let mut reversed = v.clone(); v.reverse();
reversed.reverse(); return (None, v);
return (None, reversed);
} }
} }
} }
@ -603,7 +601,7 @@ impl TokenConvertor for Convertor {
if let Some(synth_token) = self.current_synthetic.pop() { if let Some(synth_token) = self.current_synthetic.pop() {
if self.current_synthetic.is_empty() { if self.current_synthetic.is_empty() {
let (new_current, new_synth) = let (new_current, new_synth) =
Self::next_token(&mut self.preorder, &self.replace, &self.append); Self::next_token(&mut self.preorder, &mut self.replace, &mut self.append);
self.current = new_current; self.current = new_current;
self.current_synthetic = new_synth; self.current_synthetic = new_synth;
} }
@ -616,7 +614,7 @@ impl TokenConvertor for Convertor {
return None; return None;
} }
let (new_current, new_synth) = let (new_current, new_synth) =
Self::next_token(&mut self.preorder, &self.replace, &self.append); Self::next_token(&mut self.preorder, &mut self.replace, &mut self.append);
self.current = new_current; self.current = new_current;
self.current_synthetic = new_synth; self.current_synthetic = new_synth;
let token = if curr.kind().is_punct() { let token = if curr.kind().is_punct() {