Search in sources :

Example 11 with DartFile

use of com.jetbrains.lang.dart.psi.DartFile in project flutter-intellij by flutter.

the class MainFile method verify.

/**
 * Verifies that the given path points to an entrypoint file within a Flutter app.
 * <p>
 * If there is an error, {@link Result#canLaunch} will return false and the error is available via {@link Result#getError}
 */
@NotNull
public static MainFile.Result verify(@Nullable String path, Project project) {
    if (!ApplicationManager.getApplication().isReadAccessAllowed()) {
        throw new IllegalStateException("need read access");
    }
    if (StringUtil.isEmptyOrSpaces(path)) {
        return error(FlutterBundle.message("entrypoint.not.set"));
    }
    final VirtualFile file = LocalFileSystem.getInstance().findFileByPath(path);
    if (file == null) {
        return error(FlutterBundle.message("entrypoint.not.found", FileUtil.toSystemDependentName(path)));
    }
    if (file.getFileType() != DartFileType.INSTANCE) {
        return error(FlutterBundle.message("entrypoint.not.dart"));
    }
    final PsiFile psi = PsiManager.getInstance(project).findFile(file);
    if (!(psi instanceof DartFile)) {
        return error(FlutterBundle.message("entrypoint.not.dart"));
    }
    final DartFile dart = (DartFile) psi;
    if (DartResolveUtil.getMainFunction(dart) == null) {
        return error(FlutterBundle.message("main.not.in.entrypoint"));
    }
    if (!inProject(file, project)) {
        return error(FlutterBundle.message("entrypoint.not.in.project"));
    }
    final VirtualFile dir = findAppDir(file, project);
    if (dir == null) {
        return error(FlutterBundle.message("entrypoint.not.in.app.dir"));
    }
    final boolean hasFlutterImports = findImportUrls(dart).anyMatch((url) -> url.startsWith("package:flutter/"));
    return new MainFile.Result(new MainFile(file, dir, hasFlutterImports), null);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DartFile(com.jetbrains.lang.dart.psi.DartFile) PsiFile(com.intellij.psi.PsiFile) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

DartFile (com.jetbrains.lang.dart.psi.DartFile)11 VirtualFile (com.intellij.openapi.vfs.VirtualFile)9 PsiFile (com.intellij.psi.PsiFile)5 Nullable (org.jetbrains.annotations.Nullable)5 Project (com.intellij.openapi.project.Project)4 Module (com.intellij.openapi.module.Module)3 PsiElement (com.intellij.psi.PsiElement)3 PsiDirectory (com.intellij.psi.PsiDirectory)2 PubRoot (io.flutter.pub.PubRoot)2 NotNull (org.jetbrains.annotations.NotNull)2 ASTNode (com.intellij.lang.ASTNode)1 DocumentBasedFormattingModel (com.intellij.psi.formatter.DocumentBasedFormattingModel)1 DartCallExpression (com.jetbrains.lang.dart.psi.DartCallExpression)1 DartSdk (com.jetbrains.lang.dart.sdk.DartSdk)1 FlutterSdk (io.flutter.sdk.FlutterSdk)1