Issue #3445: GUI modified to show javadoc tree as subtree of COMMENT_CONTENT token
This commit is contained in:
parent
0fb94f14ef
commit
4e684926cf
21
pom.xml
21
pom.xml
|
|
@ -1409,7 +1409,7 @@
|
|||
<haltOnFailure>true</haltOnFailure>
|
||||
<branchRate>100</branchRate>
|
||||
<lineRate>100</lineRate>
|
||||
<totalBranchRate>92</totalBranchRate>
|
||||
<totalBranchRate>91</totalBranchRate>
|
||||
<totalLineRate>98</totalLineRate>
|
||||
<regexes>
|
||||
<regex>
|
||||
|
|
@ -1422,12 +1422,29 @@
|
|||
<branchRate>79</branchRate>
|
||||
<lineRate>97</lineRate>
|
||||
</regex>
|
||||
<regex>
|
||||
<pattern>com.puppycrawl.tools.checkstyle.gui.ParseTreeTablePModel</pattern>
|
||||
<branchRate>41</branchRate>
|
||||
<lineRate>44</lineRate>
|
||||
</regex>
|
||||
</regexes>
|
||||
</check>
|
||||
<instrumentation>
|
||||
<excludes>
|
||||
<exclude>com/puppycrawl/tools/checkstyle/grammars/javadoc/*.class</exclude>
|
||||
<exclude>com/puppycrawl/tools/checkstyle/gui/*.class</exclude>
|
||||
<!-- Swing related classes -->
|
||||
<exclude>com/puppycrawl/tools/checkstyle/gui/BaseCellEditor*.class</exclude>
|
||||
<exclude>com/puppycrawl/tools/checkstyle/gui/CodeSelector*.class</exclude>
|
||||
<exclude>com/puppycrawl/tools/checkstyle/gui/JTreeTable*.class</exclude>
|
||||
<exclude>com/puppycrawl/tools/checkstyle/gui/ListToTreeSelectionModelWrapper*.class</exclude>
|
||||
<exclude>com/puppycrawl/tools/checkstyle/gui/Main*.class</exclude>
|
||||
<exclude>com/puppycrawl/tools/checkstyle/gui/MainFrame*.class</exclude>
|
||||
<exclude>com/puppycrawl/tools/checkstyle/gui/ParseTreeTableModel*.class</exclude>
|
||||
<exclude>com/puppycrawl/tools/checkstyle/gui/TreeTableCellRenderer*.class</exclude>
|
||||
<exclude>com/puppycrawl/tools/checkstyle/gui/TreeTableModelAdapter*.class</exclude>
|
||||
<!-- GUI model classes without tests -->
|
||||
<exclude>com/puppycrawl/tools/checkstyle/gui/CodeSelectorPModel*.class</exclude>
|
||||
<exclude>com/puppycrawl/tools/checkstyle/gui/MainFrameModel*.class</exclude>
|
||||
<!-- deprecated classes -->
|
||||
<exclude>com/puppycrawl/tools/checkstyle/checks/AbstractFormatCheck.class</exclude>
|
||||
<exclude>com/puppycrawl/tools/checkstyle/checks/AbstractDeclarationCollector*.class</exclude>
|
||||
|
|
|
|||
|
|
@ -182,7 +182,16 @@ public class ParseTreeTablePModel {
|
|||
result = ((DetailNode) parent).getChildren().length;
|
||||
}
|
||||
else {
|
||||
result = ((DetailAST) parent).getChildCount();
|
||||
if (parseMode == ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS
|
||||
&& ((AST) parent).getType() == TokenTypes.COMMENT_CONTENT
|
||||
&& JavadocUtils.isJavadocComment(((DetailAST) parent).getParent())) {
|
||||
//getChildCount return 0 on COMMENT_CONTENT,
|
||||
//but we need to attach javadoc tree, that is separate tree
|
||||
result = 1;
|
||||
}
|
||||
else {
|
||||
result = ((DetailAST) parent).getChildCount();
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
|
|
@ -255,20 +264,22 @@ public class ParseTreeTablePModel {
|
|||
* and parseMode is JAVA_WITH_JAVADOC_AND_COMMENTS.
|
||||
*/
|
||||
private Object getChildAtDetailAst(DetailAST parent, int index) {
|
||||
int currentIndex = 0;
|
||||
DetailAST child = parent.getFirstChild();
|
||||
while (currentIndex < index) {
|
||||
child = child.getNextSibling();
|
||||
currentIndex++;
|
||||
}
|
||||
|
||||
Object result = child;
|
||||
|
||||
final Object result;
|
||||
if (parseMode == ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS
|
||||
&& child.getType() == TokenTypes.BLOCK_COMMENT_BEGIN
|
||||
&& JavadocUtils.isJavadocComment(child)) {
|
||||
result = getJavadocTree(child);
|
||||
&& parent.getType() == TokenTypes.COMMENT_CONTENT
|
||||
&& JavadocUtils.isJavadocComment(parent.getParent())) {
|
||||
result = getJavadocTree(parent.getParent());
|
||||
}
|
||||
else {
|
||||
int currentIndex = 0;
|
||||
DetailAST child = parent.getFirstChild();
|
||||
while (currentIndex < index) {
|
||||
child = child.getNextSibling();
|
||||
currentIndex++;
|
||||
}
|
||||
result = child;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
|||
Binary file not shown.
|
After Width: | Height: | Size: 62 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 76 KiB After Width: | Height: | Size: 59 KiB |
|
|
@ -0,0 +1,173 @@
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
// checkstyle: Checks Java source code for adherence to a set of rules.
|
||||
// Copyright (C) 2001-2016 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package com.puppycrawl.tools.checkstyle.gui;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import antlr.collections.AST;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.TreeWalker;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.api.DetailAST;
|
||||
import com.puppycrawl.tools.checkstyle.api.DetailNode;
|
||||
import com.puppycrawl.tools.checkstyle.api.FileContents;
|
||||
import com.puppycrawl.tools.checkstyle.api.FileText;
|
||||
import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
|
||||
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.gui.MainFrameModel.ParseMode;
|
||||
|
||||
public class ParseTreeTablePModelTest {
|
||||
|
||||
private DetailAST tree;
|
||||
|
||||
public static String getPath(String filename) {
|
||||
return "src/test/resources/com/puppycrawl/tools/checkstyle/gui/" + filename;
|
||||
}
|
||||
|
||||
public static DetailAST parseFile(File file) throws Exception {
|
||||
final FileContents contents = new FileContents(
|
||||
new FileText(file.getAbsoluteFile(), System.getProperty("file.encoding", "UTF-8")));
|
||||
return TreeWalker.parseWithComments(contents);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void loadTree() throws Exception {
|
||||
tree = parseFile(
|
||||
new File(getPath("InputJavadocAttributesAndMethods.java")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRoot() {
|
||||
final Object root = new ParseTreeTablePModel(tree).getRoot();
|
||||
final int childCount = new ParseTreeTablePModel(null).getChildCount(root);
|
||||
Assert.assertEquals(1, childCount);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChildCount() {
|
||||
final int childCount = new ParseTreeTablePModel(null).getChildCount(tree);
|
||||
Assert.assertEquals(5, childCount);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChildCountInJavaAndJavadocMode() {
|
||||
final ParseTreeTablePModel parseTree = new ParseTreeTablePModel(null);
|
||||
parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
|
||||
final int childCount = parseTree.getChildCount(tree);
|
||||
Assert.assertEquals(5, childCount);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChild() {
|
||||
final Object child = new ParseTreeTablePModel(null).getChild(tree, 1);
|
||||
Assert.assertTrue(child instanceof DetailAST);
|
||||
Assert.assertEquals(TokenTypes.BLOCK_COMMENT_BEGIN, ((AST) child).getType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChildInJavaAndJavadocMode() {
|
||||
final ParseTreeTablePModel parseTree = new ParseTreeTablePModel(null);
|
||||
parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
|
||||
final Object child = parseTree.getChild(tree, 1);
|
||||
Assert.assertTrue(child instanceof DetailAST);
|
||||
Assert.assertEquals(TokenTypes.BLOCK_COMMENT_BEGIN, ((AST) child).getType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCommentChildCount() {
|
||||
final DetailAST commentContentNode = tree.getFirstChild().getNextSibling().getFirstChild();
|
||||
final ParseTreeTablePModel parseTree = new ParseTreeTablePModel(null);
|
||||
parseTree.setParseMode(ParseMode.JAVA_WITH_COMMENTS);
|
||||
final int javadocCommentChildCount = parseTree.getChildCount(commentContentNode);
|
||||
Assert.assertEquals(0, javadocCommentChildCount);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCommentChildCountInJavaAndJavadocMode() {
|
||||
final ParseTreeTablePModel parseTree = new ParseTreeTablePModel(null);
|
||||
parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
|
||||
final DetailAST commentContentNode = tree.getLastChild().getLastChild()
|
||||
.getPreviousSibling().getLastChild().getFirstChild().getFirstChild();
|
||||
final int commentChildCount = parseTree.getChildCount(commentContentNode);
|
||||
Assert.assertEquals(0, commentChildCount);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCommentChildInJavaAndJavadocMode() {
|
||||
final ParseTreeTablePModel parseTree = new ParseTreeTablePModel(null);
|
||||
parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
|
||||
final DetailAST commentContentNode = tree.getLastChild().getLastChild()
|
||||
.getPreviousSibling().getLastChild().getFirstChild().getFirstChild();
|
||||
final Object commentChild = parseTree.getChild(commentContentNode, 0);
|
||||
Assert.assertNull(commentChild);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJavadocCommentChildCount() {
|
||||
final DetailAST commentContentNode = tree.getFirstChild().getNextSibling().getFirstChild();
|
||||
final ParseTreeTablePModel parseTree = new ParseTreeTablePModel(null);
|
||||
final int commentChildCount = parseTree.getChildCount(commentContentNode);
|
||||
Assert.assertEquals(0, commentChildCount);
|
||||
parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
|
||||
final int javadocCommentChildCount = parseTree.getChildCount(commentContentNode);
|
||||
Assert.assertEquals(1, javadocCommentChildCount);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJavadocCommentChild() {
|
||||
final DetailAST commentContentNode = tree.getFirstChild().getNextSibling().getFirstChild();
|
||||
final ParseTreeTablePModel parseTree = new ParseTreeTablePModel(null);
|
||||
parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
|
||||
final Object child = parseTree.getChild(commentContentNode, 0);
|
||||
Assert.assertTrue(child instanceof DetailNode);
|
||||
Assert.assertEquals(JavadocTokenTypes.JAVADOC, ((DetailNode) child).getType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJavadocChildCount() {
|
||||
final DetailAST commentContentNode = tree.getFirstChild().getNextSibling().getFirstChild();
|
||||
final ParseTreeTablePModel parseTree = new ParseTreeTablePModel(null);
|
||||
parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
|
||||
final Object javadoc = parseTree.getChild(commentContentNode, 0);
|
||||
Assert.assertTrue(javadoc instanceof DetailNode);
|
||||
Assert.assertEquals(JavadocTokenTypes.JAVADOC, ((DetailNode) javadoc).getType());
|
||||
final int javadocChildCount = parseTree.getChildCount(javadoc);
|
||||
Assert.assertEquals(5, javadocChildCount);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJavadocChild() {
|
||||
final DetailAST commentContentNode = tree.getFirstChild().getNextSibling().getFirstChild();
|
||||
final ParseTreeTablePModel parseTree = new ParseTreeTablePModel(null);
|
||||
parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
|
||||
final Object javadoc = parseTree.getChild(commentContentNode, 0);
|
||||
Assert.assertTrue(javadoc instanceof DetailNode);
|
||||
Assert.assertEquals(JavadocTokenTypes.JAVADOC, ((DetailNode) javadoc).getType());
|
||||
final Object javadocChild = parseTree.getChild(javadoc, 2);
|
||||
Assert.assertTrue(javadoc instanceof DetailNode);
|
||||
Assert.assertEquals(JavadocTokenTypes.TEXT, ((DetailNode) javadocChild).getType());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
/**
|
||||
* class javadoc
|
||||
*/
|
||||
class InputJavadocAttributesAndMethods {
|
||||
|
||||
/** attribute javadoc*/
|
||||
int attribute;
|
||||
|
||||
/**
|
||||
* method javadoc
|
||||
*/
|
||||
public void method() {
|
||||
/* just comment */
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -146,6 +146,20 @@ java -cp checkstyle-${projectVersion}-all.jar com.puppycrawl.tools.checkstyle.gu
|
|||
(a node that represents a class definition) while you will see token
|
||||
types like IDENT (an identifier) near the leaves of the tree. </p>
|
||||
|
||||
<p>
|
||||
In the bottom of frame you can find buttons "Open File", "Reload File"
|
||||
and dropdown list with parse modes to choose. First one opens file
|
||||
choose window. After choosing file tree that corresponds to java source
|
||||
file builds in frame. Notice that only files with ".java" extension
|
||||
can be opened. Second one reloads chosen file from file system and
|
||||
rebuilds source code tree. Dropdown list allow to choose one of three
|
||||
parse modes: "PLAIN JAVA", "JAVA WITH COMMENTS", "JAVA WITH JAVADOC AND COMMENTS".
|
||||
"PLAIN JAVA" mode uses to show java source code without comments. In
|
||||
"JAVA WITH COMMENTS" you can see also comments blocks on the tree. When
|
||||
"JAVA WITH JAVADOC AND COMMENTS" chosen javadoc tree builds and attaches
|
||||
for every comment block, that contains javadoc.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Notice: text of a tree node and its children is selected automaticaly
|
||||
after either pressing "Enter" or double-clicking on it, so there is no
|
||||
|
|
|
|||
|
|
@ -528,7 +528,28 @@ JAVADOC -> <p> First </p>\r\n<p> Second </p><EOF> [0:0]
|
|||
</section>
|
||||
|
||||
<section name="Checkstyle SDK GUI">
|
||||
Not implemented yet. See <a href="https://github.com/checkstyle/checkstyle/issues/408">Github Issue #408</a>.
|
||||
<p>
|
||||
Checkstyle GUI allows to showing javadoc tree in java files. To run in use
|
||||
</p>
|
||||
<source>
|
||||
java -cp checkstyle-${projectVersion}-all.jar com.puppycrawl.tools.checkstyle.gui.Main
|
||||
</source>
|
||||
<p>
|
||||
and choose "JAVA WITH JAVADOC MODE" in dropdown list in bottom of frame.
|
||||
</p>
|
||||
<p>
|
||||
Now you can see parsed javadoc tree as child of comment block.
|
||||
</p>
|
||||
<p>
|
||||
<img alt="screenshot" src="images/gui_javadoc_screenshot.png"/>
|
||||
</p>
|
||||
<p>
|
||||
Notice that only files with ".java" extension can be opened.
|
||||
</p>
|
||||
<p>
|
||||
For detail reference you can see
|
||||
<a href="http://checkstyle.sourceforge.net/writingchecks.html#The_Checkstyle_SDK_Gui">Checkstyle GUI documentation</a>.
|
||||
</p>
|
||||
</section>
|
||||
|
||||
<section name="Customize token types in Javadoc Checks">
|
||||
|
|
|
|||
Loading…
Reference in New Issue