private void getDocstringHover(IRegion hoverRegion, PySourceViewer s, PySelection ps) {
//Now, aside from the marker, let's check if there's some definition we should show the user about.
CompletionCache completionCache = new CompletionCache();
ArrayList<IDefinition> selected = new ArrayList<IDefinition>();
PyEdit edit = s.getEdit();
RefactoringRequest request;
IPythonNature nature = null;
try {
nature = edit.getPythonNature();
request = new RefactoringRequest(edit.getEditorFile(), ps, new NullProgressMonitor(), nature, edit);
} catch (MisconfigurationException e) {
return;
}
String[] tokenAndQual = null;
try {
tokenAndQual = PyRefactoringFindDefinition.findActualDefinition(request, completionCache, selected);
} catch (CompletionRecursionException e1) {
Log.log(e1);
buf.append("Unable to compute hover. Details: " + e1.getMessage());
return;
}
FastStringBuffer temp = new FastStringBuffer();
if (tokenAndQual != null && selected.size() > 0) {
for (IDefinition d : selected) {
Definition def = (Definition) d;
SimpleNode astToPrint = null;
if (def.ast != null) {
astToPrint = def.ast;
if ((astToPrint instanceof Name || astToPrint instanceof NameTok) && def.scope != null) {
//There's no real point in just printing the name, let's see if we're able to actually find
//the scope where it's in and print that scope.
FastStack<SimpleNode> scopeStack = def.scope.getScopeStack();
if (scopeStack != null && scopeStack.size() > 0) {
SimpleNode peek = scopeStack.peek();
if (peek != null) {
stmtType stmt = NodeUtils.findStmtForNode(peek, astToPrint);
if (stmt != null) {
astToPrint = stmt;
}
}
}
}
try {
astToPrint = astToPrint.createCopy();
MakeAstValidForPrettyPrintingVisitor.makeValid(astToPrint);
} catch (Exception e) {
Log.log(e);
}
}
temp = temp.clear();
if (def.value != null) {
if (astToPrint instanceof FunctionDef) {
temp.append("def ");
} else if (astToPrint instanceof ClassDef) {
temp.append("class ");
}
temp.append("<pydev_hint_bold>");
temp.append(def.value);
temp.append("</pydev_hint_bold>");
temp.append(' ');
}
if (def.module != null) {
temp.append("Found at: ");
temp.append("<pydev_hint_bold>");
temp.append(def.module.getName());
temp.append("</pydev_hint_bold>");
temp.append(PyInformationPresenter.LINE_DELIM);
}
if (def.module != null && def.value != null) {
ItemPointer pointer = PyRefactoringFindDefinition.createItemPointer(def);
String asPortableString = pointer.asPortableString();
if (asPortableString != null) {
//may happen if file is not in the pythonpath
temp.replaceAll(
"<pydev_hint_bold>",
com.aptana.shared_core.string.StringUtils.format("<pydev_link pointer=\"%s\">",
StringEscapeUtils.escapeXml(asPortableString)));
temp.replaceAll("</pydev_hint_bold>", "</pydev_link>");
}
}
String str = printAst(edit, astToPrint);
if (str != null && str.trim().length() > 0) {
temp.append(PyInformationPresenter.LINE_DELIM);
temp.append(str);
} else {
String docstring = d.getDocstring(nature, completionCache);
if (docstring != null && docstring.trim().length() > 0) {
IIndentPrefs indentPrefs = edit.getIndentPrefs();
temp.append(StringUtils.fixWhitespaceColumnsToLeftFromDocstring(docstring,
indentPrefs.getIndentationString()));
}
}