Is there a way to generate multiple files/classes with intellij code/file templates? [duplicate] - intellij-idea

This question already has answers here:
Does IntelliJ support any kind of templating to create multiple files automatically?
(3 answers)
Closed 1 year ago.
I like intellij's code/file templates, they are working really well for me for single files but it would be great to be able to create multiple files/classes at once.
I did find this question but since it has been a few years since I was hoping that things might have changed.
We are using the mvp pattern in our application so every time we want to create a new view, it requires two classes and two interfaces, which are always in the same pattern. It would be awesome if we could generate all of the infrastructure for this and link up the classes.
Any suggestions or new information would be appreciated?
Maybe it is time to log a feature request with JetBrains?

This feature seems to be available in ReSharper (another JetBrains IDE). It still does not seem possible in IDEA yet.

At the moment IDE doesn't support this feature. Most popular youtrack request have 60+ votes, but it still opened(since 2012). You can use 3rd party solution, for example this plugin.

It's possible (now). See below links.
https://riggaroo.co.za/custom-file-template-group-android-studiointellij/
https://medium.com/codequest/file-group-templates-in-android-studio-unofficial-guide-85dfa0a0c1ec

You could easily do that using JavaParser but I am not sure how you could invoke it from the IDEA interface

I think you are looking for this,
Open the desired file in the editor.
On the main menu, choose Tools | Save File as Template.
In the File and Code Templates dialog box that opens specify the new template name and edit the source code, if necessary.save changes and close the dialog box.

Related

IntelliJ: Search structurally in different projects

Structural search in IntelliJ IDEA is not only powerful, but also not trivial at all to get right. Now when I have created a working template of my own, I might want to use it in multiple projects.
I do not see a way to save globally. Is there anything I can do short of copying the relevant bits from one workspace.xml to another?
Unfortunatelly, it's not supported at the moment, please follow this feature request for updates.

Intellij - Find path to src directory

I am creating an IntelliJ plugin and I am using JavaParser for one of my features. My plugin will allow users to click a gutter icon next to a method and automatically navigate to the tests associated with that method.
To achieve this, temporerily I have used the line:
typeSolver.add(new JavaParserTypeSolver(new File("/home/webby/IdeaProjects/project00/src/")));
My problem is that I need to pass the source folder of the given module into this type solver. Is there any way I can find the source folder programmatically? Perhaps from an actionEvent?
I have tried things along the lines of the following:
actionEvent.getData(PlatformDataKeys.PROJECT).getBasePath()
This gives me: '/home/webby/IdeaProjects/project00/' but I'm struggling to see how I can get the source folder? I feel there should be a fairly straight forward way of doing this using IntelliJ's SDK but I have not found anything in the documentation or anywhere else online.
Any and all solutions welcome!
Many Thanks,
James
You can use
ModuleRootManager.getInstance(module).getSourceRoots()
to access sources roots of a module. Refer to IntelliJ SDK Docs for details.
BTW IntelliJ IDEA provides special API to syntax trees of Java files, it works more efficiently and better integrates with other IDE features than external JavaParsers.
And it's better to ask questions about IntelliJ IDEA API on a special forum.

Intellij IDEA: how can I open free form projects in single instance and later reopen whole group?

I found NetBeans has a lot of problems nowadays (I reported several bugs) and it is unclear if Netbeans will support Java 9 at all.
I try Intellij IDEA and want to keep several unrelated free form project opened in separate instance (Spring Security sample projects) to study sources.
How can I open it is separate window and how can I save that project group to open whole projects later in one click?
I can't get it from:
https://www.jetbrains.com/help/idea/creating-a-project-by-importing-existing-sources.html
https://www.jetbrains.com/help/idea/opening-multiple-projects.html
If you open a new project while one is already open, IntelliJ will ask you if you want to open it in a new window.
It's harder to solve the reopening problem. You could create a project and a new module for each of the separate projects. I know they're not dependant on one another, but it's convenient to treat them as a unit and that's what a project does.
Using the modular project approach also means that you don't have to open each 'module/project' in a new window -- you'd have them all available in a single place. It's better for searching too -- you can search across the whole project.

Workflow / best practices for XLIFF

I am using a command line tool (ng-xi18n) to extract the i18n strings from an angular 2 app I wrote. The output of this command is a messages.xlf file. Coming from a .po background, and being not familiar with .xlf, I assumed that this file is the equivalent to the .pot file (correct me if I am wrong).
I then assumed that if I want to translate my app, I had to cp messages.xlf messages.de.xlf to have a copy (messages.de.xlf) of the template file (messages.xlf) where I can translate each message into German (hence the .de.xlf).
After translating some dummy texts and running the app, I saw that it worked as expected, so I quit translating and continued developing the app. After some time, I added more i18n strings, and eventually thought that I had to update my template. And this is where things got hardly maintainable. I updated the template messages.xlf file, and quickly was wondering how I could update the new strings to my already translated messages.de.xlf file without loosing my progress.
When I was developing using .po files, this was no problem thanks to good tools like poEdit, but I didn't find anything comparable for .xlf. After trying some tools, I thought that the best choice would be Lokalize, but I didn't find a possibility to merge the template file to already translated (but outdated) files either.
Up to now, this was rather an essay than a question, so here's a quick summary:
Is the workflow of dealing with .xlf files really comparable to .po as I initially thought (described above), or is it completely different?
How am I suppose to update my already translated files?
What are the best practices dealing with .xlf files?
What are proof of concept tools to work with .xlf?
Sidenotes:
The Lokalize handbook was not helpful at all. I see a lot of functions that sound promising, like:
"File" > "Update file from template". I did not find anything in the handbook to explain this function. If I click on this, nothing happens.
"Sync" > "Open file for sync/merge". This seems to be a function to merge two similar files (by multiple translators) rather than a tool to update the translation file from a template. Even though there is a tooltip in Lokalize's primary sync tab, notifying me about "x unmatched entries", I just couldn't find anything to append those unmatched entries to my .de.xlf file.
[Update] Turns out, I had similar issues as in this question. After downgrading my version of Lokalize to the suggested one, many issues (including the ones mentioned in the question) disappeared. However, now the "Update file from template" option is greyed out, and I don't know why.
I also tried OmegaT, which does not work at all on my platform (Ubuntu 16.04).
[Update] Virtaal works great for merging new strings from a template, but the UI in general is very poorly designed...
Googling did not help, as every hit seems to be related to XCode or something.
Thanks for any help in advance, I really appreciate it
I wrote a small npm command line tool called xliffmerge.
In principle it does the same, that Roland Oldengarm does with his gulp tasks described in his blog article.
It is free and you can have a look at it at https://github.com/martinroob/ngx-i18nsupport#readme
The best workflow automation solution I have seen described so far is from Roland Oldengarm's blog entry "Angular 2: Automated i18n workflow using gulp". To summarize, in a few dozen lines of Gulp code he created the tooling to handle some of the challenges you faced. Specifically it runs ng-xi18n to extract the messages; creates an English translation with sources copied to targets; updates existing translations by adding new trans-units, keeping existing ones, and removing missing ones; and then exposes all xlf files as TypeScript string constants. These last strings can then be imported to supply the bootstrapModule with its translation provider options.
Caveat: I have not used this exact solution (and code) myself, but I was able to expose generated xlf as TypeScript strings and use them in an app in a manner similar to what he described. As for maintaining translations, I have leveraged IntelliJ IDEA (WebStorm) file comparison features and Counterparts Lite (for Mac) for that. My own efforts are still in early stages but are working end to end for an application that is in active development.
Official Angular docs are now updated for Internationalization (i18n) at https://angular.io/docs/ts/latest/cookbook/i18n.html including a section specifically for creating a translation source file with the ng-xi18n tool.

Hyperlink navigation - inside code files

Does anyone know if there's an existing plugin / system for setting up hyperlink-style navigation inside a code file? I've been dealing with some overly large files recently, and I was thinking it'd be nice to set up a javadoc-style list of function names up top in a block comment, with some kind of editor plugin to jump to the appropriate line number.
What I don't know, is if that sort of behavior is already present in any popular IDE or available in any plugins. I think it'd be a fairly useful tool, but I don't want to go through the effort of writing a plugin if it's already been written.
Apologies if this is slightly off topic, it seems too specific for programmers.stackoverflow.com
IntelliJ IDEA (my favourite IDE) offers Ctrl+Click navigation since long ago.
You ctrl+click on a name of class or function or variable and IDEA takes you to its definition.
It also has "jump to file member" functionality, which, I think, is more than you're looking for (because you can invoke it from any place in a file).