In a Java project that I'm working on with IntelliJ, we increased the allowed line length in the code style.
IntelliJ happily wraps lines that are longer than allowed by code style when auto-formatting. However, I would like to do the opposite: unwrap multiple lines that are way shorter than allowed.
Is there a way to clear unnecessary line breaks when auto-formatting with IntelliJ?
Well, this is a workaround for sure, but you can:
Select the full file with Ctrl+A
Join all lines with Ctrl+Shift+J
Reformat file with Ctrl+Alt+L
Related
I am making an Eclipse plugin and it includes a built-in textEditor implementation, which shall be displaying an SQL query from the database.
However, I want it to only be allowing to create a 72-character-long lines. Therefore, if i write and endless line, I want it to jump into a new line automatically once the line is 72 characters long.
I am now able to load the statement and display it there, cutting the line at 72 characters, but if i edit the query in the editor, it lets me to create a line at any length.
I tried to look for a parameter or a method that would set it, but I failed.
Does anybody have any idea?
Thanks a lot!
Why dont you try using regular expression .Refer the below link for an example.
http://git.eclipse.org/c/platform/eclipse.platform.swt.git/tree/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet196.java
I use many brackets and braces when I code. Be it casting multiple times, casting multiple times in if blocks, etc. I sometimes get lost in the brackets and also, hate putting a lot of them.
Is there any short-cut key to format this selected part of code?
I have tried Ctrl-Shift-F, but that doesn't give me what I want.
Note: I work on Eclipse Mars.
What you want is Source menu | Cleanup, then customize the profile to add the remove extra parenthesis. From the Code Style tab check the Use parenthesis in expressions Only if necessary. And then complete the wizard:
That changes:
super.start(((BundleContext)(context)));
to:
super.start(context);
As an extra you can set your project to do code cleanup tasks on save automatically if you desire.
did you try ctrl+3 and type formate? for me Ctrl-Shift-F was bound by other app and cause eclipse to miss catching it.
search for formatter Java->code style->formatter
There is a tab Braces.
If you select specific lines and press Ctrl + Shift + F eclipse will only format the lines you selected according to your formatter.
Is there a way to tell IntelliJ to stop rewriting my code to match its preferences? I'm especially concerned with this when it comes to code that has already been committed to source control. For example, the following line was mysteriously changed to the second example because IntelliJ preferred the latter spacing.
assertLess((new Date().getTime()/1000)-8,attrs.mtime);
assertLess((new Date().getTime() / 1000) - 8, attrs.mtime);
I don't want this to happen. Any ideas?
You might need to uncheck this:
There is an option in IDE Settings -> Editor -> Smart Keys named Reformat on Paste:
Check it is set to anything other than Reformat Block, for example Indent Each Line.
I like to format my code like this:
void someFunction() {
normalLine();
reallyLongLineThatNeedsToBeWrapped(
OnlyIndentOneSpaceHere = true);
normalLine();
}
But from my experimenting with Eclipse's source formatting, it seems that you can't specify wrapped line indentation independently from new block indentation.
Is there a way to accomplish this in Eclipse?
Per answer, I submitted an Eclipse enhancement request.
I don't think so. The problem is you can't change the indentation size to be different depending on context. So choosing 1 for the indentation size results in everything being indented by one space. You can have a separate wrapping policy based on it being a function call or not, but the "units" of indentation can't change if it's a function call.
Yes and no.
It is possible to indent wrapped lines differently but there is a limitation: Indentation for wrapped lines has to be multiple of indentation for blocks.
You're specifically asking to set block indentation to 3 and line wrap indentation to 1. This particular setup is not possible as of now.
So, what if you want to use, for example, Google Java Style? Their block indentation is 2 and wrapped line (continuation) indent is 4. This is achiavable since 4 is a multiple of 2.
First, open the formatter settings:
Window > Preferences > Java > Code Style > Formatter > Edit
In the Indentation tab, set Indentation size to 2. This is the block indentation.
Then open the Line Wrapping tab. There you have Default indentation for wrapped lines and Default indentation for array initializers. Set these fields to 2, meaning wrapped lines indentation will be twice the block indentation.
This question already has answers here:
Does Eclipse have line-wrap
(12 answers)
Closed 7 years ago.
I already have word wrap for Eclipse PHP (How can I get word wrap to work in Eclipse PDT for PHP files?). What about Eclipse Java? Is there a similar tool to wrap Java code lines?
Ctrl+Shift+F will format a file in Eclipse, breaking long lines into multiple lines and nicely word-wrapping comments. You can also highlight just a section of text and format that.
I realize this is not an automatic soft/hard word wrap, but I don't think the question was asking for anything fancy.
Just like to point out that word wrapping is a terrible idea in code. I suppose everyone has their own style, but typically you should refactor or rethink blocks of code that take much more than a single average horizontal screen. (about 1280 pixels)
Of course there are always exceptions... like verbose exceptions for example, but these are not critical for code understanding.
Scanning a line that goes beyond a single horizontal screen, or having to read a thought that stacks on multiple lines will make your code much more difficult to figure out.
Additionally, line counts will be weird, as word wrapping will make a single line seem like it takes up 2, 3, or shudder the thought... 4 lines of code.
As far as comments go, line returns should not be much of a problem for anyone.
Also keep in mind, word wrapping code is not a common practice, and you are essentially saying that everyone reading your code should turn word wrapping on, which is, I'm sorry to say, an unnecessarily painful thing to make others working with your code do....
If nesting is causing you the need for word wrap... well this is also a very bad code smell.
That's my two-cents.
This is for anyone who is having the following problem with the "Ahtik" word-wrap plugin in Texlipse: the plugin installs fine, but selecting the "word-wrap" option using the context menu (or pressing ctrl+alt+w) causes the text to wrap momentarily, and then just flick back to being unwrapped (this probably includes anyone using Windows 7).
Here is a fix...straight from the developer himself! I emailed Ahti Kitsik in a last-ditch attempt to find a way round, and was amazed when he actually found a solution, and sent it to me the next day. He suggested that in return I could just make this information widely available.
So, the way to fix it as follows:
Look up the texlipse plugin location. This will probably be in the Eclipse folder, and the address will probably end: ...\eclipse\plugins\net.sourceforge.texlipse_1.5.0.
Backup the plugin.xml file there just in case, and then edit the original by deleting the following chunks of code:
-Firstly, the "action" (starting at line 843):
action
class="net.sourceforge.texlipse.actions.TexWordWrapAction"
definitionId="net.sourceforge.texlipse.commands.texWordWrap"
icon="icons/wrap.gif"
id="net.sourceforge.texlipse.actions.texWordWrap"
label="W&rap text"
menubarPath="net.sourceforge.texlipse.menus.latex/latexGroup"
style="toggle"
toolbarPath="latexGroup"
tooltip="Use word wrap"
-Secondly, the "command" (starting at line 972):
command
categoryId="net.sourceforge.texlipse.latexEditingCategory"
name="Wrap text"
id="net.sourceforge.texlipse.commands.texWordWrap"
(When you have done this, the file should be 1257 lines long, instead of 1271).
Finally, Ahti says: "Start eclipse with -clean command line argument to force reloading of plugin.xml." (Personally I had no idea how to do this, so I just removed and reinstalled his plugin, then exited Eclipse, and edited the code before I restarted it).
The function should work fine now. Hope this helps someone. And thanks again Ahti!
In the meantime, there has been an fork to Ahtik's word wrap plugin which, I must say, works very nice and fixes jumbled line numbering bug: Eclipse Word-Wrap Plug-In
Open Eclipse -> Help -> Install New Software -> Click "Add.." button and copy this line "http://dev.cdhq.de/eclipse/updatesite/luna/" into field Name and Location -> Click OK
Check -> Line Number Ruler and Word Wrap Feature -> Next -> Next -> Finish -> Restard Eclipse!