通知

1、通知消息

image.png

  1. import com.intellij.notification.Notification;
  2. import com.intellij.notification.NotificationDisplayType;
  3. import com.intellij.notification.NotificationGroup;
  4. import com.intellij.notification.Notifications;
  5. import com.intellij.openapi.actionSystem.AnAction;
  6. import com.intellij.openapi.actionSystem.AnActionEvent;
  7. import com.intellij.openapi.actionSystem.PlatformDataKeys;
  8. import com.intellij.openapi.project.Project;
  9. import com.intellij.openapi.ui.MessageType;
  10. import com.intellij.openapi.ui.Messages;
  11. public class TestAction extends AnAction {
  12. @Override
  13. public void actionPerformed(AnActionEvent event) {
  14. NotificationGroup notificationGroup = new NotificationGroup("testid", NotificationDisplayType.BALLOON, false);
  15. /**
  16. * content : 通知内容
  17. * type :通知的类型,warning,info,error
  18. */
  19. Notification notification = notificationGroup.createNotification("测试通知", MessageType.INFO);
  20. Notifications.Bus.notify(notification);
  21. }
  22. }

2、弹框通知

image.png

  1. import com.intellij.notification.Notification;
  2. import com.intellij.notification.NotificationDisplayType;
  3. import com.intellij.notification.NotificationGroup;
  4. import com.intellij.notification.Notifications;
  5. import com.intellij.openapi.actionSystem.AnAction;
  6. import com.intellij.openapi.actionSystem.AnActionEvent;
  7. import com.intellij.openapi.actionSystem.PlatformDataKeys;
  8. import com.intellij.openapi.project.Project;
  9. import com.intellij.openapi.ui.MessageType;
  10. import com.intellij.openapi.ui.Messages;
  11. public class TestAction extends AnAction {
  12. @Override
  13. public void actionPerformed(AnActionEvent event) {
  14. // 弹窗显示消息
  15. Messages.showMessageDialog("Hello world", "ShuoGG Say", Messages.getInformationIcon());
  16. }
  17. }

右键菜单事件

得到选中的文字

image.png

  1. import com.intellij.openapi.actionSystem.AnAction;
  2. import com.intellij.openapi.actionSystem.AnActionEvent;
  3. import com.intellij.openapi.actionSystem.CommonDataKeys;
  4. import com.intellij.openapi.editor.Editor;
  5. import com.intellij.openapi.editor.SelectionModel;
  6. public class TestAction extends AnAction {
  7. @Override
  8. public void actionPerformed(AnActionEvent event) {
  9. // TODO: insert action logic here
  10. Editor editor = event.getRequiredData(CommonDataKeys.EDITOR);
  11. SelectionModel model = editor.getSelectionModel();
  12. String text = model.getSelectedText();
  13. System.out.println(text);
  14. }
  15. }

注意:添加的组有变化EditorPopupMenu

  1. <!-- 编写插件动作 -->
  2. <actions>
  3. <!-- Add your actions here -->
  4. <action id="test_id" class="com.kakaluote.action.TestAction" text="通知" description="测试通知的功能">
  5. <add-to-group group-id="EditorPopupMenu" anchor="first"/>
  6. </action>
  7. </actions>