Search in sources :

Example 1 with ICompletionState

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

the class TddQuickFixParticipant method addProps.

@Override
public void addProps(MarkerAnnotationAndPosition markerAnnotation, IAnalysisPreferences analysisPreferences, String line, PySelection ps, int offset, IPythonNature nature, PyEdit edit, List<ICompletionProposalHandle> props) throws BadLocationException, CoreException {
    if (nature == null) {
        return;
    }
    ICodeCompletionASTManager astManager = nature.getAstManager();
    if (astManager == null) {
        return;
    }
    if (markerAnnotation.position == null) {
        return;
    }
    IMarker marker = markerAnnotation.markerAnnotation.getMarker();
    Integer id = (Integer) marker.getAttribute(AnalysisRunner.PYDEV_ANALYSIS_TYPE);
    int start = markerAnnotation.position.offset;
    int end = start + markerAnnotation.position.length;
    ps.setSelection(start, end);
    String markerContents;
    try {
        markerContents = ps.getSelectedText();
    } catch (Exception e1) {
        // Selection may be wrong.
        return;
    }
    IDocument doc = ps.getDoc();
    List<String> parametersAfterCall = ps.getParametersAfterCall(end);
    switch(id) {
        case IAnalysisPreferences.TYPE_UNDEFINED_VARIABLE:
            addCreateClassOption(ps, edit, props, markerContents, parametersAfterCall);
            addCreateMethodOption(ps, edit, props, markerContents, parametersAfterCall);
            break;
        case IAnalysisPreferences.TYPE_UNDEFINED_IMPORT_VARIABLE:
            // Say we had something as:
            // import sys
            // sys.Bar
            // in which case 'Bar' is undefined
            // in this situation, the activationTokenAndQual would be "sys." and "Bar"
            // and we want to get the definition for "sys"
            String[] activationTokenAndQual = ps.getActivationTokenAndQualifier(true);
            if (activationTokenAndQual[0].endsWith(".")) {
                ArrayList<IDefinition> selected = findDefinitions(nature, edit, start - 2, doc);
                for (IDefinition iDefinition : selected) {
                    IModule module = iDefinition.getModule();
                    if (module.getFile() != null) {
                        Definition definition = (Definition) iDefinition;
                        File file = module.getFile();
                        if (definition.ast == null) {
                            // if we have no ast in the definition, it means the module itself was found (global scope)
                            // Add option to create class at the given module!
                            addCreateClassOption(ps, edit, props, markerContents, parametersAfterCall, file);
                            addCreateMethodOption(ps, edit, props, markerContents, parametersAfterCall, file);
                        } else if (definition.ast instanceof ClassDef) {
                            ClassDef classDef = (ClassDef) definition.ast;
                            // Ok, we should create a field or method in this case (accessing a classmethod or staticmethod)
                            PyCreateMethodOrField pyCreateMethod = new PyCreateMethodOrField();
                            String className = NodeUtils.getNameFromNameTok(classDef.name);
                            pyCreateMethod.setCreateInClass(className);
                            pyCreateMethod.setCreateAs(PyCreateMethodOrField.CLASSMETHOD);
                            addCreateClassmethodOption(ps, edit, props, markerContents, parametersAfterCall, pyCreateMethod, file, className);
                        }
                    }
                }
            }
            break;
        case IAnalysisPreferences.TYPE_UNRESOLVED_IMPORT:
            // This case is the following: from other_module4 import Foo
            // with 'Foo' being undefined.
            // So, we have to suggest creating a Foo class/method in other_module4
            PyImportsHandling importsHandling = new PyImportsHandling(ps.getDoc(), false);
            int offsetLine = ps.getLineOfOffset(start);
            String selectedText = ps.getSelectedText();
            Tuple<IModule, String> found = null;
            String foundFromImportStr = null;
            boolean isImportFrom = false;
            OUT: for (ImportHandle handle : importsHandling) {
                if (handle.startFoundLine == offsetLine || handle.endFoundLine == offsetLine || (handle.startFoundLine < offsetLine && handle.endFoundLine > offsetLine)) {
                    List<ImportHandleInfo> importInfo = handle.getImportInfo();
                    for (ImportHandleInfo importHandleInfo : importInfo) {
                        String fromImportStr = importHandleInfo.getFromImportStr();
                        List<String> importedStr = importHandleInfo.getImportedStr();
                        for (String imported : importedStr) {
                            if (selectedText.equals(imported)) {
                                if (fromImportStr != null) {
                                    foundFromImportStr = fromImportStr + "." + imported;
                                    isImportFrom = true;
                                } else {
                                    // if fromImportStr == null, it's not a from xxx import yyy (i.e.: simple import)
                                    foundFromImportStr = imported;
                                }
                                try {
                                    String currentModule = nature.resolveModule(edit.getEditorFile());
                                    ICompletionState state = CompletionStateFactory.getEmptyCompletionState(nature, new CompletionCache());
                                    found = nature.getAstManager().findModule(foundFromImportStr, currentModule, state, new SourceModule(currentModule, edit.getEditorFile(), edit.getAST(), null, nature));
                                } catch (Exception e) {
                                    Log.log(e);
                                }
                                break OUT;
                            }
                        }
                    }
                    break OUT;
                }
            }
            boolean addOptionToCreateClassOrMethod = isImportFrom;
            if (found != null && found.o1 != null) {
                // or just create a class or method at the end.
                if (found.o1 instanceof SourceModule) {
                    // if all was found, there's nothing left to create.
                    if (found.o2 != null && found.o2.length() > 0) {
                        SourceModule sourceModule = (SourceModule) found.o1;
                        File file = sourceModule.getFile();
                        if (found.o2.indexOf('.') != -1) {
                            // We have to create some intermediary structure.
                            if (!addOptionToCreateClassOrMethod) {
                                // Cannot create class or method from the info (only the module structure).
                                if (sourceModule.getName().endsWith(".__init__")) {
                                    File f = getFileStructure(file.getParentFile(), found.o2);
                                    addCreateModuleOption(ps, edit, props, markerContents, f);
                                }
                            } else {
                                // Ok, the leaf may be a class or method.
                                if (sourceModule.getName().endsWith(".__init__")) {
                                    String moduleName = FullRepIterable.getWithoutLastPart(sourceModule.getName());
                                    String withoutLastPart = FullRepIterable.getWithoutLastPart(found.o2);
                                    moduleName += "." + withoutLastPart;
                                    String classOrMethodName = FullRepIterable.getLastPart(found.o2);
                                    File f = getFileStructure(file.getParentFile(), withoutLastPart);
                                    addCreateClassInNewModuleOption(ps, edit, props, classOrMethodName, moduleName, parametersAfterCall, f);
                                    addCreateMethodInNewModuleOption(ps, edit, props, classOrMethodName, moduleName, parametersAfterCall, f);
                                }
                            }
                        } else {
                            // Ok, it's all there, we just have to create the leaf.
                            if (!addOptionToCreateClassOrMethod || sourceModule.getName().endsWith(".__init__")) {
                                // Cannot create class or method from the info (only the module structure).
                                if (sourceModule.getName().endsWith(".__init__")) {
                                    File f = new File(file.getParent(), found.o2 + FileTypesPreferences.getDefaultDottedPythonExtension());
                                    addCreateModuleOption(ps, edit, props, markerContents, f);
                                }
                            } else {
                                // Ok, the leaf may be a class or method.
                                addCreateClassOption(ps, edit, props, markerContents, parametersAfterCall, file);
                                addCreateMethodOption(ps, edit, props, markerContents, parametersAfterCall, file);
                            }
                        }
                    }
                }
            } else if (foundFromImportStr != null) {
                // We couldn't find anything there, so, we have to create the modules structure as needed and
                // maybe create a class or module at the end (but only if it's an import from).
                // Ok, it's all there, we just have to create the leaf.
                // Discover the source folder where we should create the structure.
                File editorFile = edit.getEditorFile();
                String onlyProjectPythonPathStr = nature.getPythonPathNature().getOnlyProjectPythonPathStr(false);
                List<String> split = StringUtils.splitAndRemoveEmptyTrimmed(onlyProjectPythonPathStr, '|');
                for (int i = 0; i < split.size(); i++) {
                    String fullPath = FileUtils.getFileAbsolutePath(split.get(i));
                    fullPath = PythonPathHelper.getDefaultPathStr(fullPath);
                    split.set(i, fullPath);
                }
                HashSet<String> projectSourcePath = new HashSet<String>(split);
                if (projectSourcePath.size() == 0) {
                    // No source folder for editor... this shouldn't happen (code analysis wouldn't even run on it).
                    return;
                }
                String fullPath = FileUtils.getFileAbsolutePath(editorFile);
                fullPath = PythonPathHelper.getDefaultPathStr(fullPath);
                String foundSourceFolderFullPath = null;
                if (projectSourcePath.size() == 1) {
                    foundSourceFolderFullPath = projectSourcePath.iterator().next();
                } else {
                    for (String string : projectSourcePath) {
                        if (fullPath.startsWith(string)) {
                            // Use this as the source folder
                            foundSourceFolderFullPath = string;
                            break;
                        }
                    }
                }
                if (foundSourceFolderFullPath != null) {
                    if (!addOptionToCreateClassOrMethod) {
                        // Cannot create class or method from the info (only the module structure).
                        File f = getFileStructure(new File(foundSourceFolderFullPath), foundFromImportStr);
                        addCreateModuleOption(ps, edit, props, foundFromImportStr, f);
                    } else {
                        // Ok, the leaf may be a class or method.
                        String moduleName = FullRepIterable.getWithoutLastPart(foundFromImportStr);
                        File file = getFileStructure(new File(foundSourceFolderFullPath), moduleName);
                        String lastPart = FullRepIterable.getLastPart(foundFromImportStr);
                        addCreateClassInNewModuleOption(ps, edit, props, lastPart, moduleName, parametersAfterCall, file);
                        addCreateMethodInNewModuleOption(ps, edit, props, lastPart, moduleName, parametersAfterCall, file);
                    }
                }
            }
            break;
    }
}
Also used : IModule(org.python.pydev.core.IModule) ImportHandleInfo(org.python.pydev.core.docutils.ImportHandle.ImportHandleInfo) ClassDef(org.python.pydev.parser.jython.ast.ClassDef) ICompletionState(org.python.pydev.core.ICompletionState) ArrayList(java.util.ArrayList) List(java.util.List) ImportHandle(org.python.pydev.core.docutils.ImportHandle) HashSet(java.util.HashSet) SourceModule(org.python.pydev.ast.codecompletion.revisited.modules.SourceModule) IDefinition(org.python.pydev.core.IDefinition) Definition(org.python.pydev.ast.codecompletion.revisited.visitors.Definition) PyRefactoringFindDefinition(org.python.pydev.ast.refactoring.PyRefactoringFindDefinition) CoreException(org.eclipse.core.runtime.CoreException) BadLocationException(org.eclipse.jface.text.BadLocationException) CompletionRecursionException(org.python.pydev.core.structure.CompletionRecursionException) IDefinition(org.python.pydev.core.IDefinition) ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) CompletionCache(org.python.pydev.ast.codecompletion.revisited.CompletionCache) IMarker(org.eclipse.core.resources.IMarker) File(java.io.File) PyImportsHandling(org.python.pydev.core.docutils.PyImportsHandling) IDocument(org.eclipse.jface.text.IDocument)

Example 2 with ICompletionState

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

the class ImportChecker method visitImportToken.

/**
 * This is so that we can use it without actually being in some visit.
 */
public static ImportInfo visitImportToken(boolean reportUndefinedImports, IToken token, String moduleName, IPythonNature nature, AbstractScopeAnalyzerVisitor visitor, ICompletionCache completionCache) {
    // try to find it as a relative import
    boolean wasResolved = false;
    Tuple3<IModule, String, IToken> modTok = null;
    String checkForToken = "";
    if (token instanceof SourceToken) {
        ICodeCompletionASTManager astManager = nature.getAstManager();
        ICompletionState state = CompletionStateFactory.getEmptyCompletionState(token.getRepresentation(), nature, completionCache);
        try {
            modTok = astManager.findOnImportedMods(new TokensList(token), state, moduleName, visitor.current);
        } catch (CompletionRecursionException e1) {
            // unable to resolve it
            modTok = null;
        }
        if (modTok != null && modTok.o1 != null) {
            checkForToken = modTok.o2;
            if (modTok.o2.length() == 0) {
                wasResolved = true;
            } else {
                try {
                    checkForToken = AbstractASTManager.getTokToSearchInOtherModule(modTok);
                    if (astManager.getRepInModule(modTok.o1, checkForToken, nature) != null) {
                        wasResolved = true;
                    }
                } catch (CompletionRecursionException e) {
                // not resolved...
                }
            }
        }
        if (!wasResolved && moduleName != null && moduleName.length() > 0) {
            if (moduleName.equals(token.getRepresentation()) || moduleName.equals(token.getRepresentation() + ".__init__")) {
                wasResolved = true;
                modTok = new Tuple3<IModule, String, IToken>(visitor.current, "", token);
                checkForToken = modTok.o2;
            }
        }
        // if it got here, it was not resolved
        if (!wasResolved && reportUndefinedImports) {
            visitor.onAddUnresolvedImport(token);
        }
    }
    // might still return a modTok, even if the token we were looking for was not found.
    if (modTok != null) {
        return new ImportInfo(modTok.o1, checkForToken, modTok.o3, wasResolved);
    } else {
        return new ImportInfo(null, null, null, wasResolved);
    }
}
Also used : ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) IModule(org.python.pydev.core.IModule) IToken(org.python.pydev.core.IToken) CompletionRecursionException(org.python.pydev.core.structure.CompletionRecursionException) ICompletionState(org.python.pydev.core.ICompletionState) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken) TokensList(org.python.pydev.core.TokensList)

Example 3 with ICompletionState

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

the class PyRefactoringFindDefinition method findActualDefinition.

/**
 * This method will try to find the actual definition given all the needed parameters (but it will not try to find
 * matches in the whole workspace if it's not able to find an exact match in the context)
 *
 * Note that the request must be already properly configured to be used in this function. Otherwise, the
 * function that should be used is {@link #findActualDefinition(RefactoringRequest, CompletionCache, ArrayList)}
 *
 * @param request: used only to communicateWork and checkCancelled
 * @param mod this is the module where we should find the definition
 * @param tok the token we're looking for (complete with dots)
 * @param foundDefinitions OUT: this is where the definitions should be added
 * @param beginLine starts at 1
 * @param beginCol starts at 1
 * @param pythonNature the nature that we should use to find the definition
 * @param completionCache cache to store completions
 * @return
 *
 * @throws Exception
 */
public static List<IDefinition> findActualDefinition(IProgressMonitor monitor, boolean acceptTypeshed, IModule mod, String tok, List<IDefinition> foundDefinitions, int beginLine, int beginCol, IPythonNature pythonNature, ICompletionCache completionCache, boolean searchForMethodParameterFromParticipants) throws Exception, CompletionRecursionException {
    if (foundDefinitions == null) {
        foundDefinitions = new ArrayList<IDefinition>();
    }
    ICompletionState completionState = CompletionStateFactory.getEmptyCompletionState(tok, pythonNature, beginLine - 1, beginCol - 1, completionCache);
    completionState.setAcceptTypeshed(acceptTypeshed);
    IDefinition[] definitions = mod.findDefinition(completionState, beginLine, beginCol, pythonNature);
    if (monitor != null) {
        monitor.setTaskName("Found:" + definitions.length + " definitions");
        monitor.worked(1);
        if (monitor.isCanceled()) {
            return foundDefinitions;
        }
    }
    int len = definitions.length;
    for (int i = 0; i < len; i++) {
        IDefinition definition = definitions[i];
        boolean doAdd = true;
        if (definition instanceof Definition) {
            Definition d = (Definition) definition;
            doAdd = !findActualTokenFromImportFromDefinition(pythonNature, tok, foundDefinitions, d, completionState, searchForMethodParameterFromParticipants);
        }
        if (monitor != null && monitor.isCanceled()) {
            return foundDefinitions;
        }
        if (doAdd) {
            foundDefinitions.add(definition);
        }
    }
    return foundDefinitions;
}
Also used : ICompletionState(org.python.pydev.core.ICompletionState) IDefinition(org.python.pydev.core.IDefinition) Definition(org.python.pydev.ast.codecompletion.revisited.visitors.Definition) IDefinition(org.python.pydev.core.IDefinition)

Example 4 with ICompletionState

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

the class CtxParticipant method getCompletionsForIInfo.

/**
 * Gets completions given a module and related info.
 */
private TokensList getCompletionsForIInfo(ICompletionState state, IPythonNature nature, IInfo iInfo) throws CompletionRecursionException {
    ICompletionState copy = state.getCopy();
    String path = iInfo.getPath();
    String act = iInfo.getName();
    if (path != null) {
        act = path + "." + act;
    }
    copy.setActivationToken(act);
    ICodeCompletionASTManager manager = nature.getAstManager();
    IModule mod = manager.getModule(iInfo.getDeclaringModuleName(), nature, true, state);
    if (mod != null) {
        state.checkFindDefinitionMemory(mod, iInfo.getDeclaringModuleName() + "." + act);
        TokensList tks = manager.getCompletionsForModule(mod, copy);
        if (tks != null) {
            return tks;
        }
    }
    return null;
}
Also used : ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) IModule(org.python.pydev.core.IModule) ICompletionState(org.python.pydev.core.ICompletionState) TokensList(org.python.pydev.core.TokensList)

Example 5 with ICompletionState

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

the class PyCodeCompletionsForTypedDict method getStringCompletions.

public TokensOrProposalsList getStringCompletions() throws CoreException, BadLocationException, IOException, MisconfigurationException, PythonNatureWithoutProjectException, CompletionRecursionException {
    if (artificialRequest.isPresent()) {
        CompletionRequest request = artificialRequest.get();
        int line = request.doc.getLineOfOffset(request.documentOffset);
        IRegion region = request.doc.getLineInformation(line);
        ICompletionState state = new CompletionState(line, request.documentOffset - region.getOffset(), null, request.nature, request.qualifier);
        state.setCancelMonitor(request.getCancelMonitor());
        state.setIsInCalltip(false);
        TokensList tokenCompletions = new TokensList();
        IPythonNature nature = request.nature;
        if (nature == null) {
            throw new RuntimeException("Unable to get python nature.");
        }
        ICodeCompletionASTManager astManager = nature.getAstManager();
        if (astManager == null) {
            // we're probably still loading it.
            return new TokensOrProposalsList();
        }
        String trimmed = request.getActivationToken().replace('.', ' ').trim();
        // We know it has to be a token completion, so, call it directly.
        PyCodeCompletion.doTokenCompletion(request, astManager, tokenCompletions, trimmed, state);
        TokensOrProposalsList tokensList = new TokensOrProposalsList();
        tokensList.addAll(tokenCompletions);
        List<ICompletionProposalHandle> completionProposals = new ArrayList<>();
        PyCodeCompletion.changeItokenToCompletionPropostal(request, completionProposals, tokensList, false, state);
        TokensOrProposalsList ret = new TokensOrProposalsList(completionProposals);
        return ret;
    }
    return null;
}
Also used : IPythonNature(org.python.pydev.core.IPythonNature) ArrayList(java.util.ArrayList) ICompletionState(org.python.pydev.core.ICompletionState) CompletionState(org.python.pydev.ast.codecompletion.revisited.CompletionState) TokensOrProposalsList(org.python.pydev.core.TokensOrProposalsList) IRegion(org.eclipse.jface.text.IRegion) ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) ICompletionState(org.python.pydev.core.ICompletionState) ICompletionProposalHandle(org.python.pydev.shared_core.code_completion.ICompletionProposalHandle) TokensList(org.python.pydev.core.TokensList)

Aggregations

ICompletionState (org.python.pydev.core.ICompletionState)60 TokensList (org.python.pydev.core.TokensList)41 IModule (org.python.pydev.core.IModule)23 Document (org.eclipse.jface.text.Document)20 Definition (org.python.pydev.ast.codecompletion.revisited.visitors.Definition)15 ICodeCompletionASTManager (org.python.pydev.core.ICodeCompletionASTManager)14 IToken (org.python.pydev.core.IToken)14 ArrayList (java.util.ArrayList)13 AssignDefinition (org.python.pydev.ast.codecompletion.revisited.visitors.AssignDefinition)12 CompletionRecursionException (org.python.pydev.core.structure.CompletionRecursionException)12 ITypeInfo (org.python.pydev.core.ITypeInfo)11 CompletionCache (org.python.pydev.ast.codecompletion.revisited.CompletionCache)10 CompletionState (org.python.pydev.ast.codecompletion.revisited.CompletionState)10 SourceModule (org.python.pydev.ast.codecompletion.revisited.modules.SourceModule)9 List (java.util.List)8 IDefinition (org.python.pydev.core.IDefinition)8 IterTokenEntry (org.python.pydev.core.IterTokenEntry)8 ClassDef (org.python.pydev.parser.jython.ast.ClassDef)8 SourceToken (org.python.pydev.ast.codecompletion.revisited.modules.SourceToken)7 org.python.pydev.parser.jython.ast.exprType (org.python.pydev.parser.jython.ast.exprType)7