use of org.python.pydev.parser.visitors.scope.ASTEntry in project Pydev by fabioz.
the class PyOutlineSelectionDialog method calculateHierarchy.
@Override
protected void calculateHierarchy() {
if (root != null) {
return;
}
if (this.ast == null && pyEdit != null) {
this.ast = pyEdit.getAST();
}
if (ast == null) {
return;
}
DefinitionsASTIteratorVisitor visitor = DefinitionsASTIteratorVisitor.create(ast);
if (visitor == null) {
return;
}
Map<ASTEntry, DataAndImageTreeNode<Object>> entryToTreeNode = new HashMap<ASTEntry, DataAndImageTreeNode<Object>>();
// Step 1: create 'regular' tree structure from the nodes.
DataAndImageTreeNode<Object> root = new DataAndImageTreeNode<Object>(null, null, null);
for (Iterator<ASTEntry> it = visitor.getOutline(); it.hasNext(); ) {
ASTEntry next = it.next();
DataAndImageTreeNode<Object> n;
if (next.parent != null) {
DataAndImageTreeNode<Object> parent = entryToTreeNode.get(next.parent);
if (parent == null) {
Log.log("Unexpected condition: child found before parent!");
parent = root;
}
n = new DataAndImageTreeNode<Object>(parent, new OutlineEntry(next), null);
} else {
n = new DataAndImageTreeNode<Object>(root, new OutlineEntry(next), null);
}
if (((OutlineEntry) n.data).node.beginLine <= startLineIndex) {
initialSelection = n;
}
entryToTreeNode.put(next, n);
}
this.root = root;
}
use of org.python.pydev.parser.visitors.scope.ASTEntry in project Pydev by fabioz.
the class ShowOutlineLabelProvider method getStyledText.
@Override
public StyledString getStyledText(Object element) {
if (element instanceof DataAndImageTreeNode) {
@SuppressWarnings("rawtypes") DataAndImageTreeNode treeNode = (DataAndImageTreeNode) element;
element = treeNode.data;
}
if (element instanceof OutlineEntry) {
OutlineEntry entry = (OutlineEntry) element;
String start = NodeUtils.getFullRepresentationString(entry.node);
if (entry.model != null) {
FastStringBuffer suffix = new FastStringBuffer(" (", entry.model.name.length() + 50).append(entry.model.name);
if (entry.model.moduleName != null && entry.model.moduleName.length() > 0) {
suffix.append(" - ").append(entry.model.moduleName);
}
suffix.append(')');
return new StyledString(start).append(suffix.toString(), StyledString.QUALIFIER_STYLER);
} else if (entry.parentClass != null) {
FastStringBuffer suffix = new FastStringBuffer(" (", entry.parentClass.length() + 4).append(entry.parentClass).append(')');
return new StyledString(start).append(suffix.toString(), StyledString.QUALIFIER_STYLER);
}
return new StyledString(start);
}
if (element instanceof ASTEntry) {
return new StyledString(NodeUtils.getFullRepresentationString(((ASTEntry) element).node));
}
return new StyledString(element.toString());
}
use of org.python.pydev.parser.visitors.scope.ASTEntry in project Pydev by fabioz.
the class PyMoveImportsToLocalCompletionProposal method apply.
@Override
public void apply(IDocument doc) {
RefactoringRequest req = refactoringRequest;
final IPyRefactoring2 r = (IPyRefactoring2) AbstractPyRefactoring.getPyRefactoring();
if (req.qualifier != null && req.qualifier.trim().length() > 0) {
try {
final Map<Tuple<String, File>, HashSet<ASTEntry>> occurrences = r.findAllOccurrences(req);
final Set<Entry<Tuple<String, File>, HashSet<ASTEntry>>> entrySet = occurrences.entrySet();
final MultiTextEdit multiTextEdit = new MultiTextEdit();
final IDocument document = req.getDoc();
final Set<Integer> appliedContextLines = new HashSet<Integer>();
for (Map.Entry<Tuple<String, File>, HashSet<ASTEntry>> o : entrySet) {
HashSet<ASTEntry> entries = o.getValue();
ASTEntry[] ordered = entries.toArray(new ASTEntry[0]);
Arrays.sort(ordered, (entry0, entry1) -> {
// Note: order is reversed.
return Integer.compare(entry1.node.beginLine, entry0.node.beginLine);
});
for (ASTEntry entry : entries) {
if (entry.node != null) {
int beginLine = entry.node.beginLine;
int useLine = beginLine - 1;
if (useLine >= importHandleInfo.getStartLine() && useLine <= importHandleInfo.getEndLine()) {
// Skip the import itself.
continue;
}
String currLine = TextSelectionUtils.getLine(document, useLine);
if (!currLine.isEmpty() && !Character.isWhitespace(currLine.charAt(0))) {
// Skip global occurrences of the token
continue;
}
for (int i = useLine; i < document.getNumberOfLines(); i++) {
String line = TextSelectionUtils.getLine(document, i);
if (!line.trim().isEmpty()) {
if (Character.isWhitespace(line.charAt(0))) {
useLine = i;
break;
}
}
}
boolean addLocalImport = true;
boolean addLocalImportsOnTopOfMethod = true;
boolean groupImports = false;
int offset = new PySelection(req.ps.getDoc(), useLine, 0).getAbsoluteCursorOffset();
int maxCols = 200;
char trigger = ' ';
String fromImportStr = importHandleInfo.getFromImportStr();
String realImportRep;
if (fromImportStr == null || fromImportStr.isEmpty()) {
realImportRep = "import " + this.importedToken;
} else {
realImportRep = "from " + fromImportStr + " import " + this.importedToken;
}
int fReplacementOffset = offset;
int fLen = 0;
String indentString = " ";
this.fReplacementString = "";
AddTokenAndImportStatement.ComputedInfo computedInfo = new AddTokenAndImportStatement.ComputedInfo(realImportRep, fReplacementOffset, fLen, indentString, fReplacementString, appliedWithTrigger, importLen, document);
this.appliedWithTrigger = computedInfo.appliedWithTrigger;
this.importLen = computedInfo.importLen;
AddTokenAndImportStatement t = new AddTokenAndImportStatement(document, trigger, offset, addLocalImport, addLocalImportsOnTopOfMethod, groupImports, maxCols);
LineStartingScope previousLineThatStartsScope = t.getPreviousLineThatStartsScope();
if (previousLineThatStartsScope != null) {
if (appliedContextLines.contains(previousLineThatStartsScope.iLineStartingScope)) {
continue;
}
appliedContextLines.add(previousLineThatStartsScope.iLineStartingScope);
t.createTextEdit(computedInfo);
for (ReplaceEdit edit : computedInfo.replaceEdit) {
multiTextEdit.addChild(edit);
}
}
}
}
}
try {
multiTextEdit.apply(document);
} catch (Exception e) {
Log.log(e);
}
} catch (OperationCanceledException | CoreException e) {
Log.log(e);
}
}
}
use of org.python.pydev.parser.visitors.scope.ASTEntry in project Pydev by fabioz.
the class PyRenameClassProcess method findReferencesOnOtherModule.
/**
* This method is called for each module that may have some reference to the definition
* we're looking for.
*/
@Override
protected List<ASTEntry> findReferencesOnOtherModule(RefactoringStatus status, RefactoringRequest request, String initialName, SourceModule module) {
SimpleNode root = module.getAst();
List<ASTEntry> entryOccurrences = ScopeAnalysis.getLocalOccurrences(initialName, root);
entryOccurrences.addAll(ScopeAnalysis.getAttributeReferences(initialName, root));
if (entryOccurrences.size() > 0) {
// only add comments and strings if there's at least some other occurrence
entryOccurrences.addAll(ScopeAnalysis.getCommentOccurrences(request.qualifier, root));
entryOccurrences.addAll(ScopeAnalysis.getStringOccurrences(request.qualifier, root));
}
return entryOccurrences;
}
use of org.python.pydev.parser.visitors.scope.ASTEntry in project Pydev by fabioz.
the class PyRenameFunctionProcess method getLocalOccurrences.
/**
* This method is the method that should be used to get the occurrences in the same
* module where the function is defined.
*
* @param occurencesFor the name of the function we're looking for
* @param simpleNode the root of the module
* @param status if we're unable to find the reference for the function definition in this module,
* an error is added to this status.
*
* @return a list with the entries with the references (and definition) to the function searched.
*/
private List<ASTEntry> getLocalOccurrences(String occurencesFor, SimpleNode simpleNode, RefactoringStatus status) {
List<ASTEntry> ret = new ArrayList<ASTEntry>();
// get the entry for the function itself
ASTEntry functionDefEntry = getOriginalFunctionInAst(simpleNode);
if (functionDefEntry == null) {
status.addFatalError("Unable to find the original definition for the function definition.");
return ret;
}
if (functionDefEntry.parent != null) {
// it has some parent
final SimpleNode parentNode = functionDefEntry.parent.node;
if (parentNode instanceof ClassDef) {
// ok, we're in a class, the first thing is to add the reference to the function just gotten
ret.add(new ASTEntry(functionDefEntry, ((FunctionDef) functionDefEntry.node).name));
// get the entry for the self.xxx that access that attribute in the class
SequencialASTIteratorVisitor classVisitor = SequencialASTIteratorVisitor.create(parentNode);
Iterator<ASTEntry> it = classVisitor.getIterator(Attribute.class);
while (it.hasNext()) {
ASTEntry entry = it.next();
List<SimpleNode> parts = NodeUtils.getAttributeParts((Attribute) entry.node);
if (!(parts.get(1) instanceof Attribute)) {
final String rep0 = NodeUtils.getRepresentationString(parts.get(0));
final String rep1 = NodeUtils.getRepresentationString(parts.get(1));
if (rep0 != null && rep1 != null && rep0.equals("self") && rep1.equals(occurencesFor)) {
ret.add(entry);
}
}
}
final List<ASTEntry> attributeReferences = ScopeAnalysis.getAttributeReferences(occurencesFor, simpleNode);
NameTokType funcName = ((FunctionDef) functionDefEntry.node).name;
for (ASTEntry entry : attributeReferences) {
if (entry.node != funcName) {
if (entry.node instanceof NameTok) {
NameTok nameTok = (NameTok) entry.node;
if (nameTok.ctx == NameTok.ClassName) {
continue;
}
}
ret.add(entry);
}
}
} else if (parentNode instanceof FunctionDef) {
// get the references inside of the parent (this will include the function itself)
ret.addAll(ScopeAnalysis.getLocalOccurrences(occurencesFor, parentNode));
}
} else {
ret.addAll(ScopeAnalysis.getLocalOccurrences(occurencesFor, simpleNode));
}
if (ret.size() > 0) {
// only add comments and strings if there's at least some other occurrence
ret.addAll(ScopeAnalysis.getCommentOccurrences(occurencesFor, simpleNode));
ret.addAll(ScopeAnalysis.getStringOccurrences(occurencesFor, simpleNode));
}
// get the references to Names that access that method in the same scope
return ret;
}
Aggregations