From db78ca92b213ff27a2f63d042136f2992c4ceb32 Mon Sep 17 00:00:00 2001 From: Junichi Yamamoto Date: Sat, 8 Jun 2024 07:34:22 +0900 Subject: [PATCH] Fix the formatter for no curly "if" statement with a line comment [GH-7185] - https://github.com/apache/netbeans/issues/7185 - Don't add the `WHITESPACE_BETWEEN_LINE_COMMENTS` token - Add unit tests Example: ```php if (true) $example = 1; // comment1 // comment2 ``` Before: ```php if (true) $example = 1; // comment1 new line is added here // comment2 ``` After: ```php if (true) $example = 1; // comment1 // comment2 ``` --- .../php/editor/indent/FormatVisitor.java | 11 ++++++- .../testfiles/formatting/issueGH7185_01.php | 24 ++++++++++++++ .../formatting/issueGH7185_01.php.formatted | 25 +++++++++++++++ .../testfiles/formatting/issueGH7185_02.php | 27 ++++++++++++++++ .../formatting/issueGH7185_02.php.formatted | 27 ++++++++++++++++ .../testfiles/formatting/issueGH7185_03.php | 29 +++++++++++++++++ .../formatting/issueGH7185_03.php.formatted | 30 +++++++++++++++++ .../testfiles/formatting/issueGH7185_04.php | 31 ++++++++++++++++++ .../formatting/issueGH7185_04.php.formatted | 31 ++++++++++++++++++ .../testfiles/formatting/issueGH7185_05.php | 32 +++++++++++++++++++ .../formatting/issueGH7185_05.php.formatted | 31 ++++++++++++++++++ .../php/editor/indent/PHPFormatterTest.java | 25 +++++++++++++++ 12 files changed, 322 insertions(+), 1 deletion(-) create mode 100644 php/php.editor/test/unit/data/testfiles/formatting/issueGH7185_01.php create mode 100644 php/php.editor/test/unit/data/testfiles/formatting/issueGH7185_01.php.formatted create mode 100644 php/php.editor/test/unit/data/testfiles/formatting/issueGH7185_02.php create mode 100644 php/php.editor/test/unit/data/testfiles/formatting/issueGH7185_02.php.formatted create mode 100644 php/php.editor/test/unit/data/testfiles/formatting/issueGH7185_03.php create mode 100644 php/php.editor/test/unit/data/testfiles/formatting/issueGH7185_03.php.formatted create mode 100644 php/php.editor/test/unit/data/testfiles/formatting/issueGH7185_04.php create mode 100644 php/php.editor/test/unit/data/testfiles/formatting/issueGH7185_04.php.formatted create mode 100644 php/php.editor/test/unit/data/testfiles/formatting/issueGH7185_05.php create mode 100644 php/php.editor/test/unit/data/testfiles/formatting/issueGH7185_05.php.formatted 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); + } + }