use of org.eclipse.jdt.core.dom.TagElement in project eclipse.jdt.ls by eclipse.
the class ReturnTypeSubProcessor method addMissingReturnTypeProposals.
public static void addMissingReturnTypeProposals(IInvocationContext context, IProblemLocation problem, Collection<CUCorrectionProposal> proposals) {
ICompilationUnit cu = context.getCompilationUnit();
CompilationUnit astRoot = context.getASTRoot();
ASTNode selectedNode = problem.getCoveringNode(astRoot);
if (selectedNode == null) {
return;
}
BodyDeclaration decl = ASTResolving.findParentBodyDeclaration(selectedNode);
if (decl instanceof MethodDeclaration) {
MethodDeclaration methodDeclaration = (MethodDeclaration) decl;
ReturnStatementCollector eval = new ReturnStatementCollector();
decl.accept(eval);
AST ast = astRoot.getAST();
ITypeBinding typeBinding = eval.getTypeBinding(decl.getAST());
typeBinding = Bindings.normalizeTypeBinding(typeBinding);
if (typeBinding == null) {
// $NON-NLS-1$
typeBinding = ast.resolveWellKnownType("void");
}
if (typeBinding.isWildcardType()) {
typeBinding = ASTResolving.normalizeWildcardType(typeBinding, true, ast);
}
ASTRewrite rewrite = ASTRewrite.create(ast);
String label = Messages.format(CorrectionMessages.ReturnTypeSubProcessor_missingreturntype_description, BindingLabelProvider.getBindingLabel(typeBinding, BindingLabelProvider.DEFAULT_TEXTFLAGS));
LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, cu, rewrite, IProposalRelevance.MISSING_RETURN_TYPE);
ImportRewrite imports = proposal.createImportRewrite(astRoot);
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(decl, imports);
Type type = imports.addImport(typeBinding, ast, importRewriteContext, TypeLocation.RETURN_TYPE);
rewrite.set(methodDeclaration, MethodDeclaration.RETURN_TYPE2_PROPERTY, type, null);
rewrite.set(methodDeclaration, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null);
Javadoc javadoc = methodDeclaration.getJavadoc();
if (javadoc != null && typeBinding != null) {
TagElement newTag = ast.newTagElement();
newTag.setTagName(TagElement.TAG_RETURN);
TextElement commentStart = ast.newTextElement();
newTag.fragments().add(commentStart);
JavadocTagsSubProcessor.insertTag(rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY), newTag, null);
// $NON-NLS-1$
proposal.addLinkedPosition(rewrite.track(commentStart), false, "comment_start");
}
// $NON-NLS-1$
String key = "return_type";
proposal.addLinkedPosition(rewrite.track(type), true, key);
if (typeBinding != null) {
ITypeBinding[] bindings = ASTResolving.getRelaxingTypes(ast, typeBinding);
for (int i = 0; i < bindings.length; i++) {
proposal.addLinkedPositionProposal(key, bindings[i]);
}
}
proposals.add(proposal);
// change to constructor
ASTNode parentType = ASTResolving.findParentType(decl);
if (parentType instanceof AbstractTypeDeclaration) {
boolean isInterface = parentType instanceof TypeDeclaration && ((TypeDeclaration) parentType).isInterface();
if (!isInterface) {
String constructorName = ((AbstractTypeDeclaration) parentType).getName().getIdentifier();
ASTNode nameNode = methodDeclaration.getName();
label = Messages.format(CorrectionMessages.ReturnTypeSubProcessor_wrongconstructorname_description, BasicElementLabels.getJavaElementName(constructorName));
proposals.add(new ReplaceCorrectionProposal(label, cu, nameNode.getStartPosition(), nameNode.getLength(), constructorName, IProposalRelevance.CHANGE_TO_CONSTRUCTOR));
}
}
}
}
use of org.eclipse.jdt.core.dom.TagElement in project AutoRefactor by JnRouvignac.
the class CommentsCleanUp method addPeriodAtEndOfFirstLine.
private String addPeriodAtEndOfFirstLine(final Javadoc node, final String comment) {
String beforeFirstTag = comment;
// $NON-NLS-1$
String afterFirstTag = "";
Matcher m = FIRST_JAVADOC_TAG.matcher(comment);
if (m.find()) {
if (m.start() == 0) {
return null;
}
beforeFirstTag = comment.substring(0, m.start());
afterFirstTag = comment.substring(m.start());
}
Matcher matcher = JAVADOC_WITHOUT_PUNCTUATION.matcher(beforeFirstTag);
if (matcher.matches()) {
List<TagElement> tagElements = node.tags();
if (tagElements.size() >= 2) {
TagElement firstLine = tagElements.get(0);
int relativeStart = firstLine.getStartPosition() - node.getStartPosition();
int endOfFirstLine = relativeStart + firstLine.getLength();
// $NON-NLS-1$
return comment.substring(0, endOfFirstLine) + "." + comment.substring(endOfFirstLine);
// TODO JNR do the replace here, not outside this method
}
// $NON-NLS-1$
return matcher.group(1) + "." + matcher.group(2) + afterFirstTag;
}
return null;
}
use of org.eclipse.jdt.core.dom.TagElement in project xtext-eclipse by eclipse.
the class XbaseHoverDocumentationProvider method handleContentElements.
protected void handleContentElements(List<? extends ASTNode> nodes, boolean skipLeadingWhitespace) {
ASTNode previousNode = null;
for (Iterator<? extends ASTNode> iter = nodes.iterator(); iter.hasNext(); ) {
ASTNode child = iter.next();
if (previousNode != null) {
int previousEnd = previousNode.getStartPosition() + previousNode.getLength();
int childStart = child.getStartPosition();
if (previousEnd > childStart) {
// should never happen, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=304826
} else if (previousEnd != childStart) {
// Need to preserve whitespace before a node that's not
// directly following the previous node (e.g. on a new line)
// due to https://bugs.eclipse.org/bugs/show_bug.cgi?id=206518 :
String textWithStars = rawJavaDoc.substring(previousEnd, childStart);
String text = removeDocLineIntros(textWithStars);
buffer.append(text);
}
}
previousNode = child;
if (child instanceof TextElement) {
String text = ((TextElement) child).getText();
if (skipLeadingWhitespace) {
// $NON-NLS-1$ //$NON-NLS-2$
text = text.replaceFirst("^\\s+", "");
}
handleText(text);
} else if (child instanceof TagElement) {
handleInlineTagElement((TagElement) child);
} else {
// This is unexpected. Fail gracefully by just copying the source.
int start = child.getStartPosition();
String text = rawJavaDoc.substring(start, start + child.getLength());
buffer.append(removeDocLineIntros(text));
}
}
}
use of org.eclipse.jdt.core.dom.TagElement in project xtext-eclipse by eclipse.
the class XbaseHoverDocumentationProvider method handleBlockTags.
protected void handleBlockTags(List<TagElement> tags) {
for (Iterator<TagElement> iter = tags.iterator(); iter.hasNext(); ) {
TagElement tag = iter.next();
handleBlockTagTitle(tag.getTagName());
buffer.append(BlOCK_TAG_ENTRY_START);
@SuppressWarnings("unchecked") List<ASTNode> fragments = tag.fragments();
handleContentElements(fragments);
buffer.append(BlOCK_TAG_ENTRY_END);
}
}
use of org.eclipse.jdt.core.dom.TagElement in project xtext-eclipse by eclipse.
the class XbaseHoverDocumentationProvider method computeDocumentation.
public String computeDocumentation(EObject object) {
buffer = new StringBuffer();
context = object;
fLiteralContent = 0;
List<String> parameterNames = initParameterNames();
Map<String, URI> exceptionNamesToURI = initExceptionNamesToURI();
addAnnotations(object);
getDocumentationWithPrefix(object);
Javadoc javadoc = getJavaDoc();
if (javadoc == null)
return buffer.toString();
TagElement deprecatedTag = null;
TagElement start = null;
List<TagElement> parameters = new ArrayList<TagElement>();
TagElement returnTag = null;
List<TagElement> exceptions = new ArrayList<TagElement>();
List<TagElement> versions = new ArrayList<TagElement>();
List<TagElement> authors = new ArrayList<TagElement>();
List<TagElement> sees = new ArrayList<TagElement>();
List<TagElement> since = new ArrayList<TagElement>();
List<TagElement> rest = new ArrayList<TagElement>();
@SuppressWarnings("unchecked") List<TagElement> tags = javadoc.tags();
for (Iterator<TagElement> iter = tags.iterator(); iter.hasNext(); ) {
TagElement tag = iter.next();
String tagName = tag.getTagName();
if (tagName == null) {
start = tag;
} else if (TagElement.TAG_PARAM.equals(tagName)) {
parameters.add(tag);
@SuppressWarnings("unchecked") List<? extends ASTNode> fragments = tag.fragments();
if (fragments.size() > 0) {
Object first = fragments.get(0);
if (first instanceof SimpleName) {
String name = ((SimpleName) first).getIdentifier();
int paramIndex = parameterNames.indexOf(name);
if (paramIndex != -1) {
parameterNames.set(paramIndex, null);
}
}
}
} else if (TagElement.TAG_RETURN.equals(tagName)) {
if (returnTag == null)
// the Javadoc tool only shows the first return tag
returnTag = tag;
} else if (TagElement.TAG_EXCEPTION.equals(tagName) || TagElement.TAG_THROWS.equals(tagName)) {
exceptions.add(tag);
@SuppressWarnings("unchecked") List<? extends ASTNode> fragments = tag.fragments();
if (fragments.size() > 0) {
Object first = fragments.get(0);
if (first instanceof Name) {
@SuppressWarnings("restriction") String name = org.eclipse.jdt.internal.corext.dom.ASTNodes.getSimpleNameIdentifier((Name) first);
if (exceptionNamesToURI.containsKey(name)) {
exceptionNamesToURI.put(name, null);
}
}
}
} else if (TagElement.TAG_SINCE.equals(tagName)) {
since.add(tag);
} else if (TagElement.TAG_VERSION.equals(tagName)) {
versions.add(tag);
} else if (TagElement.TAG_AUTHOR.equals(tagName)) {
authors.add(tag);
} else if (TagElement.TAG_SEE.equals(tagName)) {
sees.add(tag);
} else if (TagElement.TAG_DEPRECATED.equals(tagName)) {
if (deprecatedTag == null)
// the Javadoc tool only shows the first deprecated tag
deprecatedTag = tag;
} else {
rest.add(tag);
}
}
boolean hasParameters = parameters.size() > 0;
boolean hasReturnTag = returnTag != null;
boolean hasExceptions = exceptions.size() > 0;
if (deprecatedTag != null)
handleDeprecatedTag(deprecatedTag);
if (start != null) {
@SuppressWarnings("unchecked") List<ASTNode> fragments = start.fragments();
handleContentElements(fragments);
}
if (hasParameters || hasReturnTag || hasExceptions || versions.size() > 0 || authors.size() > 0 || since.size() > 0 || sees.size() > 0 || rest.size() > 0 || (buffer.length() > 0) && (parameterNames.size() > 0 || exceptionNamesToURI.size() > 0)) {
handleSuperMethodReferences(object);
buffer.append(BLOCK_TAG_START);
handleParameters(object, parameters, parameterNames);
handleReturnTag(returnTag);
handleExceptionTags(exceptions, exceptionNamesToURI);
handleBlockTags("Since:", since);
handleBlockTags("Version:", versions);
handleBlockTags("Author:", authors);
handleBlockTags("See Also:", sees);
handleBlockTags(rest);
buffer.append(BLOCK_TAG_END);
} else if (buffer.length() > 0) {
handleSuperMethodReferences(object);
}
String result = buffer.toString();
buffer = null;
rawJavaDoc = null;
context = null;
return result;
}
Aggregations