Java Code Examples for com.intellij.openapi.actionSystem.AnAction#actionPerformed()

The following examples show how to use com.intellij.openapi.actionSystem.AnAction#actionPerformed() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GraphQLIntrospectionHelper.java    From js-graphql-intellij-plugin with MIT License 6 votes vote down vote up
void setEditorTextAndFormatLines(String text, FileEditor fileEditor) {
    if (fileEditor instanceof TextEditor) {
        final Editor editor = ((TextEditor) fileEditor).getEditor();
        editor.getDocument().setText(text);
        AnAction reformatCode = ActionManager.getInstance().getAction("ReformatCode");
        if (reformatCode != null) {
            final AnActionEvent actionEvent = AnActionEvent.createFromDataContext(
                    ActionPlaces.UNKNOWN,
                    null,
                    new DataManagerImpl.MyDataContext(editor.getComponent())
            );
            reformatCode.actionPerformed(actionEvent);
        }

    }
}
 
Example 2
Source File: FlutterRetargetAppAction.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void actionPerformed(AnActionEvent e) {
  final AnAction action = getAction(e.getProject());
  if (action != null) {
    action.actionPerformed(e);
  }
}
 
Example 3
Source File: FlutterRetargetAppAction.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void actionPerformed(AnActionEvent e) {
  final AnAction action = getAction(e.getProject());
  if (action != null) {
    action.actionPerformed(e);
  }
}
 
Example 4
Source File: LombokLightActionTestCase.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void performActionTest() throws TimeoutException, ExecutionException {
  AnAction anAction = getAction();

  Promise<DataContext> contextResult = DataManager.getInstance().getDataContextFromFocusAsync();
  AnActionEvent anActionEvent = new AnActionEvent(null, contextResult.blockingGet(10, TimeUnit.SECONDS),
    "", anAction.getTemplatePresentation(), ActionManager.getInstance(), 0);

  anAction.actionPerformed(anActionEvent);
  FileDocumentManager.getInstance().saveAllDocuments();
}
 
Example 5
Source File: EditorNotificationPanel.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredUIAccess
protected void executeAction(final String actionId) {
  final AnAction action = ActionManager.getInstance().getAction(actionId);
  final AnActionEvent event = new AnActionEvent(null, DataManager.getInstance().getDataContext(this), ActionPlaces.UNKNOWN, action.getTemplatePresentation(),
                                                ActionManager.getInstance(), 0);
  action.beforeActionPerformedUpdate(event);
  action.update(event);

  if (event.getPresentation().isEnabled() && event.getPresentation().isVisible()) {
    action.actionPerformed(event);
  }
}
 
Example 6
Source File: CreateFromTemplateActionBase.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredUIAccess
@Override
public final void actionPerformed(@Nonnull AnActionEvent e) {
  DataContext dataContext = e.getDataContext();
  IdeView view = dataContext.getData(LangDataKeys.IDE_VIEW);
  if (view == null) return;
  PsiDirectory dir = getTargetDirectory(dataContext, view);
  if (dir == null) return;
  Project project = dir.getProject();

  FileTemplate selectedTemplate = getTemplate(project, dir);
  if (selectedTemplate != null) {
    AnAction action = getReplacedAction(selectedTemplate);
    if (action != null) {
      action.actionPerformed(e);
    }
    else {
      FileTemplateManager.getInstance(project).addRecentName(selectedTemplate.getName());
      AttributesDefaults defaults = getAttributesDefaults(dataContext);
      Map<String, Object> properties = defaults != null ? defaults.getDefaultProperties() : null;
      CreateFromTemplateDialog dialog = new CreateFromTemplateDialog(dir, selectedTemplate, defaults, properties);
      PsiElement createdElement = dialog.create();
      if (createdElement != null) {
        elementCreated(dialog, createdElement);
        view.selectElement(createdElement);
        if (selectedTemplate.isLiveTemplateEnabled() && createdElement instanceof PsiFile) {
          Map<String, String> defaultValues = getLiveTemplateDefaults(dataContext, ((PsiFile)createdElement));
          startLiveTemplate((PsiFile)createdElement, notNull(defaultValues, Collections.emptyMap()));
        }
      }
    }
  }
}
 
Example 7
Source File: JSGraphQLEndpointCreateDefinitionIntention.java    From js-graphql-intellij-plugin with MIT License 4 votes vote down vote up
@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {

    final JSGraphQLEndpointNamedTypePsiElement unknownNamedType = getUnknownNamedType(element);
    if (unknownNamedType != null) {
        JSGraphQLEndpointNamedTypeDefinition definition = PsiTreeUtil.getParentOfType(element, JSGraphQLEndpointNamedTypeDefinition.class);
        if (definition == null) {
            // nearest type before cursor if not inside a type definition
            definition = PsiTreeUtil.getPrevSiblingOfType(unknownNamedType, JSGraphQLEndpointNamedTypeDefinition.class);
        }
        if (definition != null) {
            final IElementType type = getSupportedDefinitionType();
            final String definitionText;
            final boolean insertBefore = (type == JSGraphQLEndpointTokenTypes.INPUT);
            Ref<Integer> caretOffsetAfterInsert = new Ref<>();
            boolean indent = false;
            if (type == JSGraphQLEndpointTokenTypes.UNION) {
                definitionText = "\n\nunion " + unknownNamedType.getText() + " = \n";
                caretOffsetAfterInsert.set(definitionText.length() - 1);
            } else if (type == JSGraphQLEndpointTokenTypes.SCALAR) {
                definitionText = "\n\nscalar " + unknownNamedType.getText() + "\n";
            } else {
                // all other types are <name> { ... }
                final String beforeLines = insertBefore ? "" : "\n\n";
                final String afterLines = insertBefore ? "\n\n" : "\n";
                final int caretOffsetDelta = insertBefore ? 4 : 3; // we want the caret to be placed before closing '}' and the trailing newlines
                definitionText = beforeLines + type.toString().toLowerCase() + " " + unknownNamedType.getText() + " {\n\n}" + afterLines;
                caretOffsetAfterInsert.set(definitionText.length() - caretOffsetDelta);
                indent = true;
            }
            final Document document = editor.getDocument();
            final int insertOffset;
            if(insertBefore) {
                final PsiComment documentationStartElement = JSGraphQLEndpointDocPsiUtil.getDocumentationStartElement(definition);
                if(documentationStartElement != null) {
                    insertOffset = documentationStartElement.getTextRange().getStartOffset();
                } else {
                    insertOffset = definition.getTextRange().getStartOffset();
                }
            } else {
                insertOffset = definition.getTextRange().getEndOffset();
            }
            document.insertString(insertOffset, definitionText);
            if (caretOffsetAfterInsert.get() != null) {
                // move caret to new position
                PsiDocumentManager.getInstance(element.getProject()).commitDocument(document);
                editor.getCaretModel().moveToOffset(insertOffset + caretOffsetAfterInsert.get());
                if (indent) {
                    AnAction editorLineEnd = ActionManager.getInstance().getAction("EditorLineEnd");
                    if (editorLineEnd != null) {
                        final AnActionEvent actionEvent = AnActionEvent.createFromDataContext(
                                ActionPlaces.UNKNOWN,
                                null,
                                new DataManagerImpl.MyDataContext(editor.getComponent())
                        );
                        editorLineEnd.actionPerformed(actionEvent);
                    }
                }
            }
        }
    }
}
 
Example 8
Source File: UpdateProject.java    From GitToolBox with Apache License 2.0 4 votes vote down vote up
private void invokeAction(AnActionEvent event) {
  AnAction action = getAction();
  action.actionPerformed(event);
}