Search in sources :

Example 1 with IPyRefactoring2

use of org.python.pydev.ast.refactoring.IPyRefactoring2 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);
        }
    }
}
Also used : RefactoringRequest(org.python.pydev.ast.refactoring.RefactoringRequest) AddTokenAndImportStatement(com.python.pydev.analysis.refactoring.quick_fixes.AddTokenAndImportStatement) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IPyRefactoring2(org.python.pydev.ast.refactoring.IPyRefactoring2) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) Entry(java.util.Map.Entry) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) HashSet(java.util.HashSet) Point(org.eclipse.swt.graphics.Point) LineStartingScope(org.python.pydev.core.docutils.PySelection.LineStartingScope) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) CoreException(org.eclipse.core.runtime.CoreException) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) PySelection(org.python.pydev.core.docutils.PySelection) File(java.io.File) Map(java.util.Map) Tuple(org.python.pydev.shared_core.structure.Tuple) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) IDocument(org.eclipse.jface.text.IDocument)

Example 2 with IPyRefactoring2

use of org.python.pydev.ast.refactoring.IPyRefactoring2 in project Pydev by fabioz.

the class PyFindAllOccurrences method perform.

@Override
protected String perform(IAction action, IProgressMonitor monitor) throws Exception {
    IPyRefactoring2 r = (IPyRefactoring2) AbstractPyRefactoring.getPyRefactoring();
    // as we're doing it in the background
    RefactoringRequest req = getRefactoringRequest(new NullProgressMonitor());
    req.fillActivationTokenAndQualifier();
    if (req.qualifier != null && req.qualifier.trim().length() > 0) {
        if (req.activationToken != null && req.activationToken.trim().length() == 0 && "__init__".equals(req.qualifier)) {
            LineStartingScope line = ps.getPreviousLineThatStartsScope(PySelection.CLASS_TOKEN, false, Integer.MAX_VALUE);
            if (line != null) {
                String className = PySelection.getClassNameInLine(line.lineStartingScope);
                if (className != null && className.length() > 0) {
                    int col = line.lineStartingScope.indexOf(className);
                    int len = className.length();
                    req.ps = new PySelection(ps.getDoc(), line.iLineStartingScope, col, len);
                    req.fillActivationTokenAndQualifier();
                }
            }
        }
        NewSearchUI.runQueryInBackground(newQuery(r, req));
    }
    return null;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) RefactoringRequest(org.python.pydev.ast.refactoring.RefactoringRequest) PySelection(org.python.pydev.core.docutils.PySelection) IPyRefactoring2(org.python.pydev.ast.refactoring.IPyRefactoring2) LineStartingScope(org.python.pydev.core.docutils.PySelection.LineStartingScope)

Example 3 with IPyRefactoring2

use of org.python.pydev.ast.refactoring.IPyRefactoring2 in project Pydev by fabioz.

the class PyShowHierarchy method perform.

@Override
protected String perform(IAction action, IProgressMonitor monitor) throws Exception {
    try {
        final PyHierarchyView view;
        IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        IWorkbenchPage page = workbenchWindow.getActivePage();
        view = (PyHierarchyView) page.showView("com.python.pydev.ui.hierarchy.PyHierarchyView", null, IWorkbenchPage.VIEW_VISIBLE);
        ProgressMonitorDialog monitorDialog = new AsynchronousProgressMonitorDialog(EditorUtils.getShell());
        try {
            IRunnableWithProgress operation = new IRunnableWithProgress() {

                @Override
                public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                    try {
                        final HierarchyNodeModel model;
                        // set whatever is needed for the hierarchy
                        IPyRefactoring pyRefactoring = AbstractPyRefactoring.getPyRefactoring();
                        if (pyRefactoring instanceof IPyRefactoring2) {
                            RefactoringRequest refactoringRequest = getRefactoringRequest(monitor);
                            IPyRefactoring2 r2 = (IPyRefactoring2) pyRefactoring;
                            model = r2.findClassHierarchy(refactoringRequest, false);
                            if (monitor.isCanceled()) {
                                return;
                            }
                            Runnable r = new Runnable() {

                                @Override
                                public void run() {
                                    if (!monitor.isCanceled()) {
                                        view.setHierarchy(model);
                                    }
                                }
                            };
                            Display.getDefault().asyncExec(r);
                        }
                    } catch (Exception e) {
                        Log.log(e);
                    }
                }
            };
            boolean fork = true;
            monitorDialog.run(fork, true, operation);
        } catch (Throwable e) {
            Log.log(e);
        }
    } catch (Exception e) {
        Log.log(e);
    }
    return "";
}
Also used : PyHierarchyView(com.python.pydev.ui.hierarchy.PyHierarchyView) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) AsynchronousProgressMonitorDialog(org.python.pydev.shared_ui.utils.AsynchronousProgressMonitorDialog) RefactoringRequest(org.python.pydev.ast.refactoring.RefactoringRequest) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) AsynchronousProgressMonitorDialog(org.python.pydev.shared_ui.utils.AsynchronousProgressMonitorDialog) IPyRefactoring(org.python.pydev.ast.refactoring.IPyRefactoring) HierarchyNodeModel(org.python.pydev.ast.refactoring.HierarchyNodeModel) IPyRefactoring2(org.python.pydev.ast.refactoring.IPyRefactoring2) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage)

Aggregations

IPyRefactoring2 (org.python.pydev.ast.refactoring.IPyRefactoring2)3 RefactoringRequest (org.python.pydev.ast.refactoring.RefactoringRequest)3 PySelection (org.python.pydev.core.docutils.PySelection)2 LineStartingScope (org.python.pydev.core.docutils.PySelection.LineStartingScope)2 AddTokenAndImportStatement (com.python.pydev.analysis.refactoring.quick_fixes.AddTokenAndImportStatement)1 PyHierarchyView (com.python.pydev.ui.hierarchy.PyHierarchyView)1 File (java.io.File)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 CoreException (org.eclipse.core.runtime.CoreException)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 ProgressMonitorDialog (org.eclipse.jface.dialogs.ProgressMonitorDialog)1 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)1 IDocument (org.eclipse.jface.text.IDocument)1 Point (org.eclipse.swt.graphics.Point)1 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)1