Search in sources :

Example 1 with IImageHandle

use of org.python.pydev.shared_core.image.IImageHandle in project Pydev by fabioz.

the class JavaElementToken method getImage.

@Override
public IImageHandle getImage() {
    if (this.image != null) {
        return this.image;
    }
    CompletionProposalLabelProvider provider = new CompletionProposalLabelProvider();
    CompletionProposal generatedProposal = CompletionProposal.create(completionProposalKind, 0);
    generatedProposal.setFlags(completionProposalFlags);
    if (HAS_ADDITIONAL_FLAGS) {
        generatedProposal.setAdditionalFlags(completionProposalAdditionalFlags);
    }
    generatedProposal.setDeclarationSignature(completionPropsoalSignature);
    generatedProposal.setSignature(completionPropsoalSignature);
    // uses: kind, flags, signature to create an image.
    ImageDescriptor descriptor = provider.createImageDescriptor(generatedProposal);
    Image computed = descriptor.createImage();
    return new IImageHandle() {

        @Override
        public Object getImageData() {
            return computed.getImageData();
        }

        @Override
        public Object getImage() {
            return computed;
        }
    };
}
Also used : IImageHandle(org.python.pydev.shared_core.image.IImageHandle) CompletionProposal(org.eclipse.jdt.core.CompletionProposal) CompletionProposalLabelProvider(org.eclipse.jdt.ui.text.java.CompletionProposalLabelProvider) ImageDescriptor(org.eclipse.jface.resource.ImageDescriptor) Image(org.eclipse.swt.graphics.Image)

Example 2 with IImageHandle

use of org.python.pydev.shared_core.image.IImageHandle in project Pydev by fabioz.

the class PyCodeCompletion method createOverrideCodeCompletions.

private void createOverrideCodeCompletions(CompletionRequest request, ArrayList<ICompletionProposalHandle> ret, PySelection ps) throws BadLocationException {
    IImageCache imageCache = SharedCorePlugin.getImageCache();
    IImageHandle imageOverride = imageCache != null ? imageCache.get(UIConstants.METHOD_ICON) : null;
    String lineContentsToCursor = ps.getLineContentsToCursor();
    LineStartingScope scopeStart = ps.getPreviousLineThatStartsScope(PySelection.CLASS_TOKEN, false, PySelection.getFirstCharPosition(lineContentsToCursor));
    String className = null;
    if (scopeStart != null) {
        className = PySelection.getClassNameInLine(scopeStart.lineStartingScope);
        if (className != null && className.length() > 0) {
            Tuple<List<String>, Integer> insideParensBaseClasses = ps.getInsideParentesisToks(true, scopeStart.iLineStartingScope);
            if (insideParensBaseClasses != null) {
                // representation -> token and base class
                OrderedMap<String, ImmutableTuple<IToken, String>> map = new OrderedMap<String, ImmutableTuple<IToken, String>>();
                for (String baseClass : insideParensBaseClasses.o1) {
                    try {
                        ICompletionState state = new CompletionState(-1, -1, null, request.nature, baseClass);
                        state.setActivationToken(baseClass);
                        state.setIsInCalltip(false);
                        IPythonNature pythonNature = request.nature;
                        checkPythonNature(pythonNature);
                        ICodeCompletionASTManager astManager = pythonNature.getAstManager();
                        if (astManager == null) {
                            // we're probably still loading it.
                            return;
                        }
                        // Ok, looking for a token in globals.
                        IModule module = request.getModule();
                        if (module == null) {
                            continue;
                        }
                        TokensList comps = astManager.getCompletionsForModule(module, state, true, true);
                        for (IterTokenEntry entry : comps) {
                            IToken iToken = entry.getToken();
                            String representation = iToken.getRepresentation();
                            ImmutableTuple<IToken, String> curr = map.get(representation);
                            if (curr != null && curr.o1 instanceof SourceToken) {
                                // source tokens are never reset!
                                continue;
                            }
                            int type = iToken.getType();
                            if (iToken instanceof SourceToken && ((SourceToken) iToken).getAst() instanceof FunctionDef) {
                                map.put(representation, new ImmutableTuple<IToken, String>(iToken, baseClass));
                            } else if (type == IToken.TYPE_FUNCTION || type == IToken.TYPE_UNKNOWN || type == IToken.TYPE_BUILTIN) {
                                map.put(representation, new ImmutableTuple<IToken, String>(iToken, baseClass));
                            }
                        }
                    } catch (Exception e) {
                        Log.log(e);
                    }
                }
                for (ImmutableTuple<IToken, String> tokenAndBaseClass : map.values()) {
                    FunctionDef functionDef = null;
                    // No checkings needed for type (we already did that above).
                    if (tokenAndBaseClass.o1 instanceof SourceToken) {
                        SourceToken sourceToken = (SourceToken) tokenAndBaseClass.o1;
                        SimpleNode ast = sourceToken.getAst();
                        if (ast instanceof FunctionDef) {
                            functionDef = (FunctionDef) ast;
                        } else {
                            functionDef = sourceToken.getAliased().createCopy();
                            NameTok t = (NameTok) functionDef.name;
                            t.id = sourceToken.getRepresentation();
                        }
                    } else {
                        // unfortunately, for builtins we usually cannot trust the parameters.
                        String representation = tokenAndBaseClass.o1.getRepresentation();
                        PyAstFactory factory = new PyAstFactory(new AdapterPrefs(ps.getEndLineDelim(), request.nature));
                        functionDef = factory.createFunctionDef(representation);
                        functionDef.args = factory.createArguments(true);
                        functionDef.args.vararg = new NameTok("args", NameTok.VarArg);
                        functionDef.args.kwarg = new NameTok("kwargs", NameTok.KwArg);
                        if (!representation.equals("__init__")) {
                            // signal that the return should be added
                            functionDef.body = new stmtType[] { new Return(null) };
                        }
                    }
                    if (functionDef != null) {
                        ret.add(CompletionProposalFactory.get().createOverrideMethodCompletionProposal(request, ps, ps.getAbsoluteCursorOffset(), 0, 0, imageOverride, functionDef, tokenAndBaseClass.o2, className));
                    }
                }
            }
        }
    }
}
Also used : IModule(org.python.pydev.core.IModule) IPythonNature(org.python.pydev.core.IPythonNature) ICompletionState(org.python.pydev.core.ICompletionState) CompletionState(org.python.pydev.ast.codecompletion.revisited.CompletionState) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) SimpleNode(org.python.pydev.parser.jython.SimpleNode) ISimpleNode(org.python.pydev.shared_core.model.ISimpleNode) IToken(org.python.pydev.core.IToken) ICompletionState(org.python.pydev.core.ICompletionState) TokensList(org.python.pydev.core.TokensList) List(java.util.List) TokensOrProposalsList(org.python.pydev.core.TokensOrProposalsList) ArrayList(java.util.ArrayList) PyAstFactory(org.python.pydev.parser.jython.ast.factory.PyAstFactory) TokensList(org.python.pydev.core.TokensList) Return(org.python.pydev.parser.jython.ast.Return) IImageCache(org.python.pydev.shared_core.image.IImageCache) IterTokenEntry(org.python.pydev.core.IterTokenEntry) ImmutableTuple(org.python.pydev.shared_core.structure.ImmutableTuple) LineStartingScope(org.python.pydev.core.docutils.PySelection.LineStartingScope) CoreException(org.eclipse.core.runtime.CoreException) PythonNatureWithoutProjectException(org.python.pydev.core.PythonNatureWithoutProjectException) CompletionRecursionException(org.python.pydev.core.structure.CompletionRecursionException) BadLocationException(org.eclipse.jface.text.BadLocationException) IOException(java.io.IOException) MisconfigurationException(org.python.pydev.core.MisconfigurationException) ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) IImageHandle(org.python.pydev.shared_core.image.IImageHandle) AdapterPrefs(org.python.pydev.parser.jython.ast.factory.AdapterPrefs) OrderedMap(org.python.pydev.shared_core.structure.OrderedMap) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken) NameTok(org.python.pydev.parser.jython.ast.NameTok)

Example 3 with IImageHandle

use of org.python.pydev.shared_core.image.IImageHandle in project Pydev by fabioz.

the class ImageCache method get.

/* (non-Javadoc)
     * @see org.python.pydev.shared_ui.IImageCache#get(java.lang.String)
     */
@Override
public IImageHandle get(String key) {
    Image image = getFromImageHash(key);
    if (image == null) {
        ImageDescriptor desc;
        try {
            // Don't lock for this creation (GTK has a global lock for the image
            // creation which is the same one for the main thread, so, if this
            // happens in a thread, the main thread could deadlock).
            // #PyDev-527: Deadlock in ImageCache rendering debug completions
            desc = asImageDescriptor(getDescriptor(key));
            image = desc.createImage();
            image = putOnImageHash(key, image);
        } catch (NoClassDefFoundError e) {
            // we're in tests...
            return null;
        } catch (UnsatisfiedLinkError e) {
            // we're in tests...
            return null;
        } catch (Exception e) {
            // If image is missing, create a default missing one
            Log.log("ERROR: Missing image: " + key);
            Image m = missing;
            if (m == null || m.isDisposed()) {
                desc = ImageDescriptor.getMissingImageDescriptor();
                m = missing = desc.createImage();
            }
            image = m;
        }
    }
    final Image computed = image;
    return new IImageHandle() {

        @Override
        public Object getImage() {
            return computed;
        }

        @Override
        public Object getImageData() {
            return computed.getImageData();
        }
    };
}
Also used : IImageHandle(org.python.pydev.shared_core.image.IImageHandle) IImageDescriptor(org.python.pydev.shared_core.image.IImageDescriptor) CompositeImageDescriptor(org.eclipse.jface.resource.CompositeImageDescriptor) ImageDescriptor(org.eclipse.jface.resource.ImageDescriptor) Image(org.eclipse.swt.graphics.Image) MalformedURLException(java.net.MalformedURLException)

Example 4 with IImageHandle

use of org.python.pydev.shared_core.image.IImageHandle in project Pydev by fabioz.

the class AssistDocString method getProps.

/**
 * @see org.python.pydev.editor.correctionassist.IAssistProps#getProps(org.python.pydev.core.docutils.PySelection,
 *      org.python.pydev.shared_ui.ImageCache)
 */
@Override
public List<ICompletionProposalHandle> getProps(PySelection ps, IImageCache imageCache, File f, IPythonNature nature, IPyEdit edit, int offset) throws BadLocationException {
    ArrayList<ICompletionProposalHandle> l = new ArrayList<>();
    Tuple<List<String>, Integer> tuple = ps.getInsideParentesisToks(false);
    if (tuple == null) {
        if (ps.isInClassLine()) {
            tuple = new Tuple<List<String>, Integer>(new ArrayList<String>(), offset);
        } else {
            return l;
        }
    }
    List<String> params = tuple.o1;
    int lineOfOffset = ps.getLineOfOffset(tuple.o2);
    // Calculate only the initial part of the docstring here (everything else should be lazily computed on apply).
    String initial = PySelection.getIndentationFromLine(ps.getCursorLineContents());
    String delimiter = PyAction.getDelimiter(ps.getDoc());
    String indentation = edit != null ? edit.getIndentPrefs().getIndentationString() : DefaultIndentPrefs.get(nature).getIndentationString();
    String delimiterAndIndent = delimiter + initial + indentation;
    FastStringBuffer buf = new FastStringBuffer();
    String docStringMarker = DocstringsPrefPage.getDocstringMarker();
    buf.append(delimiterAndIndent + docStringMarker);
    buf.append(delimiterAndIndent);
    int newOffset = buf.length();
    int offsetPosToAdd = ps.getEndLineOffset(lineOfOffset);
    // may be null (testing)
    IImageHandle image = null;
    if (imageCache != null) {
        image = imageCache.get(UIConstants.ASSIST_DOCSTRING);
    }
    final boolean inFunctionLine = ps.isInFunctionLine(true);
    DocstringInfo docstringFromFunction = null;
    if (inFunctionLine) {
        int currLine = ps.getLineOfOffset();
        docstringFromFunction = ps.getDocstringFromLine(currLine + 1);
    }
    final DocstringInfo finalDocstringFromFunction = docstringFromFunction;
    String preferredDocstringStyle = AssistDocString.this.docStringStyle;
    if (preferredDocstringStyle == null) {
        preferredDocstringStyle = DocstringsPrefPage.getPreferredDocstringStyle();
    }
    final String preferredDocstringStyle2 = preferredDocstringStyle;
    if (preferredDocstringStyle.equals(DocstringsPrefPage.DOCSTRINGSTYLE_GOOGLE)) {
        buf.append("Args:");
    }
    l.add(CompletionProposalFactory.get().createAssistDocstringCompletionProposal("", offsetPosToAdd, 0, newOffset, image, finalDocstringFromFunction != null ? "Update docstring" : "Make docstring", null, null, IPyCompletionProposal.PRIORITY_DEFAULT, null, initial, delimiter, docStringMarker, delimiterAndIndent, preferredDocstringStyle2, inFunctionLine, finalDocstringFromFunction, indentation, buf, params));
    return l;
}
Also used : FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) ArrayList(java.util.ArrayList) DocstringInfo(org.python.pydev.core.docutils.PySelection.DocstringInfo) IImageHandle(org.python.pydev.shared_core.image.IImageHandle) ICompletionProposalHandle(org.python.pydev.shared_core.code_completion.ICompletionProposalHandle) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with IImageHandle

use of org.python.pydev.shared_core.image.IImageHandle in project Pydev by fabioz.

the class PythonBaseModelProvider method createErrorNoWorkingSetsDefined.

public TreeNode<LabelAndImage> createErrorNoWorkingSetsDefined(Object parentElement) {
    IImageHandle img = SharedUiPlugin.getImageCache().get(UIConstants.WARNING);
    TreeNode<LabelAndImage> root = new TreeNode<LabelAndImage>(parentElement, new LabelAndImage("Warning: Top level elements set to working sets but no working sets are defined.", img));
    new TreeNode<>(root, new LabelAndImage("Access the menu (Ctrl+F10) to change to show projects or create a working set.", null));
    return root;
}
Also used : IImageHandle(org.python.pydev.shared_core.image.IImageHandle) TreeNode(org.python.pydev.shared_core.structure.TreeNode)

Aggregations

IImageHandle (org.python.pydev.shared_core.image.IImageHandle)10 Image (org.eclipse.swt.graphics.Image)4 MalformedURLException (java.net.MalformedURLException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ImageDescriptor (org.eclipse.jface.resource.ImageDescriptor)2 BadLocationException (org.eclipse.jface.text.BadLocationException)2 Display (org.eclipse.swt.widgets.Display)2 IImageCache (org.python.pydev.shared_core.image.IImageCache)2 IImageDescriptor (org.python.pydev.shared_core.image.IImageDescriptor)2 Tuple3 (org.python.pydev.shared_core.structure.Tuple3)2 File (java.io.File)1 IOException (java.io.IOException)1 CoreException (org.eclipse.core.runtime.CoreException)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 CompletionProposal (org.eclipse.jdt.core.CompletionProposal)1 CompletionProposalLabelProvider (org.eclipse.jdt.ui.text.java.CompletionProposalLabelProvider)1 CompositeImageDescriptor (org.eclipse.jface.resource.CompositeImageDescriptor)1 ITypedRegion (org.eclipse.jface.text.ITypedRegion)1 Region (org.eclipse.jface.text.Region)1