basic/qa/cppunit/test_compiler_checks.cxx |   41 ++++++++++++++++++++++++++++++
 basic/source/comp/scanner.cxx             |    7 ++++-
 basic/source/inc/scanner.hxx              |    1 
 3 files changed, 48 insertions(+), 1 deletion(-)

New commits:
commit fbce18558a58cddf910b788a67c2f2d4d25d68e9
Author:     Andreas Heinisch <andreas.heini...@yahoo.de>
AuthorDate: Fri Jun 3 09:41:05 2022 +0200
Commit:     Andreas Heinisch <andreas.heini...@yahoo.de>
CommitDate: Mon Jun 6 10:24:20 2022 +0200

    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.heini...@yahoo.de>

diff --git a/basic/qa/cppunit/test_compiler_checks.cxx 
b/basic/qa/cppunit/test_compiler_checks.cxx
index e2882f3f694c..044977670e62 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 af87757b2282..b5a923969bfd 100644
--- a/basic/source/comp/scanner.cxx
+++ b/basic/source/comp/scanner.cxx
@@ -55,6 +55,7 @@ SbiScanner::SbiScanner(OUString _aBuf, StarBASIC* p)
     , bVBASupportOn(false)
     , bPrevLineExtentsComment(false)
     , bClosingUnderscore(false)
+    , bLineEndsWithWhitespace(false)
     , bInStatement(false)
 {
 }
@@ -188,6 +189,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
@@ -665,7 +668,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 c10d809055a6..3f2c7e2e2cff 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 );

Reply via email to