Adding your own custom macros like "sout"in IntelliJ - java

I really like to be able to type sout and get System.out.println. In fact, I like it so much that I want to add more of these. For example, fb could be final boolean.
How do I add that?

Like said in the comments, these macros are called "Live Templates".
To create one:
open the settings menu
select the "Live Templates" entry in the left menu
select the category where you like to create your new template
press the "+" button on the right hand side of the settings menu window
add the abbreviation, "fb" in your case
add a description
... and then the text:
final boolean
($END$ can be omitted here)
Now select the context (link is beneath the text field) where this template will be used and recognized by IDEA
I've used the contexts Java Statement, Expression, Declaration and Other (but "Other" isn't shown in the list of contexts).
It could like this:
To use it write fb on the appropriate place and either wait until IDEA suggests to use the template or press ctrl + space to activate the auto complete, it should create:
final boolean
You may find some more information about creating and editing live templates on the official help page: Creating and Editing Live Templates

Related

How to document runtime of a function with Javadoc

I'm want to document the asymptotic runtime of a function, as it will be used for algorithm-engineering for graph problems.
What's the most idiomatic way to do this? Is there a way to create new tags in Javadoc like #return or #author?
I provided an example below, which is the method to delete a vertex in the graph.
/**
* Runtime: O( degreeOf(v) ) because every neighbour of [v] also needs to be updated.
*/
fun deleteVertex(v: V): SimpleGraph<V> {
if (v in m.keys) {
for (nb in m[v]!!)
m[nb]!!.remove(v)
m.remove(v)
}
return this
}
To create custom tags for Javadocs, simply follow these instructions.
You can create other customizations for Javadoc. For example, in Eclipse, you can create "templates" so that when you create new classes or add new methods, the IDE automatically apply this template to add a (Javadoc) comment formatted in the prescribed style of the template you applied. You can save these templates in an XML file so that you could share it with other members of your team. I am sure that IntelliJ and other modern IDEs will have similar features. I am just more familiarized with Eclipse. Here is a video I created many years ago on how to create a Code Formatter in Eclipse. If you advance to the 1:48 mark, you will see "Code Template" right above the "Code Formatter" option I selected in the video. Creating a code template is much easier than a formatter.
To do this, simply click on Windows > Preferences menu to get the Preferences popup. There, select Java > Code Style > Code Templates. in the right pane, expand Comments and select the component you wish to create a comment template for, for example Methods. Click Edit button and format the Javadoc comment to your liking. Obviously, you will have to do a bit of research to get really creative with your comments. For example, you might need to figure out how to use system variables or create your own. For example, in the image below, I made use the year variable to apply the current year whenever I create a new class.
Once you finish with all your template customizations, simply click the Export button and use the File Chooser dialog to save the file wherever you would like.
One last tip, if you need to embed code snippets in your Javadocs, you can follow the recommendations in this article. I do this very often. Sometimes I find it useful to embed a few lines of code to illustrate different use cases for the method. That way, other developers can see how to use the method in the correct context.

Is it possible to tweak the settings so it automatically places quotations when using System.out.println()?

I am using java on Intellij and I wonder if it is possible to make it so that it automatically places quotations System.out.println(here);
Thanks
Yes, it is possible.But a bit tedious. (EDITED)
ECLIPSE
you need to know that Ctrl+space opens options for autofill.
you need to create a simple template at
Window>Preferences>Java(dropping menu)>Editor>Templates>New...
In the open window at Name type: Syso
at Pattern: System.out.println("")
Make sure the checkbox Automatically insert is on.
Apply and close
Now every time you type Syso and hit Ctrl+Space there will be first option
of autofill with quotations. Profit.
oof, didn't notice about Intellij , my bad. Thought about Eclipse. Well here is one for Intellij
INTELLIJ
I believe the Intellij version of cntl+space is Tab button
Go to Settings>Editor>Live Templates
Pick any template and press Plus icon. Choose Live Template
In abbreviation type your desired word (I would put: Syso)
In Template txt type : System.out.println("")
Make sure the box with Reformat according to style is checked
Now every time you type Syso you can hit Enter and have PROFIT!

How to extend Eclipse's Java editor to recognize my own elements

I want to add references to other elements inside javadoc. The javadoc should look similar to:
/**
* ...
* #my.tag SomeElement
*/
I want to reuse the existing Java editor inside Eclipse. Of course I can type above javadoc already, however I'd like to add some features:
Navigation: upon Ctrl-Click / F3 I want to navigate to the editor showing SomeElement
Auto-completion: Ctrl-Space should complete the text / show valid alternatives
Checking: I want to create a warning marker and underline the SomeElement if it cannot be found
I already figured out that I can do the auto-completion by using the extension point org.eclipse.jdt.ui.javaCompletionProposalComputer, though this seems to be little more complex than I had hoped, so I might be on the wrong track here?
For checking my elements I might be able to use org.eclipse.jdt.core.compilationParticipant, which can generate markers upon compilation which afaik happens with every save operation.
But how can I can manage the navigation feature? Any pointers are welcome!
For navigation you can provide a Hyperlink Detector. You'll want to look at how the Java Editor and its existing detectors are declared in the org.eclipse.jdt.ui plug-in to determine the correct target ID.

How to add refactor option to my custom editor?

I have my own custom editor and now I want to add refactor option to this editor. I know I can add a new menu item for the same, but I want to use the existing refactor option. Like when user selects text and if it is valid variable (in Java) the all other variables are highlighted and later it is replaced. Also, I want to reuse the same refactor option and not provide new menu item.
Any pointers on how to go about this?
I followed the link: Eclipse rename/refactoring override
Now I get the notifications during each refactor but for my custom editor refactor -> rename menu is disabled. How do I enable it?

Jump-To-Code-Line Eclipse Shortcuts

is it possible, in Eclipse, to mark certain lines with Shortcuts and be able to quickly jump to those lines?
Example: Let's say I have maintenanceHeavyMethod() at line 120 in my class, gameLoop() at line 800 and some listener at line 1460.
I'd like to f.ex. press CTRL+SHIFT+1, 2, 3 etc. to mark those positions, and then use f.ex. CTRL+1, 2, 3 to immediately jump to them. I don't like split-screens etc, but I need to jump around when writing.
Is there such a feature?
I'm using latest Eclipse to write Java-programs.
You can add Bookmarks in your code. Select the code fragment you want to bookmark and then go to Edit > Add Bookmark... (also possible via the menu available with a right-click in the left hand column of the editor, like breakpoints).
Then, add the Bookmarks view. Select Window > Show View > Other... > Bookmarks and you'll get something like this:
Sadly, I don't think you can bind a shortcut to a particular bookmark.
Just in case, the shortcut to jump to a particular line is CTRL+L.
That would be best taken care by mylyn:
Define a task with this three method, and you will be to see only those 3 in the package explorer view
To jump to a particular method, I use ctrl+o and then start typing the method. If you're a proficient typist, this shouldn't take any longer; keep in mind that you only have to start typing the name of the method.
Unfortunately this isn't a proper solution for jumping to a line of code within a method.
You can jump to methods by using the outline view. Outline view shows all the Methods, Fields, and Inner-Classes (and their methods and fields and inner-classes...etc) of the source file you currently have open. I personally prefer this method, unless I have a stacktrace and know which line I want to jump to, in which case I use ctrl+L.

Categories