Java Code Examples for com.intellij.openapi.actionSystem.DataProvider#getDataUnchecked()

The following examples show how to use com.intellij.openapi.actionSystem.DataProvider#getDataUnchecked() . 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: 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 2
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 3
Source File: NavigatableRule.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public Navigatable getData(@Nonnull DataProvider dataProvider) {
  final Navigatable navigatable = dataProvider.getDataUnchecked(PlatformDataKeys.NAVIGATABLE);
  if (navigatable != null && navigatable instanceof OpenFileDescriptor) {
    final OpenFileDescriptor openFileDescriptor = (OpenFileDescriptor)navigatable;

    if (openFileDescriptor.getFile().isValid()) {
      return openFileDescriptor;
    }
  }
  final PsiElement element = dataProvider.getDataUnchecked(LangDataKeys.PSI_ELEMENT);
  if (element instanceof Navigatable) {
    return (Navigatable)element;
  }
  if (element != null) {
    return EditSourceUtil.getDescriptor(element);
  }

  final Object selection = dataProvider.getDataUnchecked(PlatformDataKeys.SELECTED_ITEM);
  if (selection instanceof Navigatable) {
    return (Navigatable)selection;
  }

  return null;
}
 
Example 4
Source File: VcsRevisionNumberArrayRule.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
public List<VcsRevisionNumber> getRevisionNumbers(@Nonnull DataProvider dataProvider) {
  VcsRevisionNumber revisionNumber = dataProvider.getDataUnchecked(VcsDataKeys.VCS_REVISION_NUMBER);
  if (revisionNumber != null) {
    return Collections.singletonList(revisionNumber);
  }

  ChangeList[] changeLists = dataProvider.getDataUnchecked(VcsDataKeys.CHANGE_LISTS);
  if (changeLists != null && changeLists.length > 0) {
    List<CommittedChangeList> committedChangeLists = ContainerUtil.findAll(changeLists, CommittedChangeList.class);

    if (!committedChangeLists.isEmpty()) {
      ContainerUtil.sort(committedChangeLists, CommittedChangeListByDateComparator.DESCENDING);

      return ContainerUtil.mapNotNull(committedChangeLists, CommittedChangeListToRevisionNumberFunction.INSTANCE);
    }
  }

  VcsFileRevision[] fileRevisions = dataProvider.getDataUnchecked(VcsDataKeys.VCS_FILE_REVISIONS);
  if (fileRevisions != null && fileRevisions.length > 0) {
    return ContainerUtil.mapNotNull(fileRevisions, FileRevisionToRevisionNumberFunction.INSTANCE);
  }

  return null;
}
 
Example 5
Source File: VirtualFileStreamRule.java    From consulo with Apache License 2.0 5 votes vote down vote up
@javax.annotation.Nullable
@Override
public Stream<VirtualFile> getData(@Nonnull DataProvider dataProvider) {
  VirtualFile[] files = dataProvider.getDataUnchecked(CommonDataKeys.VIRTUAL_FILE_ARRAY);
  if (files != null) {
    return Stream.of(files);
  }

  VirtualFile file = dataProvider.getDataUnchecked(CommonDataKeys.VIRTUAL_FILE);
  if (file != null) {
    return Stream.of(file);
  }

  return null;
}
 
Example 6
Source File: PsiFileRule.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public PsiFile getData(@Nonnull DataProvider dataProvider) {
  final PsiElement element = dataProvider.getDataUnchecked(LangDataKeys.PSI_ELEMENT);
  if (element != null) {
    return element.getContainingFile();
  }
  Project project = dataProvider.getDataUnchecked(CommonDataKeys.PROJECT);
  if (project != null) {
    VirtualFile vFile = dataProvider.getDataUnchecked(PlatformDataKeys.VIRTUAL_FILE);
    if (vFile != null) {
      return PsiManager.getInstance(project).findFile(vFile);
    }
  }
  return null;
}
 
Example 7
Source File: UsageInfo2ListRule.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
@Nullable
public List<UsageInfo> getData(@Nonnull final DataProvider dataProvider) {
  UsageInfo usageInfo = dataProvider.getDataUnchecked(UsageView.USAGE_INFO_KEY);
  if (usageInfo != null) return Collections.singletonList(usageInfo);
  return null;
}
 
Example 8
Source File: PsiElementFromSelectionRule.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public PsiElement getData(@Nonnull DataProvider dataProvider) {
  final Object element = dataProvider.getDataUnchecked(PlatformDataKeys.SELECTED_ITEM);
  if (element instanceof PsiElement) {
    PsiElement psiElement = (PsiElement)element;
    if (psiElement.isValid()) {
      return (PsiElement)element;
    }
  }

  return null;
}
 
Example 9
Source File: ModuleRule.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public Module getData(@Nonnull DataProvider dataProvider) {
  Module moduleContext = dataProvider.getDataUnchecked(LangDataKeys.MODULE_CONTEXT);
  if (moduleContext != null) {
    return moduleContext;
  }
  Project project = dataProvider.getDataUnchecked(CommonDataKeys.PROJECT);
  if (project == null) {
    PsiElement element = dataProvider.getDataUnchecked(LangDataKeys.PSI_ELEMENT);
    if (element == null || !element.isValid()) return null;
    project = element.getProject();
  }

  VirtualFile virtualFile = dataProvider.getDataUnchecked(PlatformDataKeys.VIRTUAL_FILE);
  if (virtualFile == null) {
    GetDataRule<VirtualFile> dataRule = ((BaseDataManager)DataManager.getInstance()).getDataRule(PlatformDataKeys.VIRTUAL_FILE);
    if (dataRule != null) {
      virtualFile = dataRule.getData(dataProvider);
    }
  }

  if (virtualFile == null) {
    return null;
  }

  return ModuleUtil.findModuleForFile(virtualFile, project);
}
 
Example 10
Source File: DiffNavigatableArrayRule.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public Navigatable[] getData(@Nonnull DataProvider dataProvider) {
  final Navigatable element = dataProvider.getDataUnchecked(DiffDataKeys.NAVIGATABLE);
  if (element == null) {
    return null;
  }

  return new Navigatable[]{element};
}
 
Example 11
Source File: ProjectFileDirectoryRule.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public VirtualFile getData(@Nonnull DataProvider dataProvider) {
  VirtualFile dir = dataProvider.getDataUnchecked(PlatformDataKeys.PROJECT_FILE_DIRECTORY);
  if (dir == null) {
    final Project project = dataProvider.getDataUnchecked(CommonDataKeys.PROJECT);
    if (project != null) {
      dir = project.getBaseDir();
    }
  }
  return dir;
}
 
Example 12
Source File: FileEditorRule.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public FileEditor getData(@Nonnull DataProvider dataProvider) {
  final Editor editor = dataProvider.getDataUnchecked(PlatformDataKeys.EDITOR);
  if (editor == null) {
    return null;
  }

  final Boolean aBoolean = editor.getUserData(EditorTextField.SUPPLEMENTARY_KEY);
  if (aBoolean != null && aBoolean.booleanValue()) {
    return null;
  }

  return TextEditorProvider.getInstance().getTextEditor(editor);
}
 
Example 13
Source File: CopyProviderRule.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public CopyProvider getData(@Nonnull DataProvider dataProvider) {
  final Editor editor = dataProvider.getDataUnchecked(PlatformDataKeys.EDITOR);
  if (editor instanceof EditorEx) {
    return ((EditorEx) editor).getCopyProvider();
  }
  return null;
}
 
Example 14
Source File: PasteProviderRule.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public PasteProvider getData(@Nonnull DataProvider dataProvider) {
  final Editor editor = dataProvider.getDataUnchecked(PlatformDataKeys.EDITOR);
  if (editor instanceof EditorEx) {
    return ((EditorEx) editor).getPasteProvider();
  }
  return null;
}
 
Example 15
Source File: CutProviderRule.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public CutProvider getData(@Nonnull DataProvider dataProvider) {
  final Editor editor = dataProvider.getDataUnchecked(PlatformDataKeys.EDITOR);
  if (editor instanceof EditorEx) {
    return ((EditorEx)editor).getCutProvider();
  }
  return null;
}
 
Example 16
Source File: NavigatableArrayRule.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public Navigatable[] getData(@Nonnull DataProvider dataProvider) {
  final Navigatable element = dataProvider.getDataUnchecked(PlatformDataKeys.NAVIGATABLE);
  if (element == null) {
    return null;
  }

  return new Navigatable[]{element};
}
 
Example 17
Source File: DockableEditorTabbedContainer.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void add(@Nonnull DockableContent content, RelativePoint dropTarget) {
  EditorWindow window = null;
  if (myCurrentOver != null) {
    final DataProvider provider = myCurrentOver.getDataProvider();
    if (provider != null) {
      window = provider.getDataUnchecked(EditorWindow.DATA_KEY);
    }
  }

  final EditorTabbedContainer.DockableEditor dockableEditor = (EditorTabbedContainer.DockableEditor)content;
  VirtualFile file = dockableEditor.getFile();


  if (window == null || window.isDisposed()) {
    window = mySplitters.getOrCreateCurrentWindow(file);
  }


  if (myCurrentOver != null) {
    int index = ((JBTabsImpl)myCurrentOver).getDropInfoIndex();
    file.putUserData(DesktopEditorWindow.INITIAL_INDEX_KEY, index);
  }

  ((FileEditorManagerImpl)FileEditorManagerEx.getInstanceEx(myProject)).openFileImpl2(UIAccess.get(), window, file, true);
  window.setFilePinned(file, dockableEditor.isPinned());
}
 
Example 18
Source File: InactiveEditorRule.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
@Nullable
public Editor getData(@Nonnull final DataProvider dataProvider) {
  return dataProvider.getDataUnchecked(PlatformDataKeys.EDITOR);
}
 
Example 19
Source File: PasteTargetRule.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public PsiElement getData(@Nonnull DataProvider dataProvider) {
  return dataProvider.getDataUnchecked(LangDataKeys.PSI_ELEMENT);
}
 
Example 20
Source File: VirtualFileArrayRule.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public VirtualFile[] getData(@Nonnull final DataProvider dataProvider) {
  // Try to detect multiselection.

  Project project = dataProvider.getDataUnchecked(PlatformDataKeys.PROJECT_CONTEXT);
  if (project != null && !project.isDisposed()) {
    return ProjectRootManager.getInstance(project).getContentRoots();
  }

  Module[] selectedModules = dataProvider.getDataUnchecked(LangDataKeys.MODULE_CONTEXT_ARRAY);
  if (selectedModules != null && selectedModules.length > 0) {
    return getFilesFromModules(selectedModules);
  }

  Module selectedModule = dataProvider.getDataUnchecked(LangDataKeys.MODULE_CONTEXT);
  if (selectedModule != null && !selectedModule.isDisposed()) {
    return ModuleRootManager.getInstance(selectedModule).getContentRoots();
  }

  PsiElement[] psiElements = dataProvider.getDataUnchecked(LangDataKeys.PSI_ELEMENT_ARRAY);
  if (psiElements != null && psiElements.length != 0) {
    return getFilesFromPsiElements(psiElements);
  }

  // VirtualFile -> VirtualFile[]
  VirtualFile vFile = dataProvider.getDataUnchecked(PlatformDataKeys.VIRTUAL_FILE);
  if (vFile != null) {
    return new VirtualFile[]{vFile};
  }

  //

  PsiFile psiFile = dataProvider.getDataUnchecked(LangDataKeys.PSI_FILE);
  if (psiFile != null && psiFile.getVirtualFile() != null) {
    return new VirtualFile[]{psiFile.getVirtualFile()};
  }

  PsiElement elem = dataProvider.getDataUnchecked(LangDataKeys.PSI_ELEMENT);
  if (elem != null) {
    return getFilesFromPsiElement(elem);
  }

  Usage[] usages = dataProvider.getDataUnchecked(UsageView.USAGES_KEY);
  UsageTarget[] usageTargets = dataProvider.getDataUnchecked(UsageView.USAGE_TARGETS_KEY);
  if (usages != null || usageTargets != null) {
    return UsageDataUtil.provideVirtualFileArray(usages, usageTargets);
  }

  return null;
}