IDEA-145725 Quick documentation shows the wrong Javadoc for package-info.java

This commit is contained in:
Dmitry Batrak
2015-10-15 19:38:43 +03:00
parent bd72fa775d
commit d333668b67
4 changed files with 35 additions and 3 deletions
@@ -41,6 +41,7 @@ import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.intellij.psi.impl.JavaConstantExpressionEvaluator;
import com.intellij.psi.impl.source.tree.JavaDocElementType;
import com.intellij.psi.impl.source.tree.JavaElementType;
import com.intellij.psi.javadoc.*;
import com.intellij.psi.search.EverythingGlobalScope;
import com.intellij.psi.util.PsiFormatUtil;
@@ -732,7 +733,7 @@ public class JavaDocInfoGenerator {
if (packageInfoFile != null) {
final ASTNode node = packageInfoFile.getNode();
if (node != null) {
final ASTNode docCommentNode = node.findChildByType(JavaDocElementType.DOC_COMMENT);
final ASTNode docCommentNode = findRelevantCommentNode(node);
if (docCommentNode != null) {
final PsiDocComment docComment = (PsiDocComment)docCommentNode.getPsi();
@@ -755,6 +756,19 @@ public class JavaDocInfoGenerator {
}
}
/**
* Finds doc comment immediately preceding package statement
*/
@Nullable
private static ASTNode findRelevantCommentNode(@NotNull ASTNode fileNode) {
ASTNode node = fileNode.findChildByType(JavaElementType.PACKAGE_STATEMENT);
if (node == null) node = fileNode.getLastChildNode();
while (node != null && node.getElementType() != JavaDocElementType.DOC_COMMENT) {
node = node.getTreePrev();
}
return node;
}
public void generateCommonSection(StringBuilder buffer, PsiDocComment docComment) {
generateDescription(buffer, docComment);
generateApiSection(buffer, docComment);
@@ -0,0 +1,8 @@
/**
* Copyright
*/
/**
* This package contains nothing but package-info.java
*/
package packageInfoWithCopyright;
@@ -0,0 +1,2 @@
<html><head> <style type="text/css"> #error { background-color: #eeeeee; margin-bottom: 10px; } p { margin: 5px 0; } </style></head><body>
This package contains nothing but package-info.java</body></html>
@@ -252,13 +252,21 @@ public class JavaDocInfoGeneratorTest extends CodeInsightTestCase {
}
public void testPackageInfoFromComment() throws Exception {
doTestPackageInfo("some");
}
public void testPackageInfoWithCopyright() throws Exception {
doTestPackageInfo("packageInfoWithCopyright");
}
private void doTestPackageInfo(String caretPositionedAt) throws Exception {
final String rootPath = getTestDataPath() + "/codeInsight/javadocIG/";
VirtualFile root = PsiTestUtil.createTestProjectStructure(myProject, myModule, rootPath, myFilesToDelete);
VirtualFile piFile = root.findFileByRelativePath("packageInfoFromComment/package-info.java");
VirtualFile piFile = root.findFileByRelativePath(getTestName(true) + "/package-info.java");
assertNotNull(piFile);
PsiFile psiFile = PsiManager.getInstance(myProject).findFile(piFile);
assertNotNull(psiFile);
final String info = JavaExternalDocumentationTest.getDocumentationText(psiFile, psiFile.getText().indexOf("some"));
final String info = JavaExternalDocumentationTest.getDocumentationText(psiFile, psiFile.getText().indexOf(caretPositionedAt));
String htmlText = FileUtil.loadFile(new File(rootPath + getTestName(true) + File.separator + "packageInfo.html"));
assertEquals(StringUtil.convertLineSeparators(htmlText.trim()), replaceEnvironmentDependentContent(info));
}