IntelliJ shortcut refactor object to list of objects - java

Lets say I have this method signature:
public String get(){};
Is there any shortcut in IntelliJ to highlight String and surround it with List<> So I can get the following:
public List<String> get(){};

You can make your own Surround Live Template:
Go to Settings/Preferences > IDE Settings > Live Templates > surround.
Add a new template.
Fill in the form
The abbreviation is used to select from the Surround With context menu.
The Description will appear in the Surround With context menu.
The $SELECTION$ variable is predefined as, you guessed it, the selected text.
You can add your own variables, such as $COLL$ to make the template more generic.
Set the Applicable in... to all of Java (or you can be more exact if you want).
To use it:
Select text.
Press Ctrl+Alt+T on Windows or ⌥⌘T on Mac
The Surround With context menu will appear with your new template.
Press C (since that's the first letter of the template's abbreviation) to quickly select the template.
IntelliJ blog post about this feature: "Surround with..."

Thanks for the answer and this is how we can create a variable :

Related

Eclipse - Enclose selection instead of replacing

In Eclipse, when i select part of a text and press any key, the whole selection is replaced.
I would like to know how to make it, instead of replacing the text, add the key pressed to both ends of the selection, as it happens on visual studio code.
Example:
System.out.println(HelloWorld);
Select Hello World
System.out.println(HelloWorld);
Then press the " button. The code will turn to
System.out.println(");
Can i config the IDE so it will instead change it to
System.out.println("HelloWorld");
There is a already answered question like this one, but considering that one as 7 years and a lot of version old, and also considering that the answer on that one is kind of a work around, i would like to know if there's any new plugin or configuration that make it work better
From what I gather, you are wanting to do something like this?
Go to Java > Editor > Templates and add a new template, e. g. called quote, as follows:
"${word_selection}"${cursor}
Then, in the editor, write a text you want to quote, select it, press Ctrl+Space, type quote and hit Return. The highlighted text should be quoted now.
If you don't get template proposals when pressing Ctrl+Space, make sure you have them checked in Java > Editor > Content Assist > Advanced.
Just tried it and it works for me!
You can use this method for other things, like (${word_selection})${cursor} for parenthesis or '${word_selection}'${cursor} for single quote.

Remove space after if/for statement - Eclipse [duplicate]

When I reformat code with Eclipse, it turns method headers like this:
public void myMethod(String param) {
into method headers like this:
public void myMethod(
String param) {
When I was brought on here I'd never used Eclipse before, and I imported project settings provided by someone else. I have seen that on small new projects I've worked on Eclipse does not do this, so it must be in the settings I've imported. But I've gone through every panel I can find, as well as every hidden file I can find in the workspace, and I can't figure out what is causing this.
How do I turn it off? I don't want a newline before parameters in my method signatures, and I can't imagine why anyone would!
Have a look at Window>Preferences>Java>Code Style>Formatter.
There you can configure almost everything. Your case is found at
Line Wrapping>Method Declarations>Parameters.
In my version of Eclipse, I found the option under Window->Preferences->Java->Code Style->Formatter, then clicked the Edit.. button.
In the new window, go to the Line Wrapping tab and find Method Declarations. You want to change the Line wrapping policy. The Force split check box seems to do the same.
Go into preferences: Java -> Code Style -> Formatter
Restore Defaults or edit what's there.
Additionally, Code Style -> Code Templates will allow you to define generate code formatting.
Note that this is for Eclipse Workspace as a whole, the same Preferences can be accessed under the project preferences if you want to get more fine grained at a per project level.
In eclipse you can define your code formatting use Code Style ,
for MAC System :--> Eclipse --> Preference -- > java--> code Style
for Window System :--> Window->Preferences->Java->Code Style->Formatter
The best way to change your format is to go to Window-> Preferences-> Java-> Code Style-> Formatter. Inside the formatter window, eclipse has a couple of built in styles, but the best way is create a new profile of your own by pressing on New. You can change the format as you please on the edit tab
You can change parentheses and curly braces positioning along with other stuff.

IntelliJ replace variable names in multiple lines

I am wondering is there a shortcut that can replace a variable name with a new name on multiple lines in the current Java file for IntelliJ. Is it just simply doing a find and replace operation, but it can't really solve the problem obviously.
Try to set cursor on variable and use shortcut Shift-F6 - after that there are dialog frame for change name of variable and it changes all occurences of variable to setted name.
Also, you can use shortcut Ctrl-Shift-R for replacing any word in project, it is useful if you working with huge project or your variable appears in config files.
Steps:
1: Put the cursor on the variable
ex:
Press shift+f6
Enter the name you want in the appeared dialogue box
ex:
Hit the Refactor button
Select your variable by right click > select refactor > rename
And rest all will be taken care by ide itself

Eclipse parameter dialog/hint when using functions in code

By default In Eclipse when you use a function or create objects it helps with parameters like this:
But once it's done, it'll never show up again. Is there any way to call this parameter helper on code that's already written when I point the cursor on the method?
The Image I uploaded only appears while writing code.
Ctrl + Shift + Space will do the trick. It is named Context information in Key preferences.
Place the cursor just inside the left parenthesis and press Ctrl+Space (Command+Space on Mac) again; Eclipse will show Content Assist again.
When the mouse cursor is in the paranthesis, click Ctrl+Space. It will pop up menu (content assist). If you enter it, it will show the context information as it was at the beginning.
You can't get place holders again once they disappeared. But you can get content assist help as said by #E-Riz
Check this answer What is Eclipse shortcut key to turn on feature that allows when pressing TAB key, for cursor to go to expected position? and also this What's this box around my function input?
i was looking for the same shortcut and didn't find it ;
now i'm using
*ctrl+Arrows to move between words
*alt+shift+arrows to select the world and replace it
JDT-Codemining is a new project (as of Aug 2018) that supports parameter hints, along with many other features, such as:
General
Show references
Show implementations
Show method parameter names
Show method parameter types
Show end statement
JUnit
Show JUnit status
Show JUnit run
Show JUnit debug
Debugging
Show variable values inline while debugging

Eclipse Java shortcut for "printf"?

When coding in Java in Eclipse, is there a shortcut similar to typing "syso" and pressing CTRL+Space for System.out.println(), but for printf instead of println?
you can create your own using eclipse template functionality.
To create your own template take a look here Window->Preferences->Java->Editor->Templates.
For example the sysout functionality has this template:
System.out.println(${word_selection}${});${cursor}
you can create similar for System.out.print()
You can make your own for printf:
In preferences choose Java->editor->templates.
I don't see one in the default completion templates, but you can create your own template
I created my own System.out.printf() shortcut on Eclipse.
Simply, go to Windows > Preferences > Java > Editor > Templates
Click on New, then fill the form with the following information:
Name: the shortcut you would like to use in the editor (e.g. printf)
Description: print formatted text (or any other description)
Pattern: System.out.printf(${word_selection}${});${cursor}
That's it! Now you can just type "printf" in your editor, followed by [Ctrl + space], then [Enter], and the editor will place a System.out.printf() for you.
If you name the new Templates as pzf (you can also try different combinations and see if that doesn't match any other Template names) it will print the statement right away, without having to press enter. Also, I added inverted commas and commas.
System.out.printf("${word_selection}${}",);${cursor}

Categories

Resources