Skip to content
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

Refactor symbol search #597

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open

Refactor symbol search #597

wants to merge 10 commits into from

Conversation

lionel-
Copy link
Contributor

@lionel- lionel- commented Oct 21, 2024

Progress towards posit-dev/positron#3822.

  • Errors are now consistently propagated as Err and only logged at the very top.

  • We no longer recurse blindly into all node types except argument lists (this is the current restriction but note that we should recurse in arg lists in the future for e.g. lapply and test_that). Instead, we only recurse in handlers of specific node types. For instance, comment sections are only expected in expression lists, and thus only handled in the expression list handler.

  • We no longer pass around a mut ref of the parent. Instead we pass a store of children by value and return it once done. If a handler inserts a note in the outline, it creates a new store, recurse into its children with this new store, and adds itself to the store of its caller.

    This simpler approach is easier to understand and should solve the lifetime issues encountered in Feature/nested-outline #593.

  • Bunch of tests.

@kv9898

This comment has been minimized.

@lionel-
Copy link
Contributor Author

lionel- commented Oct 23, 2024

I'm thinking that only index_expression_list() (added in this PR) needs to keep track of the section stack and pass the currently active store to its children.

Now that all methods own their store, we no longer need any ref or mut ref. So index_expression_list() will be able to push and pop section stores out of its stack and we only work with owned values that we move around.

Does that makes sense?

Copy link
Contributor

@DavisVaughan DavisVaughan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a nice simplification!

Comment on lines +34 to +56
fn new_symbol(name: String, kind: SymbolKind, range: Range) -> DocumentSymbol {
DocumentSymbol {
name,
kind,
detail: None,
children: Some(Vec::new()),
deprecated: None,
tags: None,
range,
selection_range: range,
}
}

fn new_symbol_node(
name: String,
kind: SymbolKind,
range: Range,
children: Vec<DocumentSymbol>,
) -> DocumentSymbol {
let mut symbol = new_symbol(name, kind, range);
symbol.children = Some(children);
symbol
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing that I really like from the Biome group is the with_<optional_feature>() pattern

fn with_children(mut self, children) -> Self {
  self.children = children;
  self
}

So it becomes

new_symbol(symbol, kind, range).with_children(children)

It would require a DocumentSymbolExt trait so this is fine if you want to keep it as is, just thought id point it out as a pattern to keep in mind

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That does seem like a nice pattern!

@kv9898
Copy link
Contributor

kv9898 commented Oct 23, 2024

I'm thinking that only index_expression_list() (added in this PR) needs to keep track of the section stack and pass the currently active store to its children.

Now that all methods own their store, we no longer need any ref or mut ref. So index_expression_list() will be able to push and pop section stores out of its stack and we only work with owned values that we move around.

Does that makes sense?

Oh yes it does. I wrote a new pull request to this specific branch in #606.

My approach was to use a store-vector to store a "flattened" version of the nested structure, which can be edited easily during comment section handling and be assembled in the end of index_expression_list.

Note that this process involves assigning a level to any symbols involved. For the non-comment symbols, I assigned them to usize::MAX to avoid them becoming parents.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants