Is there a shortcut (or menu entry) in IntelliJ IDEA to complete the current statement with an anonymous implementation. To make this more understandable:
I type something like: view.setOnClickListener( and would now like a shortcut to get the completion to:
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
<<new caret position>>
}
});
Assuming that the setOnClickListener method would accept one parameter of type OnClickListener. I don't care if there are already implementations of OnClickListener I would just like the very basic type (or interface) that is expected as an anonymous inner class.
So do anyone know if there is a possibility for this in IntelliJ IDEA, without the need of typing new OnClickListener (with auto completion) yourself?
The only thing you have to do is to write:
view.setOnClickListener(new + Ctrl+Shift+Space
This will bring up a list of alternatives but since you used the Smart Type Code Completion (instead of Basic Code Completion which is Ctrl+Space) you will have the one that fits best at the top.
So now just press Tab and the rest will be filled in for you.
You save some writing but still have to at least write new in order for it to work.
Smart Type code completion filters the suggestion list and includes only those types that are applicable to the current context.
Related
I'm using IntelliJ IDEA to refactor some Kotlin code. I have two classes in the same file and I want to move a function from one class to another using Refactor -> Move (F6), but that doesn't work, and I get tooltip message that says: "Cannot perform refactoring. Move declaration is only supported for top-level declarations and nested classes".
Am I doing something wrong? Or that refactoring is simply not supported?
[edit1] I tried to do the same operation with Java classes and everything works perfectly; so why this is not allowed for Kotlin?
[edit2] I thought that the problem is only when to two classes are in the same file, but it turns out that is not possible to move a function between classes in separate files!
It's a well-known Kotlin-only problem.
in IDEA (both free and paid editions);
in Android Studio.
Official ticket
There is an easy, but slightly janky, work around.
You just need to wrap the function you want to move in a class:
class TopLevelClass {
fun functionToMove() {
//...
}
}
wrap it in a new class
class TopLevelClass {
class TemporaryMoveClass{ /** you can now move this entire new class */
fun functionToMove() {
//...
}
}
}
and after you do the refactor, delete the temporary wrapper class you created.
The janky part is that you need to replace all instances of functionToMove() with NewTopLevelClass.functionToMove() yourself.
One of the major benefits of doing it this way, rather than just cut and pasting it yourself, is that as soon as you wrap it in the TemporaryMoveClass it will tell you any parameters you need to introduce(Refactor>Extract>Parameter). And then you can do that inside the original TopLevelClass before you move it. (this preserves the types of any TopLevelClass properties you were using, and automatically introduces the new parameter(s) into the existing function calls)
I am writing an Eclipse plugin (Indigo/Juno) that contains a text editor for a custom text format. I am following the tutorial here: http://www.realsolve.co.uk/site/tech/jface-text.php
So far I have everything working. Eclipse will use my editor to edit files. I have partitioning, damaging, repairing, syntax highlighting all working.
I added a preferences page with color pickers to control syntax highlighting. It works mostly correct. If I update the colors, the editor uses them the next time I open or reopen a file.
How do I get an editor tab to update itself without opening a new one? The built-in JDT Java editor does this, but so far I have not been able to decipher how (it is a very large and complex editor).
I gather that I need to create a preferences listener (http://www.vogella.com/articles/EclipsePreferences/article.html). I have done this and can verify that my listener code is being invoked when I set a breakpoint in it.
The missing piece is the wiring between the listener and reinitializing the editor. I have tried reconstructing the partitioning logic, the color logic, the damager/repairer, etc. but nothing seems to work. It either does nothing I can see or at worst will corrupt the display until I scroll the current text out of view to repaint it... with the old colors.
Any ideas?
I think SourceViewer.invalidatePresentation() needs to be called.
It may be already late to you, but if you want you could use LiClipse for that (http://brainwy.github.io/liclipse/) -- one of its targets is easily doing an editor with syntax highlighting, basic code-completion, outline, etc targeting Eclipse.
No java skills are required to add a new language (mostly creating a new .liclipse -- which is a YAML -- file in the proper place and creating some basic rules to say how to partition your language -- i.e.: usually just separating code from comments from strings -- and specifying the keywords you have in the partition would already give you proper syntax highlighting).
If you download it, there are a number of examples at plugins\com.brainwy.liclipse.editor\languages and there's some basic documentation at http://brainwy.github.io/liclipse/supported_languages.html and http://brainwy.github.io/liclipse/scope_definition.html on how to do it.
For anyone coming across this as I did:
My solution involved adding the following lines into the Constructor of my Editor
Activator.getActivator().getPreferenceStore().addPropertyChangeListener(new IPropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent event) {
getSourceViewer().invalidateTextPresentation();
handlePreferenceStoreChanged(event);
}
});
and then creating a custom class that extended IToken. In the constructor I pass the String of the preference field and then in the 'getObject' method I create the TextAttribute: snippets below
public class MyToken extends Token implements IToken {
public MyToken(Object data) {
super(data);
}
#Override
public Object getData() {
String dataString = (String) super.getData();
return getAttributeFromColorName(dataString);
}
private TextAttribute getAttributeFromColorName(String preferenceField) {
Color color = new Color(Display.getCurrent(), StringConverter.asRGB(Activator.getActivator().getPreferenceStore().getString(preferenceField)));
return new TextAttribute(color);
}
}
When I generate my Rules I have all of my tokens as my custom class and this allowed me to change syntax color dynamically.
I also added an example for updating the coloring if the preference changes to https://www.vogella.com/tutorials/EclipseEditors/article.html#exercise-allow-user-to-customize-the-colors
This is using the Generic editor (currently the best approach to implement a customer editor) but it should be possible to adjust this to any Eclipse editor implementation.
I need to intercept several events before they are delivered to the widget's standard handlers, so I've done this already:
//Inside the definition of my custom widget
protected override void OnRealized()
{
base.OnRealized();
this.GdkWindow.AddFilter(PreFilterMessage);
...
}
So, later I define the PreFilterMessage method:
public Gdk.FilterReturn PreFilterMessage(IntPtr xEvent, Gdk.Event evnt)
{
Console.WriteLine(evnt.Type);
...
}
But the thing is that when I test it, whatever message gets to the window (KeyEvent, ButtonEvent, etc.) it always prints "Nothing", so I'm only getting empty events every time. Somewhere I read that the real information gets through the xEvent parameter, but that's just an IntPtr, so I don't know how to get the information I need (event type, pointer coordinates, etc.) from it.
Can anyone tell me how to do this? Thanks in advance.
Per the docs on the gtk.org website, the GdkEvent received in the filter func is unpopulated. The purpose of this AddFilter mechanism is to allow the user to intercept X events before the gdk event processing starts up. We do not bind any of the X data structures in Gtk#, so you would need to manually marshal that data from the IntPtr using System.Runtime.InteropServices Marshal.
So, unless that sounds familiar as far as what you are trying to accomplish, you may want to consider other alternatives.
I'm developing a plugin that takes all enums in workspace that implements certain interface (IDomain) parses the code (Using AST) does some modification over the enum and marks it as processed with an annotation (#IDomainInfo).
For example, it takes someting like this:
public
enum SomeEnum implements IDomain {
// ...
}
And generates something like this:
public #IDomainInfo(domainId = 1)
enum SomeEnum implements IDomain {
// Some changes here...
}
The idea behind of the #IDomainInfo is that annotated enums have not to be processed anymore by the plugin.
Basically what I do to accomplish the task is to make a search with JavaSearch API to find all the enums implementing IDomain (easy task), and as result I get a list of IJavaElements (which are in fact instances of IType). Then I call a method that iterates through the resulting list and creates a new list of all the IType instances that are not annotated with #IDomainInfo and then process the resulting list: For each non annotated IType do some work, annotate the IType with the #IDomainInfo annotation (Using AST) and then save back the results to file (using IFile, so I can see the changes without refresh, and in fact, if I have the enum open in the editor I see it refreshed instantly :-)
All that works fine, but if I open an #IDomainInfo annotated enum (just for testing) then remove the #IDomainInfo, save the file (I'm sure) and then call the action that does all the job I've described before, when I get to the part that filters annotated IType from non annotated ones, code is something like this:
for (IType type : typeList) {
IAnnotation annotation = type.getAnnotation(“IDomainInfo”);
if (!annotation.exists()) {
// The annotation does not exist, so add the type to the
// list of elements to update and go on...
ret.add(type);
continue;
}
// Something else here...
}
Well, it results that for the file I've just saved the IType detects the annotation I've just removed as if it's still there. If I close and reopen eclipse all works normally.
Now, I've just checked and triple checked my code, so I'm sure that I'm not keeping a stale copy of the old IType unedited still with the annotation version (all my IType come from a fresh java search call every time I run the action).
So the question is, what might I be doing wrong? I mean, I've just read the JavaCore API many times to check If I might be using it wrong or if I have some conceptual flaw there but really I have no clue, it's like if eclipse would be caching the IType ignoring the changes I've just made in the editor :-/
If any one have an idea I would appreciate it a lot :-)
When or how is your plugin called ? Did you register a resource listener or is it a project builder or something else ? If it is called by a resource listener, your plugin may be reading the 'primary copy' for your IType, which has not been saved yet. Hence your changes are still in the Working Copy.
Is there any function in wxWidgets framework, say, click(), whose function is to emulate a left-click mouse?
No, there is no function like this. If you really need to do it, e.g. because you want to perform some action in another application you need to write platform-specific code yourself (e.g. use SendInput() under Windows).
If you want to use this to execute some code in your own application though, there is a much better solution: instead of doing unsupported, fragile and ugly
void MyClass::OnLeftUp(wxMouseEvent& event)
{
... do something with click at event.GetPosition() ...
}
void MyOtherFunction()
{
wxMouseEvent event(...);
... initialization not shown because there is no right way to do it anyhow ...
myObject->ProcessEvent(event);
}
just refactor your code to do it like this instead:
void MyClass::OnLeftUp(wxMouseEvent& event)
{
HandleClick(event.GetPosition());
}
void MyClass::HandleClick(const wxPoint& pos)
{
... do something with click at pos ...
}
void MyOtherFunction()
{
myObject->HandleClick(position);
}
If you're just talking about emulating a click on another button in your wxWidgets app, I've tried this before:
Create a fake wxEvent of the right type, tell the event what object to care
about using SetEventObject(), and tell a parent wxWindow in the hierarchy to
ProcessEvent(myNewEvent). You might want to use wxGetTopLevelParent() to get the topmost frame/dialog
If you are talking about emulating a click in another, non-wxWidgets process, it's possible you could use the OS's accessibility APIs to do this. wxAccessibility should be able to help with this on Windows -- for other OSes, last I heard (granted, a few years ago), you'll have to use the native OS functions.
You can use the wxControl::Command() method. According to Documentation:
Simulates the effect of the user issuing a command to the item.
Documentation page
List of events to use with wxCommandEvent constructor
You can simulate a click on a button like this:
$this->MyButton->Command(new wxCommandEvent(wxEVT_COMMAND_BUTTON_CLICKED));
There is also the wxUIActionSimulator class that you can use to simulate UI actions.