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

[Feature-Request] add completion for hook methods #7

Closed
didacusAbella opened this issue Aug 24, 2017 · 3 comments
Closed

[Feature-Request] add completion for hook methods #7

didacusAbella opened this issue Aug 24, 2017 · 3 comments

Comments

@didacusAbella
Copy link

it is a common practice for ruby programmers to add methods dynamically when some modules are extended, prepended or included. For example:

module MyModule
  def self.included(klass)
    klass.class_eval do
     # This method add new functionality
     # @param an_argument [SomeType] a specific argument
     # @return [SomeType]
     def another_method(an_argument)
       #Some business code
     end
   end
 end
end

class MyClass
  include MyModule
end

kl = MyClass.new
kl. # => it would be great if in method suggestion appears another_method too

the same functionality would be great for the other ruby hook methods and for klass.instance_eval method

@castwide
Copy link
Owner

Detecting dynamic methods is tricky. I might be able to make it work in limited cases, but there will always be exceptions. The only 100% reliable solution is to execute the code, which Solargraph intentionally avoids doing.

One alternative is to use YARD directives. Example using the @!method directive:

module MyModule
  # @!method another_method(an_argument)
  #   This method add new functionality
  #   @param an_argument [SomeType] a specific argument
  #   @return [SomeType]

  def self.included(klass)
    klass.class_eval do
      def another_method(an_argument)
        #Some business code
      end
    end
  end
end

class MyClass
  include MyModule
end

kl = MyClass.new
kl. # => another_method appears in suggestions

Note: suggestions from YARD directives will only be available if the file is included in the generated .yardoc directory. By default, YARD only reads the workspace's lib and app directories. That setting can be changed with a .yardopts file. A future version of the Solargraph gem might be able to parse directives locally.

I'm open to other suggestions for improving detection of dynamic methods. It's a hard problem.

@castwide
Copy link
Owner

castwide commented Dec 4, 2017

As of gem version 0.15.0, there are two additions that can help with this feature:

  • Improved macro support. Any @!method and @!attribute macros in your workspace get mapped into completion suggestions. A local yardoc is not required.

  • The experimental Runtime plugin. Solargraph can run a subprocess that queries your project's classes and modules for "magic" methods. More information is available in the Solargraph README.

@didacusAbella
Copy link
Author

Very Nice!! I'll try it as soon as possible

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

No branches or pull requests

2 participants