Intelllij IDEA Ultimate 2018.1 groovy code completion does not work - intellij-idea

I can create a Groovy project and even run it. But when I type main or print there are no auto-complete / code completion suggestions at all. I have groovy-2.4.1 in the external library folder along with Java 1.8. I find Intellij frustrating and non-intuitive to use.
In a groovy script main does show correct code completion but when I click enter it puts "main()" without the String[] args that I can see in the suggestion.

You have created a Groovy class where code has to be embedded into a class to be executable and where only class member declarations are expected (e.g. functions, fields). Select to create a Groovy script (or just delete class declaration) to be able to write executable code in top level file. See also Scripts versus classes.

I was having the same problem when I was in a rush and forgot to add a method in which to write the code. Just type 'psvm + Enter' inside the class. This will create a main method in which code completion will work. Also, you can remove the public keyword as classes and methods in Groovy are by default public.

Related

Intellisense - deduce right Class by its methods

I'm used to work with NetBeans and now I'm trying IntelliJ. So my question is: Does IntelliJ has a way to get right class by its methods?
For example, in NetBeans if I write:
glGenBu // Intellisense will kick in and will suggest me
to use GL15.glGenBuffers() method from GL15 class
This will automatically import the right library.
This is very handy because I'm working with LWJGL and it has a bad way to manage OpenGL methods ('GLXX' where XX is the version of OpenGL and all methods that appeared in that version are stored in that class) and I never remember the right class where my desired method is.
Thank you.
Pressing Ctrl+Space when you already see completion list will show more suggestions, with static methods from the entire project among them. See https://www.jetbrains.com/help/idea/auto-completing-code.html for more details.

Listener in Testng

I have created a listener class in my project. Now what I have read in web there are two methods to add a listener either to your class or to testng file.
Please check in the folder structure. For now, I m using it like this in my java class but it is giving me error as in image attached:
#Listeners({Test.tes.Utils.ListenerUse.class})
From your screenshots, I understand that your test class HomeTestCases.java resides in the package Test.test.MyHomepageTestCases in src/test/java
You are trying to refer to the listener ListenerToUse.java that resides in the package Test.tes.Utilsin src/main/java.
When you use #Listeners(Test.tes.MyHomepageTestCases.MyHomepageTestCases.ListenerToUse.class)
Here you are basically telling Java to look for the listener class ListenerToUse inside Test.tes.MyHomepageTestCases.MyHomepageTestCases (as a nested class), but that's not the case because ListenerToUse is NOT a embedded class inside MyHomepageTestCases.java.
To fix the problem please do the following:
Change: #Listeners(Test.tes.MyHomepageTestCases.MyHomepageTestCases.ListenerToUse.class)
To: #Listeners(Test.tes.Utils.ListenerToUse.class)
I think the issue is you're mixing the package of your Listener, and the package of the class where you want to add it.
Your code now:
#Listeners(Test.tes.MyHomePageTestCases.MyHomePageTestCases.ListenerUse.class)
Correct package:
#Listeners(Test.tes.Utils.ListenerUse.class)
However, you've named your package "Test", which is (besides breaking Java naming conventions -> https://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html) clashing with the TestNG Test class. On your screenshot, I can see that "Test" is grayed out. If you hover with your mouse over it, you'll probably see Eclipse has resolved it as "org.testng.annotations.Test". So you have two options:
Name your packages properly and try again
Expand the imports sections of your HomeTestCases class; manually add:
import Test.tes.Utils.ListenerUse;
and delete the following:
import org.testng.annotations.Test;

switching between languages in same file

I recently attended a user group meeting where the IntelliJ representative was demonstrating version 13.
He demonstrated how to switch the code completion view of a file. I do not exactly remember what the file extension of this particular file was, probably java.
The concept was that if the file is html with embedded javascript he could then switch the code completion between html and javascript with a shortcut. If he says treat the file as html then all code in file was treated for code completion purposes as html, and vice versa for javascript.
Does anybody know what shortcut he might have been using to enable the language switch?
Sounds like you may be referring to the IntelliLang feature. IntelliJ IDEA can be aware of other languages embedded within a file.
A simple example is in an HTML file that has CSS and JavaScript.
Notice when I am inside the HTML markup:
or inside an HTML element:
The code complete shows HTML completion options. However, when I am inside the style attribute, I get CSS code completion:
I also get CSS code completion if I am inside a <style> element. So even though I am in an HTML file, I see CSS code completion because of my location.
Same case with JavaScript. When I invoke code completion inside a <script> element, I get JavaScript completion, even though I am in an HTML file.
Anytime IntelliJ IDEA can determine that another embedded language is present, it provides, via IntelliLang, the appropriate syntax highlighting, error highlighting, and code completion. The same holds true for Java. Notice here that IDEA knows the method I am competing takes an SQL statement and therefore highlights the String value using SQL highlighting, and provides SQL code completion:
So even though I am in a .java file, I get SQL code completion. The reason is that IntelliLang comes pre-configured knowing the embedded language of some methods. You modify them, or add more, in File > Settings > [Project Settings] > Language Injections.
In addition, you can use an annotation to tell IntelliJ IDEA (as well as developers looking at the code) that a String must be valid in a particular language. For example, I can annotate a String field, variable, or parameter, to indicate it must be valid HTML:
Notice I get HTML syntax highlighting, HTML code completions, and the CSS color shows in the left gutter. If I annotate a method parameter, then any time I call the method, I get the appropriate syntax highlighting, code completion, and error/warning highlighting:
The #Language annotation is inside the annotations.jar that is contained in the redist directory inside the IntelliJ IDEA installation directory. It is also available in maven central, or IDEA will offer to attach it as a Library if you use the annotation without it being attached.
IntelliLang and the #Language annotation supports a large number of languages. Just use code Completion inside the quotes after typing #Language("") to see a list. (Inline search works in the list as well.) One of the most useful is Regexp. For example, if you have a method that expects the string passed in to be a valid Regular Expression, annotating it as such will give anyone that calls it Regex code completion and error highlighting if they are passing in an invalid Regex pattern. Even for developers using other IDEs it is useful as a form of documentation.
As for a shortcut to change the the language on the fly for code completion, the only thing I can think that you might be referring to is the "Inject Language" intention. If I am entering a String value, and I bring up the quick-fix/intention menu via Alt+Enter, I am given an option to inject a language:
If I select that, IntelliJ IDEA will ask me what language I want to use:
After making my selection, IntelliJ IDEA will give me temporary language injection (including code completion) for the selected language.
It also gives me an option to add the #Language annotation for permanent injection.
To the best of my knowledge (as a 10 year IntelliJ IDEA user) that is the only way to switch code completion language types. So hopefully that is what you are looking for. To me, IntelliLang is one of the coolest features in IntelliJ. (It actually started as a third party plug-in and JetBrains then absorbed it into the product.)

Adding custom code generator

When I work with certain types of files, such as: Java file, HTML file or Jasmine Test file I can generate some useful code snippets using Code > Generate option, for example:
if I am working with Java file Code > Generate allows me to insert getter, setter, constructor etc
if I am working with HTML file Code > Generate allows me to insert an XML tag
if I am working with Jasmine Text file Code > Generate allows me to insert a scaffolding of a test suit or a singe test case
I was wondering if (and how) I can add my own 'generator'. I know I can use Live Templates, but I like the fact that Code > Generate gives me a quick list of all available generators.
Yes, you can do it by writing an IntelliJ plugin and extending this class:
com.intellij.openapi.actionSystem.Action
If you create an intelliJ plugin project (just google intellij plugin developmentfor information on how to get started), hit alt-enter somewhere in your project source tree and select Action, you will get a dialog which allows you to configure where your action should appear.
You want to place it in relation to another action which already exists, for example right below it. In your case - have a look at the menu group named GenerateGroup (Generate).
Once your action is defined in this manner in your plugin.xml, build and run your plugin in the sandbox.
Now, when your action is triggered, the AnActionEvent will be fired which contains references to all the necessary information you need (current project, file, position of cursor within file, psi tree, etc).
Try to get this working so far and come back with any specific questions.
Good luck!

Running single test with TestNG in Intellij IDEA

How do you guys run single tests with IDEA's TestNG plugin? I tried creating a focus group, but it seems the TestNG plugin does not resolve dependencies to other tests automatically. Even when I comment out dependsOnMethods, it seems #BeforeMethod is not run.
Advice is greatly appreciated,
Robin
If you mean a single test method inside a test class there is a right-click option called Run "testMethod" where testMethod is the actual method name.
The short cut is ctrl+shift+F10.
If you put the cursor between methods (even outside the javadoc) or outside the entire class it will test all methods in that file.
Bring the class into focus in the editor, and then simply right-click, select run?