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