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);
}
}
Aggregations