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");
}
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));
}
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));
}
}
}
}
}
}
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");
}
}
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;
}
Aggregations