Search in sources :

Example 1 with Name

use of org.python.pydev.parser.jython.ast.Name in project Pydev by fabioz.

the class AbstractAdditionalDependencyInfo method addAstForCompiledModule.

private void addAstForCompiledModule(IModule module, InterpreterInfo info, ModulesKey newKey, boolean removeFirst) {
    TokensList globalTokens = module.getGlobalTokens();
    PyAstFactory astFactory = new PyAstFactory(new AdapterPrefs("\n", info.getModulesManager().getNature()));
    List<stmtType> body = new ArrayList<>(globalTokens.size());
    for (IterTokenEntry entry : globalTokens) {
        IToken token = entry.getToken();
        switch(token.getType()) {
            case IToken.TYPE_CLASS:
                body.add(astFactory.createClassDef(token.getRepresentation()));
                break;
            case IToken.TYPE_FUNCTION:
                body.add(astFactory.createFunctionDef(token.getRepresentation()));
                break;
            default:
                Name attr = astFactory.createName(token.getRepresentation());
                // assign to itself just for generation purposes.
                body.add(astFactory.createAssign(attr, attr));
                break;
        }
    }
    // System.out.println("Creating info for: " + module.getName());
    if (removeFirst) {
        removeInfoFromModule(newKey.name, false);
    }
    addAstInfo(astFactory.createModule(body), newKey, false);
}
Also used : IToken(org.python.pydev.core.IToken) org.python.pydev.parser.jython.ast.stmtType(org.python.pydev.parser.jython.ast.stmtType) ArrayList(java.util.ArrayList) AdapterPrefs(org.python.pydev.parser.jython.ast.factory.AdapterPrefs) IterTokenEntry(org.python.pydev.core.IterTokenEntry) PyAstFactory(org.python.pydev.parser.jython.ast.factory.PyAstFactory) TokensList(org.python.pydev.core.TokensList) Name(org.python.pydev.parser.jython.ast.Name)

Example 2 with Name

use of org.python.pydev.parser.jython.ast.Name in project Pydev by fabioz.

the class MessagesManager method addUnusedMessage.

/**
 * adds a message for something that was not used
 *
 * @param node the node representing the scope being closed when adding the
 *             unused message
 */
public void addUnusedMessage(SimpleNode node, Found f) {
    List<GenAndTok> all = f.getAll();
    int len = all.size();
    for (int i = 0; i < len; i++) {
        GenAndTok g = all.get(i);
        if (g.generator instanceof SourceToken) {
            SimpleNode ast = ((SourceToken) g.generator).getAst();
            // it can be an unused import
            boolean isFromImport = ast instanceof ImportFrom;
            if (isFromImport || ast instanceof Import) {
                if (isFromImport && AbstractVisitor.isWildImport((ImportFrom) ast)) {
                    addMessage(IAnalysisPreferences.TYPE_UNUSED_WILD_IMPORT, g.generator, g.tok);
                } else if (!(g.generator instanceof ImportPartSourceToken)) {
                    addMessage(IAnalysisPreferences.TYPE_UNUSED_IMPORT, g.generator, g.tok);
                }
                // finish it...
                continue;
            }
        }
        // we have to check if this is a name we should ignore
        if (startsWithNamesToIgnore(g)) {
            int type = IAnalysisPreferences.TYPE_UNUSED_VARIABLE;
            if (g.tok instanceof SourceToken) {
                SourceToken t = (SourceToken) g.tok;
                SimpleNode ast = t.getAst();
                if (ast instanceof NameTok) {
                    NameTok n = (NameTok) ast;
                    if (n.ctx == NameTok.KwArg || n.ctx == NameTok.VarArg || n.ctx == NameTok.KeywordName) {
                        type = IAnalysisPreferences.TYPE_UNUSED_PARAMETER;
                    }
                } else if (ast instanceof Name) {
                    Name n = (Name) ast;
                    if (n.ctx == Name.Param || n.ctx == Name.KwOnlyParam) {
                        type = IAnalysisPreferences.TYPE_UNUSED_PARAMETER;
                    }
                }
            }
            boolean addMessage = true;
            if (type == IAnalysisPreferences.TYPE_UNUSED_PARAMETER) {
                if (node instanceof FunctionDef) {
                    addMessage = false;
                    FunctionDef def = (FunctionDef) node;
                    for (stmtType b : def.body) {
                        if (b instanceof Pass) {
                            continue;
                        }
                        if (b instanceof Expr) {
                            Expr expr = (Expr) b;
                            if (expr.value instanceof Str) {
                                continue;
                            }
                        }
                        addMessage = true;
                        break;
                    }
                }
            }
            if (addMessage) {
                addMessage(type, g.generator, g.tok);
            }
        }
    }
}
Also used : Import(org.python.pydev.parser.jython.ast.Import) ImportPartSourceToken(org.python.pydev.ast.codecompletion.revisited.visitors.AbstractVisitor.ImportPartSourceToken) org.python.pydev.parser.jython.ast.stmtType(org.python.pydev.parser.jython.ast.stmtType) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) SimpleNode(org.python.pydev.parser.jython.SimpleNode) Name(org.python.pydev.parser.jython.ast.Name) Str(org.python.pydev.parser.jython.ast.Str) Pass(org.python.pydev.parser.jython.ast.Pass) Expr(org.python.pydev.parser.jython.ast.Expr) ImportFrom(org.python.pydev.parser.jython.ast.ImportFrom) ImportPartSourceToken(org.python.pydev.ast.codecompletion.revisited.visitors.AbstractVisitor.ImportPartSourceToken) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken) NameTok(org.python.pydev.parser.jython.ast.NameTok)

Example 3 with Name

use of org.python.pydev.parser.jython.ast.Name in project Pydev by fabioz.

the class NoSelfChecker method beforeFunctionDef.

/**
 * when a class is declared inside a function scope, it must start with self if it does
 * not start with the self parameter, unless it has a staticmethod decoration or is
 * later assigned to a staticmethod.
 *
 * @param node
 */
public void beforeFunctionDef(FunctionDef node) {
    if (scope.peek().equals(Scope.SCOPE_TYPE_CLASS)) {
        // let's check if we have to start with self or cls
        boolean startsWithSelf = false;
        boolean startsWithCls = false;
        String received = "";
        if (node.args != null) {
            if (node.args.args.length > 0) {
                exprType arg = node.args.args[0];
                if (arg instanceof Name) {
                    Name n = (Name) arg;
                    if (n.id.equals("self")) {
                        startsWithSelf = true;
                    } else if (n.id.equals("cls")) {
                        startsWithCls = true;
                    }
                    received = n.id;
                }
            }
        }
        boolean isStaticMethod = false;
        boolean isClassMethod = false;
        if (node.decs != null) {
            for (decoratorsType dec : node.decs) {
                if (dec != null) {
                    String rep = NodeUtils.getRepresentationString(dec.func);
                    if (rep != null) {
                        if (rep.equals("staticmethod")) {
                            isStaticMethod = true;
                        } else if (rep.equals("classmethod")) {
                            isClassMethod = true;
                        }
                    }
                }
            }
        }
        ZopeInterfaceComputer zopeInterfaceComputer = zopeInterfaceComputers.peek();
        // didn't have staticmethod decorator either
        String rep = NodeUtils.getRepresentationString(node);
        if (rep.equals("__new__")) {
            // __new__ could start wit cls or self
            if (!startsWithCls && !startsWithSelf) {
                if (!zopeInterfaceComputer.isZopeInterface()) {
                    // zope check is more expensive, so, do as the last thing.
                    maybeNoSelfDefinedItems.peek().put(rep, new Tuple<Expected, FunctionDef>(new Expected("self or cls", received), node));
                }
            }
        } else if (!startsWithSelf && !startsWithCls && !isStaticMethod && !isClassMethod) {
            if (!zopeInterfaceComputer.isZopeInterface()) {
                // zope check is more expensive, so, do as the last thing.
                maybeNoSelfDefinedItems.peek().put(rep, new Tuple<Expected, FunctionDef>(new Expected("self", received), node));
            }
        } else if (startsWithCls && !isClassMethod && !isStaticMethod) {
            String classBase = classBases.peek();
            if (rep.equals("__init__") && "type".equals(classBase)) {
            // ok, in this case, cls is expected
            } else {
                if (!zopeInterfaceComputer.isZopeInterface()) {
                    // zope check is more expensive, so, do as the last thing.
                    maybeNoSelfDefinedItems.peek().put(rep, new Tuple<Expected, FunctionDef>(new Expected("self", received), node));
                }
            }
        }
    }
    scope.push(Scope.SCOPE_TYPE_METHOD);
}
Also used : org.python.pydev.parser.jython.ast.exprType(org.python.pydev.parser.jython.ast.exprType) org.python.pydev.parser.jython.ast.decoratorsType(org.python.pydev.parser.jython.ast.decoratorsType) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) Tuple(org.python.pydev.shared_core.structure.Tuple) Name(org.python.pydev.parser.jython.ast.Name)

Example 4 with Name

use of org.python.pydev.parser.jython.ast.Name in project Pydev by fabioz.

the class MarkOccurrencesJob method getAnnotationsToAddAsMap.

/**
 * @param markOccurrencesRequest
 * @return true if the annotations were removed and added without any problems and false otherwise
 */
@Override
protected synchronized Map<Annotation, Position> getAnnotationsToAddAsMap(final BaseEditor baseEditor, IAnnotationModel annotationModel, MarkOccurrencesRequest markOccurrencesRequest, IProgressMonitor monitor) throws BadLocationException {
    PyEdit pyEdit = (PyEdit) baseEditor;
    PySourceViewer viewer = pyEdit.getPySourceViewer();
    if (viewer == null || monitor.isCanceled()) {
        return null;
    }
    if (monitor.isCanceled()) {
        return null;
    }
    if (markOccurrencesRequest instanceof TextBasedLocalMarkOccurrencesRequest) {
        TextBasedLocalMarkOccurrencesRequest textualMarkOccurrencesRequest = (TextBasedLocalMarkOccurrencesRequest) markOccurrencesRequest;
        PySelection pySelection = PySelection.fromTextSelection(ps);
        Tuple<Integer, Integer> startEndLines = pySelection.getCurrentMethodStartEndLines();
        int initialOffset = pySelection.getAbsoluteCursorOffset(startEndLines.o1, 0);
        int finalOffset = pySelection.getEndLineOffset(startEndLines.o2);
        List<IRegion> occurrences = ps.searchOccurrences(textualMarkOccurrencesRequest.currToken);
        if (occurrences.size() == 0) {
            return null;
        }
        Map<Annotation, Position> toAddAsMap = new HashMap<Annotation, Position>();
        for (Iterator<IRegion> it = occurrences.iterator(); it.hasNext(); ) {
            IRegion iRegion = it.next();
            if (iRegion.getOffset() < initialOffset || iRegion.getOffset() > finalOffset) {
                continue;
            }
            try {
                Annotation annotation = new Annotation(getOccurrenceAnnotationsType(), false, "occurrence");
                Position position = new Position(iRegion.getOffset(), iRegion.getLength());
                toAddAsMap.put(annotation, position);
            } catch (Exception e) {
                Log.log(e);
            }
        }
        return toAddAsMap;
    }
    PyMarkOccurrencesRequest pyMarkOccurrencesRequest = (PyMarkOccurrencesRequest) markOccurrencesRequest;
    Set<ASTEntry> occurrences = pyMarkOccurrencesRequest.getOccurrences();
    if (occurrences == null) {
        if (DEBUG) {
            System.out.println("Occurrences == null");
        }
        return null;
    }
    IDocument doc = pyEdit.getDocument();
    Map<Annotation, Position> toAddAsMap = new HashMap<Annotation, Position>();
    boolean markOccurrencesInStrings = MarkOccurrencesPreferencesPage.useMarkOccurrencesInStrings();
    // get the annotations to add
    for (ASTEntry entry : occurrences) {
        if (!markOccurrencesInStrings) {
            if (entry.node instanceof Name) {
                Name name = (Name) entry.node;
                if (name.ctx == Name.Artificial) {
                    continue;
                }
            }
        }
        SimpleNode node = entry.getNameNode();
        IRegion lineInformation = doc.getLineInformation(node.beginLine - 1);
        try {
            Annotation annotation = new Annotation(getOccurrenceAnnotationsType(), false, "occurrence");
            Position position = new Position(lineInformation.getOffset() + node.beginColumn - 1, pyMarkOccurrencesRequest.getInitialName().length());
            toAddAsMap.put(annotation, position);
        } catch (Exception e) {
            Log.log(e);
        }
    }
    return toAddAsMap;
}
Also used : Position(org.eclipse.jface.text.Position) HashMap(java.util.HashMap) IRegion(org.eclipse.jface.text.IRegion) Annotation(org.eclipse.jface.text.source.Annotation) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) BadLocationException(org.eclipse.jface.text.BadLocationException) MisconfigurationException(org.python.pydev.core.MisconfigurationException) Name(org.python.pydev.parser.jython.ast.Name) SimpleNode(org.python.pydev.parser.jython.SimpleNode) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) PySelection(org.python.pydev.core.docutils.PySelection) PySourceViewer(org.python.pydev.editor.codefolding.PySourceViewer) PyEdit(org.python.pydev.editor.PyEdit) IDocument(org.eclipse.jface.text.IDocument)

Example 5 with Name

use of org.python.pydev.parser.jython.ast.Name in project Pydev by fabioz.

the class PyRefactoringFindDefinition method findActualTokenFromImportFromDefinition.

/**
 * Given some definition, find its actual token (if that's possible)
 * @param request the original request
 * @param tok the token we're looking for
 * @param lFindInfo place to store info
 * @param selected place to add the new definition (if found)
 * @param d the definition found before (this function will only work if this definition
 * @param completionCache all completions found previously
 * @param searchForMethodParameterFromParticipants find definition for method parameters
 * maps to an ImportFrom)
 *
 * @return true if we found a new definition (and false otherwise)
 * @throws Exception
 */
private static boolean findActualTokenFromImportFromDefinition(IPythonNature nature, String tok, List<IDefinition> selected, Definition d, ICompletionState completionCache, boolean searchForMethodParameterFromParticipants) throws Exception {
    boolean didFindNewDef = false;
    Set<Tuple3<String, Integer, Integer>> whereWePassed = new HashSet<Tuple3<String, Integer, Integer>>();
    // in an import..from, the last part will always be the token imported
    tok = FullRepIterable.getLastPart(tok);
    if (searchForMethodParameterFromParticipants) {
        if (d.ast instanceof Name) {
            Name name = (Name) d.ast;
            if (name.ctx == Name.Param) {
                if (d.scope != null && !d.scope.getScopeStack().empty()) {
                    Object peek = d.scope.getScopeStack().peek();
                    if (peek instanceof FunctionDef) {
                        IDefinition found = CompletionParticipantsHelper.findDefinitionForMethodParameterFromParticipants(d, nature, completionCache);
                        if (found != null) {
                            selected.add(found);
                            return true;
                        }
                    }
                }
            }
        }
    }
    while (d.ast instanceof ImportFrom) {
        IDefinition[] found = followDefinition(d, whereWePassed, tok, nature, completionCache);
        if (found != null && found.length == 1) {
            Tuple3<String, Integer, Integer> tupFromDefinition = getTupFromDefinition((Definition) found[0]);
            if (tupFromDefinition == null) {
                break;
            }
            didFindNewDef = true;
            d = (Definition) found[0];
        } else {
            break;
        }
    }
    if (didFindNewDef) {
        selected.add(d);
    }
    return didFindNewDef;
}
Also used : FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) IDefinition(org.python.pydev.core.IDefinition) Name(org.python.pydev.parser.jython.ast.Name) Tuple3(org.python.pydev.shared_core.structure.Tuple3) ImportFrom(org.python.pydev.parser.jython.ast.ImportFrom) HashSet(java.util.HashSet)

Aggregations

Name (org.python.pydev.parser.jython.ast.Name)79 SimpleNode (org.python.pydev.parser.jython.SimpleNode)28 NameTok (org.python.pydev.parser.jython.ast.NameTok)27 ArrayList (java.util.ArrayList)25 FunctionDef (org.python.pydev.parser.jython.ast.FunctionDef)25 org.python.pydev.parser.jython.ast.exprType (org.python.pydev.parser.jython.ast.exprType)25 Attribute (org.python.pydev.parser.jython.ast.Attribute)22 Assign (org.python.pydev.parser.jython.ast.Assign)21 ClassDef (org.python.pydev.parser.jython.ast.ClassDef)20 Call (org.python.pydev.parser.jython.ast.Call)16 Expr (org.python.pydev.parser.jython.ast.Expr)14 Module (org.python.pydev.parser.jython.ast.Module)13 org.python.pydev.parser.jython.ast.stmtType (org.python.pydev.parser.jython.ast.stmtType)13 Tuple (org.python.pydev.parser.jython.ast.Tuple)9 org.python.pydev.parser.jython.ast.argumentsType (org.python.pydev.parser.jython.ast.argumentsType)8 org.python.pydev.parser.jython.ast.commentType (org.python.pydev.parser.jython.ast.commentType)7 org.python.pydev.parser.jython.ast.keywordType (org.python.pydev.parser.jython.ast.keywordType)7 Import (org.python.pydev.parser.jython.ast.Import)6 CoreException (org.eclipse.core.runtime.CoreException)5 TokensList (org.python.pydev.core.TokensList)5