Search in sources :

Example 56 with Nullable

use of org.jetbrains.annotations.Nullable in project pysonar2 by yinwang0.

the class Analyzer method loadModule.

@Nullable
public Type loadModule(@NotNull List<Name> name, @NotNull State state) {
    if (name.isEmpty()) {
        return null;
    }
    String qname = makeQname(name);
    Type mt = getBuiltinModule(qname);
    if (mt != null) {
        state.insert(name.get(0).id, new Url(Builtins.LIBRARY_URL + mt.table.path + ".html"), mt, Binding.Kind.SCOPE);
        return mt;
    }
    // If there are more than one segment
    // load the packages first
    Type prev = null;
    String startPath = locateModule(name.get(0).id);
    if (startPath == null) {
        return null;
    }
    File path = new File(startPath);
    for (int i = 0; i < name.size(); i++) {
        path = new File(path, name.get(i).id);
        File initFile = new File($.joinPath(path, "__init__.py").getPath());
        if (initFile.exists()) {
            Type mod = loadFile(initFile.getPath());
            if (mod == null) {
                return null;
            }
            Binding binding = Binding.createFileBinding(name.get(i).id, initFile.getPath(), mod);
            if (prev != null) {
                prev.table.update(name.get(i).id, binding);
            } else {
                state.update(name.get(i).id, binding);
            }
            Analyzer.self.putRef(name.get(i), binding);
            prev = mod;
        } else if (i == name.size() - 1) {
            File startFile = new File(path + Globals.FILE_SUFFIX);
            if (startFile.exists()) {
                Type mod = loadFile(startFile.getPath());
                if (mod == null) {
                    return null;
                }
                Binding binding = Binding.createFileBinding(name.get(i).id, startFile.getPath(), mod);
                if (prev != null) {
                    prev.table.update(name.get(i).id, binding);
                } else {
                    state.update(name.get(i).id, binding);
                }
                Analyzer.self.putRef(name.get(i), binding);
                prev = mod;
            } else {
                return null;
            }
        }
    }
    return prev;
}
Also used : ClassType(org.yinwang.pysonar.types.ClassType) Type(org.yinwang.pysonar.types.Type) UnionType(org.yinwang.pysonar.types.UnionType) FunType(org.yinwang.pysonar.types.FunType) ModuleType(org.yinwang.pysonar.types.ModuleType) File(java.io.File) Url(org.yinwang.pysonar.ast.Url) Nullable(org.jetbrains.annotations.Nullable)

Example 57 with Nullable

use of org.jetbrains.annotations.Nullable in project pysonar2 by yinwang0.

the class AstCache method getAST.

/**
     * Returns the syntax tree for {@code path}.  May find and/or create a
     * cached copy in the mem cache or the disk cache.
     *
     * @param path absolute path to a source file
     * @return the AST, or {@code null} if the parse failed for any reason
     */
@Nullable
public Node getAST(@NotNull String path) {
    // Cache stores null value if the parse failed.
    if (cache.containsKey(path)) {
        return cache.get(path);
    }
    // Might be cached on disk but not in memory.
    Node node = getSerializedModule(path);
    if (node != null) {
        LOG.log(Level.FINE, "reusing " + path);
        cache.put(path, node);
        return node;
    }
    node = null;
    try {
        LOG.log(Level.FINE, "parsing " + path);
        node = parser.parseFile(path);
    } finally {
        // may be null
        cache.put(path, node);
    }
    if (node != null) {
        serialize(node);
    }
    return node;
}
Also used : Node(org.yinwang.pysonar.ast.Node) Nullable(org.jetbrains.annotations.Nullable)

Example 58 with Nullable

use of org.jetbrains.annotations.Nullable in project pysonar2 by yinwang0.

the class Parser method startInterpreter.

@Nullable
public Process startInterpreter(String pythonExe) {
    Process p;
    try {
        ProcessBuilder builder = new ProcessBuilder(pythonExe, "-i", jsonizer);
        builder.redirectErrorStream(true);
        builder.redirectOutput(new File(parserLog + "-" + (logCount++)));
        builder.environment().remove("PYTHONPATH");
        p = builder.start();
    } catch (Exception e) {
        $.msg("Failed to start: " + pythonExe);
        return null;
    }
    return p;
}
Also used : File(java.io.File) Nullable(org.jetbrains.annotations.Nullable)

Example 59 with Nullable

use of org.jetbrains.annotations.Nullable in project pysonar2 by yinwang0.

the class Parser method parseFileInner.

@Nullable
public Node parseFileInner(String filename, @NotNull Process pythonProcess) {
    //        _.msg("parsing: " + filename);
    File exchange = new File(exchangeFile);
    File marker = new File(endMark);
    cleanTemp();
    String s1 = $.escapeWindowsPath(filename);
    String s2 = $.escapeWindowsPath(exchangeFile);
    String s3 = $.escapeWindowsPath(endMark);
    String dumpCommand = "parse_dump('" + s1 + "', '" + s2 + "', '" + s3 + "')";
    if (!sendCommand(dumpCommand, pythonProcess)) {
        cleanTemp();
        return null;
    }
    long waitStart = System.currentTimeMillis();
    while (!marker.exists()) {
        if (System.currentTimeMillis() - waitStart > TIMEOUT) {
            Analyzer.self.failedToParse.add(filename);
            cleanTemp();
            startPythonProcesses();
            return null;
        }
        try {
            Thread.sleep(1);
        } catch (Exception e) {
            cleanTemp();
            return null;
        }
    }
    String json = $.readFile(exchangeFile);
    if (json != null) {
        cleanTemp();
        Map<String, Object> map = gson.fromJson(json, Map.class);
        return convert(map);
    } else {
        cleanTemp();
        return null;
    }
}
Also used : File(java.io.File) Nullable(org.jetbrains.annotations.Nullable)

Example 60 with Nullable

use of org.jetbrains.annotations.Nullable in project intellij-plugins by StepicOrg.

the class StepikModuleType method modifySettingsStep.

@Nullable
@Override
public ModuleWizardStep modifySettingsStep(@NotNull SettingsStep settingsStep, @NotNull ModuleBuilder moduleBuilder) {
    JTextField nameField = settingsStep.getModuleNameField();
    if (nameField != null) {
        CourseModuleBuilder courseModuleBuilder = (CourseModuleBuilder) moduleBuilder;
        StudyObject studyObject = courseModuleBuilder.getWizardStep().getSelectedStudyObject();
        String projectDirectory = settingsStep.getContext().getProjectFileDirectory();
        String projectName = ProjectWizardUtils.getProjectDefaultName(projectDirectory, studyObject);
        nameField.setText(projectName);
    }
    return null;
}
Also used : StudyObject(org.stepik.api.objects.StudyObject) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Nullable (org.jetbrains.annotations.Nullable)4694 VirtualFile (com.intellij.openapi.vfs.VirtualFile)812 PsiElement (com.intellij.psi.PsiElement)485 File (java.io.File)405 Project (com.intellij.openapi.project.Project)396 PsiFile (com.intellij.psi.PsiFile)320 NotNull (org.jetbrains.annotations.NotNull)259 IOException (java.io.IOException)247 Module (com.intellij.openapi.module.Module)227 ArrayList (java.util.ArrayList)178 TextRange (com.intellij.openapi.util.TextRange)156 Document (com.intellij.openapi.editor.Document)124 List (java.util.List)116 ASTNode (com.intellij.lang.ASTNode)105 IElementType (com.intellij.psi.tree.IElementType)103 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)102 XmlTag (com.intellij.psi.xml.XmlTag)96 Editor (com.intellij.openapi.editor.Editor)94 Element (org.jdom.Element)93 XmlFile (com.intellij.psi.xml.XmlFile)78