Skip to content

Complete writer methods with self receiver when receiver is self #3550

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

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

Conversation

antw
Copy link
Member

@antw antw commented May 28, 2025

Motivation

Closes #3528

Setter methods where the receiver is self were being completed without the explicit self. prefix. This would result in local variable assignment rather than calling the writer method.

For example, when typing b in a class method and expecting completion for baz=, the LSP would suggest:

def bar
  baz= # This assigns to a local variable, not calling the writer method
end

Instead of the correct:

def bar
  self.baz=
end

Implementation

  • Modified the add_method_completions method in completion.rb to detect when completing setter methods with a self receiver.
  • The changes only affect files where Sorbet type checking is not enabled; those use Sorbet's completion, which has the same issue, but is outside the scope of Ruby LSP (as far as I know).

The implementation checks node.receiver.nil? to detect implicit self and avoid adding self. when the user has already typed the receiver.

Note

This is my first ruby-lsp PR. Feedback on the approach would be appreciated, particularly:

  • I read through the documentation for CompletionItem and it feels like setting the newText is the correct option (leaving the label and filterText as before) but I'd be curious if reviewers feel otherwise.
  • I put spaces around the "=" as suggested in the issue since that's how most people would write this..

Automated Tests

Added test coverage in completion_test.rb with three scenarios:

  • Implicit self receiver (b in class method) - verifies self. prefix is added to completion text
  • Explicit self receiver (self.b in class method) - verifies no additional self. prefix is added
  • External receiver (foo.b outside class) - verifies no self. prefix is added

antw added 2 commits May 28, 2025 14:35
Fixes completion of setter methods to use 'self.method=' syntax when
accessed within instance methods. Previously, completion would suggest
'method=' which assigns to a local variable instead of calling the
instance writer method.
Writer method completions now include proper spacing around the equals
sign to follow Ruby style conventions (e.g., 'self.attr = ' instead
of 'self.attr=').
Copy link

graphite-app bot commented May 28, 2025

How to use the Graphite Merge Queue

Add the label graphite-merge to this PR to add it to the merge queue.

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

@antw antw marked this pull request as ready for review May 29, 2025 02:19
@antw antw requested a review from a team as a code owner May 29, 2025 02:19
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.

Internal writer methods complete incorrectly
1 participant