com.intellij.openapi.actionSystem.PlatformDataKeys Java Examples

The following examples show how to use com.intellij.openapi.actionSystem.PlatformDataKeys. 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: BaseHaxeGenerateAction.java    From intellij-haxe with Apache License 2.0 6 votes vote down vote up
private static Pair<Editor, PsiFile> getEditorAndPsiFile(final AnActionEvent e) {
  final Project project = e.getData(PlatformDataKeys.PROJECT);
  if (project == null) return Pair.create(null, null);
  Editor editor = e.getData(PlatformDataKeys.EDITOR);
  PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);
  return Pair.create(editor, psiFile);
}
 
Example #2
Source File: FilePathRelativeToSourcepathMacro.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public String expand(final DataContext dataContext) {
  final Project project = dataContext.getData(CommonDataKeys.PROJECT);
  if (project == null) {
    return null;
  }
  VirtualFile file = dataContext.getData(PlatformDataKeys.VIRTUAL_FILE);
  if (file == null) {
    return null;
  }
  final VirtualFile sourceRoot = ProjectRootManager.getInstance(project).getFileIndex().getSourceRootForFile(file);
  if (sourceRoot == null) {
    return null;
  }
  return FileUtil.getRelativePath(getIOFile(sourceRoot), getIOFile(file));
}
 
Example #3
Source File: BundleClassGeneratorUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Nullable
public static PsiDirectory getBundleDirContext(@NotNull AnActionEvent event) {

    Project project = event.getData(PlatformDataKeys.PROJECT);
    if (project == null) {
        return null;
    }

    DataContext dataContext = event.getDataContext();
    IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);
    if (view == null) {
        return null;
    }

    PsiDirectory[] directories = view.getDirectories();
    if(directories.length == 0) {
        return null;
    }

    return FilesystemUtil.findParentBundleFolder(directories[0]);
}
 
Example #4
Source File: FileTextRule.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public String getData(@Nonnull DataProvider dataProvider) {
  final VirtualFile virtualFile = dataProvider.getDataUnchecked(PlatformDataKeys.VIRTUAL_FILE);
  if (virtualFile == null) {
    return null;
  }

  final FileType fileType = virtualFile.getFileType();
  if (fileType.isBinary() || fileType.isReadOnly()) {
    return null;
  }

  final Project project = dataProvider.getDataUnchecked(CommonDataKeys.PROJECT);
  if (project == null) {
    return null;
  }

  final Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
  if (document == null) {
    return null;
  }

  return document.getText();
}
 
Example #5
Source File: RunMypyDaemon.java    From mypy-PyCharm-plugin with Apache License 2.0 6 votes vote down vote up
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
    Project project = e.getData(PlatformDataKeys.PROJECT);
    if (project == null) {
        return;
    }
    ToolWindow tw = ToolWindowManager.getInstance(project).getToolWindow(
            MypyToolWindowFactory.MYPY_PLUGIN_ID);
    if (!tw.isVisible()) {
        tw.show(null);
    }
    MypyTerminal terminal = MypyToolWindowFactory.getMypyTerminal(project);
    if (terminal == null) {
        return;
    }
    if (terminal.getRunner().isRunning()) {
        return;
    }
    terminal.runMypyDaemonUIWrapper();
}
 
Example #6
Source File: AbstractUploadCloudActionTest.java    From markdown-image-kit with MIT License 6 votes vote down vote up
/**
 * 获取 PSI 的几种方式
 *
 * @param e the e
 */
private void getPsiFile(AnActionEvent e) {
    // 从 action 中获取
    PsiFile psiFileFromAction = e.getData(LangDataKeys.PSI_FILE);
    Project project = e.getProject();
    if (project != null) {
        VirtualFile virtualFile = e.getData(PlatformDataKeys.VIRTUAL_FILE);
        if (virtualFile != null) {
            // 从 VirtualFile 获取
            PsiFile psiFileFromVirtualFile = PsiManager.getInstance(project).findFile(virtualFile);

            // 从 document
            Document documentFromEditor = Objects.requireNonNull(e.getData(PlatformDataKeys.EDITOR)).getDocument();
            PsiFile psiFileFromDocument = PsiDocumentManager.getInstance(project).getPsiFile(documentFromEditor);

            // 在 project 范围内查找特定 PsiFile
            FilenameIndex.getFilesByName(project, "fileName", GlobalSearchScope.projectScope(project));
        }
    }

    // 找到特定 PSI 元素的使用位置
    // ReferencesSearch.search();
}
 
Example #7
Source File: MergePanel2.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public Object getData(@Nonnull Key<?> dataId) {
  if (FocusDiffSide.DATA_KEY == dataId) {
    int index = getFocusedEditorIndex();
    if (index < 0) return null;
    switch (index) {
      case 0:
        return new BranchFocusedSide(FragmentSide.SIDE1);
      case 1:
        return new MergeFocusedSide();
      case 2:
        return new BranchFocusedSide(FragmentSide.SIDE2);
    }
  }
  else if (PlatformDataKeys.DIFF_VIEWER == dataId) return MergePanel2.this;
  return super.getData(dataId);
}
 
Example #8
Source File: SymfonyContainerServiceBuilder.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
public void update(AnActionEvent event) {
    Project project = event.getData(PlatformDataKeys.PROJECT);

    if (project == null || !Symfony2ProjectComponent.isEnabled(project)) {
        this.setStatus(event, false);
        return;
    }

    Pair<PsiFile, PhpClass> pair = findPhpClass(event);
    if(pair == null) {
        return;
    }

    PsiFile psiFile = pair.getFirst();
    if(!(psiFile instanceof YAMLFile) && !(psiFile instanceof XmlFile) && !(psiFile instanceof PhpFile)) {
        this.setStatus(event, false);
    }
}
 
Example #9
Source File: SourcepathEntryMacro.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public String expand(final DataContext dataContext) {
  final Project project = dataContext.getData(CommonDataKeys.PROJECT);
  if (project == null) {
    return null;
  }
  VirtualFile file = dataContext.getData(PlatformDataKeys.VIRTUAL_FILE);
  if (file == null) {
    return null;
  }
  final VirtualFile sourceRoot = ProjectRootManager.getInstance(project).getFileIndex().getSourceRootForFile(file);
  if (sourceRoot == null) {
    return null;
  }
  return getPath(sourceRoot);
}
 
Example #10
Source File: GoogleTranslation.java    From GoogleTranslation with Apache License 2.0 6 votes vote down vote up
private void getTranslation(AnActionEvent event) {
    Editor editor = event.getData(PlatformDataKeys.EDITOR);
    if (editor == null) {
        return;
    }
    SelectionModel model = editor.getSelectionModel();
    String selectedText = model.getSelectedText();
    if (TextUtils.isEmpty(selectedText)) {
        selectedText = getCurrentWords(editor);
        if (TextUtils.isEmpty(selectedText)) {
            return;
        }
    }
    String queryText = strip(addBlanks(selectedText));
    new Thread(new RequestRunnable(mTranslator, editor, queryText)).start();
}
 
Example #11
Source File: PsiElementFromSelectionsRule.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public PsiElement[] getData(@Nonnull DataProvider dataProvider) {
  final Object[] objects = dataProvider.getDataUnchecked(PlatformDataKeys.SELECTED_ITEMS);
  if (objects != null) {
    final PsiElement[] elements = new PsiElement[objects.length];
    for (int i = 0, objectsLength = objects.length; i < objectsLength; i++) {
      Object object = objects[i];
      if (!(object instanceof PsiElement)) return null;
      if (!((PsiElement)object).isValid()) return null;
      elements[i] = (PsiElement)object;
    }

    return elements;
  }
  return null;
}
 
Example #12
Source File: ColorAndFontOptions.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static boolean edit(DataContext context, String search, Function<ColorAndFontOptions, SearchableConfigurable> function) {
  ColorAndFontOptions options = new ColorAndFontOptions();
  SearchableConfigurable page = function.apply(options);

  Configurable[] configurables = options.getConfigurables();
  try {
    if (page != null) {
      Runnable runnable = search == null ? null : page.enableSearch(search);
      Window window = UIUtil.getWindow(context.getData(PlatformDataKeys.CONTEXT_COMPONENT));
      if (window != null) {
        ShowSettingsUtil.getInstance().editConfigurable(window, page, runnable);
      }
      else {
        ShowSettingsUtil.getInstance().editConfigurable(context.getData(CommonDataKeys.PROJECT), page, runnable);
      }
    }
  }
  finally {
    for (Configurable configurable : configurables) configurable.disposeUIResources();
    options.disposeUIResources();
  }
  return page != null;
}
 
Example #13
Source File: NoSqlDatabaseConsoleAction.java    From nosql4idea with Apache License 2.0 6 votes vote down vote up
@Override
public void update(AnActionEvent e) {
    final Project project = e.getData(PlatformDataKeys.PROJECT);

    boolean enabled = project != null;
    if (!enabled) {
        return;
    }

    NoSqlConfiguration configuration = NoSqlConfiguration.getInstance(project);

    e.getPresentation().setVisible(
            configuration != null &&
                    (StringUtils.isNotBlank(configuration.getShellPath(DatabaseVendor.MONGO)) ||
                            StringUtils.isNotBlank(configuration.getShellPath(DatabaseVendor.REDIS))
                    ) &&
                    noSqlExplorerPanel.getConfiguration() != null &&
                    noSqlExplorerPanel.getConfiguration().isSingleServer()
    );
    e.getPresentation().setEnabled(
            noSqlExplorerPanel.getSelectedMongoDatabase() != null ||
                    noSqlExplorerPanel.getSelectedRedisDatabase() != null
    );
}
 
Example #14
Source File: PullCommand.java    From ADB-Duang with MIT License 6 votes vote down vote up
private void selectInTargetFile(final VirtualFile targetFile) {
    UIUtil.invokeLaterIfNeeded(new Runnable() {
        public void run() {
            Project project = deviceResult.anActionEvent.getProject();
            Editor editor = deviceResult.anActionEvent.getData(PlatformDataKeys.EDITOR);
            MySelectInContext selectInContext = new MySelectInContext(targetFile, editor, project);
            ProjectViewImpl projectView = (ProjectViewImpl) ProjectView.getInstance(project);
            AbstractProjectViewPane currentProjectViewPane = projectView.getCurrentProjectViewPane();
            SelectInTarget target = currentProjectViewPane.createSelectInTarget();
            if (target != null && target.canSelect(selectInContext)) {
                target.selectIn(selectInContext, false);
            } else {
                selectInContext = new MySelectInContext(targetFile.getParent(), editor, project);
                if (target != null && target.canSelect(selectInContext)) {
                    target.selectIn(selectInContext, false);
                }
            }
        }
    });
}
 
Example #15
Source File: InlineRefactoringActionHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void invoke(@Nonnull Project project, @Nonnull PsiElement[] elements, DataContext dataContext) {
  LOG.assertTrue(elements.length == 1);
  if (dataContext == null) {
    dataContext = DataManager.getInstance().getDataContext();
  }
  final Editor editor = dataContext.getData(PlatformDataKeys.EDITOR);
  for(InlineActionHandler handler: Extensions.getExtensions(InlineActionHandler.EP_NAME)) {
    if (handler.canInlineElement(elements[0])) {
      handler.inlineElement(project, editor, elements [0]);
      return;
    }
  }

  invokeInliner(editor, elements[0]);
}
 
Example #16
Source File: AceJumpAndReplaceRangeAction.java    From emacsIDEAs with Apache License 2.0 6 votes vote down vote up
@Override
public void actionPerformed(AnActionEvent e) {
    Editor editor = e.getData(PlatformDataKeys.EDITOR);
    String actionId = e.getActionManager().getId(this);
    String selectorClassName = "org.hunmr.common.selector."
                            + actionId.substring("emacsIDEAs.AceJumpAndReplace.".length())
                            + "Selector";

    try {
        Class<? extends Selector> selectorClass = (Class<? extends Selector>) Class.forName(selectorClassName);
        EditorUtils.copyRange(selectorClass, editor);

        AceJumpAction.getInstance().switchEditorIfNeed(e);
        AceJumpAction.getInstance().addCommandAroundJump(new ReplaceAfterJumpCommand(editor, selectorClass));
        AceJumpAction.getInstance().performAction(e);


    } catch (ClassNotFoundException e1) {
        e1.printStackTrace();
    }
}
 
Example #17
Source File: XmlInstantSorterAction.java    From android-xml-sorter with Apache License 2.0 6 votes vote down vote up
@Override
public void actionPerformed(AnActionEvent event) {

    final Project project = getEventProject(event);
    final Editor editor = event.getData(PlatformDataKeys.EDITOR);
    XmlSorterDialog dialog = new XmlSorterDialog(project);

    PropertiesComponent pc = PropertiesComponent.getInstance();
    execute(project,
            editor,
            pc.getInt(PC_KEY_INPUT_CASE, 0) == 0,
            dialog.getPrefixSpacePositionValueAt(pc.getInt(PC_KEY_PREFIX_SPACE_POS, 0)),
            pc.getBoolean(PC_KEY_SPACE_BETWEEN_PREFIX, true),
            pc.getBoolean(PC_KEY_INSERT_XML_INFO, true),
            pc.getBoolean(PC_KEY_DELETE_COMMENT, false),
            dialog.getCodeIndentValueAt(pc.getInt(PC_KEY_CODE_INDENT, 1)),
            pc.getBoolean(PC_KEY_SEPARATE_NON_TRANSLATABLE, false));
}
 
Example #18
Source File: FileDirRelativeToSourcepathMacro.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public String expand(final DataContext dataContext) {
  final Project project = dataContext.getData(CommonDataKeys.PROJECT);
  if (project == null) {
    return null;
  }
  VirtualFile file = dataContext.getData(PlatformDataKeys.VIRTUAL_FILE);
  if (file == null) {
    return null;
  }
  if (!file.isDirectory()) {
    file = file.getParent();
    if (file == null) {
      return null;
    }
  }
  final VirtualFile sourceRoot = ProjectRootManager.getInstance(project).getFileIndex().getSourceRootForFile(file);
  if (sourceRoot == null) return null;
  return FileUtil.getRelativePath(getIOFile(sourceRoot), getIOFile(file));
}
 
Example #19
Source File: DesktopEditorComposite.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public final Object getData(@Nonnull Key<?> dataId) {
  if (PlatformDataKeys.FILE_EDITOR == dataId) {
    return getSelectedEditor();
  }
  else if (CommonDataKeys.VIRTUAL_FILE == dataId) {
    return myFile.isValid() ? myFile : null;
  }
  else if (CommonDataKeys.VIRTUAL_FILE_ARRAY == dataId) {
    return myFile.isValid() ? new VirtualFile[]{myFile} : null;
  }
  else {
    JComponent component = getPreferredFocusedComponent();
    if (component instanceof DataProvider && component != this) {
      return ((DataProvider)component).getData(dataId);
    }
    return null;
  }
}
 
Example #20
Source File: DesktopContentManagerImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
@Nullable
public Object getData(@Nonnull @NonNls Key<?> dataId) {
  if (PlatformDataKeys.CONTENT_MANAGER == dataId || PlatformDataKeys.NONEMPTY_CONTENT_MANAGER == dataId && getContentCount() > 1) {
    return DesktopContentManagerImpl.this;
  }

  for (DataProvider dataProvider : myDataProviders) {
    Object data = dataProvider.getData(dataId);
    if (data != null) {
      return data;
    }
  }

  if (myUI instanceof DataProvider) {
    return ((DataProvider)myUI).getData(dataId);
  }

  DataProvider provider = DataManager.getDataProvider(this);
  return provider == null ? null : provider.getData(dataId);
}
 
Example #21
Source File: LayoutAction.java    From android-codegenerator-plugin-intellij with Apache License 2.0 5 votes vote down vote up
private void showCodeDialog(AnActionEvent event, final Project project, final VirtualFile selectedFile, Settings settings) throws ParserConfigurationException, SAXException, XPathExpressionException, IOException {
    CodeGeneratorController codeGeneratorController = new CodeGeneratorController(getTemplateName(), getResourceProvidersFactory());
    String generatedCode = codeGeneratorController.generateCode(project, selectedFile, event.getData(PlatformDataKeys.EDITOR));
    final CodeDialogBuilder codeDialogBuilder = new CodeDialogBuilder(project,
            String.format(StringResources.TITLE_FORMAT_TEXT, selectedFile.getName()), generatedCode);
    codeDialogBuilder.addSourcePathSection(projectHelper.getSourceRootPathList(project, event), settings.getSourcePath());
    codeDialogBuilder.addPackageSection(packageHelper.getPackageName(project, event));
    codeDialogBuilder.addAction(StringResources.COPY_ACTION_LABEL, new Runnable() {
        @Override
        public void run() {
            ClipboardHelper.copy(getFinalCode(codeDialogBuilder));
            codeDialogBuilder.closeDialog();
        }
    });
    codeDialogBuilder.addAction(StringResources.CREATE_ACTION_LABEL, new Runnable() {
        @Override
        public void run() {
            try {
                createFileWithGeneratedCode(codeDialogBuilder, selectedFile, project);
            } catch (IOException exception) {
                errorHandler.handleError(project, exception);
            }
        }
    }, true);
    if (codeDialogBuilder.showDialog() == DialogWrapper.OK_EXIT_CODE) {
        settings.setSourcePath(codeDialogBuilder.getSourcePath());
    }
}
 
Example #22
Source File: ExpandAll.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void actionPerformed(AnActionEvent e) {
  final Component c = e.getData(PlatformDataKeys.CONTEXT_COMPONENT);
  if (c != null) {
    final JTree tree = UIUtil.getParentOfType(JTree.class, c);
    if (tree != null) {
      TreeUtil.expandAll(tree);
    }
  }
}
 
Example #23
Source File: EditorBasedStatusBarPopup.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
protected DataContext getContext() {
  Editor editor = getEditor();
  DataContext parent = DataManager.getInstance().getDataContext((Component)myStatusBar);
  VirtualFile selectedFile = getSelectedFile();
  return SimpleDataContext.getSimpleContext(ContainerUtil.<Key, Object>immutableMapBuilder().put(CommonDataKeys.VIRTUAL_FILE, selectedFile)
                                                    .put(CommonDataKeys.VIRTUAL_FILE_ARRAY, selectedFile == null ? VirtualFile.EMPTY_ARRAY : new VirtualFile[]{selectedFile})
                                                    .put(CommonDataKeys.PROJECT, getProject()).put(PlatformDataKeys.CONTEXT_COMPONENT, editor == null ? null : editor.getComponent()).build(),
                                            parent);
}
 
Example #24
Source File: EmacsIdeasAction.java    From emacsIDEAs with Apache License 2.0 5 votes vote down vote up
private Editor getEditorFrom(AnActionEvent e) {
    if (e instanceof ChainActionEvent) {
        ChainActionEvent chainActionEvent = (ChainActionEvent) e;
        Editor editor = chainActionEvent.getEditor();
        if (editor != null) {
            return editor;
        }
    }

    return e.getData(PlatformDataKeys.EDITOR);
}
 
Example #25
Source File: DesktopPsiAwareTextEditorImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public Object getData(@Nonnull final Key<?> dataId) {
  if (PlatformDataKeys.DOMINANT_HINT_AREA_RECTANGLE == dataId) {
    final LookupImpl lookup = (LookupImpl)LookupManager.getInstance(myProject).getActiveLookup();
    if (lookup != null && lookup.isVisible()) {
      return lookup.getBounds();
    }
  }
  if (LangDataKeys.MODULE == dataId) {
    return ModuleUtilCore.findModuleForFile(myFile, myProject);
  }
  return super.getData(dataId);
}
 
Example #26
Source File: SubmitPasteAction.java    From tmc-intellij with MIT License 5 votes vote down vote up
private void paste(AnActionEvent anActionEvent) {
    new ButtonInputListener().receivePastebin();
    logger.info("Showing paste submit form. @SubmitPasteAction");
    if (pasteService == null
            || pasteService.getWindow() == null
            || pasteService.getWindow().isClosed()) {
        Project project = anActionEvent.getData(PlatformDataKeys.PROJECT);
        pasteService = new PasteService();
        pasteService.showSubmitForm(project, TmcCoreHolder.get());
    } else {
        pasteService.getWindow().show();
    }
}
 
Example #27
Source File: NavBarListWrapper.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
@Nullable
public Object getData(@Nonnull @NonNls Key<?> dataId) {
  if (PlatformDataKeys.SELECTED_ITEM == dataId){
    return myList.getSelectedValue();
  }
  if (PlatformDataKeys.SELECTED_ITEMS == dataId){
    return myList.getSelectedValues();
  }
  return null;
}
 
Example #28
Source File: BrowseChangesAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static boolean isActionEnabled(final AnActionEvent e) {
  Project project = e.getData(CommonDataKeys.PROJECT);
  if (project == null) return false;
  VirtualFile vFile = e.getData(PlatformDataKeys.VIRTUAL_FILE);
  if (vFile == null) return false;
  AbstractVcs vcs = ProjectLevelVcsManager.getInstance(project).getVcsFor(vFile);
  if (vcs == null || vcs.getCommittedChangesProvider() == null || !vcs.allowsRemoteCalls(vFile)) {
    return false;
  }
  FilePath filePath = VcsContextFactory.SERVICE.getInstance().createFilePathOn(vFile);
  return AbstractVcs.fileInVcsByFileStatus(project, filePath);
}
 
Example #29
Source File: ExtractWidgetAction.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected static boolean isVisibleFor(AnActionEvent e) {
  final DataContext dataContext = e.getDataContext();
  final Project project = dataContext.getData(PlatformDataKeys.PROJECT);
  final VirtualFile file = dataContext.getData(PlatformDataKeys.VIRTUAL_FILE);
  if (file == null || !FlutterUtils.isDartFile(file)) {
    return false;
  }

  // TODO(pq): calculating flutteriness is too expensive to happen in a call to update; we should cache the result in the datacontext.
  return true;
}
 
Example #30
Source File: AceJumpSelectAction.java    From emacsIDEAs with Apache License 2.0 5 votes vote down vote up
@Override
public void actionPerformed(AnActionEvent e) {
    Editor editor = e.getData(PlatformDataKeys.EDITOR);
    AceJumpAction.getInstance().switchEditorIfNeed(e);
    AceJumpAction.getInstance().addCommandAroundJump(new SelectAfterJumpCommand(editor));
    AceJumpAction.getInstance().performAction(e);
}