@Nullable
  private static String importKnowledgeAboutOldDartSdkAndReturnGlobalLibName(
      final @NotNull Project project) {
    final String oldDartSdkPath = PropertiesComponent.getInstance().getValue("dart_sdk_path");
    PropertiesComponent.getInstance().unsetValue("dart_sdk_path");

    final DartSdk sdk = DartSdk.getGlobalDartSdk();

    if (sdk != null) {
      return sdk.getGlobalLibName();
    } else if (DartSdkUtil.isDartSdkHome(oldDartSdkPath)) {
      if (DartiumUtil.getDartiumBrowser() == null) {
        // configure even if getDartiumPathForSdk() returns null
        DartiumUtil.ensureDartiumBrowserConfigured(
            DartiumUtil.getDartiumPathForSdk(oldDartSdkPath));
      }

      return ApplicationManager.getApplication()
          .runWriteAction(
              new Computable<String>() {
                public String compute() {
                  return DartSdkGlobalLibUtil.createDartSdkGlobalLib(project, oldDartSdkPath);
                }
              });
    }

    return null;
  }
  @NotNull
  private static String getPresentableElementPosition(
      @NotNull final CodeInsightTestFixture fixture, final @Nullable PsiElement element) {
    if (element == null) return "";

    final StringBuilder buf = new StringBuilder(element.getText());
    DartComponent component = PsiTreeUtil.getParentOfType(element, DartComponent.class);
    while (component != null) {
      final DartComponentName componentName = component.getComponentName();
      if (componentName != null && componentName != element) {
        buf.insert(0, component.getName() + " -> ");
      }
      component = PsiTreeUtil.getParentOfType(component, DartComponent.class);
    }
    String path =
        element instanceof PsiDirectoryImpl
            ? ((PsiDirectoryImpl) element).getVirtualFile().getPath()
            : element.getContainingFile().getVirtualFile().getPath();

    final String contentRoot =
        ModuleRootManager.getInstance(fixture.getModule()).getContentRoots()[0].getPath();
    if (path.equals(contentRoot)) path = "[content root]";

    final String contentRootWithSlash = contentRoot + "/";
    if (path.startsWith(contentRootWithSlash)) path = path.substring(contentRootWithSlash.length());

    final DartSdk sdk = DartSdk.getDartSdk(element.getProject());
    if (sdk != null && path.startsWith(sdk.getHomePath()))
      path = "[Dart SDK]" + path.substring(sdk.getHomePath().length());

    if (buf.length() > 0) buf.insert(0, " -> ");
    buf.insert(0, path);

    return buf.toString();
  }
 DartUrlResolverImpl(final @NotNull Project project, final @NotNull VirtualFile contextFile) {
   myProject = project;
   myDartSdk = DartSdk.getDartSdk(project);
   myPubspecYamlFile = initPackageRootsAndReturnPubspecYamlFile(contextFile);
   initLivePackageNameToDirMap();
   initPubListPackageDirsMap(contextFile);
 }
  @Nullable
  private static Library actualizePackagesLibrary(
      @NotNull final Project project, @Nullable final DartSdk sdk) {
    if (sdk == null || StringUtil.compareVersionNumbers(sdk.getVersion(), "1.12") < 0) return null;

    final DartLibInfo libInfo = collectPackagesLibraryRoots(project, sdk);

    if (libInfo.getLibRootUrls().isEmpty()) {
      return null;
    } else {
      return updatePackagesLibraryRoots(project, libInfo);
    }
  }
Пример #5
0
  /** Returns the same as {@link #getGlobalDartSdk()} but much faster */
  @Nullable
  public static DartSdk getDartSdk(@NotNull final Project project) {
    CachedValue<DartSdk> cachedValue = project.getUserData(CACHED_DART_SDK_KEY);

    if (cachedValue == null) {
      cachedValue =
          CachedValuesManager.getManager(project)
              .createCachedValue(
                  () -> {
                    final DartSdk sdk = getGlobalDartSdk();
                    if (sdk == null) {
                      return new CachedValueProvider.Result<DartSdk>(
                          null, DartProjectComponent.getProjectRootsModificationTracker(project));
                    }

                    List<Object> dependencies = new ArrayList<Object>(3);
                    dependencies.add(
                        DartProjectComponent.getProjectRootsModificationTracker(project));
                    ContainerUtil.addIfNotNull(
                        dependencies,
                        LocalFileSystem.getInstance()
                            .findFileByPath(sdk.getHomePath() + "/version"));
                    ContainerUtil.addIfNotNull(
                        dependencies,
                        LocalFileSystem.getInstance()
                            .findFileByPath(sdk.getHomePath() + "/lib/core/core.dart"));

                    return new CachedValueProvider.Result<DartSdk>(
                        sdk, ArrayUtil.toObjectArray(dependencies));
                  },
                  false);

      project.putUserData(CACHED_DART_SDK_KEY, cachedValue);
    }

    return cachedValue.getValue();
  }
 @Nullable
 private static String getUrlIfFileFromSdkLib(
     final @NotNull Project project, final @NotNull VirtualFile file, final @NotNull DartSdk sdk) {
   final VirtualFile sdkLibFolder =
       LocalFileSystem.getInstance().findFileByPath(sdk.getHomePath() + "/lib");
   final String relativeToSdkLibFolder =
       sdkLibFolder == null ? null : VfsUtilCore.getRelativePath(file, sdkLibFolder, '/');
   final String sdkLibUri =
       relativeToSdkLibFolder == null
           ? null
           : DartLibraryIndex.getSdkLibUriByRelativePath(project, relativeToSdkLibFolder);
   return sdkLibUri != null
       ? sdkLibUri
       : relativeToSdkLibFolder != null ? DART_PREFIX + relativeToSdkLibFolder : null;
 }
  @Nullable
  public static VirtualFile findFileInDartSdkLibFolder(
      final @NotNull Project project,
      final @Nullable DartSdk dartSdk,
      final @Nullable String dartUrl) {
    if (dartSdk == null || dartUrl == null || !dartUrl.startsWith(DART_PREFIX)) return null;

    final String sdkLibNameOrRelPath = dartUrl.substring(DART_PREFIX.length());
    final VirtualFile sdkLibByName =
        DartLibraryIndex.getStandardLibraryFromSdk(project, sdkLibNameOrRelPath);

    if (sdkLibByName != null) {
      return sdkLibByName;
    }

    final String path = dartSdk.getHomePath() + "/lib/" + sdkLibNameOrRelPath;
    return LocalFileSystem.getInstance().findFileByPath(path);
  }
 public static String getDartExePath(final @NotNull DartSdk sdk) {
   return sdk.getHomePath() + (SystemInfo.isWindows ? "/bin/dart.exe" : "/bin/dart");
 }