IDEA: "Assign statement to new local variable"? - java

As a long time Eclipse user, I'm playing around a little bit with IntelliJ IDEA 10. I can't seem to find out how to perform an "Assign statement to new local variable" code completion.
Feature explanation:
I type something like
new BufferedOutputStream(out)
and then hit Cmd (or Ctrl)+1 and enter, and Eclipse changes the line into:
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(out);
At the same time, I can type over "bufferedOutputStream" immediately to rename it (or select from the options "bufferedOutputStream", "outputStream" and "stream" from a dropdown).
I use this feature for absolutely every assignment, and it's an enormous time saver - this must be available in IDEA, too - but where is it hidden?

Refactor | Introduce Variable (Ctrl+Alt+V on Windows). Note that you don't need to select the text if it's the only text in the current line. Then you can change the variable name in-line just like you've described and press Enter to complete editing.
Another way is to use the Postfix Completion:
Type .var (or just .v to select it from the list) and confirm it with Enter.

As CrazyCoder mentions you can use Ctrl+Alt+V. Also instead of selecting the expression, clicking into somewhere in your expression and using Ctrl+W to expand scope is very useful while using introduce refactorings. Extract refactorings are:
Extract variable: Ctrl+Alt+V
Extract field: Ctrl+Alt+F
Extract method: Ctrl+Alt+M
Extract parameter: Ctrl+Alt+P
Extract constant: Ctrl+Alt+C
Also, Idea is a polygot editor so you can use these extract refactorings for other file types like js or html also (not all refactorings work in all file types but Ctrl+W works mostly).
There are more extract refactorings which do not have shortcuts which you can access from Refactor|Extract menu (both menu bar and context menu). To quick access all refactorings you can use Ctrl+Alt+Shift+T for a popup menu.
As a last word, I highly encourage you using "Tip of the Day" (Help|Tip of the Day). It is a fast way to learn many helpful features of Idea.

It's not as nice as Eclipse, but you can try the following:
new BufferedOutputStream(out)
Select the expression above, either with your mouse (or by using Ctrl+W).
Then hit Ctrl+Alt+V to Introduce a Variable or (Ctrl+Alt+F to Introduce a Field)

Easiest is, hit Alt+Enter, you will be offered with a list of options, and just select "Introduce local Variable".

Related

Alter order of content assist eclipse for android development

I am working on an Android app, using Eclipse 4.2. I have content assist set up almost as I want it. However, when I, for example, start typing int to create a Java int variable, the Android.R.Integer entry in content assist is the primary entry.
I have content assist set up by relevance. This is just an annoyance because every time I want to make an int variable, I have to either hit Esc or hit enter and delete "eger" from the end of the type.
Thanks in advance.
None of the solutions posted here really worked, so what I did was to go to
Java -> Appearance -> Type Filters
and add *int* (yes, it's an asterisk followed by int and then another asterisk).
There's a checkbox next to the filter you just added, make sure it is checked.
Problem solved (finally!)
You can use Preferences->Java->Appearance->Type Filters to filter out the suggestions you want to ignore.
I'm wondering why you really need content assist to create an int variable, it would be easier to simply type it. But if you really want to, you can type the integer value and then use the Ctrl-2-L shortcut to create the local variable.
In
Java->Appearance>Type Filters
click add
type 'int*' -> click ok.
Then when you type in int , the first is int instead of integer .
Cren I am in the same boat. I have the content assist set to auto activate with every letter because I find it extremely useful that way, but have the same int(eger) problem that you do.
I have come up with a few ways to deal with it and thought I would share them in case you find one of them useful.
You could use a Type Filter as ralph suggested works just fine, you just don't get the assist when you actually want Integer, which isn't that big of a deal.
You could delay the content assist long enough so that you can type "int" and enter before it pops up, giving you a delay always. (In Preferences->Java->Editor->Content Assist there is an Auto activation delay setting)
Or remove 'i', 'n', and 't' from the Auto activation triggers (again in Preferences->Java->Editor->Content Assist). This is the method I am currently trying out, and it seems to work with everything, even Integer, saving me from having to type "ger" :).

editing a string using BufferedReader in Java [duplicate]

Say I allow the user to edit something, like the phone number in an Address Book (actually, that's exactly what I'm doing). Is there something that I can add to println that will allow me to insert a variable to display as fully editable text? The assignment that I'm doing this for doesn't actually call for this, but I think it would be cool to have. I'm looking on Google but can't find anything, then again I don't really know what I'm looking for and whether or not I have the correct terms in mind ...
No, not using only what Java provides in the framework. Editing some text would require to
act on key press, which is not possible as in Java the input is buffered (i.e., wait for Enter to be pressed)
to move around in the text you output, which is also not possible
This could be done using some native code (ncurse on linux, ...), using JNI or JNA, but not that easily.
Note that there are some projects that aim to add those functionalities, so if you can use something outside of the core libraries, you could give them a tries... for instance http://code.google.com/p/java-console-api/
There are various options for this, in order of simplicity and portability to features and complexity:
Simply prompt for the information, reading a complete (return-terminated) line of response, and allow the normal terminal input facilities to be used for basic editing.
Use something like the gnu readline library to allow more advanced editing. You still won't have widgets (text input boxes at specific places on screen) as such though. There's a java implementation here: http://java-readline.sourceforge.net/
Use something like ncurses to specifically position the cursor, print text labels, handle keypresses, and implement your own text input box. Not fun.
Use a textual user interface library (TUI), like this one: http://www.bmsi.com/tuipeer/
If you opened a window that looks like the console window, and could react to keypress events, then you could do what you are asking, but, otherwise, if you are just running a program, the program will have ceased executing and returned control to your console, so it can't do anything else.
But, if you use a scriptable version of java you could write your own shell, and then you could do what you are asking, as the shell would not cease executing.
But, that will probably be beyond your course.

Eclipse shortcut to insert variable in string literal

When I need to interpolate a String variable into a quoted string literal in the Eclipse java editor, I generally type "++" and eclipse usually inserts another " after the latter one, so I then have to go past it with arrow keys and then backspace to remove the duplicate, then go insert some spaces in between the "++" and type my variable name.
This seems like a lot of work. I have found Practically Macro, but I thought there might be something built in for this situation which is very common for me.
I'm not sure if this shortcut exists, try doing CTRL+SHIFT+L when the string is selected.
Also there is this link which has a whole bunch of nice keyboard shortcuts, I know it may not answer the question completely but maybe it'll get you one step closer. :)
**** Edit: In that link the "Ctrl-2 something" post looks promising.****
The most relevant built-in feature is maybe the "Pick out part of a string" quick assist:
insert a space (or anything else) where you want to add a variable,
select it,
press Ctrl+1,
select the "Pick out part of a string" quick assist,
insert your variable name.
Reference: "Pick out part of a string" section in this post.

Eclipse IDE search a function with given name

How to search a partifular function in a particular project in Eclipse
e.g. if I want to search a function with name xyz how do I do it?
Use "Java Search" in the search menu or move the caret on the function name in the source and press Ctrl+Shift+G (-> search for reference in Workspace).
[EDIT] If you don't know in which class the function is, then use the text search (which is called "File search"). Enter the name in the search field and then limit the search to "*.java" files.
If you don't know where the method is located, use text search with a root of your whole project, working set or even the whole workspace if you've really got no idea where it might be; Search → File… brings up a dialog which lets you enter exactly what you are looking for. You'll have to sort through all the things it finds (using the Search window) to identify the real match from among the false positives, but that's better than nothing.
You can write some code that uses that function and then use the [Ctrl] + [Click] trick to go to its implementations.
Check out nWire for Java. It has a quick search feature: start typing and it will instantly show results which include types, interfaces, methods, fields and other artifacts. It has a lot more features.
In Eclipse Neon 4.6 there's CTRL+H (from the menu Search -> Search) then we can use either the "File search" or "Java Search" tabs. both are very useful

Is it possible to print text that can be edited by the user (for console programs)?

Say I allow the user to edit something, like the phone number in an Address Book (actually, that's exactly what I'm doing). Is there something that I can add to println that will allow me to insert a variable to display as fully editable text? The assignment that I'm doing this for doesn't actually call for this, but I think it would be cool to have. I'm looking on Google but can't find anything, then again I don't really know what I'm looking for and whether or not I have the correct terms in mind ...
No, not using only what Java provides in the framework. Editing some text would require to
act on key press, which is not possible as in Java the input is buffered (i.e., wait for Enter to be pressed)
to move around in the text you output, which is also not possible
This could be done using some native code (ncurse on linux, ...), using JNI or JNA, but not that easily.
Note that there are some projects that aim to add those functionalities, so if you can use something outside of the core libraries, you could give them a tries... for instance http://code.google.com/p/java-console-api/
There are various options for this, in order of simplicity and portability to features and complexity:
Simply prompt for the information, reading a complete (return-terminated) line of response, and allow the normal terminal input facilities to be used for basic editing.
Use something like the gnu readline library to allow more advanced editing. You still won't have widgets (text input boxes at specific places on screen) as such though. There's a java implementation here: http://java-readline.sourceforge.net/
Use something like ncurses to specifically position the cursor, print text labels, handle keypresses, and implement your own text input box. Not fun.
Use a textual user interface library (TUI), like this one: http://www.bmsi.com/tuipeer/
If you opened a window that looks like the console window, and could react to keypress events, then you could do what you are asking, but, otherwise, if you are just running a program, the program will have ceased executing and returned control to your console, so it can't do anything else.
But, if you use a scriptable version of java you could write your own shell, and then you could do what you are asking, as the shell would not cease executing.
But, that will probably be beyond your course.

Categories