Search in sources :

Example 1 with PyOpenAction

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);
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) PyOpenAction(org.python.pydev.editor.actions.PyOpenAction) ArrayList(java.util.ArrayList) IProject(org.eclipse.core.resources.IProject) Point(org.eclipse.swt.graphics.Point) CoreException(org.eclipse.core.runtime.CoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) IContainer(org.eclipse.core.resources.IContainer) File(java.io.File) IFile(org.eclipse.core.resources.IFile) PyEdit(org.python.pydev.editor.PyEdit) IFolder(org.eclipse.core.resources.IFolder) ItemPointer(org.python.pydev.ast.item_pointer.ItemPointer)

Example 2 with PyOpenAction

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);
}
Also used : PyOpenAction(org.python.pydev.editor.actions.PyOpenAction) PyEdit(org.python.pydev.editor.PyEdit) ItemPointer(org.python.pydev.ast.item_pointer.ItemPointer)

Example 3 with PyOpenAction

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);
    }
}
Also used : PyOpenAction(org.python.pydev.editor.actions.PyOpenAction) File(java.io.File) IFile(org.eclipse.core.resources.IFile) ItemPointer(org.python.pydev.ast.item_pointer.ItemPointer)

Example 4 with PyOpenAction

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();
    }
}
Also used : StyledText(org.eclipse.swt.custom.StyledText) MouseEvent(org.eclipse.swt.events.MouseEvent) FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) PyOpenAction(org.python.pydev.editor.actions.PyOpenAction) StyleRange(org.eclipse.swt.custom.StyleRange) MouseAdapter(org.eclipse.swt.events.MouseAdapter) Point(org.eclipse.swt.graphics.Point) IOException(java.io.IOException) Point(org.eclipse.swt.graphics.Point) Control(org.eclipse.swt.widgets.Control) GC(org.eclipse.swt.graphics.GC)

Example 5 with PyOpenAction

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);
    }
}
Also used : IModule(org.python.pydev.core.IModule) TreeItem(org.eclipse.swt.widgets.TreeItem) HashMap(java.util.HashMap) PyOpenAction(org.python.pydev.editor.actions.PyOpenAction) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) SimpleNode(org.python.pydev.parser.jython.SimpleNode) ClassDef(org.python.pydev.parser.jython.ast.ClassDef) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) DefinitionsASTIteratorVisitor(org.python.pydev.parser.visitors.scope.DefinitionsASTIteratorVisitor) ItemPointer(org.python.pydev.ast.item_pointer.ItemPointer)

Aggregations

PyOpenAction (org.python.pydev.editor.actions.PyOpenAction)13 ItemPointer (org.python.pydev.ast.item_pointer.ItemPointer)11 IFile (org.eclipse.core.resources.IFile)4 File (java.io.File)3 ArrayList (java.util.ArrayList)3 IProject (org.eclipse.core.resources.IProject)3 PartInitException (org.eclipse.ui.PartInitException)3 CoreException (org.eclipse.core.runtime.CoreException)2 MouseAdapter (org.eclipse.swt.events.MouseAdapter)2 MouseEvent (org.eclipse.swt.events.MouseEvent)2 Point (org.eclipse.swt.graphics.Point)2 IModule (org.python.pydev.core.IModule)2 IPythonNature (org.python.pydev.core.IPythonNature)2 PyEdit (org.python.pydev.editor.PyEdit)2 AdditionalInfoAndIInfo (com.python.pydev.analysis.additionalinfo.AdditionalInfoAndIInfo)1 AdditionalProjectInterpreterInfo (com.python.pydev.analysis.additionalinfo.AdditionalProjectInterpreterInfo)1 AdditionalSystemInterpreterInfo (com.python.pydev.analysis.additionalinfo.AdditionalSystemInterpreterInfo)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1