use of com.intellij.psi.javadoc.PsiDocTag in project intellij-community by JetBrains.
the class ConstructorJavadocUsageInfo method fixUsage.
@Override
public void fixUsage() throws IncorrectOperationException {
final PsiDocComment docComment = myMethod.getDocComment();
if (docComment != null) {
final List<PsiDocTag> mergedTags = new ArrayList<>();
final PsiDocTag[] paramTags = docComment.findTagsByName("param");
for (PsiDocTag paramTag : paramTags) {
final PsiElement[] dataElements = paramTag.getDataElements();
if (dataElements.length > 0) {
if (dataElements[0] instanceof PsiDocParamRef) {
final PsiReference reference = dataElements[0].getReference();
if (reference != null) {
final PsiElement resolve = reference.resolve();
if (resolve instanceof PsiParameter) {
final int parameterIndex = myMethod.getParameterList().getParameterIndex((PsiParameter) resolve);
if (myDescriptor.getParameterInfo(parameterIndex) == null)
continue;
}
}
}
mergedTags.add((PsiDocTag) paramTag.copy());
}
}
PsiMethod compatibleParamObjectConstructor = null;
final PsiMethod existingConstructor = myDescriptor.getExistingClassCompatibleConstructor();
if (existingConstructor != null && existingConstructor.getDocComment() == null) {
compatibleParamObjectConstructor = existingConstructor;
} else if (!myDescriptor.isUseExistingClass()) {
compatibleParamObjectConstructor = myDescriptor.getExistingClass().getConstructors()[0];
}
if (compatibleParamObjectConstructor != null) {
PsiDocComment psiDocComment = JavaPsiFacade.getElementFactory(myMethod.getProject()).createDocCommentFromText("/**\n*/");
psiDocComment = (PsiDocComment) compatibleParamObjectConstructor.addBefore(psiDocComment, compatibleParamObjectConstructor.getFirstChild());
for (PsiDocTag tag : mergedTags) {
psiDocComment.add(tag);
}
}
}
}
use of com.intellij.psi.javadoc.PsiDocTag in project intellij-community by JetBrains.
the class PsiDocCommentImpl method findTagsByName.
@Override
@NotNull
public PsiDocTag[] findTagsByName(String name) {
ArrayList<PsiDocTag> array = new ArrayList<>();
PsiDocTag[] tags = getTags();
name = "@" + name;
for (PsiDocTag tag : tags) {
if (tag.getNameElement().getText().equals(name)) {
array.add(tag);
}
}
return array.toArray(new PsiDocTag[array.size()]);
}
use of com.intellij.psi.javadoc.PsiDocTag in project intellij-community by JetBrains.
the class PsiDocParamRef method getReference.
@Override
public PsiReference getReference() {
final PsiDocComment comment = PsiTreeUtil.getParentOfType(this, PsiDocComment.class);
if (comment == null)
return null;
final PsiJavaDocumentedElement owner = comment.getOwner();
if (!(owner instanceof PsiMethod) && !(owner instanceof PsiClass))
return null;
final ASTNode valueToken = findChildByType(JavaDocTokenType.DOC_TAG_VALUE_TOKEN);
if (valueToken == null)
return null;
final String name = valueToken.getText();
PsiElement reference = null;
final PsiElement firstChild = getFirstChild();
if (firstChild instanceof PsiDocToken && ((PsiDocToken) firstChild).getTokenType().equals(JavaDocTokenType.DOC_TAG_VALUE_LT)) {
final PsiTypeParameter[] typeParameters = ((PsiTypeParameterListOwner) owner).getTypeParameters();
for (PsiTypeParameter typeParameter : typeParameters) {
if (typeParameter.getName().equals(name)) {
reference = typeParameter;
}
}
} else if (owner instanceof PsiMethod) {
final PsiParameter[] parameters = ((PsiMethod) owner).getParameterList().getParameters();
for (PsiParameter parameter : parameters) {
if (parameter.getName().equals(name)) {
reference = parameter;
}
}
}
final PsiElement resultReference = reference;
return new PsiJavaReference() {
@Override
public PsiElement resolve() {
return resultReference;
}
@Override
@NotNull
public String getCanonicalText() {
return valueToken.getText();
}
@Override
public PsiElement handleElementRename(String newElementName) {
final CharTable charTableByTree = SharedImplUtil.findCharTableByTree(getNode());
LeafElement newElement = Factory.createSingleLeafElement(JavaDocTokenType.DOC_TAG_VALUE_TOKEN, newElementName, charTableByTree, getManager());
replaceChild(valueToken, newElement);
return PsiDocParamRef.this;
}
@Override
public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {
if (isReferenceTo(element))
return PsiDocParamRef.this;
if (!(element instanceof PsiParameter)) {
throw new IncorrectOperationException("Unsupported operation");
}
return handleElementRename(((PsiParameter) element).getName());
}
@Override
public boolean isReferenceTo(PsiElement element) {
if (!(element instanceof PsiNamedElement))
return false;
PsiNamedElement namedElement = (PsiNamedElement) element;
if (!getCanonicalText().equals(namedElement.getName()))
return false;
return getManager().areElementsEquivalent(resolve(), element);
}
@Override
@NotNull
public PsiElement[] getVariants() {
final PsiElement firstChild = getFirstChild();
Set<String> usedNames = new HashSet<>();
for (PsiDocTag tag : comment.getTags()) {
if (tag.getName().equals("param")) {
PsiDocTagValue valueElement = tag.getValueElement();
if (valueElement != null) {
usedNames.add(valueElement.getText());
}
}
}
PsiNamedElement[] result = PsiNamedElement.EMPTY_ARRAY;
if (firstChild instanceof PsiDocToken && ((PsiDocToken) firstChild).getTokenType().equals(JavaDocTokenType.DOC_TAG_VALUE_LT)) {
result = ((PsiTypeParameterListOwner) owner).getTypeParameters();
} else if (owner instanceof PsiMethod) {
result = ((PsiMethod) owner).getParameterList().getParameters();
}
List<PsiElement> filtered = new ArrayList<>();
for (PsiNamedElement namedElement : result) {
if (!usedNames.contains(namedElement.getName())) {
filtered.add(namedElement);
}
}
return filtered.toArray(new PsiElement[filtered.size()]);
}
@Override
public boolean isSoft() {
return false;
}
@Override
public TextRange getRangeInElement() {
final int startOffsetInParent = valueToken.getPsi().getStartOffsetInParent();
return new TextRange(startOffsetInParent, startOffsetInParent + valueToken.getTextLength());
}
@Override
public PsiElement getElement() {
return PsiDocParamRef.this;
}
@Override
public void processVariants(@NotNull PsiScopeProcessor processor) {
for (final PsiElement element : getVariants()) {
if (!processor.execute(element, ResolveState.initial())) {
return;
}
}
}
@Override
@NotNull
public JavaResolveResult advancedResolve(boolean incompleteCode) {
return resultReference == null ? JavaResolveResult.EMPTY : new CandidateInfo(resultReference, PsiSubstitutor.EMPTY);
}
@Override
@NotNull
public JavaResolveResult[] multiResolve(boolean incompleteCode) {
return resultReference == null ? JavaResolveResult.EMPTY_ARRAY : new JavaResolveResult[] { new CandidateInfo(resultReference, PsiSubstitutor.EMPTY) };
}
};
}
use of com.intellij.psi.javadoc.PsiDocTag in project intellij-community by JetBrains.
the class JavadocParamTagsTest method createAndTestTag.
private static void createAndTestTag(String expectedText, String parameterName, String description) throws IncorrectOperationException {
PsiElementFactory factory = getFactory();
final PsiDocTag paramTag = factory.createParamTag(parameterName, description);
assertEquals(expectedText, paramTag.getText());
}
use of com.intellij.psi.javadoc.PsiDocTag in project intellij-community by JetBrains.
the class JavadocParamTagsTest method testAddTag1.
public void testAddTag1() throws Exception {
final PsiElementFactory factory = getFactory();
final PsiMethod method = factory.createMethodFromText("/**\n" + " * Javadoc\n" + " * @param p1\n" + " */\n" + "void m();", null);
final PsiDocComment docComment = method.getDocComment();
assertNotNull(docComment);
final PsiDocTag[] tags = docComment.getTags();
final PsiDocTag tag2 = factory.createParamTag("p2", "");
docComment.addAfter(tag2, tags[0]);
assertEquals("/**\n" + " * Javadoc\n" + " * @param p1\n" + " * @param p2\n" + " */", docComment.getText());
}
Aggregations