Eclipse already has very impressive and useful what I call "source code modifiers" (please suggest a better name).
For example, it has "Quick Fix", "Word Completion", "Externalize Strings" and other functions that modify source code via menu (or key-combination).
Now, I am looking to add my own "source code modifier" function: I would like to:
Highlight (select) an arbitrary string.
Right-click on it
Invoke a menu item that would "translate" that string to a different string, using a function that I wrote (preferably in Java). Similar to "Quick Fix" or "Replace With" currently on the default context menu.
Is this possible in Eclipse?
If so, what do I need to do to accomplish this?
The short answer:
The quick assist will have to modify the AST of the Java code. Essentially you will have to replace a org.eclipse.jdt.core.dom.SimpleName node with one that you want.
The long answer:
The org.eclipse.jdt.ui.quickAssistProcessors extension point enables you to contribute your own Java code quick assists.
To create a new extension for the extension point you need to first provide the required extension in the plugin.xml. For example, JDT defines the following processor
<extension
point="org.eclipse.jdt.ui.quickAssistProcessors">
<quickAssistProcessor
name="%defaultQuickAssistProcessor"
class="org.eclipse.jdt.internal.ui.text.correction.QuickAssistProcessor"
id="org.eclipse.jdt.ui.text.correction.QuickAssistProcessor">
</quickAssistProcessor>
</extension>
(For a description of the individual attributes, please refer to the extension point documentation)
Then you need to create the class that implements the org.eclipse.jdt.ui.text.java.IQuickAssistProcessor interface, and modify the AST in this class. (This class is the same as the one you specified while declaring the extension)
Supplying the right IJavaCompletionProposal
JDT provides the following default implementations for correction proposals that can be used to contribute quick fixes and quick assists.
ChangeCorrectionProposal
CUCorrectionProposal
ASTRewriteCorrectionProposal
If you use an ASTRewrite, you should create an ASTRewriteCorrectionProposal.
ASTView Plugin
This is something that will help you visualize the AST of a Java source file http://www.eclipse.org/jdt/ui/astview/index.php
The right name is 'Quick Assist'. You have to write some code to create your Quick Assists.
Related
While typing in Eclipse (Java) I often have the problem that when I begin to type accessors, the caret jumps down to the beginning of the constructor definition. So in a document like this:
private int mSomeInt;
public
in|public MyClass(){
}
I would like to manually type out the accessor (getter/setter) for mSomeInt, but when I press space after 'public' above, the caret jumps to the beginning of 'public MyClass'.
I often type complete lines to look up and find my methods jumbled with the constructor (like above).
Any help would be appreciated.
Note - this isn't only with accessors but rather any access modifiers that I define before the constructor or another method.
Edit
After unsuccessfully trying Deco's solution below, I've managed to narrow it down a little further.
The problem only happens if I have all the blocks in the file in a collapsed state (ctrl+shift+numPadDivide). I can see the problem is now that the new access modifier I type is then (quickly) collapsed into the below method. i.e. Eclipse is actually taking the first accessor modifier and collapsing everything from there, even though my intention is actually to write a new method.
The only solution I've been able to find is to only edit the source with all the 'fold' elements unfolded.
Under Window -> Preferences -> <Language> (e.g. Java) -> Editor there is a Content Assist menu item where you can configure auto completion and caret placement as well as auto-activation of it and the delay it uses.
Edit:
After your update to the original question I was able to successfully replicate this in Eclipse Indigo. When you have all of the code blocks collapsed it looks like Eclipse assumes that the code you are writing needs to be in that block (rather than as a variable declaration). I'm not sure if this is expected behaviour or not - but the only way around it I've found is to edit the code with the main block open, and then close it after the fact - or turn folding off altogether.
From what I can tell there are various folding plugins/addons that you can get for Eclipse which override the default behaviour and might function better? A quick Google search will be able to get you a list of them quickly.
I'd probably also suggest posting this as an issue on the Eclipse support site for their official answer.
Unfortunately this issue still exists for me in the latest Elcipse version (Kepler).
As the issue only occurs when the document is 'folded', the work around this is to either disable folding in the editor - or disable folding on 'Members' from the :
Preferences -> Java -> Editor -> Folding
By default Xtext allows to specify a single extension for DSL files when creating a new project. However, it is possible to add more extensions for a single DSL as described in Xtext FAQ. But I couldn't get it working with files with no extension at all.
A typical example is a makefile for Make build system. One can use Makefile, GNUmakefile and *.mk names, and Eclipse will open the same editor for such files.
I want to get Xtext to recognize both *.mydsl files and a file named Mydsl.
I tried to add
filenames="Mydsl"
attribute to editor node of org.eclipse.ui.editors extension point in plugin.xml of my UI project. This enables Eclipse to open Mydsl files in the proper editor. But Xtext does not index these files and reports linking errors when I try to refer an element defined in Mydsl from any other file.
Is there a way to enable Xtext to process source files with fixed name but with no extension as well as regular files?
UPD. 1
Accordingly to Sebastian's answer I tried to specify a custom content type in plugin.xml of the main project:
<extension
point="org.eclipse.core.contenttype.contentTypes">
<content-type
base-type="org.eclipse.core.runtime.text"
file-extensions="mydsl"
file-names="Mydsl"
id="org.xtext.example.mydsl.contentType"
name="My Language"
priority="normal">
</content-type>
</extension>
And binding it as follows:
<extension
point="org.eclipse.xtext.content_resourceServiceProvider">
<resourceServiceProvider
class="org.xtext.example.mydsl.MyDslResourceServiceProvider"
contentTypeIdentifier="org.xtext.example.mydsl.contentType">
</resourceServiceProvider>
</extension>
But I still get linking errors as described above. I also added breakpoints into all methods of MyDslResourceServiceProvider and it seems that it doesn't even get instantiated or somehow invoked.
I also tried to move these extensions to the UI project but with no effect too.
UPD. 2
Finally, I've done it.
Steps to get things work in a nutshell:
Define new content type using org.eclipse.core.contenttype.contentTypes extension point
Create a content handler by extending org.eclipse.emf.ecore.resource.impl.PlatformContentHandlerImpl class and override canHandle(URI) method to return true if and only if the argument is not null
Register it with org.eclipse.emf.ecore.content_handler
Create a new resource service provider with canHandle(URI) returning true always. One can extend org.eclipse.xtext.resource.impl.DefaultResourceServiceProvider and override the corresponding method
In the UI project bind it to org.eclipse.xtext.content_resourceServiceProvider, do not forget to specify an extension factory before the class name
In the UI project register org.eclipse.xtext.resource.IResourceFactory as org.eclipse.emf.ecore.content_parser, again with the extension factory
Add content type bindings to org.eclipse.ui.editors, org.eclipse.compare.contentViewers and org.eclipse.compare.contentMergeViewers
Depending on whether you need an old extension binding or not, remove org.eclipse.emf.ecore.extension_parser and org.eclipse.xtext.extension_resourceServiceProvider extensions
A change set (applied to a fresh project) for my case can be found here.
You could try to exploit the extension point for resource service providers and resource factories. It allows to register services / factory either by file extension or by content type. I think the latter should work if you provide a suitable content type for your files.
I'm a big fan of GhostDoc's automatic comment generation in Visual Studio so am looking for an plugin that does the same job with my Java code in Eclipse. Any recommendations?
You can check JAutodoc (http://jautodoc.sourceforge.net/)
From the author:
JAutodoc is an Eclipse Plugin for
automatically adding Javadoc and file
headers to your source code. It
optionally generates initial comments
from element name by using Velocity
templates for Javadoc and file
headers.
This one is the one I've found closest to GhostDoc.
It is basically the equivalent of Javadoc, which can be generating in eclipse with the shortcut:
ALT+Shift+J
(when you are within the Java function you wish to add javadoc for)
From there, if you really want XML format, you can try and use a JELDoclet
GhostDoc has a nice extra feature that infers a description of what the method does by parsing the method name and providing this as skeletal documentation. For example, using GhostDoc on a method named GetDocumentName() might return the phrase "Gets the document name". While this is hardly more information than provided by the method name, it adds method documentation where previously none existed. Some might argue that this is barely useful. I argue to the contrary because it supports generating documentation from the source code (e.g., for tools like NDoc or SandCastle).
In my opinion the greatest benefit of GhostDoc over eclipse's "Generate Element Comment" is that it encourages programmers to begin adding documentation comments by adding an extremely fast and reliable way create this. The programmer can accept the inferred text, (suitable in 50 - 80% of cases), or expand on this for more complex methods. For the junior programmer who is not as familiar with how documentation comments are used, this can quickly shorten the learning curve and encourage good programming practices.
Javadoc is not like GhostDoc my friend. Javadoc only creates the structure so one can write the documentation from scratch. GhostDoc actually fills up the information according to the Method/Property name.
Example:
/// <summary>
/// Gets the user from id.
/// </summary>
/// <param name="id">The id.</param>
/// <returns></returns>
private string GetUserFromId(string id);
JAutoDoc is the closest I've found so far but it's not as magical as GhostDoc.
Never used GhostDoc, so not sure what extra functionality it gives, but if it's about generating type and method comments based on the name, parameters, return type etc. then eclipse has it built in, so no extensions needed.
When working on C#, I often do this:
#region Properties
public int Property1{get;set;}
...(more properties)
#endregion
This makes Visual Studio add the option to fold the code inside the region.
Offcourse, you can use regions for anything, it's not limited to Properties or anything at all.
I'm wondering 2 things:
Is this just something to make Visual Studio add code folding to an area of the editor or does it affect the compiler?
Can I do something similar in Netbeans?
(In Netbeans at least) It is purely a programmer aid and has no effect on compilation
Yes you can:
Can I Create Custom Code Folds?
Apart from usual folds, you can define custom folds. To add your custom fold, type in two special comments as shown in this example:
// <editor-fold>
Your code goes here...
// </editor-fold>
You may define the default description of a collapsed fold by adding a "desc" tag:
// <editor-fold desc="This is my super secret genius code.">
Your code goes here...
// </editor-fold>
You may set a fold to be collapsed by default by adding a "defaultstate" tag:
// <editor-fold defaultstate="collapsed">
Your code goes here...
// </editor-fold>
Please note that the above examples are for java language. In other languages custom folds may not be supported or you may have to use different characters for marking line comments. Also <editor-fold/> element syntax is not a real XML, we use a simple regex to find your custom folds in a document, which means that there are limitations in the syntax. The most important one is that attributes may only be combined in this specific order: defaultstate, desc.
It does not affect compilation, it's just to organise code in the
editor. You can pick up on arguments on when best to use them in this
question.
In NetBeans you can use Custom Folds, depending on the language:
// <editor-fold defaultstate="collapsed" desc="My Fold">
Your amazing code
// </editor-fold>
The attributes are optional. Without defaultstate specified the region will default to expand (except for when you first wrap code in it!)
The latest Netbeans 8.1 has already this feature build in, You need just select you rows and then click on the "light bulb" icon, then select:
enter code hereSurround with // <editor-fold defaultstatus="collapsed" desc="comment">...
This is the source:
http://wiki.netbeans.org/SurroundWithCodeFolding
I wrote a Macro for folding by mouse draging.
My shortcute: CTRL+SHIFT+Q
Folding Macro:
cut-to-clipboard
"// <editor-fold desc=\"This is my custom folding\" defaultstate=\"collapsed\">"
paste-from-clipboard
"// </editor-fold>"
UNFolding Macro Shortcut: ctrl+SUBTRACK
I have a very big class with lots of methods, is it possible to build an interface from this class using Resharper?
Yes.
My shortcut is Ctrl + Shift + R to bring up the refactoring options. Doing this on the class name allows you to" Extract Interface..."
Optionally, you can choose from the menu > ReSharper > Refactor > Extract Interface...
Uh, maybe I'm missing something here (I've never used resharper) but you can extract an interface from a class using the standard VS IDE refactoring tools (at least you can in 2008). Right click the class, select 'Refactor' and then 'Extract Interface'. This will bring up a dialog box where you can select which properties to include.