Search in sources :

Example 1 with ASTEntryWithSourceModule

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

the class RefactoringRenameTestBase method asStr.

@SuppressWarnings("unchecked")
protected String asStr(Map<Tuple<String, File>, HashSet<ASTEntry>> referencesForModuleRename) throws Exception {
    Set<Entry<Tuple<String, File>, HashSet<ASTEntry>>> entrySet = referencesForModuleRename.entrySet();
    FastStringBuffer buf = new FastStringBuffer();
    ArrayList<Entry<Tuple<String, File>, HashSet<ASTEntry>>> lst = new ArrayList<>(entrySet);
    Comparator<Entry<Tuple<String, File>, HashSet<ASTEntry>>> c = new Comparator<Entry<Tuple<String, File>, HashSet<ASTEntry>>>() {

        @Override
        public int compare(Entry<Tuple<String, File>, HashSet<ASTEntry>> o1, Entry<Tuple<String, File>, HashSet<ASTEntry>> o2) {
            return o1.getKey().o1.compareTo(o2.getKey().o1);
        }
    };
    Collections.sort(lst, c);
    for (Entry<Tuple<String, File>, HashSet<ASTEntry>> entry : lst) {
        HashSet<ASTEntry> value = entry.getValue();
        if (value.size() > 0) {
            ArrayList<ASTEntry> lst2 = new ArrayList<>(value);
            Comparator<ASTEntry> c2 = new Comparator<ASTEntry>() {

                @Override
                public int compare(ASTEntry o1, ASTEntry o2) {
                    return o1.toString().compareTo(o2.toString());
                }
            };
            Collections.sort(lst2, c2);
            File f = entry.getKey().o2;
            String fileContents = FileUtils.getFileContents(f);
            Document initialDoc = new Document(fileContents);
            buf.append(entry.getKey().o1).append("\n");
            for (ASTEntry e : lst2) {
                buf.append("  ");
                buf.append(e.toString()).append("\n");
                List<TextEdit> edits = (List<TextEdit>) e.getAdditionalInfo(AstEntryScopeAnalysisConstants.AST_ENTRY_REPLACE_EDIT, null);
                if (edits == null) {
                    if (!(e instanceof ASTEntryWithSourceModule)) {
                        throw new AssertionError("Only ASTEntryWithSourceModule can have null edits. Found: " + e);
                    }
                } else {
                    Document changedDoc = new Document(fileContents);
                    for (TextEdit textEdit : edits) {
                        textEdit.apply(changedDoc);
                    }
                    List<String> changedLines = getChangedLines(initialDoc, changedDoc);
                    for (String i : changedLines) {
                        buf.append("    ");
                        buf.append(StringUtils.rightTrim(i)).append("\n");
                    }
                }
            }
            buf.append("\n");
        }
    }
    return buf.toString();
}
Also used : FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) ArrayList(java.util.ArrayList) Document(org.eclipse.jface.text.Document) Comparator(java.util.Comparator) Entry(java.util.Map.Entry) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) TextEdit(org.eclipse.text.edits.TextEdit) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) ASTEntryWithSourceModule(org.python.pydev.ast.codecompletion.revisited.modules.ASTEntryWithSourceModule) List(java.util.List) ArrayList(java.util.ArrayList) IFile(org.eclipse.core.resources.IFile) File(java.io.File) Tuple(org.python.pydev.shared_core.structure.Tuple) HashSet(java.util.HashSet)

Example 2 with ASTEntryWithSourceModule

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

the class PyRenameImportProcess method onModuleRenameRefactoringRequest.

private void onModuleRenameRefactoringRequest(RefactoringRequest request) {
    moduleToFind = (SourceModule) request.getModule();
    List<ASTEntry> lst = new ArrayList<ASTEntry>();
    lst.add(new ASTEntryWithSourceModule(moduleToFind));
    addOccurrences(lst, moduleToFind.getFile(), moduleToFind.getName());
}
Also used : ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) ArrayList(java.util.ArrayList) ASTEntryWithSourceModule(org.python.pydev.ast.codecompletion.revisited.modules.ASTEntryWithSourceModule)

Aggregations

ArrayList (java.util.ArrayList)2 ASTEntryWithSourceModule (org.python.pydev.ast.codecompletion.revisited.modules.ASTEntryWithSourceModule)2 ASTEntry (org.python.pydev.parser.visitors.scope.ASTEntry)2 File (java.io.File)1 Comparator (java.util.Comparator)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Entry (java.util.Map.Entry)1 IFile (org.eclipse.core.resources.IFile)1 Document (org.eclipse.jface.text.Document)1 TextEdit (org.eclipse.text.edits.TextEdit)1 FastStringBuffer (org.python.pydev.shared_core.string.FastStringBuffer)1 Tuple (org.python.pydev.shared_core.structure.Tuple)1