Search in sources :

Example 66 with PythonNature

use of org.python.pydev.plugin.nature.PythonNature in project Pydev by fabioz.

the class PythonConsoleLineTracker method checkMapFilenameToHyperlink.

private boolean checkMapFilenameToHyperlink(final int lineOffset, final int matchStartCol, final int matchEndCol, final String filename, final int lineNumberInt) {
    File realFile = new File(filename);
    if (fileExists(realFile)) {
        // Simple case (absolute file match).
        createLinkToFile(lineOffset, matchStartCol, matchEndCol, lineNumberInt, realFile);
        return true;
    }
    // Not a direct match, let's try some heuristics to get a match based on the working dir.
    final IPath path = Path.fromOSString(filename);
    if (path.isAbsolute()) {
        // Not much we can't do if the path is already absolute.
        return false;
    }
    IProject project = getProject();
    try {
        if (project != null) {
            if (createLinkFromContainerAndPath(lineOffset, matchStartCol, matchEndCol, lineNumberInt, path, project)) {
                return true;
            }
        }
    } catch (IllegalArgumentException e1) {
    // ignore
    } catch (Exception e1) {
        Log.log(e1);
    }
    IPath workingDir = getWorkingDirectory();
    if (workingDir != null && project != null) {
        if (!workingDir.equals(project.getLocation())) {
            IPath full = workingDir.append(path);
            File file = full.toFile();
            if (fileExists(file)) {
                createLinkToFile(lineOffset, matchStartCol, matchEndCol, lineNumberInt, file);
                return true;
            }
        }
    }
    PythonNature nature = PythonNature.getPythonNature(project);
    if (nature != null) {
        try {
            Set<IResource> projectSourcePathFolderSet = nature.getPythonPathNature().getProjectSourcePathFolderSet();
            for (IResource iResource : projectSourcePathFolderSet) {
                if (iResource instanceof IContainer) {
                    IContainer iContainer = (IContainer) iResource;
                    if (iContainer.equals(project)) {
                        // We already checked this.
                        continue;
                    }
                    if (workingDir != null && iContainer.getLocation().equals(workingDir)) {
                        // We already checked this.
                        continue;
                    }
                    if (createLinkFromContainerAndPath(lineOffset, matchStartCol, matchEndCol, lineNumberInt, path, iContainer)) {
                        return true;
                    }
                }
            }
        } catch (CoreException e) {
        }
    }
    return false;
}
Also used : IPath(org.eclipse.core.runtime.IPath) PythonNature(org.python.pydev.plugin.nature.PythonNature) CoreException(org.eclipse.core.runtime.CoreException) IContainer(org.eclipse.core.resources.IContainer) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) BadLocationException(org.eclipse.jface.text.BadLocationException) IResource(org.eclipse.core.resources.IResource)

Example 67 with PythonNature

use of org.python.pydev.plugin.nature.PythonNature in project Pydev by fabioz.

the class PyGlobalsBrowser method doSelect.

/**
 * @param pythonNatures the natures from were we can get info
 * @param additionalInfo the additional informations
 * @param selectedText the text that should be initially set as a filter
 */
public static void doSelect(List<IPythonNature> pythonNatures, List<AbstractAdditionalTokensInfo> additionalInfo, String selectedText) {
    SelectionDialog dialog = GlobalsDialogFactory.create(EditorUtils.getShell(), additionalInfo, selectedText);
    dialog.open();
    Object[] result = dialog.getResult();
    if (result != null && result.length > 0) {
        for (Object obj : result) {
            IInfo entry;
            if (obj instanceof AdditionalInfoAndIInfo) {
                AdditionalInfoAndIInfo additional = (AdditionalInfoAndIInfo) obj;
                try {
                    // all of the input natures).
                    if (additional.additionalInfo instanceof AdditionalProjectInterpreterInfo) {
                        AdditionalProjectInterpreterInfo projectInterpreterInfo = (AdditionalProjectInterpreterInfo) additional.additionalInfo;
                        IProject project = projectInterpreterInfo.getProject();
                        PythonNature pythonNature = PythonNature.getPythonNature(project);
                        if (pythonNature != null) {
                            pythonNatures = new ArrayList<IPythonNature>();
                            pythonNatures.add(pythonNature);
                        }
                    } else if (additional.additionalInfo instanceof AdditionalSystemInterpreterInfo) {
                        AdditionalSystemInterpreterInfo systemInterpreterInfo = (AdditionalSystemInterpreterInfo) additional.additionalInfo;
                        SystemPythonNature pythonNature = new SystemPythonNature(systemInterpreterInfo.getManager());
                        pythonNatures = new ArrayList<IPythonNature>();
                        pythonNatures.add(pythonNature);
                    }
                } catch (Throwable e) {
                    Log.log(e);
                }
                entry = additional.info;
            } else {
                entry = (IInfo) obj;
            }
            List<ItemPointer> pointers = new ArrayList<ItemPointer>();
            ICompletionState completionCache = new CompletionState();
            for (IPythonNature pythonNature : pythonNatures) {
                // try to find in one of the natures...
                ICodeCompletionASTManager astManager = pythonNature.getAstManager();
                if (astManager == null) {
                    continue;
                }
                AnalysisPlugin.getDefinitionFromIInfo(pointers, astManager, pythonNature, entry, completionCache, false, true);
                if (pointers.size() > 0) {
                    new PyOpenAction().run(pointers.get(0));
                    // don't check the other natures
                    break;
                }
            }
        }
    }
}
Also used : IPythonNature(org.python.pydev.core.IPythonNature) PythonNature(org.python.pydev.plugin.nature.PythonNature) SystemPythonNature(org.python.pydev.plugin.nature.SystemPythonNature) PyOpenAction(org.python.pydev.editor.actions.PyOpenAction) IPythonNature(org.python.pydev.core.IPythonNature) ArrayList(java.util.ArrayList) SystemPythonNature(org.python.pydev.plugin.nature.SystemPythonNature) ICompletionState(org.python.pydev.core.ICompletionState) CompletionState(org.python.pydev.ast.codecompletion.revisited.CompletionState) SelectionDialog(org.eclipse.ui.dialogs.SelectionDialog) IProject(org.eclipse.core.resources.IProject) AdditionalSystemInterpreterInfo(com.python.pydev.analysis.additionalinfo.AdditionalSystemInterpreterInfo) IInfo(org.python.pydev.core.IInfo) AdditionalInfoAndIInfo(com.python.pydev.analysis.additionalinfo.AdditionalInfoAndIInfo) AdditionalProjectInterpreterInfo(com.python.pydev.analysis.additionalinfo.AdditionalProjectInterpreterInfo) ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) AdditionalInfoAndIInfo(com.python.pydev.analysis.additionalinfo.AdditionalInfoAndIInfo) ICompletionState(org.python.pydev.core.ICompletionState) ItemPointer(org.python.pydev.ast.item_pointer.ItemPointer)

Example 68 with PythonNature

use of org.python.pydev.plugin.nature.PythonNature in project Pydev by fabioz.

the class PySearchIndexPage method checkSelectedResource.

@Override
protected void checkSelectedResource(Collection<String> projectNames, Collection<String> moduleNames, IResource resource) {
    if (resource != null && resource.isAccessible()) {
        IProject project = resource.getProject();
        projectNames.add(project.getName());
        PythonNature nature = PythonNature.getPythonNature(project);
        if (nature == null) {
            return;
        }
        String moduleName;
        try {
            moduleName = nature.resolveModule(resource);
        } catch (MisconfigurationException e) {
            Log.log(e);
            return;
        }
        if (moduleName != null) {
            for (String s : moduleNames) {
                if (s.endsWith(".*")) {
                    if (moduleName.startsWith(s.substring(0, s.length() - 1))) {
                        // There's already another one which includes what we're about to add.
                        return;
                    }
                }
            }
            if (resource instanceof IContainer) {
                moduleNames.add(moduleName + ".*");
            } else {
                moduleNames.add(moduleName);
            }
        }
    }
}
Also used : PythonNature(org.python.pydev.plugin.nature.PythonNature) MisconfigurationException(org.python.pydev.core.MisconfigurationException) IContainer(org.eclipse.core.resources.IContainer) IProject(org.eclipse.core.resources.IProject)

Example 69 with PythonNature

use of org.python.pydev.plugin.nature.PythonNature in project Pydev by fabioz.

the class ProjectsGroup method calculateChildren.

@Override
protected void calculateChildren() {
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IProject[] projects = root.getProjects();
    for (IProject iProject : projects) {
        PythonNature nature = PythonNature.getPythonNature(iProject);
        if (nature != null) {
            addChild(new NatureGroup(this, nature));
        }
    }
}
Also used : IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) PythonNature(org.python.pydev.plugin.nature.PythonNature) IProject(org.eclipse.core.resources.IProject)

Example 70 with PythonNature

use of org.python.pydev.plugin.nature.PythonNature in project Pydev by fabioz.

the class PydevPlugin method stop.

/**
 * This is called when the plugin is being stopped.
 *
 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
 */
@Override
public void stop(BundleContext context) throws Exception {
    DefaultSyncSystemModulesManagerScheduler.get().stop();
    IPath stateLocation = getStateLocation();
    File file = stateLocation.toFile();
    for (String prefix : erasePrefixes) {
        FileUtils.clearTempFilesAt(file, prefix);
    }
    this.isAlive = false;
    try {
        // stop the running shells
        AbstractShell.shutdownAllShells();
        // (no point in getting the ones not initialized)
        for (PythonNature nature : PythonNature.getInitializedPythonNatures()) {
            try {
                nature.saveAstManager();
            } catch (Exception e) {
                Log.log(e);
            }
        }
    } finally {
        super.stop(context);
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) IPythonNature(org.python.pydev.core.IPythonNature) PythonNature(org.python.pydev.plugin.nature.PythonNature) File(java.io.File) BackingStoreException(org.osgi.service.prefs.BackingStoreException) MissingResourceException(java.util.MissingResourceException) CancelException(org.python.pydev.shared_core.progress.CancelException)

Aggregations

PythonNature (org.python.pydev.plugin.nature.PythonNature)88 IProject (org.eclipse.core.resources.IProject)41 IPythonNature (org.python.pydev.core.IPythonNature)32 File (java.io.File)31 HashSet (java.util.HashSet)20 IFile (org.eclipse.core.resources.IFile)20 ArrayList (java.util.ArrayList)19 CoreException (org.eclipse.core.runtime.CoreException)18 IResource (org.eclipse.core.resources.IResource)17 IPath (org.eclipse.core.runtime.IPath)14 IContainer (org.eclipse.core.resources.IContainer)12 MisconfigurationException (org.python.pydev.core.MisconfigurationException)11 PythonSourceFolder (org.python.pydev.navigator.elements.PythonSourceFolder)10 HashMap (java.util.HashMap)9 IPythonPathNature (org.python.pydev.core.IPythonPathNature)9 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)8 ICodeCompletionASTManager (org.python.pydev.core.ICodeCompletionASTManager)8 IFolder (org.eclipse.core.resources.IFolder)7 IInterpreterInfo (org.python.pydev.core.IInterpreterInfo)7 Set (java.util.Set)6