Can the Eclipse code style formatter align assignment statements? - eclipse

Eclipse has an option in Indentation: "Alignment of fields in class declarations" that will
align assignments of class members as in:
class Foo {
int x = 3;
String y = "abc"
long z = 2;
}
Is there a setting that will do the same for method level assignments as in:
private void foo() {
int x = 3;
String y = "abc"
long z = 2;
}
I can not find such a setting.

Try creating a new Java Code Style Formatter profile (Window->Prefernces->Java->Code Style->Formatter) or modifying an older one and check Indentation and Braces tabs.

For future visitors.
There is no such option in Eclipse, but, as mentioned here, you can use plugin named OCDFormat that aligns assignments and declarations in a selected piece of code.
Installation: download the latest version of OCDFormat_*.jar from project page on github (click → Raw → Save as) and add it to your Eclipse plugins or dropins directory.
Usage: select a piece of code and press Ctrl+4.

The Eclipse code style formatter cannot align assignment statements.
columns4eclipse is plug-in that allows you to do so. I had made a gif video illustrating its use but my answer got deleted by a moderator (as >10k users will see), so I let you try the plug-in and see by yourself.

Related

How to enable camelCase warning in VS Code [Dart]

It is shown as underlined when naming wrong while writing code in Android Studio. However, I did not see such a thing in VS Code. Is there a way to turn it on or is there no such feature?
I think you are trying to 2 do things at once here.
First, dart has 2 lints rules that ensure your code is using camelCase
non_constant_identifier_names
constant_identifier_names
The rules are by default included in the recommended set of lint rules.
They prevent you from writting:
const PI = 3.14;
const kDefaultTimeout = 1000;
final URL_SCHEME = RegExp('^([a-z]+):');
class Dice {
static final NUMBER_GENERATOR = Random();
}
var Item;
HttpRequest http_request;
ALIGN(clearItems) {
// ...
}
But they don't prevent you from writing
var userage;
var familyname;
var studentnumber;
because those names verify the camelCase regexp.
userAge is a combination of 2 words ("user" and "age").
userage implies your variable is one single word ("userage"). This word doesn't exist in English but dart doesn't know that.
What you want now is to verify the spelling of your variables.
You can install the VSCode extension cSpell for that and it will verify that each sub-words of a variable exist and are properly spelled.
Install Camel Case Navigation Extensions
Download from here or Go to Extensions or search "Camel Case Navigation"

Disable peek in Visual Studio Code

Is there a way to disable the ctrl-click 'peek' feature in Visual Studio Code? Ideally I'd like ctrl-click to just open the file containing the definition in a new tab.
Edit: I submitted an issue to at least make it less confusing. Apparently my terminology is slightly wrong.
To clarify, there are two actions:
Right-click -> Peek Definition
Right-click -> Go to Definition (bound to ctrl-click)
Their behaviour is as follows:
PD, Single Definition
Opens inline interface showing definition.
PD, Multiple Definitions
Opens inline interface showing definitions.
GtD, Single Definition
Open the file containing the definition.
GtD, Multiple Definitions
Pick one of the definitions at random, open that file, and an inline interface showing all the definitions.
All of those are fine except the last. Doing both things results in a really redundant and confusing UI like this:
There should be a way to have one of these behaviours:
Pick one of the definitions at random, open that file.
Or:
Open inline interface showing all the definitions (in the current file)
I've made a pull request to fix this https://github.com/Microsoft/vscode/pull/68023, but until then here's a temp fix that patches the VSCode installation files. You'll need to re-apply every update.
EDIT: The fix was merged into vscode. It should be in later versions.
With this fix Ctrl+Click will:
Use peek if there are multiple definitions
When using peek, will not navigate to the best match in the editor and cause you to lose your spot
If there is only one definition, it will navigate to the best match and NOT open peek.
Figure out what the function that needs to be patched looks like. The method is DefinitionAction.prototype._onResult(editorService, editor, model)
https://github.com/Microsoft/vscode/blob/e82d8bb6e6c8fd07ca16eacd16663ebd221187cb/src/vs/editor/contrib/goToDefinition/goToDefinitionCommands.ts#L128
Go to the VSCode installation directory. %LocalAppData%\Programs\Microsoft VS Code and right click and open the directory in VSCode so that we can use VSCode's search feature to search for text in every file.
Search for _onResult and evaluate every result, checking to see if the signature and body matches what we are expecting from the function we saw in step 1.
We know from step 1, the function _openReference is nearby. Use that to narrow the search.
I found it in workbench.main.js line 2454. Use bracket matching to find the end or know that it ends immediately before t.prototype._openReference
The function when formatted is the following (async func is compiled down to statemachine, that's why it looks nothing like the source typescript):
t.prototype._onResult = function (e, t, r) {
return i(this, void 0, void 0, function () {
var i, s, a;
return n(this, function (n) {
switch (n.label) {
case 0:
return i = r.getAriaMessage(), o.alert(i), this._configuration.openInPeek ? (this._openInPeek(e, t, r), [3, 3]) : [3, 1];
case 1:
return s = r.nearestReference(t.getModel().uri, t.getPosition()), [4, this._openReference(t, e, s, this._configuration.openToSide)];
case 2:
(a = n.sent()) && r.references.length > 1 ? this._openInPeek(e, a, r) : r.dispose(), n.label = 3;
case 3:
return [2]
}
})
})
}
Replace the function with the following (if using same version) or format and edit the function you found to be similar to this example. Note the o variable is the global\window object and subject to change.
t.prototype._onResult = function (e, t, r) {
return i(this, void 0, void 0, function () {
return n(this, function (n) {
switch (n.label) {
case 0:
return r.getAriaMessage(), o.alert(r.getAriaMessage()), this._configuration.openInPeek || r.references.length > 1 ? (this._openInPeek(e, t, r), [3, 3]) : [3, 1];
case 1:
return [4, this._openReference(t, e, r.nearestReference(t.getModel().uri, t.getPosition()), this._configuration.openToSide)];
case 2:
r.dispose(), n.label = 3;
case 3:
return [2]
}
})
})
}
Launch VSCode. You will get a Your Code installation appears to be corrupt. Please reinstall. Just hit the gear icon and click Don't Show Again.
I tried to find a workaround changing the behavior of CMD + Click to go to implementation but it appears there is no solution yet?
The VSCode documentation shows its set by default to go to definition without a way to modify it:
https://code.visualstudio.com/docs/editor/editingevolved
On my machine (Mac) if I press CMD + Click or F12 on a method it will direct me to the Peek view on the definition, however CMD+F12 will direct me to the implementation without the peek appearing.
This seems to have been fixed in a newer version. If I now hover over FOO in foo.cpp, I see the normal tooltip #define FOO 2. If I press Ctrl, the message expands to add the text "Click to show 2 definitions" and if I click while still holding Ctrl, I get the peek window, as requested.

Eclipse: Select text in XSD programmatically from plugin

I'm currently writing an Eclipse plugin. In it, I want to programmatically open an editor and select a portion of the text. The opened file does not have to be imported into the workspace (that's why I'm using IFileStore in the code below).
I'm using code similar to this:
IFileStore fileStore = EFS.getLocalFileSystem().getStore(localPath);
IEditorPart part = IDE.openEditorOnFileStore(page, fileStore);
final int posStart = ...;
final int posEnd = ...;
part.getEditorSite().getSelectionProvider().setSelection(
new TextSelection(posStart, posEnd - posStart));
For Java files it works fine, but for XML Schema (XSD), it doesn't. The editor opens, but no text is selected.
From debugging, I can tell that the part is of type org.eclipse.wst.xsd.ui.internal.editor.InternalXSDMultiPageEditor, and the selection manager is an org.eclipse.wst.xsd.ui.internal.adt.editor.CommonSelectionManager
I'm targetting Eclipse Mars and Neon, it does not seem to work for both.
What can I do to make it work? Or at least find some further information?
After having a look at the WTP code, this does not seem to be supported at the moment. But I found a workaround by explicitly checking if the editor is a multi part editor:
private static void setSelection(IEditorPart part, TextSelection textSelection) {
if (part instanceof MultiPageEditorPart) {
final MultiPageEditorPart multiPage = (MultiPageEditorPart) part;
for (final IEditorPart subPart : multiPage.findEditors(multiPage.getEditorInput())) {
setSelection(subPart, textSelection);
}
} else {
part.getEditorSite().getSelectionProvider().setSelection(textSelection);
}
}
I was not sure whether it is better to send the selection to all sub parts or only to one specific, but so far sending it to all seems to work.

How can I set preference constants in Eclipse Plugin?

I want to change some default user settings in Eclipse using custom plugin. There are constants that define these settings (Constant Field Values) What should I do to set some of them in Plugin? For example, I want to switch on line numbers or save resources before launch (there are constants for such things) by default. How can I do that? Tried something like this, but nothing changed:
String lineNumbers = AbstractDecoratedTextEditorPreferenceConstants.EDITOR_LINE_NUMBER_RULER;
EditorsUI.getPreferenceStore().setValue(lineNumbers, true);
This question is not about finding constants tied to some settings, it's about finding an approach to change them.
I execute code in one of my Plugin classes. It looks like that:
String lineNumbers = AbstractDecoratedTextEditorPreferenceConstants.EDITOR_LINE_NUMBER_RULER;
void initVal (String lineNumbers){
EditorsUI.getPreferenceStore().setValue(lineNumbers, true);
}
Also tried this one:
IPerspectiveRegistry perspectives = PlatformUI.getWorkbench ().getPerspectiveRegistry();
void initValues (IPerspectiveRegistry perspectives){
perspectives.setDefaultPerspective ("org.eclipse.ui.resourcePerspective");
}

Eclipse JavaScript code formatter and JSHint anonymous function format conflict

I am using the Eclipse (version Indigo) JavaScript code formatter and using the jshint-eclipse plugin with white: true option for code convention validation.
Eclipse code formatter and JSHint plugin conflict with the anonymous function declaration format.
The JavaScript code formatter formats anonymous functions like the following:
var f1 = function() {
};
But the jshint-eclipse plugin gives a "Missing spaces after function" warning.
The right format for this plugin is:
var f1 = function () {
};
NOTE THE SPACE AFTER THE function
Is there a way to format anonymus function declaration differently with eclipse than regular function declarations. I would like to add one space after "function" for anonymous functions but not for normal functions.
Thanks.
Update a relevang eclipse bug is here
There is a bug for this: https://bugs.eclipse.org/bugs/show_bug.cgi?id=315507
Similar bug in Aptana was fixed: http://jira.appcelerator.org/browse/APSTUD-3792
I've worked out a patch for this: https://github.com/eclipse/webtools.jsdt.core/pull/1
which hopefully will be merged and released soon.
Check the JavaScript formatting preferences (Preference->JavaScript->Code Style->Formatter) on the White Space tab, for the Declarations of Functions.