diff --git a/php.editor/src/org/netbeans/modules/php/editor/indent/FormatVisitor.java b/php.editor/src/org/netbeans/modules/php/editor/indent/FormatVisitor.java --- a/php.editor/src/org/netbeans/modules/php/editor/indent/FormatVisitor.java +++ b/php.editor/src/org/netbeans/modules/php/editor/indent/FormatVisitor.java @@ -466,7 +466,7 @@ boolean indentationIncluded = false; while (ts.moveNext() && (ts.token().id() == PHPTokenId.WHITESPACE && countOfNewLines(ts.token().text()) == 0) - || isComment(ts.token())) { + || (lastIndex < ts.index() && isComment(ts.token()))) { // #268920 check lastIndex to prevent an infinite loop if (ts.token().id() == PHPTokenId.PHP_LINE_COMMENT && !indentationIncluded) { formatTokens.add(new FormatToken.IndentToken(ts.offset(), options.indentSize)); indentationIncluded = true; diff --git a/php.editor/src/org/netbeans/modules/php/editor/parser/GSFPHPParser.java b/php.editor/src/org/netbeans/modules/php/editor/parser/GSFPHPParser.java --- a/php.editor/src/org/netbeans/modules/php/editor/parser/GSFPHPParser.java +++ b/php.editor/src/org/netbeans/modules/php/editor/parser/GSFPHPParser.java @@ -87,6 +87,13 @@ private ParserResult result = null; private boolean projectPropertiesListenerAdded = false; + // it's for testing + private static int unitTestCaretPosition = -1; + + public static void setUnitTestCaretPosition(int unitTestCaretPosition) { + GSFPHPParser.unitTestCaretPosition = unitTestCaretPosition; + } + static { LOGGER.log(Level.INFO, "Parsing of big PHP files enabled: {0} (max size: {1})", new Object[] {PARSE_BIG_FILES, BIG_FILE_SIZE}); } @@ -163,6 +170,9 @@ aspTags = languageProperties.areAspTagsEnabled(); try { int caretOffset = GsfUtilities.getLastKnownCaretOffset(snapshot, event); + if (caretOffset < 0 && unitTestCaretPosition >= 0) { + caretOffset = unitTestCaretPosition; + } LOGGER.log(Level.FINE, "caretOffset: {0}", caretOffset); //NOI18N Context context = new Context(snapshot, caretOffset); result = parseBuffer(context, Sanitize.NONE, null); diff --git a/php.editor/test/unit/data/testfiles/formatting/issue268920.php b/php.editor/test/unit/data/testfiles/formatting/issue268920.php new file mode 100644 --- /dev/null +++ b/php.editor/test/unit/data/testfiles/formatting/issue268920.php @@ -0,0 +1,15 @@ + + diff --git a/php.editor/test/unit/data/testfiles/formatting/issue268920.php.formatted b/php.editor/test/unit/data/testfiles/formatting/issue268920.php.formatted new file mode 100644 --- /dev/null +++ b/php.editor/test/unit/data/testfiles/formatting/issue268920.php.formatted @@ -0,0 +1,15 @@ + + diff --git a/php.editor/test/unit/data/testfiles/formatting/templates/issue268920.php b/php.editor/test/unit/data/testfiles/formatting/templates/issue268920.php new file mode 100644 --- /dev/null +++ b/php.editor/test/unit/data/testfiles/formatting/templates/issue268920.php @@ -0,0 +1,15 @@ + + diff --git a/php.editor/test/unit/data/testfiles/formatting/templates/issue268920.php.formatted b/php.editor/test/unit/data/testfiles/formatting/templates/issue268920.php.formatted new file mode 100644 --- /dev/null +++ b/php.editor/test/unit/data/testfiles/formatting/templates/issue268920.php.formatted @@ -0,0 +1,15 @@ + + diff --git a/php.editor/test/unit/src/org/netbeans/modules/php/editor/indent/PHPFormatterTemplateTest.java b/php.editor/test/unit/src/org/netbeans/modules/php/editor/indent/PHPFormatterTemplateTest.java --- a/php.editor/test/unit/src/org/netbeans/modules/php/editor/indent/PHPFormatterTemplateTest.java +++ b/php.editor/test/unit/src/org/netbeans/modules/php/editor/indent/PHPFormatterTemplateTest.java @@ -553,4 +553,9 @@ reformatFileContents("testfiles/formatting/templates/issue262205_04.php", options, true); } + public void testIssue268920() throws Exception { + HashMap options = new HashMap<>(FmtOptions.getDefaults()); + reformatFileContents("testfiles/formatting/templates/issue268920.php", options, true); + } + } diff --git a/php.editor/test/unit/src/org/netbeans/modules/php/editor/indent/PHPFormatterTest.java b/php.editor/test/unit/src/org/netbeans/modules/php/editor/indent/PHPFormatterTest.java --- a/php.editor/test/unit/src/org/netbeans/modules/php/editor/indent/PHPFormatterTest.java +++ b/php.editor/test/unit/src/org/netbeans/modules/php/editor/indent/PHPFormatterTest.java @@ -764,4 +764,8 @@ reformatFileContents("testfiles/formatting/issue268171.php", options); } + public void testIssue268920() throws Exception { + HashMap options = new HashMap<>(FmtOptions.getDefaults()); + reformatFileContents("testfiles/formatting/issue268920.php", options); + } } diff --git a/php.editor/test/unit/src/org/netbeans/modules/php/editor/indent/PHPFormatterTestBase.java b/php.editor/test/unit/src/org/netbeans/modules/php/editor/indent/PHPFormatterTestBase.java --- a/php.editor/test/unit/src/org/netbeans/modules/php/editor/indent/PHPFormatterTestBase.java +++ b/php.editor/test/unit/src/org/netbeans/modules/php/editor/indent/PHPFormatterTestBase.java @@ -52,6 +52,7 @@ import org.netbeans.modules.html.editor.lib.api.HtmlVersion; import org.netbeans.modules.php.editor.PHPTestBase; import org.netbeans.modules.php.editor.lexer.PHPTokenId; +import org.netbeans.modules.php.editor.parser.GSFPHPParser; import org.openide.filesystems.FileObject; /** @@ -84,6 +85,12 @@ HtmlVersion.DEFAULT_VERSION_UNIT_TESTS_OVERRIDE = HtmlVersion.HTML41_TRANSATIONAL; } + @Override + protected void tearDown() throws Exception { + super.tearDown(); + GSFPHPParser.setUnitTestCaretPosition(-1); + } + protected void reformatFileContents(String file, IndentPrefs indentPrefs, int initialIndent) throws Exception { FileObject fo = getTestFile(file); assertNotNull(fo); @@ -184,6 +191,7 @@ } TokenFormatter.setUnitTestCarretPosition(carretPosition); + GSFPHPParser.setUnitTestCaretPosition(carretPosition); doc.remove(0, doc.getLength()); doc.insertString(0, text, null); doc.putProperty(TokenFormatter.TEMPLATE_HANDLER_PROPERTY, new Object());