Search in sources :

Example 1 with IImageCache

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

the class AnalysisImages method getImageForTypeInfo.

public static IImageHandle getImageForTypeInfo(IInfo info) {
    switch(info.getType()) {
        case IInfo.CLASS_WITH_IMPORT_TYPE:
            if (classWithImportType == null) {
                synchronized (lock) {
                    IImageCache imageCache = SharedUiPlugin.getImageCache();
                    classWithImportType = imageCache.get(UIConstants.CLASS_ICON);
                }
            }
            return classWithImportType;
        case IInfo.METHOD_WITH_IMPORT_TYPE:
            if (methodWithImportType == null) {
                synchronized (lock) {
                    IImageCache imageCache = SharedUiPlugin.getImageCache();
                    methodWithImportType = imageCache.get(UIConstants.METHOD_ICON);
                }
            }
            return methodWithImportType;
        case IInfo.ATTRIBUTE_WITH_IMPORT_TYPE:
            if (attributeWithImportType == null) {
                synchronized (lock) {
                    IImageCache imageCache = SharedUiPlugin.getImageCache();
                    attributeWithImportType = imageCache.get(UIConstants.PUBLIC_ATTR_ICON);
                }
            }
            return attributeWithImportType;
        case IInfo.MOD_IMPORT_TYPE:
            if (modImportType == null) {
                synchronized (lock) {
                    IImageCache imageCache = SharedUiPlugin.getImageCache();
                    modImportType = imageCache.get(UIConstants.FOLDER_PACKAGE_ICON);
                }
            }
            return modImportType;
        default:
            throw new RuntimeException("Undefined type.");
    }
}
Also used : IImageCache(org.python.pydev.shared_core.image.IImageCache)

Example 2 with IImageCache

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

the class AnalysisImages method getImageForAutoImportTypeInfo.

public static IImageHandle getImageForAutoImportTypeInfo(int infoType) {
    switch(infoType) {
        case IInfo.CLASS_WITH_IMPORT_TYPE:
            if (autoImportClassWithImportType == null) {
                synchronized (lock) {
                    IImageCache imageCache = SharedUiPlugin.getImageCache();
                    autoImportClassWithImportType = imageCache.getImageDecorated(UIConstants.CLASS_ICON, UIConstants.CTX_INSENSITIVE_DECORATION_ICON, IImageCache.DECORATION_LOCATION_BOTTOM_RIGHT);
                }
            }
            return autoImportClassWithImportType;
        case IInfo.METHOD_WITH_IMPORT_TYPE:
            if (autoImportMethodWithImportType == null) {
                synchronized (lock) {
                    IImageCache imageCache = SharedUiPlugin.getImageCache();
                    autoImportMethodWithImportType = imageCache.getImageDecorated(UIConstants.METHOD_ICON, UIConstants.CTX_INSENSITIVE_DECORATION_ICON, IImageCache.DECORATION_LOCATION_BOTTOM_RIGHT);
                }
            }
            return autoImportMethodWithImportType;
        case IInfo.ATTRIBUTE_WITH_IMPORT_TYPE:
            if (autoImportAttributeWithImportType == null) {
                synchronized (lock) {
                    IImageCache imageCache = SharedUiPlugin.getImageCache();
                    autoImportAttributeWithImportType = imageCache.getImageDecorated(UIConstants.PUBLIC_ATTR_ICON, UIConstants.CTX_INSENSITIVE_DECORATION_ICON, IImageCache.DECORATION_LOCATION_BOTTOM_RIGHT);
                }
            }
            return autoImportAttributeWithImportType;
        case IInfo.MOD_IMPORT_TYPE:
            if (autoImportModImportType == null) {
                synchronized (lock) {
                    IImageCache imageCache = SharedUiPlugin.getImageCache();
                    autoImportModImportType = imageCache.getImageDecorated(UIConstants.FOLDER_PACKAGE_ICON, UIConstants.CTX_INSENSITIVE_DECORATION_ICON, IImageCache.DECORATION_LOCATION_BOTTOM_RIGHT);
                }
            }
            return autoImportModImportType;
        case IInfo.USE_PACKAGE_ICON:
            IImageCache imageCache = SharedUiPlugin.getImageCache();
            return imageCache.get(UIConstants.COMPLETION_PACKAGE_ICON);
        default:
            throw new RuntimeException("Undefined type.");
    }
}
Also used : IImageCache(org.python.pydev.shared_core.image.IImageCache)

Example 3 with IImageCache

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

the class PackageTab method createPackageControlTab.

/**
 * @param exeOrJarOfInterpretersToRestore if the info is changed, the executable should be added to exeOrJarOfInterpretersToRestore.
 */
public void createPackageControlTab(TabFolder tabFolder, Set<String> exeOrJarOfInterpretersToRestore, IInterpreterManager interpreterManager) {
    Composite parent;
    GridData gd;
    TabItem tabItem = new TabItem(tabFolder, SWT.None);
    tabItem.setText("Packages");
    IImageCache imageCache = SharedUiPlugin.getImageCache();
    tabItem.setImage(ImageCache.asImage(imageCache.get(UIConstants.FOLDER_PACKAGE_ICON)));
    Composite composite = new Composite(tabFolder, SWT.None);
    parent = composite;
    GridLayout layout = new GridLayout(2, false);
    composite.setLayout(layout);
    tree = new Tree(parent, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
    tree.setHeaderVisible(true);
    createColumn("Library", 300);
    createColumn("Version", 100);
    createColumn("", 100);
    gd = new GridData(GridData.FILL_BOTH);
    gd.horizontalSpan = 1;
    gd.grabExcessHorizontalSpace = true;
    gd.grabExcessVerticalSpace = true;
    tree.setLayoutData(gd);
    // buttons at the side of the tree
    Composite control = getButtonBoxPackage(parent, interpreterManager);
    gd = new GridData();
    gd.verticalAlignment = GridData.BEGINNING;
    control.setLayoutData(gd);
    tabItem.setControl(composite);
}
Also used : TabItem(org.eclipse.swt.widgets.TabItem) GridLayout(org.eclipse.swt.layout.GridLayout) Composite(org.eclipse.swt.widgets.Composite) IImageCache(org.python.pydev.shared_core.image.IImageCache) GridData(org.eclipse.swt.layout.GridData) Tree(org.eclipse.swt.widgets.Tree)

Example 4 with IImageCache

use of org.python.pydev.shared_core.image.IImageCache 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 5 with IImageCache

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

the class PythonCorrectionProcessor method computeQuickAssistProposals.

@Override
public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationContext invocationContext) {
    int offset = invocationContext.getOffset();
    PySelection base = edit.createPySelection();
    if (!(this.edit instanceof PyEdit) || base == null) {
        return new ICompletionProposal[0];
    }
    PyEdit editor = (PyEdit) this.edit;
    List<ICompletionProposalHandle> results = new ArrayList<>();
    String sel = PyAction.getLineWithoutComments(base);
    List<IAssistProps> assists = new ArrayList<IAssistProps>();
    synchronized (PythonCorrectionProcessor.additionalAssists) {
        for (IAssistProps prop : additionalAssists.values()) {
            assists.add(prop);
        }
    }
    assists.add(new AssistSurroundWith());
    assists.add(new AssistImport());
    assists.add(new AssistDocString());
    assists.add(new AssistAssign());
    assists.add(new AssistPercentToFormat());
    assists.add(new AssistImportToLocal());
    assists.add(new AssistFString());
    assists.addAll(ExtensionHelper.getParticipants(ExtensionHelper.PYDEV_CTRL_1));
    IImageCache imageCache = SharedUiPlugin.getImageCache();
    File editorFile = edit.getEditorFile();
    IPythonNature pythonNature = null;
    try {
        pythonNature = edit.getPythonNature();
    } catch (MisconfigurationException e1) {
        Log.log(e1);
    }
    for (IAssistProps assist : assists) {
        // Always create a new for each assist, as any given assist may change it.
        PySelection ps = new PySelection(base);
        try {
            if (assist.isValid(ps, sel, editor, offset)) {
                try {
                    results.addAll(assist.getProps(ps, imageCache, editorFile, pythonNature, editor, offset));
                } catch (Exception e) {
                    Log.log(e);
                }
            }
        } catch (Exception e) {
            Log.log(e);
        }
    }
    Collections.sort(results, new ProposalsComparator("", new ProposalsComparator.CompareContext(pythonNature)));
    try {
        // handling spelling... (we only want to show spelling fixes if a spell problem annotation is found at the current location).
        // we'll only show some spelling proposal if there's some spelling problem (so, we don't have to check the preferences at this place,
        // as no annotations on spelling will be here if the spelling is not enabled).
        ICompletionProposal[] spellProps = null;
        IAnnotationModel annotationModel = editor.getPySourceViewer().getAnnotationModel();
        Iterator<Annotation> it = annotationModel.getAnnotationIterator();
        while (it.hasNext()) {
            Annotation annotation = it.next();
            if (annotation instanceof SpellingAnnotation) {
                SpellingAnnotation spellingAnnotation = (SpellingAnnotation) annotation;
                SpellingProblem spellingProblem = spellingAnnotation.getSpellingProblem();
                int problemOffset = spellingProblem.getOffset();
                int problemLen = spellingProblem.getLength();
                if (problemOffset <= offset && problemOffset + problemLen >= offset) {
                    SpellingCorrectionProcessor spellingCorrectionProcessor = new SpellingCorrectionProcessor();
                    spellProps = spellingCorrectionProcessor.computeQuickAssistProposals(invocationContext);
                    break;
                }
            }
        }
        if (spellProps == null || (spellProps.length == 1 && spellProps[0] instanceof NoCompletionsProposal)) {
            // no proposals from the spelling
            return results.toArray(new ICompletionProposal[results.size()]);
        }
        // ok, add the spell problems and return...
        ICompletionProposal[] ret = results.toArray(new ICompletionProposal[results.size() + spellProps.length]);
        System.arraycopy(spellProps, 0, ret, results.size(), spellProps.length);
        return ret;
    } catch (Throwable e) {
        if (e instanceof ClassNotFoundException || e instanceof LinkageError || e instanceof NoSuchMethodException || e instanceof NoSuchMethodError || e instanceof NoClassDefFoundError) {
            // Eclipse 3.2 support
            return results.toArray(new ICompletionProposal[results.size()]);
        }
        throw new RuntimeException(e);
    }
}
Also used : MisconfigurationException(org.python.pydev.core.MisconfigurationException) AssistDocString(org.python.pydev.editor.correctionassist.docstrings.AssistDocString) SpellingCorrectionProcessor(org.eclipse.ui.texteditor.spelling.SpellingCorrectionProcessor) ArrayList(java.util.ArrayList) IPythonNature(org.python.pydev.core.IPythonNature) SpellingProblem(org.eclipse.ui.texteditor.spelling.SpellingProblem) AssistDocString(org.python.pydev.editor.correctionassist.docstrings.AssistDocString) AssistFString(org.python.pydev.editor.correctionassist.heuristics.AssistFString) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) AssistAssign(org.python.pydev.editor.correctionassist.heuristics.AssistAssign) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ICompletionProposalHandle(org.python.pydev.shared_core.code_completion.ICompletionProposalHandle) AssistPercentToFormat(org.python.pydev.editor.correctionassist.heuristics.AssistPercentToFormat) PyEdit(org.python.pydev.editor.PyEdit) IImageCache(org.python.pydev.shared_core.image.IImageCache) AssistSurroundWith(org.python.pydev.editor.correctionassist.heuristics.AssistSurroundWith) AssistImport(org.python.pydev.editor.correctionassist.heuristics.AssistImport) ProposalsComparator(org.python.pydev.ast.codecompletion.ProposalsComparator) AssistFString(org.python.pydev.editor.correctionassist.heuristics.AssistFString) SpellingAnnotation(org.eclipse.ui.texteditor.spelling.SpellingAnnotation) MisconfigurationException(org.python.pydev.core.MisconfigurationException) Annotation(org.eclipse.jface.text.source.Annotation) SpellingAnnotation(org.eclipse.ui.texteditor.spelling.SpellingAnnotation) PySelection(org.python.pydev.core.docutils.PySelection) NoCompletionsProposal(org.eclipse.ui.internal.texteditor.spelling.NoCompletionsProposal) AssistImportToLocal(org.python.pydev.editor.correctionassist.heuristics.AssistImportToLocal) File(java.io.File)

Aggregations

IImageCache (org.python.pydev.shared_core.image.IImageCache)11 IPythonNature (org.python.pydev.core.IPythonNature)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 MisconfigurationException (org.python.pydev.core.MisconfigurationException)2 SimpleNode (org.python.pydev.parser.jython.SimpleNode)2 IImageDescriptor (org.python.pydev.shared_core.image.IImageDescriptor)2 DataAndImageTreeNode (org.python.pydev.shared_core.structure.DataAndImageTreeNode)2 IOException (java.io.IOException)1 List (java.util.List)1 CoreException (org.eclipse.core.runtime.CoreException)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)1 Annotation (org.eclipse.jface.text.source.Annotation)1 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)1 GridData (org.eclipse.swt.layout.GridData)1 GridLayout (org.eclipse.swt.layout.GridLayout)1 Composite (org.eclipse.swt.widgets.Composite)1 TabItem (org.eclipse.swt.widgets.TabItem)1