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