mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 22:01:37 +00:00
Refactor flattening logic for highlighted syntax ranges
This commit is contained in:
parent
84e3304a9b
commit
29a846464b
1 changed files with 85 additions and 54 deletions
|
@ -31,6 +31,80 @@ pub struct HighlightedRange {
|
||||||
pub binding_hash: Option<u64>,
|
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(
|
pub(crate) fn highlight(
|
||||||
db: &RootDatabase,
|
db: &RootDatabase,
|
||||||
file_id: FileId,
|
file_id: FileId,
|
||||||
|
@ -57,7 +131,7 @@ pub(crate) fn highlight(
|
||||||
let mut bindings_shadow_count: FxHashMap<Name, u32> = FxHashMap::default();
|
let mut bindings_shadow_count: FxHashMap<Name, u32> = FxHashMap::default();
|
||||||
// We use a stack for the DFS traversal below.
|
// We use a stack for the DFS traversal below.
|
||||||
// When we leave a node, the we use it to flatten the highlighted ranges.
|
// 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;
|
let mut current_macro_call: Option<ast::MacroCall> = None;
|
||||||
|
|
||||||
|
@ -65,44 +139,9 @@ pub(crate) fn highlight(
|
||||||
// If in macro, expand it first and highlight the expanded code.
|
// If in macro, expand it first and highlight the expanded code.
|
||||||
for event in root.preorder_with_tokens() {
|
for event in root.preorder_with_tokens() {
|
||||||
match &event {
|
match &event {
|
||||||
WalkEvent::Enter(_) => res.push(Vec::new()),
|
WalkEvent::Enter(_) => stack.push(),
|
||||||
WalkEvent::Leave(_) => {
|
WalkEvent::Leave(_) => stack.pop(),
|
||||||
/* 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
let current = res.last_mut().expect("during DFS traversal, the stack must not be empty");
|
|
||||||
|
|
||||||
let event_range = match &event {
|
let event_range = match &event {
|
||||||
WalkEvent::Enter(it) => it.text_range(),
|
WalkEvent::Enter(it) => it.text_range(),
|
||||||
|
@ -119,7 +158,7 @@ pub(crate) fn highlight(
|
||||||
WalkEvent::Enter(Some(mc)) => {
|
WalkEvent::Enter(Some(mc)) => {
|
||||||
current_macro_call = Some(mc.clone());
|
current_macro_call = Some(mc.clone());
|
||||||
if let Some(range) = macro_call_range(&mc) {
|
if let Some(range) = macro_call_range(&mc) {
|
||||||
current.push(HighlightedRange {
|
stack.add(HighlightedRange {
|
||||||
range,
|
range,
|
||||||
highlight: HighlightTag::Macro.into(),
|
highlight: HighlightTag::Macro.into(),
|
||||||
binding_hash: None,
|
binding_hash: None,
|
||||||
|
@ -161,7 +200,7 @@ pub(crate) fn highlight(
|
||||||
|
|
||||||
if let Some(token) = element.as_token().cloned().and_then(ast::RawString::cast) {
|
if let Some(token) = element.as_token().cloned().and_then(ast::RawString::cast) {
|
||||||
let expanded = element_to_highlight.as_token().unwrap().clone();
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -169,19 +208,11 @@ pub(crate) fn highlight(
|
||||||
if let Some((highlight, binding_hash)) =
|
if let Some((highlight, binding_hash)) =
|
||||||
highlight_element(&sema, &mut bindings_shadow_count, element_to_highlight)
|
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");
|
stack.flattened()
|
||||||
let mut res = res.pop().unwrap();
|
|
||||||
res.sort_by_key(|range| range.range.start());
|
|
||||||
// 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn macro_call_range(macro_call: &ast::MacroCall) -> Option<TextRange> {
|
fn macro_call_range(macro_call: &ast::MacroCall) -> Option<TextRange> {
|
||||||
|
@ -358,7 +389,7 @@ fn highlight_name_by_syntax(name: ast::Name) -> Highlight {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn highlight_injection(
|
fn highlight_injection(
|
||||||
acc: &mut Vec<HighlightedRange>,
|
acc: &mut HighlightedRangeStack,
|
||||||
sema: &Semantics<RootDatabase>,
|
sema: &Semantics<RootDatabase>,
|
||||||
literal: ast::RawString,
|
literal: ast::RawString,
|
||||||
expanded: SyntaxToken,
|
expanded: SyntaxToken,
|
||||||
|
@ -373,7 +404,7 @@ fn highlight_injection(
|
||||||
let (analysis, tmp_file_id) = Analysis::from_single_file(value);
|
let (analysis, tmp_file_id) = Analysis::from_single_file(value);
|
||||||
|
|
||||||
if let Some(range) = literal.open_quote_text_range() {
|
if let Some(range) = literal.open_quote_text_range() {
|
||||||
acc.push(HighlightedRange {
|
acc.add(HighlightedRange {
|
||||||
range,
|
range,
|
||||||
highlight: HighlightTag::StringLiteral.into(),
|
highlight: HighlightTag::StringLiteral.into(),
|
||||||
binding_hash: None,
|
binding_hash: None,
|
||||||
|
@ -383,12 +414,12 @@ fn highlight_injection(
|
||||||
for mut h in analysis.highlight(tmp_file_id).unwrap() {
|
for mut h in analysis.highlight(tmp_file_id).unwrap() {
|
||||||
if let Some(r) = literal.map_range_up(h.range) {
|
if let Some(r) = literal.map_range_up(h.range) {
|
||||||
h.range = r;
|
h.range = r;
|
||||||
acc.push(h)
|
acc.add(h)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(range) = literal.close_quote_text_range() {
|
if let Some(range) = literal.close_quote_text_range() {
|
||||||
acc.push(HighlightedRange {
|
acc.add(HighlightedRange {
|
||||||
range,
|
range,
|
||||||
highlight: HighlightTag::StringLiteral.into(),
|
highlight: HighlightTag::StringLiteral.into(),
|
||||||
binding_hash: None,
|
binding_hash: None,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue