Search in sources :

Example 1 with AssistImportToLocal

use of org.python.pydev.editor.correctionassist.heuristics.AssistImportToLocal in project Pydev by fabioz.

the class PythonCorrectionProcessor method computeQuickAssistProposals.

@Override
public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationContext invocationContext) {
    int offset = invocationContext.getOffset();
    PySelection base = edit.createPySelection();
    if (!(this.edit instanceof PyEdit) || base == null) {
        return new ICompletionProposal[0];
    }
    PyEdit editor = (PyEdit) this.edit;
    List<ICompletionProposalHandle> results = new ArrayList<>();
    String sel = PyAction.getLineWithoutComments(base);
    List<IAssistProps> assists = new ArrayList<IAssistProps>();
    synchronized (PythonCorrectionProcessor.additionalAssists) {
        for (IAssistProps prop : additionalAssists.values()) {
            assists.add(prop);
        }
    }
    assists.add(new AssistSurroundWith());
    assists.add(new AssistImport());
    assists.add(new AssistDocString());
    assists.add(new AssistAssign());
    assists.add(new AssistPercentToFormat());
    assists.add(new AssistImportToLocal());
    assists.add(new AssistFString());
    assists.addAll(ExtensionHelper.getParticipants(ExtensionHelper.PYDEV_CTRL_1));
    IImageCache imageCache = SharedUiPlugin.getImageCache();
    File editorFile = edit.getEditorFile();
    IPythonNature pythonNature = null;
    try {
        pythonNature = edit.getPythonNature();
    } catch (MisconfigurationException e1) {
        Log.log(e1);
    }
    for (IAssistProps assist : assists) {
        // Always create a new for each assist, as any given assist may change it.
        PySelection ps = new PySelection(base);
        try {
            if (assist.isValid(ps, sel, editor, offset)) {
                try {
                    results.addAll(assist.getProps(ps, imageCache, editorFile, pythonNature, editor, offset));
                } catch (Exception e) {
                    Log.log(e);
                }
            }
        } catch (Exception e) {
            Log.log(e);
        }
    }
    Collections.sort(results, new ProposalsComparator("", new ProposalsComparator.CompareContext(pythonNature)));
    try {
        // handling spelling... (we only want to show spelling fixes if a spell problem annotation is found at the current location).
        // we'll only show some spelling proposal if there's some spelling problem (so, we don't have to check the preferences at this place,
        // as no annotations on spelling will be here if the spelling is not enabled).
        ICompletionProposal[] spellProps = null;
        IAnnotationModel annotationModel = editor.getPySourceViewer().getAnnotationModel();
        Iterator<Annotation> it = annotationModel.getAnnotationIterator();
        while (it.hasNext()) {
            Annotation annotation = it.next();
            if (annotation instanceof SpellingAnnotation) {
                SpellingAnnotation spellingAnnotation = (SpellingAnnotation) annotation;
                SpellingProblem spellingProblem = spellingAnnotation.getSpellingProblem();
                int problemOffset = spellingProblem.getOffset();
                int problemLen = spellingProblem.getLength();
                if (problemOffset <= offset && problemOffset + problemLen >= offset) {
                    SpellingCorrectionProcessor spellingCorrectionProcessor = new SpellingCorrectionProcessor();
                    spellProps = spellingCorrectionProcessor.computeQuickAssistProposals(invocationContext);
                    break;
                }
            }
        }
        if (spellProps == null || (spellProps.length == 1 && spellProps[0] instanceof NoCompletionsProposal)) {
            // no proposals from the spelling
            return results.toArray(new ICompletionProposal[results.size()]);
        }
        // ok, add the spell problems and return...
        ICompletionProposal[] ret = results.toArray(new ICompletionProposal[results.size() + spellProps.length]);
        System.arraycopy(spellProps, 0, ret, results.size(), spellProps.length);
        return ret;
    } catch (Throwable e) {
        if (e instanceof ClassNotFoundException || e instanceof LinkageError || e instanceof NoSuchMethodException || e instanceof NoSuchMethodError || e instanceof NoClassDefFoundError) {
            // Eclipse 3.2 support
            return results.toArray(new ICompletionProposal[results.size()]);
        }
        throw new RuntimeException(e);
    }
}
Also used : MisconfigurationException(org.python.pydev.core.MisconfigurationException) AssistDocString(org.python.pydev.editor.correctionassist.docstrings.AssistDocString) SpellingCorrectionProcessor(org.eclipse.ui.texteditor.spelling.SpellingCorrectionProcessor) ArrayList(java.util.ArrayList) IPythonNature(org.python.pydev.core.IPythonNature) SpellingProblem(org.eclipse.ui.texteditor.spelling.SpellingProblem) AssistDocString(org.python.pydev.editor.correctionassist.docstrings.AssistDocString) AssistFString(org.python.pydev.editor.correctionassist.heuristics.AssistFString) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) AssistAssign(org.python.pydev.editor.correctionassist.heuristics.AssistAssign) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ICompletionProposalHandle(org.python.pydev.shared_core.code_completion.ICompletionProposalHandle) AssistPercentToFormat(org.python.pydev.editor.correctionassist.heuristics.AssistPercentToFormat) PyEdit(org.python.pydev.editor.PyEdit) IImageCache(org.python.pydev.shared_core.image.IImageCache) AssistSurroundWith(org.python.pydev.editor.correctionassist.heuristics.AssistSurroundWith) AssistImport(org.python.pydev.editor.correctionassist.heuristics.AssistImport) ProposalsComparator(org.python.pydev.ast.codecompletion.ProposalsComparator) AssistFString(org.python.pydev.editor.correctionassist.heuristics.AssistFString) SpellingAnnotation(org.eclipse.ui.texteditor.spelling.SpellingAnnotation) MisconfigurationException(org.python.pydev.core.MisconfigurationException) Annotation(org.eclipse.jface.text.source.Annotation) SpellingAnnotation(org.eclipse.ui.texteditor.spelling.SpellingAnnotation) PySelection(org.python.pydev.core.docutils.PySelection) NoCompletionsProposal(org.eclipse.ui.internal.texteditor.spelling.NoCompletionsProposal) AssistImportToLocal(org.python.pydev.editor.correctionassist.heuristics.AssistImportToLocal) File(java.io.File)

Aggregations

File (java.io.File)1 ArrayList (java.util.ArrayList)1 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)1 Annotation (org.eclipse.jface.text.source.Annotation)1 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)1 NoCompletionsProposal (org.eclipse.ui.internal.texteditor.spelling.NoCompletionsProposal)1 SpellingAnnotation (org.eclipse.ui.texteditor.spelling.SpellingAnnotation)1 SpellingCorrectionProcessor (org.eclipse.ui.texteditor.spelling.SpellingCorrectionProcessor)1 SpellingProblem (org.eclipse.ui.texteditor.spelling.SpellingProblem)1 ProposalsComparator (org.python.pydev.ast.codecompletion.ProposalsComparator)1 IPythonNature (org.python.pydev.core.IPythonNature)1 MisconfigurationException (org.python.pydev.core.MisconfigurationException)1 PySelection (org.python.pydev.core.docutils.PySelection)1 PyEdit (org.python.pydev.editor.PyEdit)1 AssistDocString (org.python.pydev.editor.correctionassist.docstrings.AssistDocString)1 AssistAssign (org.python.pydev.editor.correctionassist.heuristics.AssistAssign)1 AssistFString (org.python.pydev.editor.correctionassist.heuristics.AssistFString)1 AssistImport (org.python.pydev.editor.correctionassist.heuristics.AssistImport)1 AssistImportToLocal (org.python.pydev.editor.correctionassist.heuristics.AssistImportToLocal)1 AssistPercentToFormat (org.python.pydev.editor.correctionassist.heuristics.AssistPercentToFormat)1