Java Code Examples for com.intellij.psi.PsiFileSystemItem#getParent()

The following examples show how to use com.intellij.psi.PsiFileSystemItem#getParent() . 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: FilePathCompletionContributor.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static boolean fileMatchesPathPrefix(@Nullable final PsiFileSystemItem file, @Nonnull final List<String> pathPrefix) {
  if (file == null) return false;

  final List<String> contextParts = new ArrayList<>();
  PsiFileSystemItem parentFile = file;
  PsiFileSystemItem parent;
  while ((parent = parentFile.getParent()) != null) {
    if (parent.getName().length() > 0) contextParts.add(0, parent.getName().toLowerCase());
    parentFile = parent;
  }

  final String path = StringUtil.join(contextParts, "/");

  int nextIndex = 0;
  for (@NonNls final String s : pathPrefix) {
    if ((nextIndex = path.indexOf(s.toLowerCase(), nextIndex)) == -1) return false;
  }

  return true;
}
 
Example 2
Source File: PsiFileUtils.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
static public List<PsiFileSystemItem> getRange(PsiFileSystemItem from, PsiFileSystemItem to) {
  LinkedList<PsiFileSystemItem> items = new LinkedList<PsiFileSystemItem>();
  PsiFileSystemItem current = to;
  while (current != null) {
    items.addFirst(current);
    if (current == from) break;
    current = current.getParent();
  }
  return items;
}
 
Example 3
Source File: PsiFileUtils.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
@Nullable
static public PsiFileSystemItem findRoot(@NotNull PsiFileSystemItem file) {
  //HaxelibModuleManager
  Project project = file.getProject();
  Module[] modules = ModuleManager.getInstance(project).getModules();
  List<VirtualFile> roots = new ArrayList<VirtualFile>();
  for (Module module : modules) {
    Sdk sdk = ModuleRootManager.getInstance(module).getSdk();
    if (null != sdk) {
      roots.addAll(Arrays.asList(sdk.getRootProvider().getFiles(OrderRootType.CLASSES)));
    }
    // issue #387: add roots from external libraries
    for (OrderEntry entry : ModuleRootManager.getInstance(module).getOrderEntries()) {
      Collections.addAll(roots, entry.getFiles(OrderRootType.CLASSES));
    }
  }
  roots.addAll(Arrays.asList(ProjectRootManager.getInstance(project).getContentSourceRoots()));

  PsiFileSystemItem current = file;
  while (current != null) {
    for (VirtualFile root : roots) {
      String rootCanonicalPath = root.getCanonicalPath();
      String currentCanonicalPath = current.getVirtualFile().getCanonicalPath();

      if (rootCanonicalPath != null && currentCanonicalPath != null && currentCanonicalPath.equals(rootCanonicalPath)) {
        return current;
      }
    }
    current = current.getParent();
  }
  return null;
}
 
Example 4
Source File: NullFileReferenceHelper.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
public Collection<PsiFileSystemItem> getContexts(final Project project, @Nonnull final VirtualFile file) {
  final PsiFileSystemItem item = getPsiFileSystemItem(project, file);
  if (item != null) {
    final PsiFileSystemItem parent = item.getParent();
    if (parent != null) {
      return Collections.singleton(parent);
    }
  }
  return Collections.emptyList();
}
 
Example 5
Source File: PsiFileSystemItemUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
static PsiFileSystemItem[] getPathComponents(PsiFileSystemItem file) {
  LinkedList<PsiFileSystemItem> componentsList = new LinkedList<PsiFileSystemItem>();
  while (file != null) {
    componentsList.addFirst(file);
    file = file.getParent();
  }
  return componentsList.toArray(new PsiFileSystemItem[componentsList.size()]);
}
 
Example 6
Source File: PsiFileSystemItemUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static String getRelativePathFromAncestor(PsiFileSystemItem file, PsiFileSystemItem ancestor) {
  int length = 0;
  PsiFileSystemItem parent = file;

  while (true) {
    if (parent == null) return null;
    if (parent.equals(ancestor)) break;
    if (length > 0) {
      length++;
    }
    length += parent.getName().length();
    parent = parent.getParent();
  }

  char[] chars = new char[length];
  int index = chars.length;
  parent = file;

  while (true) {
    if (parent.equals(ancestor)) break;
    if (index < length) {
      chars[--index] = '/';
    }
    String name = parent.getName();
    for (int i = name.length() - 1; i >= 0; i--) {
      chars[--index] = name.charAt(i);
    }
    parent = parent.getParent();
  }
  return StringFactory.createShared(chars);
}
 
Example 7
Source File: GotoFileCellRenderer.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected String getContainerText(PsiFileSystemItem element, String name) {
  PsiFileSystemItem parent = element.getParent();
  final PsiDirectory psiDirectory = parent instanceof PsiDirectory ? (PsiDirectory)parent : null;
  if (psiDirectory == null) return null;
  final VirtualFile virtualFile = psiDirectory.getVirtualFile();
  final String relativePath = getRelativePath(virtualFile, element.getProject());
  if (relativePath == null) return "( " + File.separator + " )";
  String path = FilePathSplittingPolicy.SPLIT_BY_SEPARATOR.getOptimalTextForComponent(name + "          ", new File(relativePath), this, myMaxWidth);
  return "(" + path + ")";
}
 
Example 8
Source File: BaseIconProvider.java    From intellij-extra-icons-plugin with MIT License 4 votes vote down vote up
private String parent(@NotNull PsiFileSystemItem fileSystemItem) {
    return fileSystemItem.getParent() == null ? null : fileSystemItem.getParent().getName().toLowerCase();
}