[java] javadoc in modules

This commit is contained in:
Roman Shevchenko
2016-09-12 17:13:22 +03:00
parent 71a05d01e7
commit 48ce294241
10 changed files with 108 additions and 10 deletions
@@ -365,7 +365,7 @@ public class JavaDocInfoGenerator {
return packageName;
}
public boolean generateDocInfoCore (StringBuilder buffer, boolean generatePrologueAndEpilogue) {
public boolean generateDocInfoCore(StringBuilder buffer, boolean generatePrologueAndEpilogue) {
if (myElement instanceof PsiClass) {
generateClassJavaDoc(buffer, (PsiClass)myElement, generatePrologueAndEpilogue);
}
@@ -387,7 +387,10 @@ public class JavaDocInfoGenerator {
generatePackageJavaDoc(buffer, aPackage, generatePrologueAndEpilogue);
}
else if (myElement instanceof PsiPackage) {
generatePackageJavaDoc(buffer, (PsiPackage) myElement, generatePrologueAndEpilogue);
generatePackageJavaDoc(buffer, (PsiPackage)myElement, generatePrologueAndEpilogue);
}
else if (myElement instanceof PsiJavaModule) {
generateModuleJavaDoc(buffer, (PsiJavaModule)myElement, generatePrologueAndEpilogue);
}
else {
return false;
@@ -723,6 +726,19 @@ public class JavaDocInfoGenerator {
}
}
private void generateModuleJavaDoc(StringBuilder buffer, PsiJavaModule module, boolean generatePrologueAndEpilogue) {
if (generatePrologueAndEpilogue) generatePrologue(buffer);
buffer.append("<pre>module <b>").append(module.getName()).append("</b></pre>");
PsiDocComment comment = module.getDocComment();
if (comment != null) {
generateCommonSection(buffer, comment);
}
if (generatePrologueAndEpilogue) generateEpilogue(buffer);
}
/**
* Finds doc comment immediately preceding package statement
*/
@@ -22,7 +22,7 @@ import org.jetbrains.annotations.NotNull;
*
* @since 2016.3
*/
public interface PsiJavaModule extends NavigatablePsiElement, PsiNamedElement {
public interface PsiJavaModule extends NavigatablePsiElement, PsiNamedElement, PsiJavaDocumentedElement {
String MODULE_INFO_CLASS = "module-info";
String MODULE_INFO_FILE = MODULE_INFO_CLASS + ".java";
String MODULE_INFO_CLS_FILE = MODULE_INFO_CLASS + ".class";
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -110,7 +110,9 @@ public class JavaParserUtil {
}
}
private static final TokenSet PRECEDING_COMMENT_SET = ElementType.FULL_MEMBER_BIT_SET;
private static final TokenSet PRECEDING_COMMENT_SET = TokenSet.orSet(
TokenSet.create(JavaElementType.MODULE), ElementType.FULL_MEMBER_BIT_SET);
private static final TokenSet TRAILING_COMMENT_SET = TokenSet.orSet(
TokenSet.create(JavaElementType.PACKAGE_STATEMENT),
ElementType.IMPORT_STATEMENT_BASE_BIT_SET, ElementType.FULL_MEMBER_BIT_SET, ElementType.JAVA_STATEMENT_BIT_SET);
@@ -61,7 +61,7 @@ public class ModuleParser {
parseModuleContent(builder);
}
module.done(JavaElementType.MODULE);
JavaParserUtil.done(module, JavaElementType.MODULE);
if (builder.getTokenType() != null) {
parseExtras(builder, JavaErrorMessages.message("unexpected.tokens"));
@@ -22,9 +22,11 @@ import com.intellij.psi.impl.java.stubs.PsiJavaModuleStub;
import com.intellij.psi.impl.source.SourceTreeToPsiMap;
import com.intellij.psi.impl.source.tree.JavaElementType;
import com.intellij.psi.impl.source.tree.TreeElement;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class ClsJavaModuleImpl extends ClsRepositoryPsiElement<PsiJavaModuleStub> implements PsiJavaModule {
private PsiJavaModuleReferenceElement myReference;
@@ -67,6 +69,12 @@ public class ClsJavaModuleImpl extends ClsRepositoryPsiElement<PsiJavaModuleStub
throw cannotModifyException(this);
}
@Nullable
@Override
public PsiDocComment getDocComment() {
return null;
}
@Override
public ItemPresentation getPresentation() {
return ItemPresentationProviders.getItemPresentation(this);
@@ -21,9 +21,11 @@ import com.intellij.navigation.ItemPresentationProviders;
import com.intellij.psi.*;
import com.intellij.psi.impl.java.stubs.JavaStubElementTypes;
import com.intellij.psi.impl.java.stubs.PsiJavaModuleStub;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class PsiJavaModuleImpl extends JavaStubPsiElement<PsiJavaModuleStub> implements PsiJavaModule {
public PsiJavaModuleImpl(@NotNull PsiJavaModuleStub stub) {
@@ -64,6 +66,12 @@ public class PsiJavaModuleImpl extends JavaStubPsiElement<PsiJavaModuleStub> imp
return this;
}
@Nullable
@Override
public PsiDocComment getDocComment() {
return PsiTreeUtil.getChildOfType(this, PsiDocComment.class);
}
@Override
public ItemPresentation getPresentation() {
return ItemPresentationProviders.getItemPresentation(this);
@@ -1,7 +1,7 @@
PsiJavaFile:Simple2.java
PsiComment(C_STYLE_COMMENT)('/* comment */')
PsiWhiteSpace('\n')
PsiJavaModule:X
PsiComment(C_STYLE_COMMENT)('/* comment */')
PsiWhiteSpace('\n')
PsiKeyword:module('module')
PsiWhiteSpace(' ')
PsiJavaModuleReference
@@ -0,0 +1,51 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInsight.javadoc
import com.intellij.openapi.util.JDOMUtil
import com.intellij.psi.PsiJavaFile
import com.intellij.testFramework.Assertions.assertThat
import com.intellij.testFramework.LightProjectDescriptor
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
class LightJavadocGeneratorTest : LightCodeInsightFixtureTestCase() {
override fun getProjectDescriptor(): LightProjectDescriptor = JAVA_9
fun testPlainModule() = doTestModule("module M.N { }", """
<body>
<pre>
module
<b>M.N</b>
</pre>
</body>""".trimIndent())
fun testDocumentedModule() = doTestModule("/** One humble module. */\nmodule M.N { }", """
<body>
<pre>
module
<b>M.N</b>
</pre>
One humble module.
</body>""".trimIndent())
private fun doTestModule(text: String, expected: String) {
val file = myFixture.configureByText("module-info.java", text)
val module = (file as PsiJavaFile).moduleDeclaration!!
val docInfo = JavaDocInfoGeneratorFactory.create(project, module).generateDocInfo(null)!!
val body = JDOMUtil.loadDocument(docInfo).rootElement.getChild("body")
assertThat(body).isEqualTo(expected)
}
}
@@ -0,0 +1,4 @@
/**
* A nice little module.
*/
module M { }
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,9 +16,13 @@
package com.siyeh.ig.javadoc;
import com.intellij.codeInspection.InspectionProfileEntry;
import com.intellij.openapi.roots.LanguageLevelModuleExtension;
import com.intellij.openapi.roots.ModuleRootModificationUtil;
import com.siyeh.ig.LightInspectionTestCase;
import org.jetbrains.annotations.Nullable;
import static com.intellij.pom.java.LanguageLevel.JDK_1_9;
/**
* @author Bas Leijdekkers
*/
@@ -32,9 +36,14 @@ public class DanglingJavadocInspectionTest extends LightInspectionTestCase {
doNamedTest("package-info");
}
public void testModuleInfo() {
ModuleRootModificationUtil.updateModel(myModule, m -> m.getModuleExtension(LanguageLevelModuleExtension.class).setLanguageLevel(JDK_1_9));
doNamedTest("module-info");
}
@Nullable
@Override
protected InspectionProfileEntry getInspection() {
return new DanglingJavadocInspection();
}
}
}