use of com.jetbrains.lang.dart.util.DartUrlResolver in project intellij-plugins by JetBrains.
the class DartTestRunConfigurationProducer method isFileInTestDirAndTestPackageExists.
public static boolean isFileInTestDirAndTestPackageExists(@NotNull final Project project, @NotNull final VirtualFile file) {
final DartUrlResolver urlResolver = DartUrlResolver.getInstance(project, file);
final VirtualFile dartTestLib = urlResolver.findFileByDartUrl("package:test/test.dart");
if (dartTestLib == null)
return false;
final VirtualFile pubspec = urlResolver.getPubspecYamlFile();
final VirtualFile rootDir = pubspec == null ? null : pubspec.getParent();
final VirtualFile testDir = rootDir == null ? null : rootDir.findChild("test");
return testDir != null && testDir.isDirectory() && VfsUtilCore.isAncestor(testDir, file, true);
}
use of com.jetbrains.lang.dart.util.DartUrlResolver in project intellij-plugins by JetBrains.
the class DartPackageAwareFileReference method bindToElement.
@Override
public PsiElement bindToElement(@NotNull final PsiElement element, final boolean absolute) throws IncorrectOperationException {
final String path = getFileReferenceSet().getPathString();
if (path.startsWith(PACKAGES_FOLDER_NAME + "/") || path.contains("/" + PACKAGES_FOLDER_NAME + "/")) {
final VirtualFile contextFile = DartResolveUtil.getRealVirtualFile(getElement().getContainingFile());
final VirtualFile targetFile = DartResolveUtil.getRealVirtualFile(element.getContainingFile());
if (contextFile != null && targetFile != null) {
final DartUrlResolver urlResolver = DartUrlResolver.getInstance(element.getProject(), contextFile);
final String newUrl = urlResolver.getDartUrlForFile(targetFile);
if (newUrl.startsWith(PACKAGE_PREFIX)) {
final int index = path.startsWith(PACKAGES_FOLDER_NAME + "/") ? 0 : (path.indexOf("/" + PACKAGES_FOLDER_NAME + "/") + 1);
final String newName = path.substring(0, index) + PACKAGES_FOLDER_NAME + "/" + newUrl.substring(PACKAGE_PREFIX.length());
return rename(newName);
}
}
}
return super.bindToElement(element, absolute);
}
use of com.jetbrains.lang.dart.util.DartUrlResolver in project intellij-plugins by JetBrains.
the class DartTreeStructureProvider method modify.
@NotNull
public Collection<AbstractTreeNode> modify(@NotNull final AbstractTreeNode parentNode, @NotNull final Collection<AbstractTreeNode> children, final ViewSettings settings) {
if (parentNode instanceof ExternalLibrariesNode) {
return ContainerUtil.map(children, node -> {
if (node instanceof NamedLibraryElementNode && (DartPackagesLibraryType.DART_PACKAGES_LIBRARY_NAME.equals(node.getName()) || DartSdk.DART_SDK_LIB_NAME.equals(node.getName()))) {
final boolean isSdkRoot = DartSdk.DART_SDK_LIB_NAME.equals(node.getName());
return new NamedLibraryElementNode(node.getProject(), ((NamedLibraryElementNode) node).getValue(), settings) {
@Override
public boolean canNavigate() {
return isSdkRoot;
}
@Override
public void navigate(boolean requestFocus) {
final Project project = getProject();
if (project != null) {
DartConfigurable.openDartSettings(project);
}
}
};
}
return node;
});
}
if (parentNode instanceof NamedLibraryElementNode && (DartPackagesLibraryType.DART_PACKAGES_LIBRARY_NAME.equals(parentNode.getName()) || DartSdk.DART_SDK_LIB_NAME.equals(parentNode.getName()))) {
final boolean isSdkRoot = DartSdk.DART_SDK_LIB_NAME.equals(parentNode.getName());
return ContainerUtil.map(children, node -> {
final VirtualFile dir = node instanceof PsiDirectoryNode ? ((PsiDirectoryNode) node).getVirtualFile() : null;
if (dir != null && dir.isInLocalFileSystem() && dir.isDirectory() && (isSdkRoot || "lib".equals(dir.getName()))) {
return new DartSdkOrLibraryRootNode(node.getProject(), ((PsiDirectoryNode) node).getValue(), settings);
}
return node;
});
}
// root/packages/ThisProject and root/packages/PathPackage folders are excluded in dart projects (see DartProjectComponent.excludeBuildAndPackagesFolders),
// this provider adds location string tho these nodes in Project View like "ThisProject (ThisProject/lib)"
final Project project = parentNode.getProject();
final VirtualFile packagesDir = parentNode instanceof PsiDirectoryNode && project != null ? ((PsiDirectoryNode) parentNode).getVirtualFile() : null;
final VirtualFile parentFolder = packagesDir != null && packagesDir.isDirectory() && PACKAGES_FOLDER_NAME.equals(packagesDir.getName()) ? packagesDir.getParent() : null;
final VirtualFile pubspecYamlFile = parentFolder != null ? parentFolder.findChild(PUBSPEC_YAML) : null;
if (pubspecYamlFile != null && !pubspecYamlFile.isDirectory()) {
final ArrayList<AbstractTreeNode> modifiedChildren = new ArrayList<>(children);
final DartUrlResolver resolver = DartUrlResolver.getInstance(project, pubspecYamlFile);
resolver.processLivePackages((packageName, packageDir) -> {
final VirtualFile folder = packagesDir.findChild(packageName);
if (folder != null) {
final AbstractTreeNode node = getFolderNode(children, folder);
if (node == null) {
modifiedChildren.add(new SymlinkToLivePackageNode(project, packageName, packageDir));
} else {
node.getPresentation().setLocationString(getPackageLocationString(packageDir));
}
}
});
return modifiedChildren;
}
return children;
}
use of com.jetbrains.lang.dart.util.DartUrlResolver in project intellij-plugins by JetBrains.
the class DartDocumentationProvider method getLibRelatedUrlPart.
@Nullable
private static String getLibRelatedUrlPart(@NotNull final PsiElement element) {
for (VirtualFile libFile : DartResolveUtil.findLibrary(element.getContainingFile())) {
final DartUrlResolver urlResolver = DartUrlResolver.getInstance(element.getProject(), libFile);
final String dartUrl = urlResolver.getDartUrlForFile(libFile);
// "dart:html" -> "dart-html"
if (dartUrl.startsWith(DartUrlResolver.DART_PREFIX)) {
return "dart-" + dartUrl.substring(DartUrlResolver.DART_PREFIX.length());
}
}
return null;
}
Aggregations