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