diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/indent/FormatVisitor.java b/php/php.editor/src/org/netbeans/modules/php/editor/indent/FormatVisitor.java index 509b8030d356..2e5c0c83ada0 100644 --- a/php/php.editor/src/org/netbeans/modules/php/editor/indent/FormatVisitor.java +++ b/php/php.editor/src/org/netbeans/modules/php/editor/indent/FormatVisitor.java @@ -2659,7 +2659,16 @@ private void addFormatToken(List tokens) { } tokens.add(new FormatToken(FormatToken.Kind.WHITESPACE_INDENT, newOffset, "\n" + ts.token().text().toString())); if (ts.moveNext() && ts.token().id() == PHPTokenId.PHP_LINE_COMMENT) { - tokens.add(new FormatToken(FormatToken.Kind.WHITESPACE_BETWEEN_LINE_COMMENTS, ts.offset())); + ASTNode parent = path.get(0); + if (!(parent instanceof IfStatement) || isCurly) { + // GH-7185 don't add this to an "if" statement without curly braces + // e.g. + // if (true) + // $example = 1; // comment1 + // + // // comment2 + tokens.add(new FormatToken(FormatToken.Kind.WHITESPACE_BETWEEN_LINE_COMMENTS, ts.offset())); + } } // #268710 for adding/checking the PHP_LINE_COMMENT token later ts.movePrevious(); diff --git a/php/php.editor/test/unit/data/testfiles/formatting/issueGH7185_01.php b/php/php.editor/test/unit/data/testfiles/formatting/issueGH7185_01.php new file mode 100644 index 000000000000..280a5c023cbb --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/formatting/issueGH7185_01.php @@ -0,0 +1,24 @@ + options = new HashMap<>(FmtOptions.getDefaults()); + reformatFileContents("testfiles/formatting/issueGH7185_01.php", options, false, false); + } + + public void testGH7185_02() throws Exception { + HashMap options = new HashMap<>(FmtOptions.getDefaults()); + reformatFileContents("testfiles/formatting/issueGH7185_02.php", options, false, false); + } + + public void testGH7185_03() throws Exception { + HashMap options = new HashMap<>(FmtOptions.getDefaults()); + reformatFileContents("testfiles/formatting/issueGH7185_03.php", options, false, false); + } + + public void testGH7185_04() throws Exception { + HashMap options = new HashMap<>(FmtOptions.getDefaults()); + reformatFileContents("testfiles/formatting/issueGH7185_04.php", options, false, false); + } + + public void testGH7185_05() throws Exception { + HashMap options = new HashMap<>(FmtOptions.getDefaults()); + reformatFileContents("testfiles/formatting/issueGH7185_05.php", options, false, false); + } + }