Skip to content

Fix the range of sections in document symbols (#846) #854

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

Closed
wants to merge 1 commit into from

Conversation

kylebutts
Copy link

This solution fixes #846 and posit-dev/positron#6137.

The problem comes from here:

fn index_comments(
node: &Node,
mut store_stack: StoreStack,
contents: &Rope,
) -> anyhow::Result<StoreStack> {
let comment_text = contents.node_slice(&node)?.to_string();
// Check if the comment starts with one or more '#' followed by any text and ends with 4+ punctuations
let Some((level, title)) = parse_comment_as_section(&comment_text) else {
return Ok(store_stack);
};
// Create a section symbol based on the parsed comment
let start = convert_point_to_position(contents, node.start_position());
let end = convert_point_to_position(contents, node.end_position());
let symbol = new_symbol(title, SymbolKind::STRING, Range { start, end });
// Now pop all sections still on the stack that have a higher or equal
// level. Because we pop sections with equal levels, i.e. siblings, we
// ensure that there is only one active section per level on the stack.
// That simplifies things because we need to assign popped sections to their
// parents and we can assume the relevant parent is always the next on the
// stack.
loop {
let Some((last_level, _, _)) = store_stack.last() else {
return Err(anyhow!("Unexpectedly reached the end of the store stack"));
};
if *last_level >= level {
if store_stack_pop(&mut store_stack)?.is_some() {
return Err(anyhow!("Unexpectedly reached the end of the store stack"));
}
continue;
}
break;
}
store_stack.push((level, Some(symbol), vec![]));
Ok(store_stack)
}

  • node is the single comment line, so the "section" is defined as the single line.

The solution I settled on was to take the created nested symbols and then "correct" the the end positions of the sections

  • I did this to avoid a O(n^2) loop where you loop through the lines to find section starts and then loop through the lines again to find where the next header starts.

This also fixes the sticky scroll which was also breaking from the sections having "1-line height"
Example of headers working in outline and sticky header

@lionel-
Copy link
Contributor

lionel- commented Jun 26, 2025

Thanks @kylebutts! I went with a different approach in #855

@kylebutts
Copy link
Author

Great! The new logic looks a lot nicer :->

@kylebutts kylebutts closed this Jun 26, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Jun 26, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Problem with breadcrumbs and special-comment based sections
2 participants