Search in sources :

Example 1 with IModulesManager

use of org.python.pydev.core.IModulesManager in project Pydev by fabioz.

the class AdditionalProjectInterpreterInfo method recreateAllInfo.

public static void recreateAllInfo(IPythonNature nature, IProgressMonitor monitor) {
    try (GlobalFeedbackReporter r = GlobalFeedback.start("Full projects reindex...")) {
        synchronized (additionalNatureInfoLock) {
            // Note: at this point we're 100% certain that the ast manager is there.
            ICodeCompletionASTManager astManager = nature.getAstManager();
            if (astManager == null) {
                return;
            }
            IModulesManager m = astManager.getModulesManager();
            if (m == null) {
                return;
            }
            IProject project = nature.getProject();
            AbstractAdditionalDependencyInfo currInfo = AdditionalProjectInterpreterInfo.getAdditionalInfoForProject(nature);
            if (currInfo != null) {
                currInfo.clearAllInfo();
                currInfo.dispose();
            }
            String feedback = "(project:" + project.getName() + ")";
            synchronized (m) {
                AbstractAdditionalDependencyInfo info = (AbstractAdditionalDependencyInfo) restoreInfoForModuleManager(monitor, m, feedback, new AdditionalProjectInterpreterInfo(project), nature, nature.getGrammarVersion());
                if (info != null) {
                    // ok, set it and save it
                    additionalNatureInfo.put(FileUtilsFileBuffer.getValidProjectName(project), info);
                    info.save();
                }
            }
        }
    } catch (Exception e) {
        Log.log(e);
        throw new RuntimeException(e);
    }
}
Also used : ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) IModulesManager(org.python.pydev.core.IModulesManager) GlobalFeedbackReporter(org.python.pydev.shared_core.global_feedback.GlobalFeedback.GlobalFeedbackReporter) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) PythonNatureWithoutProjectException(org.python.pydev.core.PythonNatureWithoutProjectException) MisconfigurationException(org.python.pydev.core.MisconfigurationException)

Example 2 with IModulesManager

use of org.python.pydev.core.IModulesManager in project Pydev by fabioz.

the class MypyAnalysis method createMypyProcess.

/**
 * Creates the mypy process and starts getting its output.
 */
void createMypyProcess(IExternalCodeAnalysisStream out) throws CoreException, MisconfigurationException, PythonNatureWithoutProjectException {
    String mypyExecutable = FileUtils.getFileAbsolutePath(mypyLocation);
    String target = location.toOSString();
    ArrayList<String> cmdList = new ArrayList<String>();
    cmdList.add(mypyExecutable);
    String userArgs = StringUtils.replaceNewLines(MypyPreferences.getMypyArgs(resource), " ");
    List<String> userArgsAsList = new ArrayList<>(Arrays.asList(ProcessUtils.parseArguments(userArgs)));
    if (!userArgsAsList.contains("--show-column-numbers")) {
        userArgsAsList.add("--show-column-numbers");
    }
    boolean foundFollowImports = false;
    boolean foundCacheDir = false;
    for (String arg : userArgsAsList) {
        if (arg.startsWith("--follow-imports=silent")) {
            foundFollowImports = true;
        }
        if (arg.startsWith("--cache-dir")) {
            foundCacheDir = true;
        }
    }
    if (!foundFollowImports) {
        // We just want warnings for the current file.
        userArgsAsList.add("--follow-imports=silent");
    }
    // run mypy in project location
    IProject project = resource.getProject();
    if (project == null || !project.isAccessible()) {
        // If the project is no longer valid, we can't do much.
        Log.log("Unable to run mypy in: " + target + ". Project not available (" + project + ").");
        return;
    }
    File workingDir = project.getLocation().toFile();
    if (!foundCacheDir) {
        // Set a cache dir if one is not given.
        userArgsAsList.add("--cache-dir=" + new File(workingDir, ".mypy_cache").toString());
    }
    cmdList.addAll(userArgsAsList);
    cmdList.add(target);
    String[] args = cmdList.toArray(new String[0]);
    // run executable command (mypy or mypy.bat or mypy.exe)
    WriteToStreamHelper.write("Mypy: Executing command line:", out, (Object) args);
    IPythonNature nature = PythonNature.getPythonNature(project);
    ICallback<String[], String[]> updateEnv = null;
    if (MypyPreferences.getAddProjectFoldersToMyPyPath(resource)) {
        Collection<String> addToMypyPath = new HashSet<String>();
        IModulesManager[] managersInvolved = nature.getAstManager().getModulesManager().getManagersInvolved(false);
        for (IModulesManager iModulesManager : managersInvolved) {
            for (String s : StringUtils.split(iModulesManager.getNature().getPythonPathNature().getOnlyProjectPythonPathStr(true), "|")) {
                if (!s.isEmpty()) {
                    addToMypyPath.add(s);
                }
            }
        }
        if (addToMypyPath.size() > 0) {
            updateEnv = new ICallback<String[], String[]>() {

                @Override
                public String[] call(String[] arg) {
                    for (int i = 0; i < arg.length; i++) {
                        // Update var
                        if (arg[i].startsWith("MYPYPATH=")) {
                            arg[i] = arg[i] + SimpleRunner.getPythonPathSeparator() + StringUtils.join(SimpleRunner.getPythonPathSeparator(), addToMypyPath);
                            return arg;
                        }
                    }
                    // Create new var.
                    return ArrayUtils.concatArrays(arg, new String[] { "MYPYPATH=" + StringUtils.join(SimpleRunner.getPythonPathSeparator(), addToMypyPath) });
                }
            };
        }
    }
    final ICallback<String[], String[]> finalUpdateEnv = updateEnv;
    ICallback0<Process> launchProcessCallback = () -> {
        SimpleRunner simpleRunner = new SimpleRunner();
        final Tuple<Process, String> r = simpleRunner.run(args, workingDir, nature, null, finalUpdateEnv);
        Process process = r.o1;
        return process;
    };
    this.processWatchDoc = new ExternalAnalizerProcessWatchDoc(out, monitor, this, launchProcessCallback, project, true);
    this.processWatchDoc.start();
}
Also used : SimpleRunner(org.python.pydev.ast.runners.SimpleRunner) ArrayList(java.util.ArrayList) IPythonNature(org.python.pydev.core.IPythonNature) IModulesManager(org.python.pydev.core.IModulesManager) ExternalAnalizerProcessWatchDoc(com.python.pydev.analysis.external.ExternalAnalizerProcessWatchDoc) IProject(org.eclipse.core.resources.IProject) IFile(org.eclipse.core.resources.IFile) File(java.io.File) Tuple(org.python.pydev.shared_core.structure.Tuple) HashSet(java.util.HashSet)

Example 3 with IModulesManager

use of org.python.pydev.core.IModulesManager in project Pydev by fabioz.

the class AbstractPythonNature method endRequests.

/**
 * End a request for an ast manager (end caching things)
 */
@Override
public void endRequests() {
    synchronized (modulesManagerStackLock) {
        try {
            IModulesManager modulesManager = modulesManagerStack.pop();
            modulesManager.endCompletionCache();
        } catch (EmptyStackException e) {
            Log.log(e);
        }
    }
}
Also used : EmptyStackException(java.util.EmptyStackException) IModulesManager(org.python.pydev.core.IModulesManager)

Example 4 with IModulesManager

use of org.python.pydev.core.IModulesManager in project Pydev by fabioz.

the class AbstractPythonNature method startRequests.

/**
 * Start a request for an ast manager (start caching things)
 */
@Override
public boolean startRequests() {
    ICodeCompletionASTManager astManager = this.getAstManager();
    if (astManager == null) {
        return false;
    }
    IModulesManager modulesManager = astManager.getModulesManager();
    if (modulesManager == null) {
        return false;
    }
    synchronized (modulesManagerStackLock) {
        modulesManagerStack.push(modulesManager);
        return modulesManager.startCompletionCache();
    }
}
Also used : ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) IModulesManager(org.python.pydev.core.IModulesManager)

Example 5 with IModulesManager

use of org.python.pydev.core.IModulesManager in project Pydev by fabioz.

the class GlobalsTwoPanelElementSelector2 method fillContentProvider.

/**
 * This is the place where we put all the info in the content provider. Note that here we must add
 * ALL the info -- later, we'll filter it based on the active working set.
 */
@Override
protected void fillContentProvider(AbstractContentProvider contentProvider, ItemsFilter itemsFilter, IProgressMonitor progressMonitor) throws CoreException {
    if (itemsFilter instanceof InfoFilter) {
        if (progressMonitor != null) {
            progressMonitor.beginTask("Searching...", this.additionalInfo.size());
        }
        for (AbstractAdditionalTokensInfo additionalInfo : this.additionalInfo) {
            if (progressMonitor != null) {
                if (progressMonitor.isCanceled()) {
                    return;
                } else {
                    progressMonitor.worked(1);
                }
            }
            // no duplicates
            Collection<IInfo> allTokens = new HashSet<IInfo>(additionalInfo.getAllTokens());
            for (IInfo iInfo : allTokens) {
                contentProvider.add(new AdditionalInfoAndIInfo(additionalInfo, iInfo), itemsFilter);
            }
            // Also show to the user the modules available as globals (2.2.3)
            IModulesManager modulesManager = null;
            try {
                if (additionalInfo instanceof AdditionalProjectInterpreterInfo) {
                    AdditionalProjectInterpreterInfo projectInterpreterInfo = (AdditionalProjectInterpreterInfo) additionalInfo;
                    IProject project = projectInterpreterInfo.getProject();
                    PythonNature nature = PythonNature.getPythonNature(project);
                    if (nature != null) {
                        ICodeCompletionASTManager astManager = nature.getAstManager();
                        if (astManager != null) {
                            modulesManager = astManager.getModulesManager();
                        }
                    }
                } else if (additionalInfo instanceof AdditionalSystemInterpreterInfo) {
                    AdditionalSystemInterpreterInfo systemInterpreterInfo = (AdditionalSystemInterpreterInfo) additionalInfo;
                    IInterpreterInfo defaultInterpreterInfo = systemInterpreterInfo.getManager().getDefaultInterpreterInfo(false);
                    modulesManager = defaultInterpreterInfo.getModulesManager();
                }
            } catch (Throwable e) {
                Log.log(e);
            }
            if (modulesManager != null) {
                SortedMap<ModulesKey, ModulesKey> allDirectModulesStartingWith = modulesManager.getAllDirectModulesStartingWith("");
                Collection<ModulesKey> values = allDirectModulesStartingWith.values();
                for (ModulesKey modulesKey : values) {
                    contentProvider.add(new AdditionalInfoAndIInfo(additionalInfo, new ModInfo(modulesKey.name, modulesManager.getNature(), modulesKey.file != null ? modulesKey.file.toString() : null, 1, 1)), itemsFilter);
                }
            }
        }
    }
    if (progressMonitor != null) {
        progressMonitor.done();
    }
}
Also used : IPythonNature(org.python.pydev.core.IPythonNature) PythonNature(org.python.pydev.plugin.nature.PythonNature) IModulesManager(org.python.pydev.core.IModulesManager) IProject(org.eclipse.core.resources.IProject) AdditionalSystemInterpreterInfo(com.python.pydev.analysis.additionalinfo.AdditionalSystemInterpreterInfo) AdditionalInfoAndIInfo(com.python.pydev.analysis.additionalinfo.AdditionalInfoAndIInfo) IInfo(org.python.pydev.core.IInfo) AdditionalProjectInterpreterInfo(com.python.pydev.analysis.additionalinfo.AdditionalProjectInterpreterInfo) ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) AdditionalInfoAndIInfo(com.python.pydev.analysis.additionalinfo.AdditionalInfoAndIInfo) ModulesKey(org.python.pydev.core.ModulesKey) AbstractAdditionalTokensInfo(com.python.pydev.analysis.additionalinfo.AbstractAdditionalTokensInfo) ModInfo(com.python.pydev.analysis.additionalinfo.ModInfo) HashSet(java.util.HashSet)

Aggregations

IModulesManager (org.python.pydev.core.IModulesManager)24 ICodeCompletionASTManager (org.python.pydev.core.ICodeCompletionASTManager)13 HashSet (java.util.HashSet)7 IPythonNature (org.python.pydev.core.IPythonNature)7 MisconfigurationException (org.python.pydev.core.MisconfigurationException)7 IProject (org.eclipse.core.resources.IProject)6 ArrayList (java.util.ArrayList)5 File (java.io.File)4 IModule (org.python.pydev.core.IModule)4 IProjectModulesManager (org.python.pydev.core.IProjectModulesManager)4 ModulesKey (org.python.pydev.core.ModulesKey)4 AbstractAdditionalTokensInfo (com.python.pydev.analysis.additionalinfo.AbstractAdditionalTokensInfo)3 IFile (org.eclipse.core.resources.IFile)3 CompareContext (org.python.pydev.ast.codecompletion.ProposalsComparator.CompareContext)3 PythonPathHelper (org.python.pydev.ast.codecompletion.revisited.PythonPathHelper)3 SourceModule (org.python.pydev.ast.codecompletion.revisited.modules.SourceModule)3 ISystemModulesManager (org.python.pydev.core.ISystemModulesManager)3 PythonNature (org.python.pydev.plugin.nature.PythonNature)3 ICompareContext (org.python.pydev.shared_core.code_completion.IPyCompletionProposal.ICompareContext)3 Tuple (org.python.pydev.shared_core.structure.Tuple)3