IDEA-111524 Javadoc code reformat generates <p> on JDK 8 instead <p/>, when generate <p/> on empty lines option is on [CR-IC-1950]

This commit is contained in:
Yaroslav Lepenkin
2013-08-16 13:27:21 +04:00
parent 6178ef20e7
commit 8ad9c40d1e
3 changed files with 83 additions and 7 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2013 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.
@@ -20,6 +20,7 @@ import com.intellij.lang.ASTNode;
import com.intellij.lang.java.JavaLanguage;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.LanguageLevelProjectExtension;
import com.intellij.openapi.util.text.LineTokenizer;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
@@ -45,7 +46,7 @@ public class CommentFormatter {
public CommentFormatter(@NotNull Project project) {
mySettings = CodeStyleSettingsManager.getSettings(project);
myParser = new JDParser(mySettings);
myParser = new JDParser(mySettings, LanguageLevelProjectExtension.getInstance(project).getLanguageLevel());
myProject = project;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2013 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.
@@ -17,6 +17,7 @@ package com.intellij.psi.impl.source.codeStyle.javadoc;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -39,9 +40,11 @@ public class JDParser {
private static final String SELF_CLOSED_P_TAG = "<p/>";
private final CodeStyleSettings mySettings;
private final LanguageLevel myLanguageLevel;
public JDParser(@NotNull CodeStyleSettings settings) {
public JDParser(@NotNull CodeStyleSettings settings, @NotNull LanguageLevel languageLevel) {
mySettings = settings;
myLanguageLevel = languageLevel;
}
private static final char lineSeparator = '\n';
@@ -514,7 +517,13 @@ public class JDParser {
if (line.length() == 0 && !mySettings.JD_KEEP_EMPTY_LINES) continue;
if (i != 0) sb.append(prefix);
if (line.length() == 0 && mySettings.JD_P_AT_EMPTY_LINES && !insidePreTag) {
sb.append(SELF_CLOSED_P_TAG);
if (myLanguageLevel.isAtLeast(LanguageLevel.JDK_1_8)) {
//Self-closing elements are not allowed for javadoc tool from JDK8
sb.append(P_START_TAG);
}
else {
sb.append(SELF_CLOSED_P_TAG);
}
}
else {
sb.append(line);
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
* Copyright 2000-2013 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.
@@ -17,6 +17,8 @@ package com.intellij.psi.formatter.java;
import com.intellij.lang.java.JavaLanguage;
import com.intellij.openapi.roots.LanguageLevelProjectExtension;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
/**
@@ -95,6 +97,7 @@ public class JavadocFormatterTest extends AbstractJavaFormatterTest {
getSettings().getRootSettings().ENABLE_JAVADOC_FORMATTING = true;
getSettings().getRootSettings().WRAP_COMMENTS = true;
getSettings().getRootSettings().RIGHT_MARGIN = 20;
LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(LanguageLevel.JDK_1_7);
doTextTest("/**\n" + " * <p />\n" + " * Another paragraph of the description placed after blank line.\n" + " */\n" + "class A{}",
"/**\n" +
@@ -107,11 +110,31 @@ public class JavadocFormatterTest extends AbstractJavaFormatterTest {
"class A {\n" +
"}");
}
public void testSCR2632_JDK8_LanguageLevel() throws Exception {
getSettings().getRootSettings().ENABLE_JAVADOC_FORMATTING = true;
getSettings().getRootSettings().WRAP_COMMENTS = true;
getSettings().getRootSettings().RIGHT_MARGIN = 20;
LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(LanguageLevel.JDK_1_8);
doTextTest("/**\n" + " * <p />\n" + " * Another paragraph of the description placed after blank line.\n" + " */\n" + "class A{}",
"/**\n" +
" * <p>\n" +
" * Another paragraph\n" +
" * of the description\n" +
" * placed after\n" +
" * blank line.\n" +
" */\n" +
"class A {\n" +
"}");
}
public void testParagraphTagGeneration() {
// Inspired by IDEA-61811
getSettings().getRootSettings().ENABLE_JAVADOC_FORMATTING = true;
getSettings().getRootSettings().JD_P_AT_EMPTY_LINES = true;
LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(LanguageLevel.JDK_1_7);
doTextTest(
"/**\n" +
" * line 1\n" +
@@ -618,4 +641,47 @@ public class JavadocFormatterTest extends AbstractJavaFormatterTest {
"}";
doTextTest(before, before);
}
public void testNotGenerateSelfClosingPTagIfLanguageLevelJava8() throws Exception {
getSettings().getRootSettings().JD_P_AT_EMPTY_LINES = true;
getSettings().getRootSettings().ENABLE_JAVADOC_FORMATTING = true;
String before = "/**\n" +
" * Super method\n" +
" *\n" +
" * Super multiple times\n" +
" */\n" +
"public void voo() {\n" +
"}\n";
String after = "/**\n" +
" * Super method\n" +
" * <p>\n" +
" * Super multiple times\n" +
" */\n" +
"public void voo() {\n" +
"}\n";
doClassTest(before, after);
}
public void testGenerateSelfClosingPTagIfLanguageLevelNotJava8() throws Exception {
getSettings().getRootSettings().JD_P_AT_EMPTY_LINES = true;
getSettings().getRootSettings().ENABLE_JAVADOC_FORMATTING = true;
LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(LanguageLevel.JDK_1_7);
String before = "/**\n" +
" * Super method\n" +
" *\n" +
" * Super multiple times\n" +
" */\n" +
"public void voo() {\n" +
"}\n";
String after = "/**\n" +
" * Super method\n" +
" * <p/>\n" +
" * Super multiple times\n" +
" */\n" +
"public void voo() {\n" +
"}\n";
doClassTest(before, after);
}
}