diff --git a/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/expected/javadoc-with-format-off.txt b/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/expected/javadoc-with-format-off.txt new file mode 100644 index 00000000..157ea11b --- /dev/null +++ b/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/expected/javadoc-with-format-off.txt @@ -0,0 +1,8 @@ +/** + * This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. + * @formatter:off + * @formatter:on + */ +public class Format { + +} diff --git a/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/source/javadoc-with-format-off.txt b/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/source/javadoc-with-format-off.txt new file mode 100644 index 00000000..dbfef5c0 --- /dev/null +++ b/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/source/javadoc-with-format-off.txt @@ -0,0 +1,7 @@ +/** + * This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. + * @formatter:off + * @formatter:on + */ +public class Format { +} diff --git a/spring-javaformat/spring-javaformat-formatter/src/main/java/io/spring/javaformat/formatter/jdk17/eclipse/JavadocLineBreakPreparator.java b/spring-javaformat/spring-javaformat-formatter/src/main/java/io/spring/javaformat/formatter/jdk17/eclipse/JavadocLineBreakPreparator.java index f2e273bf..e0dd5d8c 100644 --- a/spring-javaformat/spring-javaformat-formatter/src/main/java/io/spring/javaformat/formatter/jdk17/eclipse/JavadocLineBreakPreparator.java +++ b/spring-javaformat/spring-javaformat-formatter/src/main/java/io/spring/javaformat/formatter/jdk17/eclipse/JavadocLineBreakPreparator.java @@ -93,7 +93,9 @@ private static class Vistor extends ASTVisitor { public boolean visit(Javadoc node) { int commentIndex = this.tokenManager.firstIndexIn(node, TerminalTokens.TokenNameCOMMENT_JAVADOC); Token commentToken = this.tokenManager.get(commentIndex); - this.commentTokenManager = new TokenManager(commentToken.getInternalStructure(), this.tokenManager); + this.commentTokenManager = (commentToken.getInternalStructure() != null) + ? new TokenManager(commentToken.getInternalStructure(), this.tokenManager) + : null; this.declaration = node.getParent(); this.firstTagElement = true; this.hasText = false; @@ -108,7 +110,7 @@ public boolean visit(TextElement node) { @Override public boolean visit(TagElement node) { - if (isSquashRequired(node, this.declaration)) { + if (this.commentTokenManager != null && isSquashRequired(node, this.declaration)) { int startIndex = this.commentTokenManager.findIndex(node.getStartPosition(), -1, false); Token token = this.commentTokenManager.get(startIndex); token.clearLineBreaksBefore(); diff --git a/spring-javaformat/spring-javaformat-formatter/src/main/java/io/spring/javaformat/formatter/jdk8/eclipse/JavadocLineBreakPreparator.java b/spring-javaformat/spring-javaformat-formatter/src/main/java/io/spring/javaformat/formatter/jdk8/eclipse/JavadocLineBreakPreparator.java index 68723201..4113bbf2 100644 --- a/spring-javaformat/spring-javaformat-formatter/src/main/java/io/spring/javaformat/formatter/jdk8/eclipse/JavadocLineBreakPreparator.java +++ b/spring-javaformat/spring-javaformat-formatter/src/main/java/io/spring/javaformat/formatter/jdk8/eclipse/JavadocLineBreakPreparator.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2023 the original author or authors. + * Copyright 2017-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -93,7 +93,9 @@ private static class Vistor extends ASTVisitor { public boolean visit(Javadoc node) { int commentIndex = this.tokenManager.firstIndexIn(node, TerminalTokens.TokenNameCOMMENT_JAVADOC); Token commentToken = this.tokenManager.get(commentIndex); - this.commentTokenManager = new TokenManager(commentToken.getInternalStructure(), this.tokenManager); + this.commentTokenManager = (commentToken.getInternalStructure() != null) + ? new TokenManager(commentToken.getInternalStructure(), this.tokenManager) + : null; this.declaration = node.getParent(); this.firstTagElement = true; this.hasText = false; @@ -108,7 +110,7 @@ public boolean visit(TextElement node) { @Override public boolean visit(TagElement node) { - if (isSquashRequired(node, this.declaration)) { + if (this.commentTokenManager != null && isSquashRequired(node, this.declaration)) { int startIndex = this.commentTokenManager.findIndex(node.getStartPosition(), -1, false); Token token = this.commentTokenManager.get(startIndex); token.clearLineBreaksBefore();