Search in sources :

Example 1 with UndefinedVariableFixParticipant

use of com.python.pydev.analysis.ctrl_1.UndefinedVariableFixParticipant in project Pydev by fabioz.

the class OrganizeImports method beforePerformArrangeImports.

/**
 * That's where everything happens.
 *
 * Important: if the document is in a rewrite session, trying to highlight a given session does not work
 * (so, we cannot be in a rewrite session in this case).
 */
@Override
public boolean beforePerformArrangeImports(final PySelection ps, final PyEdit edit, IFile f) {
    if ((!AnalysisPreferences.doAutoImportOnOrganizeImports(edit)) || edit == null) {
        return true;
    }
    didChange = false;
    ArrayList<MarkerAnnotationAndPosition> undefinedVariablesMarkers = getUndefinedVariableMarkers(edit);
    // sort them
    TreeMap<Integer, MarkerAnnotationAndPosition> map = new TreeMap<Integer, MarkerAnnotationAndPosition>();
    for (MarkerAnnotationAndPosition marker : undefinedVariablesMarkers) {
        if (marker.position == null) {
            continue;
        }
        int start = marker.position.offset;
        map.put(start, marker);
    }
    // create the participant that'll help (will not force a reparse)
    final UndefinedVariableFixParticipant variableFixParticipant = new UndefinedVariableFixParticipant(false);
    // These are the completions to apply. We must apply them all at once after finishing it because we can't do
    // it one by one during the processing because that'd make markers change.
    final List<ICompletionProposalExtension2> completionsToApply = new ArrayList<ICompletionProposalExtension2>();
    // keeps the strings we've already treated.
    final HashSet<String> treatedVars = new HashSet<String>();
    // variable to hold whether we should keep on choosing the imports
    final Boolean[] keepGoing = new Boolean[] { true };
    final IDialogSettings dialogSettings = AnalysisUiPlugin.getDialogSettings();
    // analyse the markers (one by one)
    for (final MarkerAnnotationAndPosition marker : map.values()) {
        if (!keepGoing[0]) {
            break;
        }
        try {
            final int start = marker.position.offset;
            final int end = start + marker.position.length;
            if (start >= 0 && end > start) {
                IDocument doc = ps.getDoc();
                ArrayList<ICompletionProposalHandle> props = new ArrayList<ICompletionProposalHandle>();
                try {
                    String string = doc.get(start, end - start);
                    if (treatedVars.contains(string)) {
                        continue;
                    }
                    variableFixParticipant.addProps(marker, null, null, ps, start, edit.getPythonNature(), edit, props);
                    if (props.size() > 0) {
                        // Sorting proposals on Ctrl+Shift+O.
                        ProposalsComparator proposalsComparator = new ProposalsComparator("", new ProposalsComparator.CompareContext(edit.getPythonNature()));
                        props.sort(proposalsComparator);
                        edit.selectAndReveal(start, end - start);
                        treatedVars.add(string);
                        Shell activeShell = Display.getCurrent().getActiveShell();
                        // Changed from ElementListSelectionDialog so that we can control the sorting.
                        TreeSelectionDialog dialog = new TreeSelectionDialog(activeShell, new LabelProvider() {

                            // get the image and text for each completion
                            @Override
                            public Image getImage(Object element) {
                                CtxInsensitiveImportComplProposal comp = ((CtxInsensitiveImportComplProposal) element);
                                return comp.getImage();
                            }

                            @Override
                            public String getText(Object element) {
                                CtxInsensitiveImportComplProposal comp = ((CtxInsensitiveImportComplProposal) element);
                                return comp.getDisplayString();
                            }
                        }, new ListContentProvider()) {

                            // override things to return the last position of the dialog correctly
                            @Override
                            protected Control createContents(Composite parent) {
                                Control ret = super.createContents(parent);
                                org.python.pydev.plugin.PydevPlugin.setCssId(parent, "py-add-imports-dialog", true);
                                return ret;
                            }

                            @Override
                            public boolean isHelpAvailable() {
                                return false;
                            }

                            @Override
                            protected void updateStatus(IStatus status) {
                                super.updateStatus(status);
                                PydevPlugin.fixSelectionStatusDialogStatusLineColor(this, this.getDialogArea().getBackground());
                            }

                            /**
                             * @see org.eclipse.ui.dialogs.SelectionDialog#getDialogBoundsSettings()
                             */
                            @Override
                            protected IDialogSettings getDialogBoundsSettings() {
                                IDialogSettings section = dialogSettings.getSection(DIALOG_SETTINGS);
                                if (section == null) {
                                    section = dialogSettings.addNewSection(DIALOG_SETTINGS);
                                }
                                return section;
                            }

                            /* (non-Javadoc)
                                 * @see org.eclipse.jface.dialogs.Dialog#getInitialSize()
                                 */
                            @Override
                            protected Point getInitialSize() {
                                IDialogSettings settings = getDialogBoundsSettings();
                                if (settings != null) {
                                    try {
                                        // $NON-NLS-1$
                                        int width = settings.getInt("DIALOG_WIDTH");
                                        // $NON-NLS-1$
                                        int height = settings.getInt("DIALOG_HEIGHT");
                                        if (width > 0 & height > 0) {
                                            return new Point(width, height);
                                        }
                                    } catch (NumberFormatException nfe) {
                                    // make the default return
                                    }
                                }
                                return new Point(300, 300);
                            }
                        };
                        dialog.setTitle("Choose import");
                        dialog.setMessage("Which import should be added?");
                        dialog.setInput(props);
                        dialog.setInitialSelection(props.get(0));
                        int returnCode = dialog.open();
                        if (returnCode == Window.OK) {
                            ICompletionProposalExtension2 firstResult = (ICompletionProposalExtension2) dialog.getFirstResult();
                            completionsToApply.add(firstResult);
                        } else if (returnCode == Window.CANCEL) {
                            keepGoing[0] = false;
                            continue;
                        }
                    }
                } catch (Exception e) {
                    Log.log(e);
                }
            }
        } catch (Exception e) {
            Log.log(e);
        }
    }
    for (ICompletionProposalExtension2 comp : completionsToApply) {
        // the offset is not used in this case, because the actual completion does nothing,
        int offset = 0;
        // we'll only add the import.
        comp.apply(edit.getPySourceViewer(), ' ', 0, offset);
        didChange = true;
    }
    return true;
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) ArrayList(java.util.ArrayList) ICompletionProposalExtension2(org.eclipse.jface.text.contentassist.ICompletionProposalExtension2) Image(org.eclipse.swt.graphics.Image) Shell(org.eclipse.swt.widgets.Shell) ListContentProvider(org.python.pydev.ui.dialogs.ListContentProvider) Control(org.eclipse.swt.widgets.Control) ICompletionProposalHandle(org.python.pydev.shared_core.code_completion.ICompletionProposalHandle) MarkerAnnotationAndPosition(org.python.pydev.editor.codefolding.MarkerAnnotationAndPosition) HashSet(java.util.HashSet) TreeSelectionDialog(org.python.pydev.ui.dialogs.TreeSelectionDialog) CtxInsensitiveImportComplProposal(org.python.pydev.editor.codecompletion.proposals.CtxInsensitiveImportComplProposal) Composite(org.eclipse.swt.widgets.Composite) ProposalsComparator(org.python.pydev.ast.codecompletion.ProposalsComparator) Point(org.eclipse.swt.graphics.Point) TreeMap(java.util.TreeMap) Point(org.eclipse.swt.graphics.Point) UndefinedVariableFixParticipant(com.python.pydev.analysis.ctrl_1.UndefinedVariableFixParticipant) IDialogSettings(org.eclipse.jface.dialogs.IDialogSettings) LabelProvider(org.eclipse.jface.viewers.LabelProvider) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

UndefinedVariableFixParticipant (com.python.pydev.analysis.ctrl_1.UndefinedVariableFixParticipant)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 TreeMap (java.util.TreeMap)1 IStatus (org.eclipse.core.runtime.IStatus)1 IDialogSettings (org.eclipse.jface.dialogs.IDialogSettings)1 IDocument (org.eclipse.jface.text.IDocument)1 ICompletionProposalExtension2 (org.eclipse.jface.text.contentassist.ICompletionProposalExtension2)1 LabelProvider (org.eclipse.jface.viewers.LabelProvider)1 Image (org.eclipse.swt.graphics.Image)1 Point (org.eclipse.swt.graphics.Point)1 Composite (org.eclipse.swt.widgets.Composite)1 Control (org.eclipse.swt.widgets.Control)1 Shell (org.eclipse.swt.widgets.Shell)1 ProposalsComparator (org.python.pydev.ast.codecompletion.ProposalsComparator)1 CtxInsensitiveImportComplProposal (org.python.pydev.editor.codecompletion.proposals.CtxInsensitiveImportComplProposal)1 MarkerAnnotationAndPosition (org.python.pydev.editor.codefolding.MarkerAnnotationAndPosition)1 ICompletionProposalHandle (org.python.pydev.shared_core.code_completion.ICompletionProposalHandle)1 ListContentProvider (org.python.pydev.ui.dialogs.ListContentProvider)1 TreeSelectionDialog (org.python.pydev.ui.dialogs.TreeSelectionDialog)1