Search in sources :

Example 1 with OrderedMap

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

the class PythonPathNature method getProjectSourcePath.

/**
 * Function which can take care of getting the paths just for the project (i.e.: without external
 * source folders).
 *
 * @param replace used only if returnType == RETURN_STRING_WITH_SEPARATOR.
 *
 * @param substitution the object which will do the string substitutions (only internally used as an optimization as
 * creating the instance may be expensive, so, if some other place already creates it, it can be passed along).
 *
 * @param returnType if RETURN_STRING_WITH_SEPARATOR returns a string using '|' as the separator.
 * If RETURN_MAP_RESOLVED_TO_UNRESOLVED returns a map which points from the paths resolved to the maps unresolved.
 */
private Object getProjectSourcePath(boolean replace, StringSubstitution substitution, int returnType) throws CoreException {
    String projectSourcePath;
    boolean restore = false;
    IProject project = fProject;
    PythonNature nature = fNature;
    if (project == null || nature == null) {
        if (returnType == RETURN_STRING_WITH_SEPARATOR) {
            return "";
        } else if (returnType == RETURN_MAP_RESOLVED_TO_UNRESOLVED) {
            return new OrderedMap<String, String>();
        } else {
            throw new AssertionError("Unexpected return: " + returnType);
        }
    }
    projectSourcePath = nature.getStore().getPathProperty(PythonPathNature.getProjectSourcePathQualifiedName());
    if (projectSourcePath == null) {
        // has not been set
        if (returnType == RETURN_STRING_WITH_SEPARATOR) {
            return "";
        } else if (returnType == RETURN_MAP_RESOLVED_TO_UNRESOLVED) {
            return new OrderedMap<String, String>();
        } else {
            throw new AssertionError("Unexpected return: " + returnType);
        }
    }
    if (replace && substitution == null) {
        substitution = new StringSubstitution(fNature);
    }
    // we have to validate it, because as we store the values relative to the workspace, and not to the
    // project, the path may become invalid (in which case we have to make it compatible again).
    StringBuffer buffer = new StringBuffer();
    List<String> paths = StringUtils.splitAndRemoveEmptyTrimmed(projectSourcePath, '|');
    IPath projectPath = project.getFullPath();
    for (String path : paths) {
        if (path.trim().length() > 0) {
            if (path.indexOf("${") != -1) {
                // Account for the string substitution.
                buffer.append(path);
            } else {
                IPath p = new Path(path);
                if (p.isEmpty()) {
                    // go to the next...
                    continue;
                }
                if (projectPath != null && !FileUtils.isPrefixOf(projectPath, p)) {
                    p = p.removeFirstSegments(1);
                    p = projectPath.append(p);
                    restore = true;
                }
                buffer.append(p.toString());
            }
            buffer.append("|");
        }
    }
    // it was wrong and has just been fixed
    if (restore) {
        projectSourcePath = buffer.toString();
        setProjectSourcePath(projectSourcePath);
        if (nature != null) {
            // yeap, everything has to be done from scratch, as all the filesystem paths have just
            // been turned to dust!
            nature.rebuildPath();
        }
    }
    if (returnType == RETURN_STRING_WITH_SEPARATOR) {
        return trimAndReplaceVariablesIfNeeded(replace, projectSourcePath, nature, substitution);
    } else if (returnType == RETURN_MAP_RESOLVED_TO_UNRESOLVED) {
        String ret = StringUtils.leftAndRightTrim(projectSourcePath, '|');
        OrderedMap<String, String> map = new OrderedMap<String, String>();
        List<String> unresolvedVars = StringUtils.splitAndRemoveEmptyTrimmed(ret, '|');
        // Always resolves here!
        List<String> resolved = StringUtils.splitAndRemoveEmptyTrimmed(substitution.performPythonpathStringSubstitution(ret), '|');
        int size = unresolvedVars.size();
        if (size != resolved.size()) {
            throw new AssertionError("Error: expected same size from:\n" + unresolvedVars + "\nand\n" + resolved);
        }
        for (int i = 0; i < size; i++) {
            String un = unresolvedVars.get(i);
            String res = resolved.get(i);
            map.put(res, un);
        }
        return map;
    } else {
        throw new AssertionError("Unexpected return: " + returnType);
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPythonNature(org.python.pydev.core.IPythonNature) IPath(org.eclipse.core.runtime.IPath) StringSubstitution(org.python.pydev.core.docutils.StringSubstitution) IProject(org.eclipse.core.resources.IProject) FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) List(java.util.List) OrderedMap(org.python.pydev.shared_core.structure.OrderedMap)

Example 2 with OrderedMap

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

the class PyCodeCompletion method createOverrideCodeCompletions.

private void createOverrideCodeCompletions(CompletionRequest request, ArrayList<ICompletionProposalHandle> ret, PySelection ps) throws BadLocationException {
    IImageCache imageCache = SharedCorePlugin.getImageCache();
    IImageHandle imageOverride = imageCache != null ? imageCache.get(UIConstants.METHOD_ICON) : null;
    String lineContentsToCursor = ps.getLineContentsToCursor();
    LineStartingScope scopeStart = ps.getPreviousLineThatStartsScope(PySelection.CLASS_TOKEN, false, PySelection.getFirstCharPosition(lineContentsToCursor));
    String className = null;
    if (scopeStart != null) {
        className = PySelection.getClassNameInLine(scopeStart.lineStartingScope);
        if (className != null && className.length() > 0) {
            Tuple<List<String>, Integer> insideParensBaseClasses = ps.getInsideParentesisToks(true, scopeStart.iLineStartingScope);
            if (insideParensBaseClasses != null) {
                // representation -> token and base class
                OrderedMap<String, ImmutableTuple<IToken, String>> map = new OrderedMap<String, ImmutableTuple<IToken, String>>();
                for (String baseClass : insideParensBaseClasses.o1) {
                    try {
                        ICompletionState state = new CompletionState(-1, -1, null, request.nature, baseClass);
                        state.setActivationToken(baseClass);
                        state.setIsInCalltip(false);
                        IPythonNature pythonNature = request.nature;
                        checkPythonNature(pythonNature);
                        ICodeCompletionASTManager astManager = pythonNature.getAstManager();
                        if (astManager == null) {
                            // we're probably still loading it.
                            return;
                        }
                        // Ok, looking for a token in globals.
                        IModule module = request.getModule();
                        if (module == null) {
                            continue;
                        }
                        TokensList comps = astManager.getCompletionsForModule(module, state, true, true);
                        for (IterTokenEntry entry : comps) {
                            IToken iToken = entry.getToken();
                            String representation = iToken.getRepresentation();
                            ImmutableTuple<IToken, String> curr = map.get(representation);
                            if (curr != null && curr.o1 instanceof SourceToken) {
                                // source tokens are never reset!
                                continue;
                            }
                            int type = iToken.getType();
                            if (iToken instanceof SourceToken && ((SourceToken) iToken).getAst() instanceof FunctionDef) {
                                map.put(representation, new ImmutableTuple<IToken, String>(iToken, baseClass));
                            } else if (type == IToken.TYPE_FUNCTION || type == IToken.TYPE_UNKNOWN || type == IToken.TYPE_BUILTIN) {
                                map.put(representation, new ImmutableTuple<IToken, String>(iToken, baseClass));
                            }
                        }
                    } catch (Exception e) {
                        Log.log(e);
                    }
                }
                for (ImmutableTuple<IToken, String> tokenAndBaseClass : map.values()) {
                    FunctionDef functionDef = null;
                    // No checkings needed for type (we already did that above).
                    if (tokenAndBaseClass.o1 instanceof SourceToken) {
                        SourceToken sourceToken = (SourceToken) tokenAndBaseClass.o1;
                        SimpleNode ast = sourceToken.getAst();
                        if (ast instanceof FunctionDef) {
                            functionDef = (FunctionDef) ast;
                        } else {
                            functionDef = sourceToken.getAliased().createCopy();
                            NameTok t = (NameTok) functionDef.name;
                            t.id = sourceToken.getRepresentation();
                        }
                    } else {
                        // unfortunately, for builtins we usually cannot trust the parameters.
                        String representation = tokenAndBaseClass.o1.getRepresentation();
                        PyAstFactory factory = new PyAstFactory(new AdapterPrefs(ps.getEndLineDelim(), request.nature));
                        functionDef = factory.createFunctionDef(representation);
                        functionDef.args = factory.createArguments(true);
                        functionDef.args.vararg = new NameTok("args", NameTok.VarArg);
                        functionDef.args.kwarg = new NameTok("kwargs", NameTok.KwArg);
                        if (!representation.equals("__init__")) {
                            // signal that the return should be added
                            functionDef.body = new stmtType[] { new Return(null) };
                        }
                    }
                    if (functionDef != null) {
                        ret.add(CompletionProposalFactory.get().createOverrideMethodCompletionProposal(request, ps, ps.getAbsoluteCursorOffset(), 0, 0, imageOverride, functionDef, tokenAndBaseClass.o2, className));
                    }
                }
            }
        }
    }
}
Also used : IModule(org.python.pydev.core.IModule) IPythonNature(org.python.pydev.core.IPythonNature) ICompletionState(org.python.pydev.core.ICompletionState) CompletionState(org.python.pydev.ast.codecompletion.revisited.CompletionState) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) SimpleNode(org.python.pydev.parser.jython.SimpleNode) ISimpleNode(org.python.pydev.shared_core.model.ISimpleNode) IToken(org.python.pydev.core.IToken) ICompletionState(org.python.pydev.core.ICompletionState) TokensList(org.python.pydev.core.TokensList) List(java.util.List) TokensOrProposalsList(org.python.pydev.core.TokensOrProposalsList) ArrayList(java.util.ArrayList) PyAstFactory(org.python.pydev.parser.jython.ast.factory.PyAstFactory) TokensList(org.python.pydev.core.TokensList) Return(org.python.pydev.parser.jython.ast.Return) IImageCache(org.python.pydev.shared_core.image.IImageCache) IterTokenEntry(org.python.pydev.core.IterTokenEntry) ImmutableTuple(org.python.pydev.shared_core.structure.ImmutableTuple) LineStartingScope(org.python.pydev.core.docutils.PySelection.LineStartingScope) CoreException(org.eclipse.core.runtime.CoreException) PythonNatureWithoutProjectException(org.python.pydev.core.PythonNatureWithoutProjectException) CompletionRecursionException(org.python.pydev.core.structure.CompletionRecursionException) BadLocationException(org.eclipse.jface.text.BadLocationException) IOException(java.io.IOException) MisconfigurationException(org.python.pydev.core.MisconfigurationException) ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) IImageHandle(org.python.pydev.shared_core.image.IImageHandle) AdapterPrefs(org.python.pydev.parser.jython.ast.factory.AdapterPrefs) OrderedMap(org.python.pydev.shared_core.structure.OrderedMap) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken) NameTok(org.python.pydev.parser.jython.ast.NameTok)

Example 3 with OrderedMap

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

the class PySearchIndexQuery method run.

@Override
public IStatus run(IProgressMonitor monitor) throws OperationCanceledException {
    SearchIndexResult searchResult = (SearchIndexResult) getSearchResult();
    // Remove all so that we don't get duplicates on a search refresh.
    searchResult.removeAll();
    StringMatcherWithIndexSemantics stringMatcher = createStringMatcher();
    Set<String> moduleNamesFilter = scopeAndData.getModuleNamesFilter();
    OrderedMap<String, Set<String>> fieldNameToValues = new OrderedMap<>();
    if (moduleNamesFilter != null && !moduleNamesFilter.isEmpty()) {
        fieldNameToValues.put(IReferenceSearches.FIELD_MODULE_NAME, moduleNamesFilter);
    }
    Set<String> split = makeTextFieldPatternsToSearchFromText();
    fieldNameToValues.put(IReferenceSearches.FIELD_CONTENTS, split);
    final List<IPythonNature> pythonNatures = PyScopeAndData.getPythonNatures(scopeAndData);
    monitor.beginTask("Search indexes", pythonNatures.size());
    try {
        for (IPythonNature nature : pythonNatures) {
            AbstractAdditionalDependencyInfo info;
            try {
                info = AdditionalProjectInterpreterInfo.getAdditionalInfoForProject(nature);
            } catch (MisconfigurationException e) {
                Log.log(e);
                continue;
            }
            IReferenceSearches referenceSearches = info.getReferenceSearches();
            List<ModulesKey> search = referenceSearches.search(nature.getProject(), fieldNameToValues, new SubProgressMonitor(monitor, 1));
            IFile workspaceFile;
            for (ModulesKey modulesKey : search) {
                File file = modulesKey.file;
                if (file == null || !file.exists()) {
                    Log.logInfo(StringUtils.format("Ignoring: %s. File no longer exists.", file));
                }
                workspaceFile = FindWorkspaceFiles.getWorkspaceFile(file, nature.getProject());
                if (workspaceFile == null) {
                    Log.logInfo(StringUtils.format("Ignoring: %s. Unable to resolve to a file in the Eclipse workspace.", file));
                    continue;
                }
                IDocument doc = FileUtilsFileBuffer.getDocFromResource(workspaceFile);
                String text = doc.get();
                createMatches(doc, text, stringMatcher, workspaceFile, searchResult, modulesKey);
            }
        }
    } finally {
        monitor.done();
    }
    return Status.OK_STATUS;
}
Also used : StringMatcherWithIndexSemantics(org.python.pydev.shared_ui.search.StringMatcherWithIndexSemantics) Set(java.util.Set) IFile(org.eclipse.core.resources.IFile) MisconfigurationException(org.python.pydev.core.MisconfigurationException) IPythonNature(org.python.pydev.core.IPythonNature) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IReferenceSearches(com.python.pydev.analysis.additionalinfo.IReferenceSearches) SearchIndexResult(org.python.pydev.shared_ui.search.SearchIndexResult) ModulesKey(org.python.pydev.core.ModulesKey) OrderedMap(org.python.pydev.shared_core.structure.OrderedMap) AbstractAdditionalDependencyInfo(com.python.pydev.analysis.additionalinfo.AbstractAdditionalDependencyInfo) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IDocument(org.eclipse.jface.text.IDocument)

Example 4 with OrderedMap

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

the class PythonPathHelper method updatePyPath.

/**
 * Helper to update the pythonpath when a copy, move or delete operation is done which could affect a source folder
 * (so, should work when moving/copying/deleting the parent folder of a source folder for instance).
 *
 * Note that the destination may be null in a delete operation.
 */
public static void updatePyPath(IResource[] copiedResources, IContainer destination, int operation) {
    try {
        Map<IProject, OrderedMap<String, String>> projectSourcePathMapsCache = new HashMap<IProject, OrderedMap<String, String>>();
        List<String> addToDestProjects = new ArrayList<String>();
        // Step 1: remove source folders from the copied projects
        HashSet<IProject> changed = new HashSet<IProject>();
        for (IResource resource : copiedResources) {
            if (!(resource instanceof IFolder)) {
                continue;
            }
            OrderedMap<String, String> sourceMap = PythonPathHelper.getResourcePythonPathMap(projectSourcePathMapsCache, resource);
            Set<String> keySet = sourceMap.keySet();
            for (Iterator<String> it = keySet.iterator(); it.hasNext(); ) {
                String next = it.next();
                IPath existingInPath = Path.fromPortableString(next);
                if (FileUtils.isPrefixOf(resource.getFullPath(), existingInPath)) {
                    if (operation == PythonPathHelper.OPERATION_MOVE || operation == PythonPathHelper.OPERATION_DELETE) {
                        // Remove from that project (but not on copy)
                        it.remove();
                        changed.add(resource.getProject());
                    }
                    if (operation == PythonPathHelper.OPERATION_COPY || operation == PythonPathHelper.OPERATION_MOVE) {
                        // Add to new project (but not on delete)
                        String addToNewProjectPath = destination.getFullPath().append(existingInPath.removeFirstSegments(resource.getFullPath().segmentCount() - 1)).toPortableString();
                        addToDestProjects.add(addToNewProjectPath);
                    }
                }
            }
        }
        if (operation != PythonPathHelper.OPERATION_DELETE) {
            // Step 2: add source folders to the project it was copied to
            OrderedMap<String, String> destSourceMap = PythonPathHelper.getResourcePythonPathMap(projectSourcePathMapsCache, destination);
            // Get the PYTHONPATH of the destination project. It may be modified to include the pasted resources.
            IProject destProject = destination.getProject();
            for (String addToNewProjectPath : addToDestProjects) {
                String destActualPath = PyStructureConfigHelpers.convertToProjectRelativePath(destProject.getFullPath().toPortableString(), addToNewProjectPath);
                destSourceMap.put(addToNewProjectPath, destActualPath);
                changed.add(destProject);
            }
        }
        // Step 3: update the target project
        for (IProject project : changed) {
            OrderedMap<String, String> sourceMap = PythonPathHelper.getResourcePythonPathMap(projectSourcePathMapsCache, project);
            PythonNature nature = PythonNature.getPythonNature(project);
            if (nature == null) {
                // don't change non-pydev projects
                continue;
            }
            nature.getPythonPathNature().setProjectSourcePath(StringUtils.join("|", sourceMap.values()));
            nature.rebuildPath();
        }
    } catch (Exception e) {
        Log.log(IStatus.ERROR, "Unexpected error setting project properties", e);
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) PythonNature(org.python.pydev.plugin.nature.PythonNature) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) IOException(java.io.IOException) OrderedMap(org.python.pydev.shared_core.structure.OrderedMap) IResource(org.eclipse.core.resources.IResource) HashSet(java.util.HashSet) IFolder(org.eclipse.core.resources.IFolder)

Example 5 with OrderedMap

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

the class IndexingTest method testSearchModuleKey.

public void testSearchModuleKey() throws Exception {
    Map<String, String> map = new HashMap<>();
    map.put(IFields.FILENAME, "my.mod");
    Reader reader = new StringReader("ab");
    indexApi.index(map, reader, IFields.GENERAL_CONTENTS);
    map = new HashMap<>();
    map.put(IFields.FILENAME, "my.mod2");
    reader = new StringReader("ab");
    indexApi.index(map, reader, IFields.GENERAL_CONTENTS);
    IDocumentsVisitor visitor = new IDocumentsVisitor() {

        @Override
        public void visit(DocumentInfo documentInfo) {
        }
    };
    OrderedMap<String, Set<String>> fieldNameToValues = new OrderedMap<>();
    fieldNameToValues.put(IFields.GENERAL_CONTENTS, new HashSet<>(Arrays.asList("*ab*")));
    fieldNameToValues.put(IFields.FILENAME, new HashSet<>(Arrays.asList("my.mod")));
    SearchResult result = indexApi.searchWildcard(fieldNameToValues, true, visitor, null, IFields.FILENAME);
    assertEquals(1, result.getNumberOfDocumentMatches());
    fieldNameToValues.put(IFields.FILENAME, new HashSet<>(Arrays.asList("my.mod*")));
    result = indexApi.searchWildcard(fieldNameToValues, true, visitor, null, IFields.FILENAME);
    assertEquals(2, result.getNumberOfDocumentMatches());
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) IDocumentsVisitor(org.python.pydev.shared_core.index.IndexApi.IDocumentsVisitor) Reader(java.io.Reader) StringReader(java.io.StringReader) StringReader(java.io.StringReader) OrderedMap(org.python.pydev.shared_core.structure.OrderedMap) DocumentInfo(org.python.pydev.shared_core.index.IndexApi.DocumentInfo)

Aggregations

OrderedMap (org.python.pydev.shared_core.structure.OrderedMap)7 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 Set (java.util.Set)3 IProject (org.eclipse.core.resources.IProject)3 CoreException (org.eclipse.core.runtime.CoreException)3 IPythonNature (org.python.pydev.core.IPythonNature)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 List (java.util.List)2 IPath (org.eclipse.core.runtime.IPath)2 MisconfigurationException (org.python.pydev.core.MisconfigurationException)2 ModulesKey (org.python.pydev.core.ModulesKey)2 AbstractAdditionalDependencyInfo (com.python.pydev.analysis.additionalinfo.AbstractAdditionalDependencyInfo)1 IReferenceSearches (com.python.pydev.analysis.additionalinfo.IReferenceSearches)1 File (java.io.File)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 IFile (org.eclipse.core.resources.IFile)1 IFolder (org.eclipse.core.resources.IFolder)1