tdf#149402 - BASIC: Don't extend comment if line ends in a whitespace

Change-Id: I8adf530e77a0e65329fa59ac2873b99f48befac4
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/135336
Tested-by: Jenkins
Reviewed-by: Andreas Heinisch <andreas.heinisch@yahoo.de>
(cherry picked from commit fbce18558a58cddf910b788a67c2f2d4d25d68e9)
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/135378
Reviewed-by: Xisco Fauli <xiscofauli@libreoffice.org>
diff --git a/basic/qa/cppunit/test_compiler_checks.cxx b/basic/qa/cppunit/test_compiler_checks.cxx
index e2882f3..0449776 100644
--- a/basic/qa/cppunit/test_compiler_checks.cxx
+++ b/basic/qa/cppunit/test_compiler_checks.cxx
@@ -73,4 +73,45 @@ CPPUNIT_TEST_FIXTURE(CppUnit::TestFixture, testTdf149157_vba)
    CPPUNIT_ASSERT(!aMacro.HasError());
}

CPPUNIT_TEST_FIXTURE(CppUnit::TestFixture, testTdf149402)
{
    MacroSnippet aMacro("Function extentComment() As Integer\n"
                        "  ' _ \n"
                        "  If Not extentComment Then\n"
                        "     extentComment = 1\n"
                        "  Else\n"
                        "  End If\n"
                        "End Function\n");
    aMacro.Compile();
    CPPUNIT_ASSERT(!aMacro.HasError());
}

CPPUNIT_TEST_FIXTURE(CppUnit::TestFixture, testTdf149402_compatible)
{
    MacroSnippet aMacro("Option Compatible\n"
                        "Function extentComment() As Integer\n"
                        "  ' _ \n"
                        "  If Not extentComment Then\n"
                        "     extentComment = 1\n"
                        "  Else\n"
                        "  End If\n"
                        "End Function\n");
    aMacro.Compile();
    CPPUNIT_ASSERT(!aMacro.HasError());
}

CPPUNIT_TEST_FIXTURE(CppUnit::TestFixture, testTdf149402_vba)
{
    MacroSnippet aMacro("Option VBASupport 1\n"
                        "Function extentComment() As Integer\n"
                        "  ' _ \n"
                        "  If Not extentComment Then\n"
                        "     extentComment = 1\n"
                        "  Else\n"
                        "  End If\n"
                        "End Function\n");
    aMacro.Compile();
    CPPUNIT_ASSERT(!aMacro.HasError());
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
diff --git a/basic/source/comp/scanner.cxx b/basic/source/comp/scanner.cxx
index d391e7b..8c63dcd 100644
--- a/basic/source/comp/scanner.cxx
+++ b/basic/source/comp/scanner.cxx
@@ -53,6 +53,7 @@ SbiScanner::SbiScanner(const OUString& rBuf, StarBASIC* p)
    , bVBASupportOn(false)
    , bPrevLineExtentsComment(false)
    , bClosingUnderscore(false)
    , bLineEndsWithWhitespace(false)
    , bInStatement(false)
{
}
@@ -186,6 +187,8 @@ bool SbiScanner::readLine()
    while(nBufPos < nEnd && BasicCharClass::isWhitespace(aBuf[nEnd - 1]))
        --nEnd;

    // tdf#149402 - check if line ends with a whitespace
    bLineEndsWithWhitespace = (n > nEnd);
    aLine = aBuf.copy(nBufPos, nEnd - nBufPos);

    // Fast-forward past the line ending
@@ -663,7 +666,9 @@ PrevLineCommentLbl:
        bPrevLineExtentsComment = false;
        aSym = "REM";
        sal_Int32 nLen = aLine.getLength() - nLineIdx;
        if( bCompatible && aLine[nLineIdx + nLen - 1] == '_' && aLine[nLineIdx + nLen - 2] == ' ' )
        // tdf#149402 - don't extend comment if line ends in a whitespace (asicCharClass::isWhitespace)
        if (bCompatible && !bLineEndsWithWhitespace && aLine[nLineIdx + nLen - 1] == '_'
            && aLine[nLineIdx + nLen - 2] == ' ')
            bPrevLineExtentsComment = true;
        nCol2 = nCol2 + nLen;
        nLineIdx = -1;
diff --git a/basic/source/inc/scanner.hxx b/basic/source/inc/scanner.hxx
index 39dda9f..cc3cbb5d 100644
--- a/basic/source/inc/scanner.hxx
+++ b/basic/source/inc/scanner.hxx
@@ -62,6 +62,7 @@ protected:
    bool   bVBASupportOn;               // true: OPTION VBASupport 1 otherwise default False
    bool   bPrevLineExtentsComment;     // true: Previous line is comment and ends on "... _"
    bool   bClosingUnderscore;          // true: Closing underscore followed by end of line
    bool   bLineEndsWithWhitespace;     // true: Line ends with whitespace (BasicCharClass::isWhitespace)

    bool   bInStatement;
    void   GenError( ErrCode );