Search in sources :

Example 6 with DataAndImageTreeNode

use of org.python.pydev.shared_core.structure.DataAndImageTreeNode in project Pydev by fabioz.

the class SyncSystemModulesManager method computeChanges.

/**
 * Given the tree structure we created initially with all the changes (root) and the elements
 * that the user selected in the tree (selectElements), return a list of infos updated with the
 * proper pythonpath.
 */
private List<IInterpreterInfo> computeChanges(final DataAndImageTreeNode root, List<TreeNode> selectElements) {
    List<IInterpreterInfo> changedInfos = new ArrayList<>();
    HashSet<TreeNode> set = new HashSet<TreeNode>(selectElements.size());
    set.addAll(selectElements);
    for (Object n : root.getChildren()) {
        DataAndImageTreeNode interpreterNode = (DataAndImageTreeNode) n;
        if (set.contains(interpreterNode)) {
            IInterpreterInfo info = (IInterpreterInfo) interpreterNode.getData();
            List<String> pythonPath = info.getPythonPath();
            boolean changed = false;
            OrderedSet<String> newPythonPath = new OrderedSet<String>(pythonPath);
            for (Object entryNode : interpreterNode.getChildren()) {
                DataAndImageTreeNode pythonpathNode = (DataAndImageTreeNode) entryNode;
                if (set.contains(pythonpathNode)) {
                    PythonpathChange change = (PythonpathChange) pythonpathNode.data;
                    change.apply(newPythonPath);
                    changed = true;
                }
            }
            if (changed) {
                InterpreterInfo copy = (InterpreterInfo) info.makeCopy();
                copy.libs.clear();
                copy.libs.addAll(newPythonPath);
                changedInfos.add(copy);
            }
        }
    }
    return changedInfos;
}
Also used : OrderedSet(org.python.pydev.shared_core.structure.OrderedSet) ArrayList(java.util.ArrayList) DataAndImageTreeNode(org.python.pydev.shared_core.structure.DataAndImageTreeNode) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) DataAndImageTreeNode(org.python.pydev.shared_core.structure.DataAndImageTreeNode) TreeNode(org.python.pydev.shared_core.structure.TreeNode) DefaultPathsForInterpreterInfo(org.python.pydev.ast.interpreter_managers.DefaultPathsForInterpreterInfo) InterpreterInfo(org.python.pydev.ast.interpreter_managers.InterpreterInfo) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) HashSet(java.util.HashSet)

Example 7 with DataAndImageTreeNode

use of org.python.pydev.shared_core.structure.DataAndImageTreeNode in project Pydev by fabioz.

the class SyncSystemModulesManager method saveUnselected.

/**
 * When the user selects changes in selectElementsInDialog, it's possible that he doesn't check some of the
 * proposed changes, thus, in this case, we should save the unselected items in the preferences and the next
 * time such a change is proposed, it should appear unchecked (and if all changes are unchecked, we shouldn't
 * present the user with a dialog).
 *
 * @param root this is the initial structure, containing all the proposed changes.
 * @param selectedElements this is a structure which will hold only the selected changes.
 * @param iPreferenceStore this is the store where we'll keep the selected changes.
 */
public void saveUnselected(DataAndImageTreeNode root, List<TreeNode> selectedElements, IEclipsePreferences iPreferenceStore) {
    // root has null data, level 1 has IInterpreterInfo and level 2 has PythonpathChange.
    HashSet<TreeNode> selectionSet = new HashSet<>();
    if (selectedElements != null && selectedElements.size() > 0) {
        selectionSet.addAll(selectedElements);
    }
    boolean changed = false;
    for (DataAndImageTreeNode<IInterpreterInfo> interpreterNode : (List<DataAndImageTreeNode<IInterpreterInfo>>) root.getChildren()) {
        Set<TreeNode> addToIgnore = new HashSet<>();
        if (!selectionSet.contains(interpreterNode)) {
            // ignore all the entries below this interpreter.
            addToIgnore.addAll(interpreterNode.getChildren());
        } else {
            // check each entry and only add the ones not selected.
            for (TreeNode<PythonpathChange> pathNode : interpreterNode.getChildren()) {
                if (!selectionSet.contains(pathNode)) {
                    addToIgnore.add(pathNode);
                }
            }
        }
        if (addToIgnore.size() > 0) {
            IInterpreterInfo info = interpreterNode.getData();
            String key = createKeyForInfo(info);
            ArrayList<String> addToIgnorePaths = new ArrayList<String>(addToIgnore.size());
            for (TreeNode<PythonpathChange> node : addToIgnore) {
                PythonpathChange data = node.getData();
                addToIgnorePaths.add(data.path);
            }
            if (DEBUG) {
                System.out.println("Setting key: " + key);
                System.out.println("Paths ignored: " + addToIgnorePaths);
            }
            changed = true;
            iPreferenceStore.put(key, StringUtils.join("|||", addToIgnorePaths));
        }
    }
    if (changed) {
        try {
            iPreferenceStore.flush();
        } catch (BackingStoreException e) {
            Log.log(e);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) BackingStoreException(org.osgi.service.prefs.BackingStoreException) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) DataAndImageTreeNode(org.python.pydev.shared_core.structure.DataAndImageTreeNode) TreeNode(org.python.pydev.shared_core.structure.TreeNode) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 8 with DataAndImageTreeNode

use of org.python.pydev.shared_core.structure.DataAndImageTreeNode in project Pydev by fabioz.

the class SyncSystemModulesManager method updateStructures.

/**
 * Here we'll update the tree structure to be shown to the user with the changes (root).
 * The managerToNameToInfo structure has the information on the interpreter manager and related
 * interpreter infos for which the changes should be checked.
 */
public void updateStructures(IProgressMonitor monitor, final DataAndImageTreeNode root, ManagerInfoToUpdate managerToNameToInfo, CreateInterpreterInfoCallback callback) {
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    IImageCache imageCache = SharedCorePlugin.getImageCache();
    if (imageCache == null) {
        imageCache = new IImageCache() {

            @Override
            public IImageHandle getStringDecorated(String key, String stringToAddToDecoration) {
                return null;
            }

            @Override
            public IImageHandle getImageDecorated(String key, String decoration, int decorationLocation, String secondDecoration, int secondDecorationLocation) {
                return null;
            }

            @Override
            public IImageHandle getImageDecorated(String key, String decoration, int decorationLocation) {
                return null;
            }

            @Override
            public IImageHandle getImageDecorated(String key, String decoration) {
                return null;
            }

            @Override
            public IImageDescriptor getDescriptor(String projectIcon) {
                return null;
            }

            @Override
            public IImageHandle get(String key) {
                return null;
            }
        };
    }
    for (Tuple<IInterpreterManager, IInterpreterInfo> infos : managerToNameToInfo.getManagerAndInfos()) {
        IInterpreterManager manager = infos.o1;
        IInterpreterInfo internalInfo = infos.o2;
        String executable = internalInfo.getExecutableOrJar();
        IInterpreterInfo newInterpreterInfo = callback.createInterpreterInfo(manager, executable, monitor);
        if (newInterpreterInfo == null) {
            continue;
        }
        DefaultPathsForInterpreterInfo defaultPaths = new DefaultPathsForInterpreterInfo();
        OrderedSet<String> newEntries = new OrderedSet<String>(newInterpreterInfo.getPythonPath());
        newEntries.removeAll(internalInfo.getPythonPath());
        // Iterate over the new entries to suggest what should be added (we already have only what's not there).
        for (Iterator<String> it = newEntries.iterator(); it.hasNext(); ) {
            String entryInPythonpath = it.next();
            if (!defaultPaths.selectByDefault(entryInPythonpath) || !defaultPaths.exists(entryInPythonpath)) {
                // Don't suggest the addition of entries in the workspace or entries which do not exist.
                it.remove();
            }
        }
        // Iterate over existing entries to suggest what should be removed.
        OrderedSet<String> removedEntries = new OrderedSet<String>();
        List<String> pythonPath = internalInfo.getPythonPath();
        for (String string : pythonPath) {
            if (!new File(string).exists()) {
                // Only suggest a removal if it was removed from the filesystem.
                removedEntries.add(string);
            }
        }
        if (newEntries.size() > 0 || removedEntries.size() > 0) {
            DataAndImageTreeNode<IInterpreterInfo> interpreterNode = new DataAndImageTreeNode<IInterpreterInfo>(root, internalInfo, imageCache.get(UIConstants.PY_INTERPRETER_ICON));
            for (String s : newEntries) {
                new DataAndImageTreeNode(interpreterNode, new PythonpathChange(s, true), imageCache.get(UIConstants.LIB_SYSTEM));
            }
            for (String s : removedEntries) {
                new DataAndImageTreeNode(interpreterNode, new PythonpathChange(s, false), imageCache.get(UIConstants.REMOVE_LIB_SYSTEM));
            }
        }
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) OrderedSet(org.python.pydev.shared_core.structure.OrderedSet) IImageCache(org.python.pydev.shared_core.image.IImageCache) IImageDescriptor(org.python.pydev.shared_core.image.IImageDescriptor) DefaultPathsForInterpreterInfo(org.python.pydev.ast.interpreter_managers.DefaultPathsForInterpreterInfo) DataAndImageTreeNode(org.python.pydev.shared_core.structure.DataAndImageTreeNode) IInterpreterManager(org.python.pydev.core.IInterpreterManager) IImageHandle(org.python.pydev.shared_core.image.IImageHandle) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) File(java.io.File)

Example 9 with DataAndImageTreeNode

use of org.python.pydev.shared_core.structure.DataAndImageTreeNode in project Pydev by fabioz.

the class PyOutlineSelectionDialog method addMethods.

private void addMethods(DataAndImageTreeNode<Object> nextEntry, HierarchyNodeModel model) {
    if (model == null || model.parents == null) {
        return;
    }
    for (HierarchyNodeModel parent : model.parents) {
        DefinitionsASTIteratorVisitor visitor = DefinitionsASTIteratorVisitor.createForChildren(parent.ast);
        if (visitor == null) {
            continue;
        }
        Iterator<ASTEntry> outline = visitor.getOutline();
        while (outline.hasNext()) {
            ASTEntry entry = outline.next();
            if (entry.parent == null) {
                // only direct children...
                new DataAndImageTreeNode<Object>(nextEntry, new OutlineEntry(entry, parent), null);
            }
        }
        addMethods(nextEntry, parent);
    }
}
Also used : ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) DefinitionsASTIteratorVisitor(org.python.pydev.parser.visitors.scope.DefinitionsASTIteratorVisitor) DataAndImageTreeNode(org.python.pydev.shared_core.structure.DataAndImageTreeNode) HierarchyNodeModel(org.python.pydev.ast.refactoring.HierarchyNodeModel)

Example 10 with DataAndImageTreeNode

use of org.python.pydev.shared_core.structure.DataAndImageTreeNode in project Pydev by fabioz.

the class PyOutlineSelectionDialog method gatherClasses.

@SuppressWarnings({ "rawtypes", "unchecked" })
private void gatherClasses(DataAndImageTreeNode<Object> entry, IProgressMonitor monitor, List<Tuple<ClassDef, DataAndImageTreeNode<Object>>> gathered) {
    List children = entry.getChildren();
    if (children.size() == 0) {
        return;
    }
    // Iterate in a copy, since we may change the original...
    for (Object o : children) {
        DataAndImageTreeNode<Object> nextEntry = (DataAndImageTreeNode<Object>) o;
        if (((OutlineEntry) nextEntry.data).node instanceof ClassDef) {
            ClassDef classDef = (ClassDef) ((OutlineEntry) nextEntry.data).node;
            gathered.add(new Tuple<ClassDef, DataAndImageTreeNode<Object>>(classDef, nextEntry));
        }
        // Enter the leaf to fill it too.
        gatherClasses(nextEntry, monitor, gathered);
    }
}
Also used : ClassDef(org.python.pydev.parser.jython.ast.ClassDef) ArrayList(java.util.ArrayList) List(java.util.List) DataAndImageTreeNode(org.python.pydev.shared_core.structure.DataAndImageTreeNode)

Aggregations

DataAndImageTreeNode (org.python.pydev.shared_core.structure.DataAndImageTreeNode)22 ArrayList (java.util.ArrayList)9 IInterpreterInfo (org.python.pydev.core.IInterpreterInfo)8 HierarchyNodeModel (org.python.pydev.ast.refactoring.HierarchyNodeModel)7 TreeNode (org.python.pydev.shared_core.structure.TreeNode)7 HashSet (java.util.HashSet)5 InterpreterInfo (org.python.pydev.ast.interpreter_managers.InterpreterInfo)5 ASTEntry (org.python.pydev.parser.visitors.scope.ASTEntry)5 HashMap (java.util.HashMap)4 List (java.util.List)4 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)4 StyledString (org.eclipse.jface.viewers.StyledString)4 SyncSystemModulesManager (org.python.pydev.ast.codecompletion.revisited.SyncSystemModulesManager)4 IInterpreterManager (org.python.pydev.core.IInterpreterManager)4 AdditionalSystemInterpreterInfo (com.python.pydev.analysis.additionalinfo.AdditionalSystemInterpreterInfo)3 File (java.io.File)3 Collection (java.util.Collection)3 Map (java.util.Map)3 IEclipsePreferences (org.eclipse.core.runtime.preferences.IEclipsePreferences)2 BackingStoreException (org.osgi.service.prefs.BackingStoreException)2