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 completions #754

Merged
merged 37 commits into from
Mar 28, 2025
Merged

Refactor completions #754

merged 37 commits into from
Mar 28, 2025

Conversation

jennybc
Copy link
Member

@jennybc jennybc commented Mar 25, 2025

Relates to #681

The main goal is to introduce a new struct to serve as headquarters when generating completions. This gives us a home for features that we compute about the completion site, that can then be used downstream in one or multiple concrete completion sources. Two existing examples of this, each previously implemented in different ways, are:

  • "parameter hints": Distinguishes whether we (think) we're completing a function invocation or a function value. See Analyze completion site re: trailing parens #680.
  • "pipe root": If we're dealing with a pipe chain that starts with, e.g., a data frame, you might want to expose its column names as completions.

Qualities we wanted in this refactoring:

  • Completion site features are lazy: computed upon first need and are not re-computed unnecessarily.
  • Features and/or document don't need to be threaded through as arguments through all layers of completion sources and completion item helpers as "loose parts".
  • Make the completion tooling easier to work on and debug, e.g. through more intentional logging.
  • General effort to remove incidental inconsistencies across individual completion sources.

I'll make a few comments on the source as well.

(I've drafted a 2nd PR #755 that builds on this one, that proposes changes to how we manage completion items as they roll in from various sources. That's in draft form until the dust settles here.)

@jennybc jennybc marked this pull request as ready for review March 25, 2025 22:07
@jennybc jennybc requested review from DavisVaughan and lionel- March 25, 2025 22:08
@jennybc jennybc force-pushed the feature/refactor-completions branch from 5db87e7 to fe58072 Compare March 25, 2025 23:18
Copy link
Contributor

@lionel- lionel- left a comment

Choose a reason for hiding this comment

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

Nice progress! I quite like the way sources are now structs that implement a common trait. However it feels like we could improve the way things are structured by:

  • Removing the build() method from the builder, and renaming the builder to something more like "state" or "context".
  • Not having the aggregators implement the source trait
  • Using a generic function to statically dispatch on sources instead of for loops with dynamic dispatch.

See comments for justifications on these thoughts. Happy to discuss these points during our casual!

let root = find_pipe_root(self.context)?;

// Cache it for future calls (ignore failure if race condition, which shouldn't happen)
let _ = self.pipe_root_cell.set(root.clone());
Copy link
Contributor

Choose a reason for hiding this comment

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

We should at least panic (e.g. via expect()) or log an error if the assumption isn't satisfied.

But we could just use get_or_init() to avoid this I think? This would be the more idiomatic use of once cell.

Also I think the cell should contain the result so that we don't try to recompute the root when there is an error?

Copy link
Member Author

Choose a reason for hiding this comment

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

I mostly implemented this and certainly think I've gotten at the spirit of it. If you have concerns with how I handled it, we can refine.

jennybc added 23 commits March 27, 2025 11:55
Allows us to iterate in a clean way over unique sources
Finally all completion sources are shifted over to a new, common pattern.

Some return types were changed (to Option<Vec<CompletionItem>>) as a move towards consistency in completion source functions
Remove on-entry logging for completion sources, since the 2 main aggregators now log before trying each source

Shift towards debug for logging from a completion source or related helpers
@jennybc jennybc force-pushed the feature/refactor-completions branch from 0ebe2b9 to 6ef4fef Compare March 27, 2025 21:31
@jennybc jennybc force-pushed the feature/refactor-completions branch from dd70c8d to ecea7ad Compare March 28, 2025 02:24
Copy link
Contributor

@lionel- lionel- left a comment

Choose a reason for hiding this comment

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

Looks great!

@jennybc jennybc merged commit b037c6d into main Mar 28, 2025
6 checks passed
@jennybc jennybc deleted the feature/refactor-completions branch March 28, 2025 23:14
@github-actions github-actions bot locked and limited conversation to collaborators Mar 28, 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.

2 participants