Stop Eclipse from cutting code lines on clean up - java

In Eclipse I want (for example) that code like this
public Foo bar() {
}
gets formatted to this
public Foo bar()
{
}
via the clean up function.
But to do that I have to check "Format source code" in the clean up profile.
But that also formats code like this
alert.setHeaderText("blablablablablablablablablablablablablablablabla");
to this
alert.setHeaderText(
"blablablablablablablablablablablablablablablabla");
which I absolutely do not want. Is there any possible way to stop Eclipse from cutting lines like that?

Go to Window->Preferences->Java->Code Style->Formatter. Create new formatter. Click on edit and then pick tab Line Wrapping and set Line Wrapping policy to Do not wrap.
For more clarification refer the below Link :-
http://eclipsesource.com/blogs/2013/07/09/invisible-chaos-mastering-white-spaces-in-eclipse/

You can configure the style to which code is formatted. Under
Preferences: Java -> CodeStyle -> Formatter
Then look for "Line wrapping".

Related

Use own CodeFormatter in eclipse plugin with CDT

I'm trying to make use of the extension point org.eclipse.cdt.core.CodeFormatter but it seems to have no effect. Unfortunately I cannot find any example. The extension point description isn't very comprehensive.
My plugin.xml looks like this:
<extension
point="org.eclipse.cdt.core.CodeFormatter">
<codeFormatter
class="de.verified.rtt.ide.editors.rts.RTTLCodeFormatter"
id="de.verified.rtt.ide.editors.rts.codeformatter"
name="RTTL Code Formatter">
</codeFormatter>
This extension is on top level. Maybe it has to inside a language, file association, perspective or something else?
Detailed problem description:
In my plugin I'm using a language that extends C++ with some keywords and concepts. To parse this file I'm writing my own Source Parser that extends GNUCPPParser. At the moment my parser creates standard IASTDeclarations for tokens that unknown to CDT. E.g for "#rttConcept{...}" my parser uses a "ICPPASTNamespaceDefinition" because #rttConcept is in a way similar to a namespace definition. Now using "#rttConcept" doesn't create syntax error highlightings in the editor anymore. When trying to format this code using the CodeFormatter an exception is thrown
org.eclipse.cdt.internal.formatter.AbortFormatting: [1/1] Unexpected token type, expecting:91, actual:Token type=1006 image =# offset=0
at org.eclipse.cdt.internal.formatter.Scribe.printNextToken(Scribe.java:1653)
There is a check whether the NamespaceDefinition really corresponds to the token "namespace" in the code which it doesn't. I only want to use my own CodeFormatter to catch the AbortFormatting exception:
#SuppressWarnings("restriction")
public class MyCodeFormatter extends CCodeFormatter {
public static final String ID = "de.blub.rtt.ide.editors.rts.codeformatter";
#Override
public TextEdit[] format(int kind, String source, IRegion[] regions, String lineSeparator) {
try {
return super.format(kind, source, regions, lineSeparator);
} catch(AbortFormatting ex) {
return null;
}
}
}
Declaring your formatter in plugin.xml just makes it available as a formatter.
If you want to use it as the current formatter, you have to select it in the preferences UI (Preferences -> C/C++ -> Code Style -> Formatter, there should be a drop-down with your formatter's name as one of the options).
(The above selection affects the entire workspace. You can also choose a formatter on a per-project basis in Project Properties -> C/C++ General -> Formatter.)
That said, note the caveat in greywolf82's comment.
UPDATE: To answer your comment, yes, I believe the current formatter can be changed programmatically via CDT's public API. I would expect something like the following to work:
HashMap<String, String> options = CCorePlugin.getOptions();
options.put(CCorePreferenceConstants.CODE_FORMATTER,
"de.verified.rtt.ide.editors.rts.codeformatter");
CCorePlugin.setOptions(options);

IntelliJ: add formatter:off/on comments around selected block

In IntelliJ IDEA (for Java code at least), it's poissble to instruct the code-formatter to ignore lines with these (see https://stackoverflow.com/a/19492318/117750):
// #formatter:off
...
// #formatter:on
What I'd like to do is automate adding these around a code block. Workflow I want:
Select a block of code.
Invoke an action (with a shortcut or a menu item etc. or with the Cmd-Shit-A).
This command needs to
add // #formatter:off on a new line before the first selected line, at the correct indentation.
add // #formatter:on on a new line after the last selected line, at the correct indentation.
From what I am reading, it is not possible to do with a Macro. Is it?
If not, do I need to write a plugin to do this? I am happy to write one, can someone give me brief high-level get-started steps on:
how to approach this action in a plugin
and pointers to get-started with plugins
You can get (close to) what you want using a Live Template. Go to Editor | Live Templates in the settings and add a new template (e.g. under the surround group) with the following text:
// #formatter:off
$SELECTION$
// #formatter:on
You can use the defined live template by selecting some text in the editor and invoking Code | Surround with Live Template... (Ctrl/Cmd+Alt+J) and selecting the live template you have created.

How to do a specific completion action without adding some unnecessary comment ?

I have the following warning
The serializable class myClassD4 does not declare a static final
serialVersionUID field of type long
From this interesting discussion https://stackoverflow.com/a/285809/813853 I know what to do to handle this warning.
BUT my problem is kind of different, it is related to Eclipse parametrization. When I get this warning, I do right click. And choose completion action :
Add generated serial version id
the problem is that this add a comment block , something like /* ** */. That I don't want. Please, how to get rid of that ? I have looked on the configuration of Eclipse but not found it YET.
Found it :)
Window -> Preferences -> Java -> Code Style -> Code Templates -> Comments -> Fields
All I have to do is to edit the default string.
The only way that I know is to turn off comment-generation for ALL fields (=instance vairables). This is how to do it:
Windows -> Preferences -> Java -> Code Style -> Code Template .
Open Comments tab, click on Fields, click on Edit button and remove the text

IntelliJ Idea: code completion for creating method like in Eclipse

I am using Eclipse for about 5 years. Now I'm begging with IntelliJ Idea 13. I can not get used to code completion :-(
How can I create new public method?
In Eclipse i press:
pub CTRL+SPACE ENTER int CTRL+SPACE test ENTER
and get
public int test() {
}
how can I do it in Idea?
IF this scenario is important to you,
You can use IntelliJ's Live Templates
There are no built-in templates for that. You can create your own, like other answers advise. But in general, just typing the method with autopopup completion seems to require almost the same number of key presses:
pub < autopopup appears with "public" selected, hit ENTER > int test( < CTRL+SHIFT+ENTER >
You can add a 'Live Template' in IntelliJ. Go to Preferences -> Live Templates -> Select your language (if Java is not there, you can select Other), select the "+" symbol and add a new template. e.g. Abbreviation = test, and put your method in the Template text.
IntelliJ will display a message 'No applicaable contexts yet'. Define' just below the box. If you click on 'Define' you can select 'Java' in the list that shows up.
In your editor, type the template abbreviation and press the tab key.

Eclipse JDT: Call 'correct indentation' programmatically?

I am working on an Eclipse plugin that modifies Java code in a user's project.
Basically the result of this plugin is that Java annotations are added to some methods, so
void foo() { ... }
becomes
#MyAnnotation
void foo() { ... }
Except that it doesn't quite look like that; the indentation on the newly inserted annotation is wack (specifically, the new annotation is all the way to the left-hand side of the line). I'd like to make all my changes to the file, and then programmatically call "Correct Indentation."
Does anyone know how to do this? I can't find the answer here or in the JDT forums, and all the classes that look relevant (IndentAction, JavaIndenter) are in internal packages which I'm not supposed to use...
Thanks!
Well I think I may have figured out the solution I want. Guess I should have spend more time searching before asking... but for future reference, here's what I did! The good stuff was in the ToolFactory...
import org.eclipse.jdt.core.ToolFactory;
import org.eclipse.jdt.core.formatter.CodeFormatter;
import org.eclipse.jdt.core.ISourceRange;
import org.eclipse.text.edits.TextEdit;
import org.eclipse.jdt.core.ICompilationUnit;
...
ICompilationUnit cu = ...
...
CodeFormatter formatter = ToolFactory.createCodeFormatter(null);
ISourceRange range = cu.getSourceRange();
TextEdit indent_edit =
formatter.format(CodeFormatter.K_COMPILATION_UNIT,
cu.getSource(), range.getOffset(), range.getLength(), 0, null);
cu.applyTextEdit(indent_edit, null);
cu.reconcile();
This reformats the entire file. There are other options if you need to reformat less...
It's probably easier to add the indentation as you process the Java code.
Your Eclipse plugin had to read the void foo() { ... } line to know to add the #MyAnnotation, right? Just get the indentation from the Java line, and append your annotation to the indentation.

Categories

Resources