Why I can't see what I type?

I followed a simple tutorial to make a plugin that shows a dialog box when I type a character in the editor. 

https://github.com/JetBrains/intellij-sdk-docs/tree/master/code_samples/editor_basics/src/org/jetbrains/tutorials/editor/basics 

But I can't see what I type. I think there's a problem in MyTypedHandler.java file showed below.

import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.*;
import com.intellij.openapi.editor.actionSystem.TypedActionHandler;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import org.jetbrains.annotations.NotNull;

public class MyTypedHandler implements TypedActionHandler {
@Override
public void execute(@NotNull Editor editor, char c, @NotNull DataContext dataContext) {
   final Document document = editor.getDocument();
   Project project = editor.getProject();
   Runnable runnable = new Runnable() {
   @Override
    public void run() {
           Messages.showMessageDialog(project, "You Typed - " + Character.toString(c), "SCA Arosha", Messages.getInformationIcon());
   }
};
WriteCommandAction.runWriteCommandAction(project, runnable);
}
}

 

0
9 comments

Have you tried to debug it? Does the execution come at all to MyTypedHandler.execute method?
Have you registered you handler (see static block of code in EditorIllustration class in the tutorial for an example)?

0
Avatar
Permanently deleted user

Yes, the execution comes to execute method and also the static block is defined as in the tutorial. Even the code in the tutorial does't show me what I type in the editor but the dialog box does pop up. I just want to show a dialog box using Messages.showMessageDialog() when a user type a key in the editor. Key should also be visible after I closed that dialog box. I also get an IDE error when running the plugin. It says that "AWT events are not allowed inside write action". I'm working on a static code analysis tool for a project. Thanks.

 

Following is my EditorIllustration.java file

import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.editor.actionSystem.*;


public class EditorIllustration extends AnAction {

static {
final EditorActionManager actionManager = EditorActionManager.getInstance();
final TypedAction typedAction = actionManager.getTypedAction();
typedAction.setupHandler(new MyTypedHandler());
}

@Override
public void actionPerformed(final AnActionEvent e) {

}
}
0

As for the error, you shouldn't wrap 'showMessageDialog' call into WriteCommandAction, just invoke it directly in your typed handler's 'execute' method.

If you also want to preserve existing typing logic, you need to store a reference to the original typed handler (it's returned by 'setupHandler' mehtod) and invoke it from your handler. Otherwise your handler's logic completely replaces default typing logic.

As an alternative, you can implement TypedHandlerDelegate instead - there's no need to implement delegation in that case.

 

0
Avatar
Permanently deleted user

I still have both problems.

Here's my execute method

public void execute(@NotNull Editor editor, char c, @NotNull DataContext dataContext){
final Document document = editor.getDocument();
final Project project = editor.getProject();
try {
Guideline06_09 obj = new Guideline06_09();
String result = obj.runalgorithm(document.getText());
if(!result.isEmpty()){
Messages.showMessageDialog(project, result, "SCA Arosha", Messages.getInformationIcon());
}
}catch (Exception e){

}
Runnable runnable = new Runnable() {
@Override
public void run() {
//identifyviolations(project, document);
//insertCharacter(editor, document, c);

}
};
WriteCommandAction.runWriteCommandAction(project, runnable);
}


Here's my MyTypedHandlerDelegate.java file


import com.intellij.codeInsight.editorActions.TypedHandlerDelegate;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFile;

public class MyTypedHandlerDelegate extends TypedHandlerDelegate {
@Override
public Result charTyped(char c, Project project, Editor editor, PsiFile file) {
return Result.CONTINUE;
}
}

plugin.xml file

<extensions defaultExtensionNs="com.intellij">
<!-- Add your extensions here -->
<typedHandler implementation="MyTypedHandlerDelegate"/>
</extensions>

<actions>
<action id="EditorBasics.EditorIllustration"
class="EditorIllustration"
text="SCA Tool"
description="Illustrates how to plug an action in">
<add-to-group group-id="EditorPopupMenu" anchor="first"/>
</action>
</actions>
 
0

What two problems do you mean? Are you still getting an exception? Please post its stacktrace.

As for typing, you still override default TypedActionHandler and not delegate typing to it.

0

You can try using 'setupRawHandler' instead of 'setupHandler' to avoid exception.

0
Avatar
Permanently deleted user

Exception is gone after using setupRawHandler instead of setupHandler. Thanks. But typing issue is still there. I have 3 files EditorIllustration,java, MyTypedHandler.java and MyTypedHandlerDelegate.java. What I did wrong to avoid default typing behaviour? 

0

As 'setupHandler'/'setupRawHandler' javadoc mentions, you need to delegate to the previously registered handler. Just call 'myOriginalHandler.execute(...)' from within your 'execute' method.

TypedHandlerDelegate approach probably won't work for you, if you need to show message dialog from that code (it's already invoked from within write action).

0
Avatar
Permanently deleted user

I think it worked. Thanks a ton!!

MyTypedHandler.java
public class MyTypedHandler implements TypedActionHandler {
private TypedActionHandler myOriginalHandler;
String result = "";

public MyTypedHandler(TypedActionHandler originalHandler){
myOriginalHandler = originalHandler;
}
@Override
public void execute(@NotNull Editor editor, char c, @NotNull DataContext dataContext){
myOriginalHandler.execute(editor, c, dataContext);
final Document document = editor.getDocument();
final Project project = editor.getProject();
try {
Guideline06_09 obj = new Guideline06_09();
String result = obj.runalgorithm(document.getText());
if(!result.isEmpty()){
Messages.showMessageDialog(project, result, "SCA Arosha", Messages.getInformationIcon());
}
}catch (Exception e){

}
Runnable runnable = new Runnable() {
@Override
public void run() {
//identifyviolations(project, document);
//insertCharacter(editor, document, c);

}
};
WriteCommandAction.runWriteCommandAction(project, runnable);

}
}

EditorIllustration.java
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.editor.actionSystem.*;


public class EditorIllustration extends AnAction {

static {
final EditorActionManager actionManager = EditorActionManager.getInstance();
final TypedAction typedAction = actionManager.getTypedAction();
typedAction.setupRawHandler(new MyTypedHandler(typedAction.getRawHandler()));
}

@Override
public void actionPerformed(final AnActionEvent e) {

}
}
0

Please sign in to leave a comment.