[Libreoffice-commits] core.git: sc/source

Mike Kaganski (via logerrit) logerrit at kemper.freedesktop.org
Fri Nov 29 20:22:05 UTC 2019


 sc/source/ui/app/inputhdl.cxx |   49 ++++++++++++------------------------------
 1 file changed, 15 insertions(+), 34 deletions(-)

New commits:
commit d997addd504c92fea58cf62fddc2b1e9b8216a7d
Author:     Mike Kaganski <mike.kaganski at collabora.com>
AuthorDate: Fri Nov 29 21:56:38 2019 +0300
Commit:     Mike Kaganski <mike.kaganski at collabora.com>
CommitDate: Fri Nov 29 21:20:56 2019 +0100

    tdf#129007: fix autocompletion
    
    Using EditView::SelectCurrentWord in arbitrary selection positions
    is not a correct approach. When cursor is after "_" in "=_+FUNC",
    the method will select "=_+", i.e. the substring that cannot start
    any Calc identifier (and actually ending *after* current cursor).
    When matching identifiers like "bb.bbb" in "=bb.bb", the end result
    will be "=bb.bb.bbb", etc.
    
    So instead of trying to match words and hack around problems, let's
    simply find longest match in the string starting from beginning of
    the line, and ending at current cursor position. Testing trailing
    parts of that string of decreasing length, the first found match is
    the longest, which is what we need. This also avoids multiple calls
    to expensive EditEngine methods.
    
    Change-Id: Ia3470cc85a4ba9f9ab34cbe9db3250bf28e93350
    Reviewed-on: https://gerrit.libreoffice.org/84087
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kaganski at collabora.com>

diff --git a/sc/source/ui/app/inputhdl.cxx b/sc/source/ui/app/inputhdl.cxx
index 640ea40060e0..2ea4ba7a6c2d 100644
--- a/sc/source/ui/app/inputhdl.cxx
+++ b/sc/source/ui/app/inputhdl.cxx
@@ -1478,48 +1478,29 @@ void ScInputHandler::NextFormulaEntry( bool bBack )
 
 namespace {
 
-bool needToExtendSelection(const OUString& rSelectedText, const OUString& rInsertText)
-{
-    return !ScGlobal::GetpTransliteration()->isMatch( rSelectedText, rInsertText);
-}
-
 void completeFunction( EditView* pView, const OUString& rInsert, bool& rParInserted )
 {
     if (pView)
     {
         ESelection aSel = pView->GetSelection();
-        --aSel.nStartPos;
-        --aSel.nEndPos;
-        pView->SetSelection(aSel);
-        pView->SelectCurrentWord();
-
-        // a dot and underscore are word separators so we need special
-        // treatment for any formula containing a dot or underscore
-        if(rInsert.indexOf(".") != -1 || rInsert.indexOf("_") != -1)
+
         {
-            // need to make sure that we replace also the part before the dot
-            // go through the word to find the match with the insert string
-            aSel = pView->GetSelection();
-            ESelection aOldSelection = aSel;
-            OUString aSelectedText = pView->GetSelected();
-            if ( needToExtendSelection( aSelectedText, rInsert ) )
-            {
-                while(needToExtendSelection(aSelectedText, rInsert))
-                {
-                    assert(aSel.nStartPos > 0);
-                    --aSel.nStartPos;
-                    aSel.nEndPos = aSel.nStartPos;
-                    pView->SetSelection(aSel);
-                    pView->SelectCurrentWord();
-                    aSelectedText = pView->GetSelected();
-                }
-                aSel.nStartPos = aSel.nEndPos - ( aSelectedText.getLength() - 1 );
-            }
-            else
+            const sal_Int32 nMinLen = std::max(aSel.nEndPos - aSel.nStartPos, sal_Int32(1));
+            // Since transliteration service is used to test for match, the replaced string could be
+            // longer than rInsert, so in order to find longest match before the cursor, test whole
+            // string from start to current cursor position (don't limit to length of rInsert)
+            aSel.nStartPos = 0;
+            pView->SetSelection(aSel);
+            const OUString aAll = pView->GetSelected();
+            OUString aMatch;
+            for (sal_Int32 n = aAll.getLength(); n >= nMinLen && aMatch.isEmpty(); --n)
             {
-                aSel.nStartPos = aSel.nEndPos - aSelectedText.getLength();
+                const OUString aTest = aAll.copy(aAll.getLength() - n); // n trailing chars
+                if (ScGlobal::GetpTransliteration()->isMatch(aTest, rInsert))
+                    aMatch = aTest; // Found => break the loop
             }
-            aSel.nEndPos = aOldSelection.nEndPos;
+
+            aSel.nStartPos = aSel.nEndPos - aMatch.getLength();
             pView->SetSelection(aSel);
         }
 


More information about the Libreoffice-commits mailing list