private boolean isStored(String var, List<SimpleAdapter> scopeVariables) { boolean isStored = false; // must traverse all variables, because a // variable may be used in other context! for (SimpleAdapter adapter : scopeVariables) { SimpleNode astNode = adapter.getASTNode(); if (astNode instanceof Name) { Name scopeVar = (Name) astNode; if (scopeVar.id.equals(var)) { isStored = (scopeVar.ctx != Name.Load && scopeVar.ctx != Name.AugLoad); } } else if (astNode instanceof NameTok) { NameTok scopeVar = (NameTok) astNode; if (scopeVar.id.equals(var)) { isStored = true; // NameTok are always store contexts. } } else if (astNode instanceof Import) { Import importNode = (Import) astNode; isStored = checkNames(var, importNode.names); } else if (astNode instanceof ImportFrom) { ImportFrom importFrom = (ImportFrom) astNode; isStored = checkNames(var, importFrom.names); } if (isStored) { break; } } return isStored; }
private void deduceParameters(List<SimpleAdapter> before, List<SimpleAdapter> selected) { Set<String> globalVariableNames = new HashSet<String>(moduleAdapter.getGlobalVariableNames()); for (SimpleAdapter adapter : before) { SimpleNode astNode = adapter.getASTNode(); String id; if (astNode instanceof Name) { Name variable = (Name) astNode; id = variable.id; } else if (astNode instanceof NameTok) { NameTok variable = (NameTok) astNode; id = variable.id; } else { continue; } if (globalVariableNames.contains(id) && !isStored(id, before)) { // It's a global variable and there's no assignment // shadowing it in the local scope, so don't add it as a // parameter. continue; } if (id.equals("True") || id.equals("False") || id.equals("None")) { // The user most likely doesn't want them to be passed. continue; } if (isUsed(id, selected)) { if (!parameters.contains(id)) { parameters.add(id); } } } }
/** Fix (fabioz): to check if it is used, it must be in a load context */ private boolean isUsed(String var, List<SimpleAdapter> scopeVariables) { for (SimpleAdapter adapter : scopeVariables) { SimpleNode astNode = adapter.getASTNode(); if (astNode instanceof Name) { Name scopeVar = (Name) astNode; if ((scopeVar.ctx == Name.Load || scopeVar.ctx == Name.AugLoad) && scopeVar.id.equals(var)) { return true; } } // Note: NameTok are always only in store context. } return false; }
private void deduceReturns(List<SimpleAdapter> after, List<SimpleAdapter> selected) { for (SimpleAdapter adapter : after) { SimpleNode astNode = adapter.getASTNode(); String id; if (astNode instanceof Name) { Name variable = (Name) astNode; id = variable.id; } else if (astNode instanceof NameTok) { NameTok variable = (NameTok) astNode; id = variable.id; } else { continue; } if (isStored(id, selected)) { returns.add(id); } } }
private boolean isBeforeSelectedLine(SimpleAdapter firstSelectedVariable, SimpleAdapter adapter) { return adapter.getNodeFirstLine() < firstSelectedVariable.getNodeFirstLine(); }
private boolean isBeforeOnSameLine(SimpleAdapter firstSelectedVariable, SimpleAdapter adapter) { return adapter.getNodeFirstLine() == firstSelectedVariable.getNodeFirstLine() && (adapter.getNodeIndent() < firstSelectedVariable.getNodeIndent()); }
private boolean isAfterSelectedLine(SimpleAdapter lastSelectedVariable, SimpleAdapter adapter) { return adapter.getNodeFirstLine() > lastSelectedVariable.getNodeFirstLine(); }
private boolean isAfterOnSameLine(SimpleAdapter lastSelectedVariable, SimpleAdapter adapter) { return adapter.getNodeFirstLine() == lastSelectedVariable.getNodeFirstLine() && (adapter.getNodeIndent() > lastSelectedVariable.getNodeIndent()); }