Search in sources :

Example 1 with DartUrlResolver

use of com.jetbrains.lang.dart.util.DartUrlResolver in project intellij-plugins by JetBrains.

the class DartRunner method doExecuteDartDebug.

private RunContentDescriptor doExecuteDartDebug(@NotNull final RunProfileState state, @NotNull final ExecutionEnvironment env, @Nullable final String dasExecutionContextId) throws RuntimeConfigurationError, ExecutionException {
    final DartSdk sdk = DartSdk.getDartSdk(env.getProject());
    // already checked
    assert (sdk != null);
    final RunProfile runConfiguration = env.getRunProfile();
    final VirtualFile contextFileOrDir;
    VirtualFile currentWorkingDirectory;
    final ExecutionResult executionResult;
    final String debuggingHost;
    final int observatoryPort;
    if (runConfiguration instanceof DartRunConfigurationBase) {
        contextFileOrDir = ((DartRunConfigurationBase) runConfiguration).getRunnerParameters().getDartFileOrDirectory();
        final String cwd = ((DartRunConfigurationBase) runConfiguration).getRunnerParameters().computeProcessWorkingDirectory(env.getProject());
        currentWorkingDirectory = LocalFileSystem.getInstance().findFileByPath((cwd));
        executionResult = state.execute(env.getExecutor(), this);
        if (executionResult == null) {
            return null;
        }
        debuggingHost = null;
        observatoryPort = ((DartCommandLineRunningState) state).getObservatoryPort();
    } else if (runConfiguration instanceof DartRemoteDebugConfiguration) {
        final String path = ((DartRemoteDebugConfiguration) runConfiguration).getParameters().getDartProjectPath();
        contextFileOrDir = LocalFileSystem.getInstance().findFileByPath(path);
        if (contextFileOrDir == null) {
            throw new RuntimeConfigurationError("Folder not found: " + FileUtil.toSystemDependentName(path));
        }
        currentWorkingDirectory = contextFileOrDir;
        executionResult = null;
        debuggingHost = ((DartRemoteDebugConfiguration) runConfiguration).getParameters().getHost();
        observatoryPort = ((DartRemoteDebugConfiguration) runConfiguration).getParameters().getPort();
    } else {
        LOG.error("Unexpected run configuration: " + runConfiguration.getClass().getName());
        return null;
    }
    FileDocumentManager.getInstance().saveAllDocuments();
    final XDebuggerManager debuggerManager = XDebuggerManager.getInstance(env.getProject());
    final XDebugSession debugSession = debuggerManager.startSession(env, new XDebugProcessStarter() {

        @Override
        @NotNull
        public XDebugProcess start(@NotNull final XDebugSession session) {
            final DartUrlResolver dartUrlResolver = getDartUrlResolver(env.getProject(), contextFileOrDir);
            return new DartVmServiceDebugProcess(session, StringUtil.notNullize(debuggingHost, "localhost"), observatoryPort, executionResult, dartUrlResolver, dasExecutionContextId, runConfiguration instanceof DartRemoteDebugConfiguration, getTimeout(), currentWorkingDirectory);
        }
    });
    return debugSession.getRunContentDescriptor();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XDebugSession(com.intellij.xdebugger.XDebugSession) XDebugProcess(com.intellij.xdebugger.XDebugProcess) DartRunConfigurationBase(com.jetbrains.lang.dart.ide.runner.base.DartRunConfigurationBase) XDebuggerManager(com.intellij.xdebugger.XDebuggerManager) ExecutionResult(com.intellij.execution.ExecutionResult) RunProfile(com.intellij.execution.configurations.RunProfile) NotNull(org.jetbrains.annotations.NotNull) DartVmServiceDebugProcess(com.jetbrains.lang.dart.ide.runner.server.vmService.DartVmServiceDebugProcess) DartSdk(com.jetbrains.lang.dart.sdk.DartSdk) DartRemoteDebugConfiguration(com.jetbrains.lang.dart.ide.runner.server.DartRemoteDebugConfiguration) XDebugProcessStarter(com.intellij.xdebugger.XDebugProcessStarter) DartUrlResolver(com.jetbrains.lang.dart.util.DartUrlResolver) RuntimeConfigurationError(com.intellij.execution.configurations.RuntimeConfigurationError)

Example 2 with DartUrlResolver

use of com.jetbrains.lang.dart.util.DartUrlResolver in project intellij-plugins by JetBrains.

the class DartPackageAwareFileIncludeProvider method resolveIncludedFile.

@Nullable
@Override
public PsiFileSystemItem resolveIncludedFile(@NotNull final FileIncludeInfo info, @NotNull final PsiFile context) {
    final VirtualFile contextFile = DartResolveUtil.getRealVirtualFile(context);
    final VirtualFile pubspecYamlFile = contextFile == null ? null : PubspecYamlUtil.findPubspecYamlFile(context.getProject(), contextFile);
    if (pubspecYamlFile == null)
        return null;
    VirtualFile targetFile = null;
    final int slashIndex = info.path.indexOf('/');
    if (slashIndex > 0) {
        final String packageName = info.path.substring(0, slashIndex);
        final String relPath = info.path.substring(slashIndex + 1);
        final DartUrlResolver urlResolver = DartUrlResolver.getInstance(context.getProject(), contextFile);
        final VirtualFile packageDir = urlResolver.getPackageDirIfNotInOldStylePackagesFolder(packageName, relPath);
        if (packageDir != null) {
            targetFile = packageDir.findFileByRelativePath(relPath);
        }
    }
    if (targetFile == null) {
        targetFile = VfsUtilCore.findRelativeFile(PACKAGES_FOLDER_NAME + "/" + info.path, pubspecYamlFile);
    }
    if (targetFile != null) {
        return targetFile.isDirectory() ? context.getManager().findDirectory(targetFile) : context.getManager().findFile(targetFile);
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DartUrlResolver(com.jetbrains.lang.dart.util.DartUrlResolver) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with DartUrlResolver

use of com.jetbrains.lang.dart.util.DartUrlResolver in project intellij-plugins by JetBrains.

the class DartFileUrlMapper method getUrls.

@NotNull
@Override
public List<Url> getUrls(@NotNull final VirtualFile file, @NotNull final Project project, @Nullable final String currentAuthority) {
    if (currentAuthority == null || file.getFileType() != DartFileType.INSTANCE)
        return Collections.emptyList();
    if (Registry.is("dart.redirect.to.pub.server", true) && ProjectFileIndex.getInstance(project).isInContent(file)) {
        final Pair<VirtualFile, String> servedDirAndPath = PubServerPathHandlerKt.getServedDirAndPathForPubServer(project, file);
        if (servedDirAndPath != null) {
            final VirtualFile servedDir = servedDirAndPath.first;
            final String path = servedDirAndPath.second;
            final String pubAuthority = BuiltInServerManagerImpl.isOnBuiltInWebServerByAuthority(currentAuthority) ? PubServerManager.getInstance(project).getPubServerAuthorityForServedDir(servedDir) : currentAuthority;
            if (pubAuthority != null) {
                return Collections.singletonList(Urls.newHttpUrl(pubAuthority, path));
            }
        }
    }
    final DartUrlResolver urlResolver = DartUrlResolver.getInstance(project, file);
    final String dartUri = urlResolver.getDartUrlForFile(file);
    if (!dartUri.startsWith(PACKAGE_PREFIX))
        return Collections.emptyList();
    if (BuiltInServerManagerImpl.isOnBuiltInWebServerByAuthority(currentAuthority)) {
        final List<Url> result = new SmartList<>();
        final VirtualFile pubspec = urlResolver.getPubspecYamlFile();
        final VirtualFile dartRoot = pubspec != null ? pubspec.getParent() : null;
        if (Registry.is("dart.redirect.to.pub.server", true)) {
            // package:PackageName/subdir/foo.dart -> http://localhost:45455/packages/PackageName/subdir/foo.dart
            final Collection<String> authorities = dartRoot != null ? PubServerManager.getInstance(project).getAlivePubServerAuthoritiesForDartRoot(pubspec.getParent()) : PubServerManager.getInstance(project).getAllAlivePubServerAuthorities();
            for (String pubAuthority : authorities) {
                final String pubUrlPath = "/packages/" + dartUri.substring(PACKAGE_PREFIX.length());
                result.add(Urls.newHttpUrl(pubAuthority, pubUrlPath));
            }
        } else if (dartRoot != null) {
            // for built-in server:
            // package:PackageName/subdir/foo.dart -> http://localhost:63342/ProjectName/MayBeRelPathToDartProject/web/packages/PackageName/subdir/foo.dart
            final String dartRootUrlPath = WebServerPathToFileManager.getInstance(project).getPath(dartRoot);
            if (dartRootUrlPath == null)
                return Collections.emptyList();
            //BuiltInWebBrowserUrlProviderKt.getBuiltInServerUrls(pubspec, project, currentAuthority);
            final Url dartRootUrl = Urls.newHttpUrl(currentAuthority, "/" + project.getName() + "/" + dartRootUrlPath);
            final String urlPath = StringUtil.trimEnd(dartRootUrl.getPath(), "/") + "/web/packages/" + dartUri.substring(PACKAGE_PREFIX.length());
            result.add(Urls.newHttpUrl(currentAuthority, urlPath));
        }
        return result;
    } else {
        // for any other server (e.g. localhost:8181):
        // package:PackageName/subdir/foo.dart -> http://localhost:8181/packages/PackageName/subdir/foo.dart
        final String urlPath = "/packages/" + dartUri.substring(PACKAGE_PREFIX.length());
        return Collections.singletonList(Urls.newHttpUrl(currentAuthority, urlPath));
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DartUrlResolver(com.jetbrains.lang.dart.util.DartUrlResolver) SmartList(com.intellij.util.SmartList) Url(com.intellij.util.Url) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with DartUrlResolver

use of com.jetbrains.lang.dart.util.DartUrlResolver in project intellij-plugins by JetBrains.

the class DartWorkflowTest method testDartUrlResolver.

public void testDartUrlResolver() throws Exception {
    final String rootPath = ModuleRootManager.getInstance(myModule).getContentRoots()[0].getPath();
    final String rootUrl = "file:///" + StringUtil.trimLeading(rootPath, '/');
    myFixture.addFileToProject("pubspec.yaml", "name: RootProject");
    myFixture.addFileToProject("lib/rootlib.dart", "");
    final VirtualFile nestedPubspec = myFixture.addFileToProject("example/pubspec.yaml", "name: NestedProject\n" + "dependencies:\n" + "  RootProject:\n" + "    path: ../").getVirtualFile();
    myFixture.addFileToProject("example/lib/src/nestedlib.dart", "");
    myFixture.addFileToProject("example/packages/NestedProject/nestedlib.dart", "");
    myFixture.addFileToProject("example/packages/RootProject/rootlib.dart", "");
    myFixture.addFileToProject("pub/global/cache/SomePackage/lib/somepack.dart", "");
    myFixture.saveText(myFixture.addFileToProject("example/.packages", "").getVirtualFile(), "RootProject:../lib/\n" + "NestedProject:lib/\n" + "SomePackage:" + rootUrl + "/pub/global/cache/SomePackage/lib/");
    DartUrlResolver resolver = DartUrlResolver.getInstance(getProject(), nestedPubspec);
    VirtualFile file;
    file = resolver.findFileByDartUrl("dart:collection");
    assertNotNull(file);
    assertEquals(DartTestUtils.SDK_HOME_PATH + "/lib/collection/collection.dart", file.getPath());
    assertEquals("dart:collection", resolver.getDartUrlForFile(file));
    file = resolver.findFileByDartUrl("dart:collection/hash_map.dart");
    assertNotNull(file);
    assertEquals(DartTestUtils.SDK_HOME_PATH + "/lib/collection/hash_map.dart", file.getPath());
    assertEquals("dart:collection/hash_map.dart", resolver.getDartUrlForFile(file));
    file = resolver.findFileByDartUrl("package:NestedProject/src/nestedlib.dart");
    assertNotNull(file);
    assertEquals(rootPath + "/example/lib/src/nestedlib.dart", file.getPath());
    assertEquals("package:NestedProject/src/nestedlib.dart", resolver.getDartUrlForFile(file));
    file = resolver.findFileByDartUrl("package:RootProject/rootlib.dart");
    assertNotNull(file);
    assertEquals(rootPath + "/lib/rootlib.dart", file.getPath());
    assertEquals("package:RootProject/rootlib.dart", resolver.getDartUrlForFile(file));
    file = resolver.findFileByDartUrl("package:SomePackage/somepack.dart");
    assertNotNull(file);
    assertEquals(rootPath + "/pub/global/cache/SomePackage/lib/somepack.dart", file.getPath());
    assertEquals("package:SomePackage/somepack.dart", resolver.getDartUrlForFile(file));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DartUrlResolver(com.jetbrains.lang.dart.util.DartUrlResolver)

Example 5 with DartUrlResolver

use of com.jetbrains.lang.dart.util.DartUrlResolver in project intellij-plugins by JetBrains.

the class DartTestRunConfigurationProducer method setupRunnerParametersForFolderIfApplicable.

private static boolean setupRunnerParametersForFolderIfApplicable(@NotNull final Project project, @NotNull final DartTestRunnerParameters params, @NotNull final VirtualFile dir) {
    final DartUrlResolver urlResolver = DartUrlResolver.getInstance(project, dir);
    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");
    if (testDir == null || !testDir.isDirectory())
        return false;
    if (!rootDir.equals(dir) && !VfsUtilCore.isAncestor(testDir, dir, false))
        return false;
    return setupRunnerParametersForFolder(params, dir);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DartUrlResolver(com.jetbrains.lang.dart.util.DartUrlResolver)

Aggregations

VirtualFile (com.intellij.openapi.vfs.VirtualFile)9 DartUrlResolver (com.jetbrains.lang.dart.util.DartUrlResolver)9 NotNull (org.jetbrains.annotations.NotNull)3 Nullable (org.jetbrains.annotations.Nullable)2 ExecutionResult (com.intellij.execution.ExecutionResult)1 RunProfile (com.intellij.execution.configurations.RunProfile)1 RuntimeConfigurationError (com.intellij.execution.configurations.RuntimeConfigurationError)1 ExternalLibrariesNode (com.intellij.ide.projectView.impl.nodes.ExternalLibrariesNode)1 NamedLibraryElementNode (com.intellij.ide.projectView.impl.nodes.NamedLibraryElementNode)1 PsiDirectoryNode (com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode)1 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)1 Project (com.intellij.openapi.project.Project)1 SmartList (com.intellij.util.SmartList)1 Url (com.intellij.util.Url)1 XDebugProcess (com.intellij.xdebugger.XDebugProcess)1 XDebugProcessStarter (com.intellij.xdebugger.XDebugProcessStarter)1 XDebugSession (com.intellij.xdebugger.XDebugSession)1 XDebuggerManager (com.intellij.xdebugger.XDebuggerManager)1 DartRunConfigurationBase (com.jetbrains.lang.dart.ide.runner.base.DartRunConfigurationBase)1 DartRemoteDebugConfiguration (com.jetbrains.lang.dart.ide.runner.server.DartRemoteDebugConfiguration)1