From 445dfda53ecbbc770de4735e595b3d3f7412f05f Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 5 Mar 2019 09:44:43 -0800 Subject: [PATCH] binding: Restore handling of multiple symbols w/ same name Even though normal aliases don't cause this, simple (single-use) aliases still do cause it. --- highlight/src/lib.rs | 32 ++++++++++++++++-- lib/binding/lib.rs | 80 ++++++++++++++++++++++++-------------------- 2 files changed, 73 insertions(+), 39 deletions(-) diff --git a/highlight/src/lib.rs b/highlight/src/lib.rs index e5499fbc..8bc0e6bf 100644 --- a/highlight/src/lib.rs +++ b/highlight/src/lib.rs @@ -3,7 +3,7 @@ mod escape; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use serde_derive::*; use std::cmp; -use std::fmt::Write; +use std::fmt::{self, Write}; use std::mem::transmute; use std::str; use std::usize; @@ -572,8 +572,9 @@ where } } -impl<'a, T: Fn(&str) -> Option<(Language, &'a PropertySheet)>> Iterator - for Highlighter<'a, T> +impl<'a, T> Iterator for Highlighter<'a, T> +where + T: Fn(&str) -> Option<(Language, &'a PropertySheet)>, { type Item = HighlightEvent<'a>; @@ -653,6 +654,31 @@ impl<'a, T: Fn(&str) -> Option<(Language, &'a PropertySheet)>> Itera } } +impl<'a, T> fmt::Debug for Highlighter<'a, T> +where + T: Fn(&str) -> Option<(Language, &'a PropertySheet)>, +{ + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if let Some(layer) = self.layers.first() { + let node = layer.cursor.node(); + let position = if layer.at_node_end { + node.end_position() + } else { + node.start_position() + }; + write!( + f, + "{{Highlighter position: {:?}, kind: {}, at_end: {}, props: {:?}}}", + position, + node.kind(), + layer.at_node_end, + layer.cursor.node_properties() + )?; + } + Ok(()) + } +} + impl<'a> Layer<'a> { fn new( source: &'a [u8], diff --git a/lib/binding/lib.rs b/lib/binding/lib.rs index 926f95b4..4f72c280 100644 --- a/lib/binding/lib.rs +++ b/lib/binding/lib.rs @@ -907,53 +907,61 @@ impl

PropertySheet

{ None }; - let kind_id = transition.kind.as_ref().and_then(|kind| { - let named = transition.named.unwrap(); - for i in 0..(node_kind_count as u16) { - if kind == language.node_kind_for_id(i) - && named == language.node_kind_is_named(i) - { - return Some(i); - } - } - None - }); - + let state_id = transition.state_id as u16; + let child_index = transition.index.map(|i| i as u16); let field_id = transition .field .as_ref() .and_then(|field| language.field_id_for_name(&field)); - if let Some(field_id) = field_id { + if let Some(kind) = transition.kind.as_ref() { + for kind_id in 0..(node_kind_count as u16) { + if kind != language.node_kind_for_id(kind_id) + || transition.named != Some(language.node_kind_is_named(kind_id)) + { + continue; + } + + if let Some(field_id) = field_id { + field_transitions + .entry(field_id) + .or_insert(Vec::new()) + .push(PropertyTransition { + node_kind_id: Some(kind_id), + state_id, + child_index, + text_regex_index, + }); + } else { + for (_, entries) in field_transitions.iter_mut() { + entries.push(PropertyTransition { + node_kind_id: Some(kind_id), + state_id, + child_index, + text_regex_index, + }); + } + + kind_transitions.entry(kind_id).or_insert(Vec::new()).push( + PropertyTransition { + node_kind_id: None, + state_id, + child_index, + text_regex_index, + }, + ); + } + } + } else if let Some(field_id) = field_id { field_transitions .entry(field_id) .or_insert(Vec::new()) .push(PropertyTransition { - node_kind_id: kind_id, - child_index: transition.index.map(|i| i as u16), - state_id: transition.state_id as u16, + node_kind_id: None, + state_id, + child_index, text_regex_index, }); - } else { - for (_, entries) in field_transitions.iter_mut() { - entries.push(PropertyTransition { - node_kind_id: kind_id, - child_index: transition.index.map(|i| i as u16), - state_id: transition.state_id as u16, - text_regex_index, - }); - } - - if let Some(kind_id) = kind_id { - kind_transitions.entry(kind_id).or_insert(Vec::new()).push( - PropertyTransition { - node_kind_id: None, - child_index: transition.index.map(|i| i as u16), - state_id: transition.state_id as u16, - text_regex_index, - }, - ); - } } } states.push(PropertyState {