diff --git a/php.editor/src/org/netbeans/modules/php/editor/indent/CodeStyle.java b/php.editor/src/org/netbeans/modules/php/editor/indent/CodeStyle.java --- a/php.editor/src/org/netbeans/modules/php/editor/indent/CodeStyle.java +++ b/php.editor/src/org/netbeans/modules/php/editor/indent/CodeStyle.java @@ -444,6 +444,10 @@ return preferences.getBoolean(SPACE_BEFORE_CLOSE_PHP_TAG, getDefaultAsBoolean(SPACE_BEFORE_CLOSE_PHP_TAG)); } + public boolean spaceBetweenOpenPHPTagAndNamespace() { + return preferences.getBoolean(SPACE_BETWEEN_OPEN_PHP_TAG_AND_NAMESPACE, getDefaultAsBoolean(SPACE_BETWEEN_OPEN_PHP_TAG_AND_NAMESPACE)); + } + // alignment public boolean alignMultilineMethodParams() { return preferences.getBoolean(ALIGN_MULTILINE_METHOD_PARAMS, getDefaultAsBoolean(ALIGN_MULTILINE_METHOD_PARAMS)); diff --git a/php.editor/src/org/netbeans/modules/php/editor/indent/FmtOptions.java b/php.editor/src/org/netbeans/modules/php/editor/indent/FmtOptions.java --- a/php.editor/src/org/netbeans/modules/php/editor/indent/FmtOptions.java +++ b/php.editor/src/org/netbeans/modules/php/editor/indent/FmtOptions.java @@ -179,6 +179,7 @@ public static final String SPACE_CHECK_AFTER_KEYWORDS = "spaceCheckAfterKeywords"; //NOI18N public static final String SPACE_AFTER_SHORT_PHP_TAG = "spaceAfterShortPHPTag"; //NOI18N public static final String SPACE_BEFORE_CLOSE_PHP_TAG = "spaceBeforeClosePHPTag"; //NOI18N + public static final String SPACE_BETWEEN_OPEN_PHP_TAG_AND_NAMESPACE = "spaceBetweenOpenPHPTagAndNamespace"; //NOI18N public static final String PLACE_ELSE_ON_NEW_LINE = "placeElseOnNewLine"; //NOI18N public static final String PLACE_WHILE_ON_NEW_LINE = "placeWhileOnNewLine"; //NOI18N public static final String PLACE_CATCH_ON_NEW_LINE = "placeCatchOnNewLine"; //NOI18N @@ -340,6 +341,7 @@ {SPACE_CHECK_AFTER_KEYWORDS, TRUE}, {SPACE_AFTER_SHORT_PHP_TAG, TRUE}, {SPACE_BEFORE_CLOSE_PHP_TAG, TRUE}, + {SPACE_BETWEEN_OPEN_PHP_TAG_AND_NAMESPACE, FALSE}, {ALIGN_MULTILINE_METHOD_PARAMS, FALSE}, //NOI18N {ALIGN_MULTILINE_CALL_ARGS, FALSE}, //NOI18N {ALIGN_MULTILINE_IMPLEMENTS, FALSE}, //NOI18N diff --git a/php.editor/src/org/netbeans/modules/php/editor/indent/TokenFormatter.java b/php.editor/src/org/netbeans/modules/php/editor/indent/TokenFormatter.java --- a/php.editor/src/org/netbeans/modules/php/editor/indent/TokenFormatter.java +++ b/php.editor/src/org/netbeans/modules/php/editor/indent/TokenFormatter.java @@ -148,6 +148,7 @@ public boolean spaceAfterTypeCast; public boolean spaceAfterShortTag; public boolean spaceBeforeClosePHPTag; + public boolean spaceBetweenOpenPHPTagAndNamespace; public boolean placeElseOnNewLine; public boolean placeWhileOnNewLine; public boolean placeCatchOnNewLine; @@ -281,6 +282,7 @@ spaceAfterTypeCast = codeStyle.spaceAfterTypeCast(); spaceAfterShortTag = codeStyle.spaceAfterShortPHPTag(); spaceBeforeClosePHPTag = codeStyle.spaceBeforeClosePHPTag(); + spaceBetweenOpenPHPTagAndNamespace = codeStyle.spaceBetweenOpenPHPTagAndNamespace(); placeElseOnNewLine = codeStyle.placeElseOnNewLine(); placeWhileOnNewLine = codeStyle.placeWhileOnNewLine(); @@ -700,7 +702,13 @@ countSpaces = indent; } else { if (newLines == 0) { - countSpaces = 1; // one space before OPEN_TAG and NS_DECLARATION - probably in one line + if (docOptions.spaceBetweenOpenPHPTagAndNamespace) { + countSpaces = 1; // one space before OPEN_TAG and NS_DECLARATION - probably in one line + } else { + // for PSR-2 + newLines = 1; + countSpaces = indent; + } } countSpaces = Math.max(indent, countSpaces); } diff --git a/php.editor/src/org/netbeans/modules/php/editor/indent/ui/Bundle.properties b/php.editor/src/org/netbeans/modules/php/editor/indent/ui/Bundle.properties --- a/php.editor/src/org/netbeans/modules/php/editor/indent/ui/Bundle.properties +++ b/php.editor/src/org/netbeans/modules/php/editor/indent/ui/Bundle.properties @@ -150,6 +150,7 @@ LBL_spaceCheckAfterKeywords=After Keywords LBL_spaceAfterShortPHPTag=After Short PHP Tag LBL_spaceBeforeClosePHPTag=Before Close PHP Tag +LBL_spaceBetweenOpenPHPTagAndNamespace=Between Open PHP Tag and Namespace LBL_bp_If="&if", "else", "elseif" LBL_bp_FOR="&for", "foreach" diff --git a/php.editor/src/org/netbeans/modules/php/editor/indent/ui/FmtSpaces.java b/php.editor/src/org/netbeans/modules/php/editor/indent/ui/FmtSpaces.java --- a/php.editor/src/org/netbeans/modules/php/editor/indent/ui/FmtSpaces.java +++ b/php.editor/src/org/netbeans/modules/php/editor/indent/ui/FmtSpaces.java @@ -323,7 +323,8 @@ new Item(SPACE_AFTER_TYPE_CAST), new Item(SPACE_CHECK_AFTER_KEYWORDS), new Item(SPACE_AFTER_SHORT_PHP_TAG), - new Item(SPACE_BEFORE_CLOSE_PHP_TAG)) + new Item(SPACE_BEFORE_CLOSE_PHP_TAG), + new Item(SPACE_BETWEEN_OPEN_PHP_TAG_AND_NAMESPACE)) }; diff --git a/php.editor/test/unit/data/testfiles/formatting/blankLines/issue243744.php b/php.editor/test/unit/data/testfiles/formatting/blankLines/issue243744.php new file mode 100644 --- /dev/null +++ b/php.editor/test/unit/data/testfiles/formatting/blankLines/issue243744.php @@ -0,0 +1,8 @@ + options = new HashMap(FmtOptions.getDefaults()); options.put(FmtOptions.BLANK_LINES_AFTER_OPEN_PHP_TAG, 0); options.put(FmtOptions.BLANK_LINES_BEFORE_NAMESPACE, 0); + options.put(FmtOptions.SPACE_BETWEEN_OPEN_PHP_TAG_AND_NAMESPACE, true); reformatFileContents("testfiles/formatting/blankLines/issue235710_01.php", options); } @@ -955,4 +956,11 @@ reformatFileContents("testfiles/formatting/blankLines/issue235972_02.php", options); } + public void testIssue243744() throws Exception { + HashMap options = new HashMap<>(FmtOptions.getDefaults()); + options.put(FmtOptions.BLANK_LINES_AFTER_OPEN_PHP_TAG, 0); + options.put(FmtOptions.BLANK_LINES_BEFORE_NAMESPACE, 0); + reformatFileContents("testfiles/formatting/blankLines/issue243744.php", options); + } + }