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