Can IntelliJ auto-complete constructor parameters on "new" expression? - intellij-idea

If my class has a non-empty constructor, is it possible to auto-complete parameters in the new expression?
With Eclipse, if you press ctrl+space when the cursor is between the parenthesis:
MyClass myObject = new MyClass();
it will find the appropriate parameters.
--> MyClass myObject = new MyClass(name, value);
When I use ctrl+shift+spacebar after the new, Intellij shows me the constructors, but I can't choose one for auto-completion. Am I missing an option?

I usually start with CtrlP (Parameter Info action) to see what arguments are accepted (auto guess complete is way to error prone in my opinion). And if as in your case you want to fill in name type n a dropdown menu appears with all available variables/fields (etc) starting with n Arrow Up/Down and Tab to select name, or CtrlSpace to select a method (or even CtrlAltSpace to be killed by suggestions;-), followed by , and v Tab for value.

Well I used the eclipse key map where Parameter Info is unassigned.
Here is how to change that:

Well there's the Ctrl+Shift+Space combination, which tries to come up with a set of possible arguments. And if you press the Ctrl+Shift+Space a second time, Idea tries find arguments which fit across multiple calls & conversions.
So in your example Ctrl+Shift+Space would almost certainly bring up the 'name' as suggestion. And the next Ctrl+Shift+Space would bring up 'value' as suggestion.

In Intellij Idea 2016.3 you can use option + return. It will ask you if you want to introduce the named argument for the argument you are on and all the followers.

There's no such possibility yet. As IDEA doesn't fill the arguments automatically, distinguishing the constructors in the lookup makes no sense. There's a request for that (http://youtrack.jetbrains.net/issue/IDEABKL-5496) although I sincerely believe such a behavior is too dangerous and error-prone.

Related

JetBrains: How to watch the return value of a function?

I have a question about debugging mode in JetBrains IDEs (PyCharm, WebStorm, IntelliJ ..). Let's say I have a line in the code that looks like this:
....func1()...func2()...func3()...
Several functinos are called in the same line, and none of them is assigned to a variable. Now, I want to know what is the return value of each of these functions. I know the feature Evaluate Expression, but I don't want to use it, since it may invoke these functions again.
Do you know any way to find the return values of a function without assigning its value to a variable and checking its value in debugger?
As of PyCharm 2016.2, you can show function return values; to do so, you need to:
Click on the Settings gear icon in the left-hand toolbar of the Debug panel
Ensure that Show Return Values is checked
Then when a Return Value is present, you will see it listed under Return Values at the top of the Variables section of the Debug panel (and that information is retained while still in the calling function)
I don't think that this is possible right now but you could set breakpoints inside the functions itself.
Additionally you could add a "Disable until selected breakpoint is hit" + "Disable again" and join them with a breakpoint above the line you posted to make sure they are only called from this line.
Or simply refactor your code:
foobar.huey()
.dewey()
.louie();
and set line breakpoints as usual.
I was looking also for this, and I can link you an answer I found, extending the answer of David Fraser: in IntelliJ, someone replied with screenshots to a similar question in this same site:
java - Can I find out the return value before returning while debugging in Intellij
Remember to put a breakpoint inside the function and step out :)
As said there (although it includes screenshots, much better than this) by the user Birchlabs:
On IntelliJ IDEA 2016.3: it's hidden inside the cog button of the
debug panel. Ensure Show Method Return Values is checked.
IntelliJ IDEA 2016.3 "Show Method Return Values"
Use the debugger to break somewhere inside the function whose return
value you'd like to see.
step into function
Step out of the function (or step over until you escape):
step out
Observe that the return value appears in your variables:
observe the return value

Intellij - is there autocomplete for the type of a for loop?

like in eclipse, if you wrote a for loop like this:
for (name : names)
eclipse auto complete would let you add the type, so you'll get:
for (String name : names)
is there a similar feature in IntelliJ?
normal auto complete CTRL+SHIFT+SPACE or CTRL+SPACE aren't working.
You can type "iter" and then press Tab. Then you can select in the suggested list of all iterable variables which one to use for the loop.
This type of code generation is also available for other loops. See this answer for a list of live templates for loops.
I'm not familiar with the equivalent eclipse feature, but the closest thing I can think of (or alternative solution) is to use postfix completion.
if you type
names.for
and select the first suggestion, it will auto-expand into
for (Object name : names) {
}
and it will automatically add the type of name for you, based on what the collection type of names is.

How does compiler handle missing parameter names in Objective-C?

I have run into someone else's code that declares methods like this:
- (void) method:(id)a:(NSString*)b { }
The compiler accepts this code, giving just a warning:
'a' used as the name of the previous parameter rather than as part of the selector
The code declares various functions with this type and then invokes them through NSSelectorFromString, using the signature "methodname::". So it's all consistent.
I wonder if that method signature is just a mistake or if there's more to it. Since it's used consistently in the code, I don't think this is a typo. I don't know the author so I can't tell if this is code of a genius or the opposite.
Is 'b' an anonymous parameter? (If so, shouldn't it rather be written with a blank between the "a" and ":" to indicate this better?) I can't find anything about anon parms in the ObjC docs, though.
Will there be any change in behavior if I change the syntax to giving the second parameter a name, and fixing the signature reference accordingly? I plan to make this change to get rid of the warnings, but I wonder I might create an issue that I'm not aware of.
Everything you describe is pretty much correct. It's very bad style, but technically it's just a two-argument selector which happens to have no text before the second :. I wouldn't call b an anonymous argument since you can still give it a name, it just doesn't have any descriptive text before it as part of the selector's name.
Yes, there should probably be a space after the a.
If you want to rename it, you can use Xcode's standard Refactor->Rename functionality and just insert some text before the second :. It will update all the references and you should encounter no problems.
You can use the signature method::, even though it is not recommended by most people.
Just insert a space character before each : separating the parameters, and the compiler is happy:
- (void) method:(id)a :(NSString*)b
On page 16 "Message Syntax" of The Objective-C Programming Language
this is called an "unlabeled argument", or an "argument without keyword".
Of course you can change it to
- (void) method:(id)a withB:(NSString*)b
but this changes the selector to method:withB:.

Is there an Xcode version of "Override/Implement Method"?

This is one of my favorite eclipse features. Does it exist in Xcode? I'm getting tired of cutting and pasting from my header files in to my implementations.
Just type "dash" then "space" and start typing the method name that you want to override. Now push Esc.
Example:
- tab
will prompt your to pick a method that overrides any of the TableViewDatasource / Delegate methods. Hit Return and it will automatically provide the return type too...
Here's a pic of what it looks like and notice that I did not provide the return type myself:
Cheers...
This is the kind of task that a user script is useful for. I use this one I banged out in ruby.
#! /usr/bin/env ruby -w
dash="------------------------------------"
r=/(^.+);/ # find entire function definition
pr=/(\w+(:|;))/ #find named parameters to make selector style string
s=STDIN.read
s.each_line() do |l|
m=l.match(r)
if m
n=l.match(/:/)
if n #if the function as one or more parameters
params=l.scan(/(\w+:)/)
puts m.captures[0] + "{\n\n}//"+dash + params.to_s + dash +"\n\n"
else #method has no parameters
puts m.captures[0]+ "{\n\n}//"+dash + m.captures[0] + dash +"\n\n"
end
end
end
To use, select a header method definition, run the script, switch to implementation and paste. This one adds in my preferred method comments boiler plate so you can customized that as you wish.
Check out Accessorizer, it may not be exactly what you're looking for, but it could help in other things that you may like. I haven't used it extensively yet, but I got it as part of one of MobileOrchard's bundle.
Take a look at the ODCompletionDictionary plug-in for Xcode. It allows you to define expandable macros that are configurable with many options. It is an enormous time saver.
With Swift, pressing CTRL+SPACE in the class body will bring up auto-complete for methods. Just start typing the method name.
If you're extending a class, XCode 10 doesn't seem to automatically insert override when necessary.

VB.NET - How to get the instance used by a With statement in the immediate window

VB.NET has a very handy "with" statement, but it also lets you use it on an unnamed variable, like this:
With New FancyClass()
.Level = "SuperSpiffy"
.Style = Slimming
.Execute()
End With
Is there a way to get at the "hidden" instance, so I can view its properties in the Immediate window? I doubt I'll get it in the watch windows, so immediate is fine.
If you try to access the instance the same way (say, when .Execute() throws an exception) from the Immediate window, you get an error:
? .Style
'With' contexts and statements are not valid in debug windows.
Is there any trick that can be used to get this, or do I have to convert the code to another style? If With functioned more like a Using statement, (e.g. "With v = New FancyClass()") this wouldn't pose a problem.
I know how With is working, what alternatives exist, what the compiler does, etc. I just want to know if this is possible.
As answered, the simple answer is "no".
But isn't another way to do it: instead of declaring and then cleaning up the variable is to use the "Using".
Using fc as new FancyClass()
With fc
.Level = "SuperSpiffy"
.Style = Slimming
.Execute()
End With
End Using
Then you can use fc in the immediate window and don't have to remember to write a
fc=nothing
line.
Just some more thoughts on it ;)
What's wrong with defining a variable on one line and using it in a with-statement on the next? I realise it keeps the variable alive longer but is that so appalling?
Dim x = new SomethingOrOther()
With x
.DoSomething()
End With
x = Nothing ' for the memory conscious
Two extra lines wont kill you =)
Edit: If you're just looking for a yes/no, I'd have to say: No.
I hope there really isn't a way to get at it, since the easy answer is "no", and I haven't found a way yet either. Either way, nothing said so far really has a rationale for being "no", just that no one has =) It's just one of those things you figure the vb debugger team would have put in, considering how classic "with" is =)
Anyway, I know all about usings and Idisposable, I know how to fix the code, as some would call it, but I might not always want to.
As for Using, I don't like implementing IDisposable on my classes just to gain a bit of sugar.
What we really need is a "With var = New FancyClass()", but that might just be confusing!
You're creating a variable either way - in the first case (your example) the compiler is creating an implicit variable that you aren't allowed to really get to, and the in the second case (another answer, by Oli) you'd be creating the variable explicitly.
If you create it explicitly you can use it in the immediate window, and you can explicitly destroy it when you're through with it (I'm one of the memory conscious, I guess!), instead of leaving those clean up details to the magic processes. I don't think there is any way to get at an implicit variable in the immediate window. (and I don't trust the magic processes, either. I never use multiple-dot notation or implicit variables for this reason)