use of org.python.pydev.editor.actions.PyOpenAction in project Pydev by fabioz.
the class TddRefactorCompletionInInexistentModule method apply.
@Override
public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
// Now, we need to go on and create the module
List<File> parents = new ArrayList<File>();
File f = module.getParentFile();
while (f != null && !f.exists()) {
parents.add(f);
f = f.getParentFile();
}
IProject project = null;
if (edit != null) {
project = edit.getProject();
}
IContainer container = FindWorkspaceFiles.getContainerForLocation(Path.fromOSString(f.getAbsolutePath()), project);
if (container == null) {
return;
}
Collections.reverse(parents);
int size = parents.size();
for (int i = 0; i < size; i++) {
File parent = parents.get(i);
// create folder with __init__.
IFolder folder = container.getFolder(new Path(parent.getName()));
if (!folder.exists()) {
try {
folder.create(true, true, null);
} catch (CoreException e) {
Log.log(e);
}
}
container = folder;
IFile file = container.getFile(new Path("__init__" + FileTypesPreferences.getDefaultDottedPythonExtension()));
if (!file.exists()) {
try {
file.create(new ByteArrayInputStream(new byte[0]), true, null);
} catch (CoreException e) {
Log.log(e);
}
}
}
// Now that the package structure is created, create the actual module.
IFile file = container.getFile(new Path(module.getName()));
if (!file.exists()) {
try {
file.create(new ByteArrayInputStream(new byte[0]), true, null);
} catch (CoreException e) {
Log.log(e);
}
}
// Upon creation, opens the new editor and creates the class.
PyOpenAction openAction = new PyOpenAction();
openAction.run(new ItemPointer(file));
PyEdit pyEdit = (PyEdit) openAction.editor;
TddRefactorCompletion completion = new TddRefactorCompletion(fReplacementString, fImage, fDisplayString, fContextInformation, fAdditionalProposalInfo, 0, pyEdit, PyCreateClass.LOCATION_STRATEGY_END, parametersAfterCall, pyCreateAction, ps);
completion.apply(pyEdit.getEditorSourceViewer(), '\n', 0, 0);
// As the change was done in another module, let's ask for a new code analysis for the current editor,
// as the new contents should fix the marker which we used for the fix.
forceReparseInBaseEditorAnd(pyEdit);
}
use of org.python.pydev.editor.actions.PyOpenAction in project Pydev by fabioz.
the class TddRefactorCompletionInModule method apply.
@Override
public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
// Upon creation, opens the new editor and creates the class.
PyOpenAction openAction = new PyOpenAction();
openAction.run(new ItemPointer(module));
PyEdit pyEdit = (PyEdit) openAction.editor;
TddRefactorCompletion completion = new TddRefactorCompletion(fReplacementString, fImage, fDisplayString, fContextInformation, fAdditionalProposalInfo, 0, pyEdit, locationStrategy, parametersAfterCall, pyCreateAction, ps);
completion.apply(pyEdit.getEditorSourceViewer(), '\n', 0, 0);
// As the change was done in another module, let's ask for a new code analysis for the current editor,
// as the new contents should fix the marker which we used for the fix.
forceReparseInBaseEditorAnd(pyEdit);
}
use of org.python.pydev.editor.actions.PyOpenAction in project Pydev by fabioz.
the class PyUnitTestResult method open.
public void open() {
File file = new File(this.location);
if (file.exists()) {
PyOpenAction openAction = new PyOpenAction();
String fileContents = FileUtils.getFileContents(file);
String thisTest = this.test;
// This happens when parameterizing pytest tests.
int i = thisTest.indexOf('[');
if (i != -1) {
thisTest = thisTest.substring(0, i).trim();
}
// This happens with unittest subtests.
i = thisTest.indexOf('(');
if (i != -1) {
thisTest = thisTest.substring(0, i).trim();
}
ItemPointer itemPointer = getItemPointer(file, fileContents, thisTest);
openAction.run(itemPointer);
}
}
use of org.python.pydev.editor.actions.PyOpenAction in project Pydev by fabioz.
the class PyInformationPresenter method updatePresentation.
/*
* @see IHoverInformationPresenterExtension#updatePresentation(Drawable drawable, String, TextPresentation, int, int)
* @since 3.2
*/
@Override
public String updatePresentation(Drawable drawable, String hoverInfo, TextPresentation presentation, int maxWidth, int maxHeight) {
if (drawable instanceof StyledText) {
final StyledText styledText = (StyledText) drawable;
styledText.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
int offset;
try {
offset = styledText.getOffsetAtLocation(new Point(e.x, e.y));
} catch (IllegalArgumentException e1) {
// invalid location
return;
}
StyleRange r = styledText.getStyleRangeAtOffset(offset);
if (r instanceof PyStyleRange) {
String tagReplaced = ((PyStyleRange) r).tagReplaced;
if (tagReplaced != null) {
String start = "<pydev_link pointer=\"";
String end = "\">";
if (tagReplaced.startsWith(start) && tagReplaced.endsWith(end)) {
String pointer = tagReplaced.substring(start.length(), tagReplaced.length() - end.length());
new PyOpenAction().run(ItemPointer.fromPortableString(pointer));
}
}
}
}
});
}
if (drawable instanceof Control) {
Control control = (Control) drawable;
if (!Arrays.asList(control.getListeners(SWT.Resize)).contains(resizeListener)) {
control.addListener(SWT.Resize, resizeListener);
control.addListener(SWT.Move, resizeListener);
}
}
if (hoverInfo == null) {
return null;
}
GC gc = new GC(drawable);
try {
FastStringBuffer buffer = new FastStringBuffer();
int maxNumberOfLines = Math.round((float) maxHeight / (float) gc.getFontMetrics().getHeight());
fCounter = 0;
PyLineBreakReader reader = new PyLineBreakReader(createReader(hoverInfo, presentation), gc, maxWidth);
boolean lastLineFormatted = false;
String lastLineIndent = null;
String line = reader.readLine();
boolean lineFormatted = reader.isFormattedLine();
boolean firstLineProcessed = false;
while (line != null) {
if (fEnforceUpperLineLimit && maxNumberOfLines <= 0) {
break;
}
if (firstLineProcessed) {
if (!lastLineFormatted) {
append(buffer, LINE_DELIM, null);
} else {
append(buffer, LINE_DELIM, presentation);
if (lastLineIndent != null) {
append(buffer, lastLineIndent, presentation);
}
}
}
append(buffer, line, null);
firstLineProcessed = true;
lastLineFormatted = lineFormatted;
if (!lineFormatted) {
lastLineIndent = null;
} else if (lastLineIndent == null) {
lastLineIndent = getIndent(line);
}
line = reader.readLine();
lineFormatted = reader.isFormattedLine();
maxNumberOfLines--;
}
if (line != null) {
append(buffer, LINE_DELIM, lineFormatted ? presentation : null);
}
return trim(buffer, presentation);
} catch (IOException e) {
// ignore TODO do something else?
return null;
} finally {
gc.dispose();
}
}
use of org.python.pydev.editor.actions.PyOpenAction in project Pydev by fabioz.
the class HierarchyViewer method onClick.
private void onClick(final HierarchyNodeModel model, int clickCount) {
if (clickCount == 2) {
if (model != null) {
IModule m = model.module;
if (m != null && model.ast != null) {
ItemPointer pointer = new ItemPointer(m.getFile(), model.ast.name);
new PyOpenAction().run(pointer);
}
}
} else {
Runnable r = new Runnable() {
@Override
public void run() {
synchronized (lock) {
if (treeMembers.getItemCount() > 0) {
treeMembers.removeAll();
}
if (model == null) {
return;
}
ClassDef ast = model.ast;
if (ast != null && treeMembers != null) {
DefinitionsASTIteratorVisitor visitor = DefinitionsASTIteratorVisitor.create(ast);
Iterator<ASTEntry> outline = visitor.getOutline();
HashMap<SimpleNode, TreeItem> c = new HashMap<SimpleNode, TreeItem>();
boolean first = true;
while (outline.hasNext()) {
ASTEntry entry = outline.next();
if (first) {
// Don't add the class itself.
first = false;
continue;
}
TreeItem item = null;
if (entry.node instanceof FunctionDef) {
item = createTreeItem(c, entry);
item.setImage(ImageCache.asImage(SharedUiPlugin.getImageCache().get(UIConstants.METHOD_ICON)));
if (model.module != null) {
item.setData(new ItemPointer(model.module.getFile(), ((FunctionDef) entry.node).name));
}
} else if (entry.node instanceof ClassDef) {
item = createTreeItem(c, entry);
item.setImage(ImageCache.asImage(SharedUiPlugin.getImageCache().get(UIConstants.CLASS_ICON)));
if (model.module != null) {
item.setData(new ItemPointer(model.module.getFile(), ((ClassDef) entry.node).name));
}
} else {
item = createTreeItem(c, entry);
item.setImage(ImageCache.asImage(SharedUiPlugin.getImageCache().get(UIConstants.PUBLIC_ATTR_ICON)));
if (model.module != null) {
item.setData(new ItemPointer(model.module.getFile(), entry.node));
}
}
item.setText(entry.getName());
item.setExpanded(true);
}
}
}
}
private TreeItem createTreeItem(HashMap<SimpleNode, TreeItem> c, ASTEntry entry) {
TreeItem parent = null;
ASTEntry par = entry.parent;
if (par != null) {
parent = c.get(par.node);
}
TreeItem item;
if (parent == null) {
item = new TreeItem(treeMembers, 0);
} else {
item = new TreeItem(parent, 0);
}
c.put(entry.node, item);
return item;
}
};
Display.getDefault().asyncExec(r);
}
}
Aggregations