Package org.python.pydev.core

Examples of org.python.pydev.core.IPythonNature


            IDocument doc = viewer.getDocument();

            //list for storing the proposals
            ArrayList<ICompletionProposal> pythonAndTemplateProposals = new ArrayList<ICompletionProposal>();

            IPythonNature nature = edit.getPythonNature();

            if (nature == null) {
                IInterpreterManager manager = ChooseInterpreterManager.chooseInterpreterManager();
                if (manager != null) {
                    nature = new SystemPythonNature(manager);
                } else {
                    CompletionError completionError = new CompletionError(new RuntimeException(
                            "No interpreter configured."));
                    this.error = completionError.getErrorMessage();
                    return new ICompletionProposal[] { completionError };
                }
            }

            if (nature == null || !nature.startRequests()) {
                return new ICompletionProposal[0];
            }
            try {
                CompletionRequest request = new CompletionRequest(edit.getEditorFile(), nature, doc, documentOffset,
                        codeCompletion);

                //SECOND: getting code completions and deciding if templates should be shown too.
                //Get code completion proposals
                if (PyCodeCompletionPreferencesPage.useCodeCompletion()) {
                    if (whatToShow == SHOW_ALL) {
                        try {
                            pythonAndTemplateProposals.addAll(getPythonProposals(viewer, documentOffset, doc, request));
                        } catch (Throwable e) {
                            Log.log(e);
                            CompletionError completionError = new CompletionError(e);
                            this.error = completionError.getErrorMessage();
                            //Make the error visible to the user!
                            return new ICompletionProposal[] { completionError };
                        }
                    }

                }

                String[] strs = PySelection.getActivationTokenAndQual(doc, documentOffset, false);

                String activationToken = strs[0];
                String qualifier = strs[1];

                //THIRD: Get template proposals (if asked for)
                if (request.showTemplates && (activationToken == null || activationToken.trim().length() == 0)) {
                    List templateProposals = getTemplateProposals(viewer, documentOffset, activationToken, qualifier);
                    pythonAndTemplateProposals.addAll(templateProposals);
                }

                //to show the valid ones, we'll get the qualifier from the initial request
                proposals = PyCodeCompletionUtils.onlyValidSorted(pythonAndTemplateProposals, request.qualifier,
                        request.isInCalltip);
            } finally {
                nature.endRequests();
            }
        } catch (Exception e) {
            Log.log(e);
            CompletionError completionError = new CompletionError(e);
            this.error = completionError.getErrorMessage();
View Full Code Here


        PyEdit pyEdit = (PyEdit) part;
        SimpleNode ast = pyEdit.getAST();
        if (ast == null) {
            IDocument doc = pyEdit.getDocument();
            SourceModule sourceModule;
            IPythonNature nature = null;
            try {
                nature = pyEdit.getPythonNature();
            } catch (MisconfigurationException e) {
                //Let's try to find a suitable nature
                File editorFile = pyEdit.getEditorFile();
View Full Code Here

                                    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 ret;
                                    }
                                    //Ok, looking for a token in globals.
                                    IModule module = request.getModule();
                                    if (module == null) {
                                        continue;
                                    }
                                    IToken[] comps = astManager.getCompletionsForModule(module, state, true, true);
                                    for (int i = 0; i < comps.length; i++) {
                                        IToken iToken = comps[i];
                                        String representation = iToken.getRepresentation();
                                        ImmutableTuple<IToken, String> curr = map.get(representation);
                                        if (curr != null && curr.o1 instanceof SourceToken) {
                                            continue; //source tokens are never reset!
                                        }

                                        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__")) {
                                        functionDef.body = new stmtType[] { new Return(null) }; //signal that the return should be added
                                    }
                                }

                                if (functionDef != null) {
                                    ret.add(new OverrideMethodCompletionProposal(ps.getAbsoluteCursorOffset(), 0, 0,
                                            imageOverride, functionDef, tokenAndBaseClass.o2, //baseClass
                                            className));
                                }
                            }

                        }
                    }
                }
            }
            request.showTemplates = false;
            return ret;
        }

        try {
            IPythonNature pythonNature = request.nature;
            checkPythonNature(pythonNature);

            ICodeCompletionASTManager astManager = pythonNature.getAstManager();
            if (astManager == null) {
                //we're probably still loading it.
                return ret;
            }
View Full Code Here

        String launchConfigurationType;
        String defaultType = Constants.ID_PYTHON_REGULAR_LAUNCH_CONFIGURATION_TYPE;
        IInterpreterManager interpreterManager = PydevPlugin.getPythonInterpreterManager();

        try {
            IPythonNature nature = pyEdit.getPythonNature();
            if (nature == null) {
                launchConfigurationType = defaultType;
            } else {
                int interpreterType = nature.getInterpreterType();
                interpreterManager = nature.getRelatedInterpreterManager();
                switch (interpreterType) {
                    case IInterpreterManager.INTERPRETER_TYPE_PYTHON:
                        if (isUnitTest) {
                            launchConfigurationType = Constants.ID_PYTHON_UNITTEST_LAUNCH_CONFIGURATION_TYPE;
                        } else {
View Full Code Here

    }

    public RefactoringInfo(PyEdit edit, ITextSelection selection) throws MisconfigurationException {
        IEditorInput input = edit.getEditorInput();
        this.indentPrefs = edit.getIndentPrefs();
        IPythonNature localNature = edit.getPythonNature();

        if (input instanceof IFileEditorInput) {
            IFileEditorInput editorInput = (IFileEditorInput) input;
            this.sourceFile = editorInput.getFile();
            this.realFile = sourceFile != null ? sourceFile.getLocation().toFile() : null;
View Full Code Here

        //Other possibilities
        //org.eclipse.jface.text.source.SourceViewer (in compare)

        if (this.viewer instanceof PySourceViewer) {
            try {
                IPythonNature nature = ((PySourceViewer) this.viewer).getEdit().getPythonNature();
                if (nature != null) {
                    return nature.getGrammarVersion();
                }
            } catch (MisconfigurationException e) {
            }
        }
View Full Code Here

    public String getModuleName() {
        if (this.viewer instanceof PySourceViewer) {
            try {
                PySourceViewer pyViewer = (PySourceViewer) this.viewer;
                PyEdit edit = pyViewer.getEdit();
                IPythonNature nature = edit.getPythonNature();
                if (nature != null) {
                    return nature.resolveModule(edit.getEditorFile());
                }
            } catch (MisconfigurationException e) {
            }
        }
        return "";
View Full Code Here

            //this is needed because this method might be called alone (not in the grouper that checks
            //if it is in the pythonpath before)
            //
            //this happens only when a .pyc file is found... if it was a .py file, this would not be needed (as is the
            //case in the visit removed resource)
            IPythonNature nature = PythonNature.getPythonNature(resource);
            if (nature == null) {
                markAsDerived(resource);
                return;
            }
            try {
                if (!nature.isResourceInPythonpathProjectSources(dotPyLoc, false)) {
                    return; // we only analyze resources that are source folders (not external folders)
                }
            } catch (Exception e) {
                Log.log(e);
                return;
View Full Code Here

    private void performFullBuild(IProgressMonitor monitor) throws CoreException {
        IProject project = getProject();

        //we need the project...
        if (project != null) {
            IPythonNature nature = PythonNature.getPythonNature(project);

            //and the nature...
            if (nature != null && nature.startRequests()) {

                try {
                    IPythonPathNature pythonPathNature = nature.getPythonPathNature();
                    pythonPathNature.getProjectSourcePath(false); //this is just to update the paths (in case the project name has just changed)

                    List<IFile> resourcesToParse = new ArrayList<IFile>();

                    List<PyDevBuilderVisitor> visitors = getVisitors();
                    notifyVisitingWillStart(visitors, monitor, true, nature);

                    monitor.beginTask("Building...", (visitors.size() * 100) + 30);

                    IResource[] members = project.members();

                    if (members != null) {
                        // get all the python files to get information.
                        for (int i = 0; i < members.length; i++) {
                            try {
                                IResource member = members[i];
                                if (member == null) {
                                    continue;
                                }

                                if (member.getType() == IResource.FILE) {
                                    addToResourcesToParse(resourcesToParse, (IFile) member, nature);

                                } else if (member.getType() == IResource.FOLDER) {
                                    //if it is a folder, let's get all python files that are beneath it
                                    //the heuristics to know if we have to analyze them are the same we have
                                    //for a single file
                                    List<IFile> l = PyFileListing.getAllIFilesBelow((IFolder) member);

                                    for (Iterator<IFile> iter = l.iterator(); iter.hasNext();) {
                                        IFile element = iter.next();
                                        if (element != null) {
                                            addToResourcesToParse(resourcesToParse, element, nature);
                                        }
                                    }
                                } else {
                                    if (DEBUG) {
                                        System.out.println("Unknown type: " + member.getType());
                                    }
                                }
                            } catch (Exception e) {
                                // that's ok...
                            }
                        }
                        monitor.worked(30);
                        buildResources(resourcesToParse, monitor, visitors);
                    }
                    notifyVisitingEnded(visitors, monitor);
                } finally {
                    nature.endRequests();
                }
            }
        }
        monitor.done();

View Full Code Here

            total += inc;
            IFile r = iter.next();

            PythonPathHelper.markAsPyDevFileIfDetected(r);

            IPythonNature nature = PythonNature.getPythonNature(r);
            if (nature == null) {
                continue;
            }
            if (!nature.startRequests()) {
                continue;
            }
            try {
                String moduleName;
                try {
                    //we visit external because we must index them
                    moduleName = nature.resolveModuleOnlyInProjectSources(r, true);
                    if (moduleName == null) {
                        continue; // we only analyze resources that are in the pythonpath
                    }
                } catch (Exception e1) {
                    if (!loggedMisconfiguration) {
                        loggedMisconfiguration = true; //No point in logging it over and over again.
                        Log.log(e1);
                    }
                    continue;
                }

                //create new memo for each resource
                HashMap<String, Object> memo = new HashMap<String, Object>();
                memo.put(PyDevBuilderVisitor.IS_FULL_BUILD, true); //mark it as full build

                ICallback0<IDocument> doc = FileUtilsFileBuffer.getDocOnCallbackFromResource(r);
                memo.put(PyDevBuilderVisitor.DOCUMENT_TIME, System.currentTimeMillis());

                PyDevBuilderVisitor.setModuleNameInCache(memo, r, moduleName);

                for (Iterator<PyDevBuilderVisitor> it = visitors.iterator(); it.hasNext()
                        && monitor.isCanceled() == false;) {

                    try {
                        PyDevBuilderVisitor visitor = it.next();
                        visitor.memo = memo; //setting the memo must be the first thing.

                        communicateProgress(monitor, totalResources, i, r, visitor, bufferToCreateString);

                        //on a full build, all visits are as some add...
                        visitor.visitAddedResource(r, doc, monitor);
                    } catch (Exception e) {
                        Log.log(e);
                    }
                }

                if (total > 1) {
                    monitor.worked((int) total);
                    total -= (int) total;
                }
            } finally {
                nature.endRequests();
            }
        }
    }
View Full Code Here

TOP

Related Classes of org.python.pydev.core.IPythonNature

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.