Skip to content

Suggest the correct method name in Ruby::NoMethod. - #1705

Open
ksss wants to merge 2 commits into
soutaro:masterfrom
ksss:did-you-mean
Open

Suggest the correct method name in Ruby::NoMethod.#1705
ksss wants to merge 2 commits into
soutaro:masterfrom
ksss:did-you-mean

Conversation

@ksss

@ksss ksss commented Jun 3, 2025

Copy link
Copy Markdown
Contributor

This pull request introduces a new feature to provide method suggestions when a Ruby::NoMethod error occurs in the steep LSP.
It leverages the did_you_mean gem to suggest possible corrections based on a dictionary of method name list by RBS.

textDocument/codeAction

I implemented this feature using textDocument/codeAction.
Currently only for Ruby::NoMethod, but can be extended to support Ruby::UnknownConstant and RBS::UnknownTypeName, etc.

Implementation

I implemented Steep::Services::CodeActionProvider.
It is designed for textDocument/codeAction with reference to Steep::Services::CompletionProvider.

Example

sample.rb

o = Object.new
o.calss

# @type var a: {a: 1}
a = _ = nil
x = a.foo(1)

With an editor that can use LSP, such as VSCode, it is possible to propose a “Quick Fix” and actually fix the problem.

image

@ParadoxV5 ParadoxV5 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👏

Comment thread lib/steep/diagnostic/ruby.rb Outdated

def detail_lines
if defined?(DidYouMean)
corrects = DidYouMean::SpellChecker.new(dictionary: dictionary).correct(method)

@ParadoxV5 ParadoxV5 Jun 3, 2025

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It doesn’t look right to construct a new temporary spellchecker for each diagnostic instance and on every call.
(Correct me if I’m wrong: I’m not familiar with how Did You Mean works.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The initialization cost of DidYouMean::SpellChecker is very low.
https://github.com/ruby/did_you_mean/blob/2375fc5f2a66542df62cde3f08c2263677df3511/lib/did_you_mean/spell_checker.rb#L6-L10

module DidYouMean
  class SpellChecker
    def initialize(dictionary:)
      @dictionary = dictionary
    end

dictionary is a valid candidate for a method name, and I believe it's natural for the suggested candidates to differ depending on the error.

Use did_you_mean for `Ruby::NoMethod` and present the proposed fix as a “Quick Fix” in the editor.
@ksss

ksss commented Jun 12, 2025

Copy link
Copy Markdown
Contributor Author

I reimplemented to use textDocument/codeAction.
The previous version, which added hover text.

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.

2 participants