Issue #3507: column number in first javadoc line fixed

This commit is contained in:
kazachka 2017-01-05 18:08:54 +03:00 committed by rnveach
parent 9dab0e74eb
commit 2852e45bf6
7 changed files with 44 additions and 16 deletions

View File

@ -41,6 +41,9 @@ public final class DetailNodeTreeStringPrinter {
/** OS specific line separator. */
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
/** Symbols with which javadoc starts. */
private static final String JAVADOC_START = "/**";
/** Prevent instances. */
private DetailNodeTreeStringPrinter() {
// no code
@ -182,13 +185,15 @@ public final class DetailNodeTreeStringPrinter {
blockCommentBegin.setType(TokenTypes.BLOCK_COMMENT_BEGIN);
blockCommentBegin.setText("/*");
blockCommentBegin.setLineNo(0);
blockCommentBegin.setColumnNo(0);
blockCommentBegin.setColumnNo(-JAVADOC_START.length());
final DetailAST commentContent = new DetailAST();
commentContent.setType(TokenTypes.COMMENT_CONTENT);
commentContent.setText("*" + content);
commentContent.setLineNo(0);
commentContent.setColumnNo(2);
// javadoc should starts at 0 column, so COMMENT_CONTENT node
// that contains javadoc identificator has -1 column
commentContent.setColumnNo(-1);
final DetailAST blockCommentEnd = new DetailAST();
blockCommentEnd.setType(TokenTypes.BLOCK_COMMENT_END);

View File

@ -78,6 +78,9 @@ public class JavadocDetailNodeParser {
public static final String MSG_KEY_UNRECOGNIZED_ANTLR_ERROR =
"javadoc.unrecognized.antlr.error";
/** Symbols with which javadoc starts. */
private static final String JAVADOC_START = "/**";
/**
* Line number of the Block comment AST that is being parsed.
*/
@ -96,6 +99,7 @@ public class JavadocDetailNodeParser {
*/
public ParseStatus parseJavadocAsDetailNode(DetailAST javadocCommentAst) {
blockCommentLineNumber = javadocCommentAst.getLineNo();
final String javadocComment = JavadocUtils.getJavadocCommentContent(javadocCommentAst);
// Use a new error listener each time to be able to use
@ -114,6 +118,10 @@ public class JavadocDetailNodeParser {
final ParseTree parseTree = parseJavadocAsParseTree(javadocComment);
final DetailNode tree = convertParseTreeToDetailNode(parseTree);
// adjust first line to indent of /**
adjustFirstLineToJavadocIndent(tree,
javadocCommentAst.getColumnNo()
+ JAVADOC_START.length());
result.setTree(tree);
}
catch (ParseCancellationException ex) {
@ -303,6 +311,21 @@ public class JavadocDetailNodeParser {
return node;
}
/**
* Adjust first line nodes to javadoc indent.
* @param tree DetailNode tree root
* @param javadocColumnNumber javadoc indent
*/
private void adjustFirstLineToJavadocIndent(DetailNode tree, int javadocColumnNumber) {
if (tree.getLineNumber() == blockCommentLineNumber) {
((JavadocNodeImpl) tree).setColumnNumber(tree.getColumnNumber() + javadocColumnNumber);
final DetailNode[] children = tree.getChildren();
for (DetailNode child : children) {
adjustFirstLineToJavadocIndent(child, javadocColumnNumber);
}
}
}
/**
* Gets line number from ParseTree node.
* @param tree

View File

@ -101,7 +101,7 @@ public class CodeSelectorPModelTest {
.getChild(tree.getFirstChild().getNextSibling().getFirstChild(), 0);
final CodeSelectorPModel selector = new CodeSelectorPModel(javadoc, linesToPosition);
selector.findSelectionPositions();
Assert.assertEquals(0, selector.getSelectionStart());
Assert.assertEquals(3, selector.getSelectionStart());
Assert.assertEquals(25, selector.getSelectionEnd());
}

View File

@ -259,7 +259,7 @@ public class ParseTreeTablePModelTest {
Assert.assertNull(treeModel);
Assert.assertEquals("JAVADOC", type);
Assert.assertEquals(1, line);
Assert.assertEquals(0, column);
Assert.assertEquals(3, column);
Assert.assertEquals(expectedText, text);
try {

View File

@ -2,8 +2,8 @@ CLASS_DEF -> CLASS_DEF [4:0]
|--MODIFIERS -> MODIFIERS [4:0]
|--BLOCK_COMMENT_BEGIN -> /* [1:0]
| |--COMMENT_CONTENT -> *\n* class javadoc\n [1:2]
| | `--JAVADOC -> \n* class javadoc\n<EOF> [1:0]
| | |--NEWLINE -> \n [1:0]
| | `--JAVADOC -> \n* class javadoc\n<EOF> [1:3]
| | |--NEWLINE -> \n [1:3]
| | |--LEADING_ASTERISK -> * [2:0]
| | |--TEXT -> class javadoc [2:1]
| | |--NEWLINE -> \n [2:15]
@ -18,9 +18,9 @@ CLASS_DEF -> CLASS_DEF [4:0]
| |--TYPE -> TYPE [7:4]
| | |--BLOCK_COMMENT_BEGIN -> /* [6:4]
| | | |--COMMENT_CONTENT -> * attribute javadoc [6:6]
| | | | `--JAVADOC -> attribute javadoc<EOF> [6:0]
| | | | |--TEXT -> attribute javadoc [6:0]
| | | | `--EOF -> <EOF> [6:18]
| | | | `--JAVADOC -> attribute javadoc<EOF> [6:7]
| | | | |--TEXT -> attribute javadoc [6:7]
| | | | `--EOF -> <EOF> [6:25]
| | | `--BLOCK_COMMENT_END -> */ [6:24]
| | `--LITERAL_INT -> int [7:4]
| |--IDENT -> attribute [7:8]
@ -29,8 +29,8 @@ CLASS_DEF -> CLASS_DEF [4:0]
| |--MODIFIERS -> MODIFIERS [12:4]
| | |--BLOCK_COMMENT_BEGIN -> /* [9:4]
| | | |--COMMENT_CONTENT -> *\n * method javadoc\n [9:6]
| | | | `--JAVADOC -> \n * method javadoc\n <EOF> [9:0]
| | | | |--NEWLINE -> \n [9:0]
| | | | `--JAVADOC -> \n * method javadoc\n <EOF> [9:7]
| | | | |--NEWLINE -> \n [9:7]
| | | | |--LEADING_ASTERISK -> * [10:0]
| | | | |--TEXT -> method javadoc [10:5]
| | | | |--NEWLINE -> \n [10:20]

View File

@ -2,8 +2,8 @@ CLASS_DEF -> CLASS_DEF [4:0]
|--MODIFIERS -> MODIFIERS [4:0]
| |--BLOCK_COMMENT_BEGIN -> /* [1:0]
| | |--COMMENT_CONTENT -> *\n* some text\n [1:2]
| | | `--JAVADOC -> \n* some text\n<EOF> [1:0]
| | | |--NEWLINE -> \n [1:0]
| | | `--JAVADOC -> \n* some text\n<EOF> [1:3]
| | | |--NEWLINE -> \n [1:3]
| | | |--LEADING_ASTERISK -> * [2:0]
| | | |--TEXT -> some text [2:1]
| | | |--NEWLINE -> \n [2:11]

View File

@ -12,9 +12,9 @@ CLASS_DEF -> CLASS_DEF [4:0]
|--MODIFIERS -> MODIFIERS [4:0]
|--BLOCK_COMMENT_BEGIN -> /* [3:0]
| |--COMMENT_CONTENT -> *javadoc [3:2]
| | `--JAVADOC -> javadoc<EOF> [3:0]
| | |--TEXT -> javadoc [3:0]
| | `--EOF -> <EOF> [3:7]
| | `--JAVADOC -> javadoc<EOF> [3:3]
| | |--TEXT -> javadoc [3:3]
| | `--EOF -> <EOF> [3:10]
| `--BLOCK_COMMENT_END -> */ [3:9]
|--LITERAL_CLASS -> class [4:0]
|--IDENT -> InputAstTreeStringPrinterJavadoc [4:6]