Skip to content

Commit

Permalink
Refactor flattening logic for highlighted sytax ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
ltentrup committed Apr 17, 2020
1 parent 8d296be commit cce49e2
Showing 1 changed file with 85 additions and 53 deletions.
138 changes: 85 additions & 53 deletions crates/ra_ide/src/syntax_highlighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,80 @@ pub struct HighlightedRange {
pub binding_hash: Option<u64>,
}

#[derive(Debug)]
struct HighlightedRangeStack {
stack: Vec<Vec<HighlightedRange>>,
}

/// We use a stack to implement the flattening logic for the highlighted
/// syntax ranges.
impl HighlightedRangeStack {
fn new() -> Self {
Self { stack: vec![Vec::new()] }
}

fn push(&mut self) {
self.stack.push(Vec::new());
}

/// Flattens the highlighted ranges.
///
/// For example `#[cfg(feature = "foo")]` contains the nested ranges:
/// 1) parent-range: Attribute [0, 23)
/// 2) child-range: String [16, 21)
///
/// The following code implements the flattening, for our example this results to:
/// `[Attribute [0, 16), String [16, 21), Attribute [21, 23)]`
fn pop(&mut self) {
let children = self.stack.pop().unwrap();
let prev = self.stack.last_mut().unwrap();
let needs_flattening = !children.is_empty()
&& !prev.is_empty()
&& children.first().unwrap().range.is_subrange(&prev.last().unwrap().range);
if !needs_flattening {
prev.extend(children);
} else {
let mut parent = prev.pop().unwrap();
for ele in children {
assert!(ele.range.is_subrange(&parent.range));
let mut cloned = parent.clone();
parent.range = TextRange::from_to(parent.range.start(), ele.range.start());
cloned.range = TextRange::from_to(ele.range.end(), cloned.range.end());
if !parent.range.is_empty() {
prev.push(parent);
}
prev.push(ele);
parent = cloned;
}
if !parent.range.is_empty() {
prev.push(parent);
}
}
}

fn add(&mut self, range: HighlightedRange) {
self.stack
.last_mut()
.expect("during DFS traversal, the stack must not be empty")
.push(range)
}

fn flattened(mut self) -> Vec<HighlightedRange> {
assert_eq!(
self.stack.len(),
1,
"after DFS traversal, the stack should only contain a single element"
);
let res = self.stack.pop().unwrap();
// Check that ranges are sorted and disjoint
assert!(res
.iter()
.zip(res.iter().skip(1))
.all(|(left, right)| left.range.end() <= right.range.start()));
res
}
}

pub(crate) fn highlight(
db: &RootDatabase,
file_id: FileId,
Expand All @@ -57,52 +131,17 @@ pub(crate) fn highlight(
let mut bindings_shadow_count: FxHashMap<Name, u32> = FxHashMap::default();
// We use a stack for the DFS traversal below.
// When we leave a node, the we use it to flatten the highlighted ranges.
let mut res: Vec<Vec<HighlightedRange>> = vec![Vec::new()];
let mut stack = HighlightedRangeStack::new();

let mut current_macro_call: Option<ast::MacroCall> = None;

// Walk all nodes, keeping track of whether we are inside a macro or not.
// If in macro, expand it first and highlight the expanded code.
for event in root.preorder_with_tokens() {
match &event {
WalkEvent::Enter(_) => res.push(Vec::new()),
WalkEvent::Leave(_) => {
/* Flattens the highlighted ranges.
*
* For example `#[cfg(feature = "foo")]` contains the nested ranges:
* 1) parent-range: Attribute [0, 23)
* 2) child-range: String [16, 21)
*
* The following code implements the flattening, for our example this results to:
* `[Attribute [0, 16), String [16, 21), Attribute [21, 23)]`
*/
let children = res.pop().unwrap();
let prev = res.last_mut().unwrap();
let needs_flattening = !children.is_empty()
&& !prev.is_empty()
&& children.first().unwrap().range.is_subrange(&prev.last().unwrap().range);
if !needs_flattening {
prev.extend(children);
} else {
let mut parent = prev.pop().unwrap();
for ele in children {
assert!(ele.range.is_subrange(&parent.range));
let mut cloned = parent.clone();
parent.range = TextRange::from_to(parent.range.start(), ele.range.start());
cloned.range = TextRange::from_to(ele.range.end(), cloned.range.end());
if !parent.range.is_empty() {
prev.push(parent);
}
prev.push(ele);
parent = cloned;
}
if !parent.range.is_empty() {
prev.push(parent);
}
}
}
WalkEvent::Enter(_) => stack.push(),
WalkEvent::Leave(_) => stack.pop(),
};
let current = res.last_mut().expect("during DFS traversal, the stack must not be empty");

let event_range = match &event {
WalkEvent::Enter(it) => it.text_range(),
Expand All @@ -119,7 +158,7 @@ pub(crate) fn highlight(
WalkEvent::Enter(Some(mc)) => {
current_macro_call = Some(mc.clone());
if let Some(range) = macro_call_range(&mc) {
current.push(HighlightedRange {
stack.add(HighlightedRange {
range,
highlight: HighlightTag::Macro.into(),
binding_hash: None,
Expand Down Expand Up @@ -161,26 +200,19 @@ pub(crate) fn highlight(

if let Some(token) = element.as_token().cloned().and_then(ast::RawString::cast) {
let expanded = element_to_highlight.as_token().unwrap().clone();
if highlight_injection(current, &sema, token, expanded).is_some() {
if highlight_injection(&mut stack, &sema, token, expanded).is_some() {
continue;
}
}

if let Some((highlight, binding_hash)) =
highlight_element(&sema, &mut bindings_shadow_count, element_to_highlight)
{
current.push(HighlightedRange { range, highlight, binding_hash });
stack.add(HighlightedRange { range, highlight, binding_hash });
}
}

assert_eq!(res.len(), 1, "after DFS traversal, the stack should only contain a single element");
let res = res.pop().unwrap();
// Check that ranges are sorted and disjoint
assert!(res
.iter()
.zip(res.iter().skip(1))
.all(|(left, right)| left.range.end() <= right.range.start()));
res
stack.flattened()
}

fn macro_call_range(macro_call: &ast::MacroCall) -> Option<TextRange> {
Expand Down Expand Up @@ -357,7 +389,7 @@ fn highlight_name_by_syntax(name: ast::Name) -> Highlight {
}

fn highlight_injection(
acc: &mut Vec<HighlightedRange>,
acc: &mut HighlightedRangeStack,
sema: &Semantics<RootDatabase>,
literal: ast::RawString,
expanded: SyntaxToken,
Expand All @@ -372,7 +404,7 @@ fn highlight_injection(
let (analysis, tmp_file_id) = Analysis::from_single_file(value);

if let Some(range) = literal.open_quote_text_range() {
acc.push(HighlightedRange {
acc.add(HighlightedRange {
range,
highlight: HighlightTag::StringLiteral.into(),
binding_hash: None,
Expand All @@ -382,12 +414,12 @@ fn highlight_injection(
for mut h in analysis.highlight(tmp_file_id).unwrap() {
if let Some(r) = literal.map_range_up(h.range) {
h.range = r;
acc.push(h)
acc.add(h)
}
}

if let Some(range) = literal.close_quote_text_range() {
acc.push(HighlightedRange {
acc.add(HighlightedRange {
range,
highlight: HighlightTag::StringLiteral.into(),
binding_hash: None,
Expand Down

0 comments on commit cce49e2

Please sign in to comment.