How to get the "Reformat code" function out of IntelliJ IDEA? - intellij-idea

I love the "Reformat code..." (Ctrl+Alt+L) function of IntelliJ IDEA.
Now I have a lot of XML files to look through. What I am doing now is, I open a xml file, select all, copy and paste into a xml file opened by IntelliJ IDEA, then use the "Reformat code..." to beautify the xml file, after that, I copy the formatted xml file back to the original xml file.
It is time consuming and not fun at all.
It would be great if the "Reformat code..." function can be taken out of IntelliJ IDEA into a command line tool to beautify all my xml files (any other files like java, ...) all at once.
So I begin to study the source code of ideaIC-99.18. And find the CodeFormatterFacade class and package com.intellij.formatting. But it still looks like, to me, will need a lot of work to do.
Does anyone ever think of it of have done it before? Any suggestions are appreciated. Thanks a lot!

The IntelliJ IDEA formatter is pretty tightly coupled to the rest of the code. The easiest way to have it run standalone is to write a plugin implementing the ApplicationStarter interface, which allows to create command line-based tools based on IntelliJ IDEA. The big downside of this approach is that running the tool will still initialize most of IntelliJ IDEA's internals, so it will not be very fast.

Related

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.

How to rename a file in IntelliJ without finding usages?

I am developing a Node.js project in IntelliJ.
The only way to rename files seems to be Shift+F6 which attempts to find all usages which takes too long (~30s - 1min).
Is there a way to simply rename the file without searching for usages?
This only happens when code is stored in Modules (which is necessary to be able to compact empty middle packages).
Best way I have found is to map ALT+SHIFT+F6 to Reveal in Finder. Then just press enter and type in new name.
It's good because its very similar to SHIFT+F6 rename refactor.
No.
IntelliJ must find the usages to rename them, otherwise you're just renaming the file, not refactoring. If you only want to rename the file, use the mv command from a terminal. You can also tell IntelliJ not to look in strings and text, which speeds things up somewhat, but is probably a bad idea in a javascript project (where almost everything is string or text).
I use rename a lot, and on my codebase, which is pretty big, it only takes a couple of seconds. Maybe intellij needs more memory to operate in, so you could try increasing that.
If You are doing it many times, You can create a custom scope for the refactoring:
There You can narrow the scope to few files/folders/modules etc. And for very narrow scope it will work as normal rename.

Override the .vimrc file in specific instances

I am looking to make vim function more closely to an IDE in specific situations. To do this I plan to write a Perl script called jvim that is passed a workspace path and have it open a java specific instance of my modified vim. I would then extend this for other file types.
What I would like to do is have a .jvimrc that is loaded in preference to the standard .vimrc. this would then lead to me having plvim with a .plvimrc and pyvim with a .pyvimrc.
This should be fairly straight forward. I would also be looking to map commands to run scrips such as :newclass, :newinterface, :newproject and :newpackage but i think you get the idea.
Any advice you could give as well as the .vimrc overriding would be great.

Java program for code-formatting

Is there a program (Java program) to format code according to Java code format conventions?
Does an Ant task exist?
There is an Ant external task for Jalopy.
http://ant.apache.org/external.html
http://jalopy.sourceforge.net/
http://ant.apache.org/faq.html#adding-external-tasks
Hope this helps
Eclipse will automatically do this for you if you tell it to. or at least it will do an awful lot of it. I doubt it would rename variables to meet the coding standard though.
Click on the properties for you project and look at the options under "Java Code Style"
You will have to open each file individually and save it though.
You can also install CheckStyle to enforce coding standards.

Other options for a Rebol editor|IDE?

I currently use Programmer's Notepad with the Rebol syntax scheme. It's not bad--does any insightful person have another suggestion?
For my Windows programming work I use the Zeus editor, but I'm not sure if it does Rebol?
Another windows option is TextPad. It is commercial but it is quite a useful editor.
There are 2 Rebol syntax files available from the official site
http://www.textpad.com/add-ons/synn2t.html
I also wrote a TextPad syntax file generator uploaded it to rebol.org
http://www.rebol.org/view-script.r?script=textpad-syngen.r
It is probably quite easy to modify this script to support other editors.
vim.
Especially with the following binding in your _vimrc/.vimrc:
nnoremap <Leader>fr :w<CR>:silent ! %<CR>
In normal mode, Leaderfr saves your current file and executes it: (fr is a memo for 'fast-run')
:wEnter save current file
:silent execute without messages: ! open shell % paste current file name Enter
Leader is usually \ key, I have this mapped to spacebar. In case anyone is interested on how to do that, post a comment.
Programmer’s Notepad better than Crimson Editor with Code Folding and Great Project Management
http://www.pnotepad.org/
It's opensource so you can even modify it in C++
For Windows, there is Crimson Editor or E with the REBOL bundle.
For Mac, there is TextMate.
Emacs, I believe has a REBOL syntax too.
Sublime Text is a really nice Windows editor (commercial, but reasonably priced) that supports TextMate configurations (well, at least for syntax and snippets) so if you manage to get a REBOL bundle from somewhere, you can use it with this.
SciTE also has REBOL syntax coloring support because the Scintilla editor component it's based on includes this.
Notepad++ should also support REBOL syntax coloring, being Scintilla based, but as it is currently distributed, the support is not compiled in. If you're so inclined, you could probably compile it yourself and add the support back in. It might be worth it because Notepad++ is quite a good editor too.
I can't include proper links because I don't have enough rep, but this should do:
www.sublimetext.com
www.scintilla.org/SciTE.html
www.scintilla.org/index.html
notepad-plus-plus.org
http://rebol.wik.is/index.php?title=Notepad%2b%2b
which is a REBOL plugin for Notepad++
I use JEdit which not only has REBOL syntax highlighting but also auto-indenting. It has most of the features you'd expect from a text editor (e.g. block selection, configurable keyboard shortcuts).
There are versions for Windows, Mac OS X and Linux so if you choose to work cross-platform you won't need to learn a new editor. The web page is Jedit.org1
I use UltraEdit.
with its advanced project, syntax highlighting, macro, command-line control and total keyboard shortcut configuration, per language and project, you can program the editor to do just about all of what you need at a single click of the mouse, or keyboard.
my setup starts rebol on any file, and assigns a launch "default project script " to a shortcut, so wherever I am in the files, I still launch the project's relevant script. change project, it will run that new project's scripts. another key for unit tests, another key for "find in all opened files, etc, etc..
also, the actual text-exiting, when combined with a few macros which create functions, objects, and more using the clipboard and "currently highlighted" text makes it much faster than any Visual IDE including MSVC.
ultra edit itself has thousands of other advanced features, and they all work... really they do.
I've tried other editors and they always fall short when I start to push them.
yeah, you have to buy it... but its cheap (like one or two hours of your life salary ;-)
so considering you might use it for several months or years... its a cheap investment.
also, ultra edit is now released on linux and the mac port is just around the corner.
I use EditPlus for several years, it is not free but not expensive. It has Rebol syntax highlighting file (downloadable from its web site).
It is especially useful & very fast if you work with huge files (over 100 mb) or with lots of files (say 300 files.), find & replace takes a second.
For syntax highlighting and a simple autocomplete, you can use http://komodoide.com/komodo-edit/.
It's free and open source with several nice features, including folder browsing while editing, which I personally find very useful.
There is also a bunch of other languages supported in case you want to take a closer look and give this editor a chance.