Code Completion
Here is the code:
require 'date'
dob = "08/04/1957"
d=Date.strptime(dob,"%m/%d/%Y")
puts d.month
When I type the last line "puts d." I get code completion for object and kernel but not Date. If I type "puts d.class" I get Date.
Why do I not get code completion for Date? BTW, If I just run the above code it works.
请先登录再写评论。
Hello Ron,
This happens because RubyMine wasn't able to infer the return type of call Date.strptime(dob,"%m/%d/%Y").
Our type inference system tries to extract type from comment of method in this case and cannot cope with it because it's description doesn't have any callseqs within.
Type inference engine depth restriction is insufficient in this case. So it's assumed that this is Object class type.
Regards,
Oleg
Hello Ron,
As a workaround you can copy the attached file explicit_types.rb to $RUBYMINE_INSTALLATION_FOLDER$/rb/scripts/
RubyMine API is not officially published yet but we'll definitely get it published before official release.
Regards,
Oleg
Attachment(s):
explicit_types.rb.zip
Hi Ron,
Also see http://www.jetbrains.net/devnet/message/5246033#5246033 thread about using our Ruby API
This link doesn't exist.
Fixed link - http://www.jetbrains.net/devnet/thread/283912?tstart=0
This is too complicated. I'm going back to Textmate.
What is complicated? If you don't want extend RM's code insight or wait until we improve something in type inference - you shouldn't write any extensions and RM will be still cleverer than TextMate
I've never written a Ruby extension.
Ron,
It was optional and isn't required by RubyMine's workflow.
Also probably we didn't understand each other because I only mentioned extension to RubyMine using RubyMine's API in Ruby language and I didn't suggest to write extension for Ruby language or Ruby Core library. Several our users asked as to create some API in Ruby language for improving RubyMine code insight in their projects. API isn't ready yet but it is possible to use it and I gave your a link to an example.
I got it to work. I guess I don't understand why if it was that simple why wasn't it there in the first place?
Hi Roman, Netbeans 6.8m1 method return type support, it is a great idea. I think we can introduce # :tyep:=>Date for variable to implement type inference, and it is very simple.
require 'date'
dob = "08/04/1975"
# :type:=>Date
date = Date.strptime(dob, "%m/%d/%Y")
puts date.month
# :type:=>Date can help us resolve some simple problems, and "Ruby API for defining dynamic types" is the best, but some guys can not accept it.
Linux_china,
RubyMine also supports NB return type annotations, we added support several 1.5 EAP builds ago. But it isn't possible to annotate files from Ruby SDK or gems =)
Hi guys,
NB type annotations are supported only for methods return types now. We'll easily add them for local variables too if people really use them.
Regards,
Oleg
It is very useful for me. I can not add annotation for third gem, but now I can use it for my variables.
Done, this annotation will be available in next public build.
Trying to understand state of this a little better (resurrected this thread from a while ago).
My understanding is that with RM 3.0.1 the completion still does not take into account the return types of methods and shows methods only from Object/Kernel. Is that correct?
(Do I still have to extract the attached Ruby script and do something on my own to make completion work?)
thanks,
Kedar
No, in general RubyMine infers return types where it can. At the moment the above example with Date is too complicated for our return type infer algorithm. So in the example as a workaround you may specify type explicitly using type annotations. I took a look on Date object implementation, most likely RubyMine doesn't infer Date.strptime type only due to some minor bugs or rare usecases in type inference algorithm.
P.S: I filed an issue - http://youtrack.jetbrains.net/issue/RUBY-7485
In this particular case RubyMine cannot infer type, because of call depth restrictions during Infer Types Analyzis. Sorry for the inconveniences.
Regards,
Oleg
Is there some documentation or blog post available explaining how to use comments to improve type inference? I'm trying to do stuff like this but it doesn't work.
# :type: => User
def foo
User.get(1)
end
foo.[invoke code completion]
What about being able to select type if Rubymine can't infer type?
Like say I have a "params[:name]", it would be nice of I could quickly define the type (permenantly in a project config file). If params[:name] is a string, Rubymine can't see it and you loose the code completion string methods. I'd rather be able to specify the type insidie Rubymine real fast to see the string methods than go out to the API because I can't remember the exact method name I'm thinking of.
OK. Thanks.
How about the type annotations? Do I need to learn them separately?
And it seems that yardoc is the way to go when it comes to documenting the Ruby code the Javadoc style. So, can we configure RubyMine to use yardoc annotations when available for type inference?
Regards,
Kedar
Yard support isn't implemented, please vote for http://youtrack.jetbrains.net/issue/RUBY-5590.
Reno,
Good idea, please submit a feature request at http://youtrack.jetbrains.net/issues/ruby
Probably file with "User" class definition isn't required in a context of your example.
Is it rails application?