summaryrefslogtreecommitdiff
path: root/sc
diff options
context:
space:
mode:
authorMike Kaganski <mike.kaganski@collabora.com>2019-11-29 21:56:38 +0300
committerXisco FaulĂ­ <xiscofauli@libreoffice.org>2019-12-18 11:07:10 +0100
commite56a2c67bd0cc6204ec50eae619c907c9026e9cc (patch)
tree73e91c0b5628f004edb805c09b124a80cc581785 /sc
parentb89ccad446a28cf1a2396836199a6f8a4f77903b (diff)
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@collabora.com> (cherry picked from commit d997addd504c92fea58cf62fddc2b1e9b8216a7d) Reviewed-on: https://gerrit.libreoffice.org/84130 Reviewed-by: Xisco FaulĂ­ <xiscofauli@libreoffice.org>
Diffstat (limited to 'sc')
-rw-r--r--sc/source/ui/app/inputhdl.cxx49
1 files changed, 15 insertions, 34 deletions
diff --git a/sc/source/ui/app/inputhdl.cxx b/sc/source/ui/app/inputhdl.cxx
index 6f064ba50269..c50ea4ad22e8 100644
--- a/sc/source/ui/app/inputhdl.cxx
+++ b/sc/source/ui/app/inputhdl.cxx
@@ -1421,48 +1421,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);
}