Opening an editor for a Java class that is within a jar (with source included) - eclipse-plugin

I am working on an eclipse plugin, and I'm trying to implement F3 (Open Declaration) functionality. After finding the project that contains a class, I use findType to get the IType of the class. If it a .java file in my workspace, I can use getResource to get the IFile, and then open its editor (as is described here).
However, if it's a BinaryType, then getResource returns null. I can get the class file using getClassFile, but when I use its getResource I also get null. If I try using its getPath method, I get an IPath to the jar file which contains it.
There must be some way to reach the source file, and open it in an editor, but I can't seem to find it.

Use
JavaUI.openInEditor(IJavaElement element);
to open all Java elements (such as your IType).

Related

IntelliJ macOS Mojave - Moving Java Class into another Package does not consider JSON Files

I am using IntelliJ 2018.3.4 with default settings on macOS Mojave 10.14 for a spring boot project containing JSON files for data population. Refactoring Package names for Java-Classes (move a class into existing package) does not consider JSON files.
IntellJ Moving Java Class in another package
I've already tried to change the file extension into *.txt or *.html, after this change the refactoring seems to work. Changed package name occurs in those files.
Java class: com.test.MyClass
Json file (src/main/resources/file.json):
{
"name" : "com.test.MyClass"
}
After changing the Package name into "com.test2.MyClass", the "name" property in src/main/resources/file.json is expected to be changed into the same value "com.test2.MyClass".
When you rename a variable/property/class/method/package in IntelliJ IDEA, you're given a choice to search over strings and text occurrences. This will do the trick
To change the scope, use the Preview button.
To move a class to another package, use the Move refactoring action.

Creating class inside package A of A.B in IntelliJ

I have a maven project in IntelliJ IDE. I have created a package with name event.handlers. After which I created multiple classes inside this package.
There are no classes inside event everything is inside event.handlers.
Now I want to create a java class inside package event.
Is there any way from the IDE I can do that?
Currently, I am manually creating the java file inside the event folder in my code repository.
In the left Project structure pane, there is Settings button, in that please Uncheck, Compact Empty Middle Packages.
The settings will be like
.
Now you will have a tree structure, where you can right-click or (ctrl + enter/ alt + insert) on the package and create the file.
For example,
Try creating a Java Class at src folder named event.NewClass. This will create a NewClass.java inside src/event.
You can create folder with the dot symbol . at creating a Java Class or a Package, or Kotlin File/Class when Type is not File. For example create a Package with name com.company.example at src folder will automatically generate the folders recursively, src/com/company/example, create a Java Class with name com.company.example.AClass will automatically generate a file AClass.java inside the automatically generated folder tree src/com/company/example.
It might be easier to just create the class inside package AB and Refactor > Move to place it inside of A.
It seems that all other answers here are workarounds anyway, so if it's just a one-off, this might be the easiest option.
However, in my case, after doing this I had to "Invalidate Caches and Restart" before IDEA updated the UI properly.

IntelliJ does not recognize kotlin file after deleting it and recreate with the same name

I am currently having a problem with IntelliJ. I am using Kotlin in my project. I have deleted a file (let's say test.kt), and now, I want to create a new file with the same name. IntelliJ does not recognize the kotlin syntaxe and show it as a text file.
When I have deleted I have unchecked "safe delete" and "Search in comment and strings"
Can anyone help me in this matter ?
Edit : I tried to delete .idea and .iml file, restart intelliJ. It does not change anything.
"Overrid File Type" to Kotlin would work.
Expanding on a comment:
Is test.kt listed in Preferences > Editor > File Types > Text, under the Registered Patterns? An entry there may override the default Kotlin filetype
I had a class, MyProxy.kt, that as the question implies was not being picked up as a Kotlin class in IntelliJ. I scrolled through my list of file associations and did not find anything that could match MyProxy.kt except for the Kotlin extension, *.kt (it is entirely possible I missed something.)
Most regex matching will apply the most specific rule, though. On the off chance my class was being picked up by another association, I explicitly declared it as a Kotlin file pattern. It is a little hacky, but it did work! My Kotlin file name patterns are now:
*.kt
*.kts
*.main.kts
MyProxy.kt
Note: IntelliJ did complain that *.kt would already catch MyProxy.kt, but I overrode it.
Ctrl+alt+s, Editor, File Types, under recognized filetypes,
Under Filetype auto-detected by file content...
remove Main.kt

Selenium ide code library

I was going through the selenium ide. Under the reference tab it shows the documentation of function like this:
assertAttributeFromAllWindows(attributeName, pattern)
Generated from getAttributeFromAllWindows(attributeName)
Arguments:
attributeName - name of an attribute on the windows
Returns:
the set of values of this attribute from all known windows.
Returns an array of JavaScript property values from all known windows having one.
Doubt: where can I find the implementation logic of this function. Like in java we can see from jar file, where the logic for all the function is residing? How can I get that code?
You can view all the source code on github. https://github.com/SeleniumHQ/selenium/tree/master/ide

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!