Search in sources :

Example 46 with SourceToken

use of org.python.pydev.ast.codecompletion.revisited.modules.SourceToken in project Pydev by fabioz.

the class AbstractVisitorTest method testImportCreation5.

public void testImportCreation5() throws Exception {
    Iterator<ASTEntry> iterator = createModuleAndGetImports("from os.path import *", ImportFrom.class);
    MODULE_NAME = "some.dotted.name";
    SimpleNode simpleNode = iterator.next().node;
    List<IToken> toks = AbstractVisitor.makeImportToken(simpleNode, new ArrayList<IToken>(), "some.dotted.name", true, null);
    assertEquals(1, toks.size());
    SourceToken token = (SourceToken) toks.get(0);
    checkIt(simpleNode, token, "os.path", "some.dotted.os.path", "os.path");
}
Also used : IToken(org.python.pydev.core.IToken) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken) SimpleNode(org.python.pydev.parser.jython.SimpleNode)

Example 47 with SourceToken

use of org.python.pydev.ast.codecompletion.revisited.modules.SourceToken in project Pydev by fabioz.

the class ScopeAnalyzerVisitorTest method testIt18.

public void testIt18() throws Exception {
    doc = new Document("import bla as fooo\n" + "raise fooo.ffff(msg)\n");
    List<IToken> tokenOccurrences = getTokenOccurrences(0, 16);
    assertEquals(2, tokenOccurrences.size());
    IToken t0 = tokenOccurrences.get(0);
    IToken t1 = tokenOccurrences.get(1);
    SimpleNode ast0 = ((SourceToken) t0).getAst();
    SimpleNode ast1 = ((SourceToken) t1).getAst();
    assertEquals("fooo", t0.getRepresentation());
    assertEquals("fooo", t1.getRepresentation());
    assertEquals(15, NodeUtils.getColDefinition(ast0));
    assertEquals(7, NodeUtils.getColDefinition(ast1));
}
Also used : IToken(org.python.pydev.core.IToken) Document(org.eclipse.jface.text.Document) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken) SimpleNode(org.python.pydev.parser.jython.SimpleNode)

Example 48 with SourceToken

use of org.python.pydev.ast.codecompletion.revisited.modules.SourceToken in project Pydev by fabioz.

the class PyCodeCompletion method handleKeywordParam.

private void handleKeywordParam(CompletionRequest request, int line, Map<String, IterTokenEntry> alreadyChecked) throws BadLocationException, CompletionRecursionException {
    if (request.isInMethodKeywordParam) {
        PySelection ps = new PySelection(request.doc, request.offsetForKeywordParam);
        RefactoringRequest findRequest = new RefactoringRequest(request.editorFile, ps, new NullProgressMonitor(), request.nature, null);
        ArrayList<IDefinition> selected = new ArrayList<IDefinition>();
        CompletionState completionState = new CompletionState();
        PyRefactoringFindDefinition.findActualDefinition(findRequest, completionState, selected);
        // Changed: showing duplicated parameters (only removing self and cls).
        // Tuple<List<String>, Integer> insideParentesisToks = ps.getInsideParentesisToks(false, completionRequestForKeywordParam.documentOffset);
        HashSet<String> ignore = new HashSet<String>();
        ignore.add("self");
        ignore.add("cls");
        for (IDefinition iDefinition : selected) {
            if (iDefinition instanceof Definition) {
                Definition definition = (Definition) iDefinition;
                if (definition.ast != null) {
                    String args = NodeUtils.getNodeArgs(definition.ast);
                    String fullArgs = NodeUtils.getFullArgs(definition.ast);
                    StringTokenizer tokenizer = new StringTokenizer(args, "(, )");
                    while (tokenizer.hasMoreTokens()) {
                        String nextToken = tokenizer.nextToken();
                        if (ignore.contains(nextToken)) {
                            continue;
                        }
                        String kwParam = nextToken + "=";
                        SimpleNode node = new NameTok(kwParam, NameTok.KwArg);
                        SourceToken sourceToken = new SourceToken(node, kwParam, "", "", "", IToken.TYPE_LOCAL, definition.module != null ? definition.module.getNature() : null);
                        sourceToken.setDocStr(fullArgs);
                        alreadyChecked.put(kwParam, new IterTokenEntry(sourceToken));
                    }
                }
            }
        }
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) RefactoringRequest(org.python.pydev.ast.refactoring.RefactoringRequest) ArrayList(java.util.ArrayList) IDefinition(org.python.pydev.core.IDefinition) Definition(org.python.pydev.ast.codecompletion.revisited.visitors.Definition) PyRefactoringFindDefinition(org.python.pydev.ast.refactoring.PyRefactoringFindDefinition) ICompletionState(org.python.pydev.core.ICompletionState) CompletionState(org.python.pydev.ast.codecompletion.revisited.CompletionState) IterTokenEntry(org.python.pydev.core.IterTokenEntry) IDefinition(org.python.pydev.core.IDefinition) SimpleNode(org.python.pydev.parser.jython.SimpleNode) ISimpleNode(org.python.pydev.shared_core.model.ISimpleNode) StringTokenizer(java.util.StringTokenizer) PySelection(org.python.pydev.core.docutils.PySelection) NameTok(org.python.pydev.parser.jython.ast.NameTok) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken) HashSet(java.util.HashSet)

Example 49 with SourceToken

use of org.python.pydev.ast.codecompletion.revisited.modules.SourceToken in project Pydev by fabioz.

the class AbstractMessage method getStartLine.

public static int getStartLine(IToken generator, IDocument doc, String shortMessage, boolean returnAsName) {
    if (!generator.isImport()) {
        return generator.getLineDefinition();
    }
    // ok, it is an import... (can only be a source token)
    SourceToken s = (SourceToken) generator;
    SimpleNode ast = s.getAst();
    if (ast instanceof ImportFrom) {
        ImportFrom i = (ImportFrom) ast;
        // if it is a wild import, it starts on the module name
        if (AbstractVisitor.isWildImport(i)) {
            return i.module.beginLine;
        } else {
            // no wild import, let's check the 'as name'
            return getNameForRepresentation(i, shortMessage, returnAsName).beginLine;
        }
    } else if (ast instanceof Import) {
        return getNameForRepresentation(ast, shortMessage, returnAsName).beginLine;
    } else {
        throw new RuntimeException("It is not an import");
    }
}
Also used : Import(org.python.pydev.parser.jython.ast.Import) ImportFrom(org.python.pydev.parser.jython.ast.ImportFrom) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken) SimpleNode(org.python.pydev.parser.jython.SimpleNode)

Example 50 with SourceToken

use of org.python.pydev.ast.codecompletion.revisited.modules.SourceToken in project Pydev by fabioz.

the class AbstractMessage method getEndCol.

/**
 * @param generator is the token that generated this message
 * @param doc is the document where this message will be put
 * @param shortMessage is used when it is an import ( = foundTok.getRepresentation())
 *
 * @return the end column for this message
 *
 * @see org.python.pydev.ast.analysis.messages.IMessage#getEndCol(org.eclipse.jface.text.IDocument)
 */
public static int getEndCol(IToken generator, IDocument doc, String shortMessage, boolean getOnlyToFirstDot) {
    int endCol = -1;
    if (generator.isImport()) {
        // ok, it is an import... (can only be a source token)
        SourceToken s = (SourceToken) generator;
        SimpleNode ast = s.getAst();
        if (ast instanceof ImportFrom) {
            ImportFrom i = (ImportFrom) ast;
            // ok, now, this depends on the name
            NameTok it = getNameForRepresentation(i, shortMessage, true);
            if (it != null) {
                endCol = it.beginColumn + shortMessage.length();
                return endCol;
            }
            // if still not returned, it is a wild import... find the '*'
            try {
                IRegion lineInformation = doc.getLineInformation(i.module.beginLine - 1);
                // ok, we have the line... now, let's find the absolute offset
                int absolute = lineInformation.getOffset() + i.module.beginColumn - 1;
                while (doc.getChar(absolute) != '*') {
                    absolute++;
                }
                // 1 for the *
                int absoluteCol = absolute + 1;
                IRegion region = doc.getLineInformationOfOffset(absoluteCol);
                // 1 because we should return as if starting in 1 and not 0
                endCol = absoluteCol - region.getOffset() + 1;
                return endCol;
            } catch (BadLocationException e) {
                throw new RuntimeException(e);
            }
        } else if (ast instanceof Import) {
            NameTok it = getNameForRepresentation(ast, shortMessage, true);
            endCol = it.beginColumn + shortMessage.length();
            return endCol;
        } else {
            throw new RuntimeException("It is not an import");
        }
    }
    // no import... make it regular
    if (generator instanceof SourceToken) {
        return ((SourceToken) generator).getColEnd(getOnlyToFirstDot);
    }
    return -1;
}
Also used : Import(org.python.pydev.parser.jython.ast.Import) ImportFrom(org.python.pydev.parser.jython.ast.ImportFrom) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken) NameTok(org.python.pydev.parser.jython.ast.NameTok) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException) SimpleNode(org.python.pydev.parser.jython.SimpleNode)

Aggregations

SourceToken (org.python.pydev.ast.codecompletion.revisited.modules.SourceToken)50 SimpleNode (org.python.pydev.parser.jython.SimpleNode)26 IToken (org.python.pydev.core.IToken)25 FunctionDef (org.python.pydev.parser.jython.ast.FunctionDef)13 TokensList (org.python.pydev.core.TokensList)12 ArrayList (java.util.ArrayList)11 ClassDef (org.python.pydev.parser.jython.ast.ClassDef)11 IModule (org.python.pydev.core.IModule)9 NameTok (org.python.pydev.parser.jython.ast.NameTok)9 ICompletionState (org.python.pydev.core.ICompletionState)8 ImportFrom (org.python.pydev.parser.jython.ast.ImportFrom)8 ASTEntry (org.python.pydev.parser.visitors.scope.ASTEntry)8 Import (org.python.pydev.parser.jython.ast.Import)7 ISimpleNode (org.python.pydev.shared_core.model.ISimpleNode)7 IterTokenEntry (org.python.pydev.core.IterTokenEntry)6 CompletionRecursionException (org.python.pydev.core.structure.CompletionRecursionException)6 org.python.pydev.parser.jython.ast.exprType (org.python.pydev.parser.jython.ast.exprType)6 HashSet (java.util.HashSet)5 Attribute (org.python.pydev.parser.jython.ast.Attribute)5 Found (com.python.pydev.analysis.visitors.Found)4