* bug #12393 fixed - During completion replacment by common part of suggested words... 63/10863/2
Simon Marchetto [Fri, 15 Mar 2013 15:51:59 +0000 (16:51 +0100)]
fix completion insert position: if line does not end by common part of suggested words, search last occurence of common part in line.
+ fix suggested word window: get window location only after common part completion is done

Change-Id: I9dbf6ebbc9c09ded22cbb6f76e060e93ddd74371

scilab/modules/completion/src/c/completeLine.c
scilab/modules/completion/tests/nonreg_tests/bug_12393.dia.ref [new file with mode: 0644]
scilab/modules/completion/tests/nonreg_tests/bug_12393.tst [new file with mode: 0644]
scilab/modules/completion/tests/unit_tests/completeline.dia.ref
scilab/modules/completion/tests/unit_tests/completeline.tst
scilab/modules/console/src/java/org/scilab/modules/console/CompletionAction.java
scilab/modules/string/src/c/stristr.c

index ccb5b5c..b26be50 100644 (file)
@@ -46,18 +46,18 @@ static int findMatchingPrefixSuffix(const char* string, const char* find, BOOL s
     pointerOnFindCopy = strdup(find);
     convstr(&pointerOnFindCopy, &pointerOnFindCopy, UPPER, 1);
     //last character of string
-    lastchar = *(string+strlen(string)-1);
+    lastchar = *(string + strlen(string) - 1);
     stringLength = strlen(string);
 
     //Tips : no infinite loop there, tmpfind string length is always reduced at each iteration
 
     movingPointerOnFindCopy = strrchr(pointerOnFindCopy, toupper(lastchar));
 
-    while( movingPointerOnFindCopy )
+    while ( movingPointerOnFindCopy )
     {
         //find the last occurence of last char of string in tmpfind
         movingPointerOnFindCopy = strrchr(pointerOnFindCopy, toupper(lastchar));
-        if(movingPointerOnFindCopy == NULL)
+        if (movingPointerOnFindCopy == NULL)
         {
             break;
         }
@@ -65,7 +65,7 @@ static int findMatchingPrefixSuffix(const char* string, const char* find, BOOL s
         movingPointerOnFindCopy[0] = '\0';
         //Check if the cutted tmpfind match with the suffix of string that has adequat length
         pointerOnString = (char*)(string + stringLength - 1 - strlen(pointerOnFindCopy));
-        if( !strnicmp(pointerOnFindCopy, pointerOnString, strlen(pointerOnFindCopy)) )
+        if ( !strnicmp(pointerOnFindCopy, pointerOnString, strlen(pointerOnFindCopy)) )
         {
             FREE(pointerOnFindCopy);
             pointerOnFindCopy = NULL;
@@ -79,8 +79,8 @@ static int findMatchingPrefixSuffix(const char* string, const char* find, BOOL s
     return (int)stringLength;
 }
 /*--------------------------------------------------------------------------*/
-char *completeLine(char *currentline,char *stringToAdd,char *filePattern,
-    char *defaultPattern,BOOL stringToAddIsPath, char *postCaretLine)
+char *completeLine(char *currentline, char *stringToAdd, char *filePattern,
+                   char *defaultPattern, BOOL stringToAddIsPath, char *postCaretLine)
 {
     char *new_line = NULL;
     int lengthNewLine = 0;
@@ -121,7 +121,11 @@ char *completeLine(char *currentline,char *stringToAdd,char *filePattern,
             strcat(new_line, stringToAddAtTheEnd);
         }
 
-        if (stringToAddAtTheEnd) {FREE(stringToAddAtTheEnd); stringToAddAtTheEnd = NULL;}
+        if (stringToAddAtTheEnd)
+        {
+            FREE(stringToAddAtTheEnd);
+            stringToAddAtTheEnd = NULL;
+        }
 
         return new_line;
     }
@@ -143,12 +147,12 @@ char *completeLine(char *currentline,char *stringToAdd,char *filePattern,
 
         if (filePatternBuf)
         {
-            char* drv = (char*)MALLOC(sizeof(char)*(PATH_MAX+1));
-            char* dir = (char*)MALLOC(sizeof(char)*(PATH_MAX+1));
-            char* name = (char*)MALLOC(sizeof(char)*(PATH_MAX+1));
-            char* ext = (char*)MALLOC(sizeof(char)*(PATH_MAX+1));
+            char* drv = (char*)MALLOC(sizeof(char) * (PATH_MAX + 1));
+            char* dir = (char*)MALLOC(sizeof(char) * (PATH_MAX + 1));
+            char* name = (char*)MALLOC(sizeof(char) * (PATH_MAX + 1));
+            char* ext = (char*)MALLOC(sizeof(char) * (PATH_MAX + 1));
 
-            splitpath(filePatternBuf,TRUE, drv,dir, name, ext);
+            splitpath(filePatternBuf, TRUE, drv, dir, name, ext);
 
             if (bfilePatternBuf)
             {
@@ -156,15 +160,31 @@ char *completeLine(char *currentline,char *stringToAdd,char *filePattern,
                 filePatternBuf = NULL;
             }
 
-            if ( strcmp(drv,"") || strcmp(dir,"") )
+            if ( strcmp(drv, "") || strcmp(dir, "") )
             {
                 /* bug 4365 */
                 /*cd SCI/modules/arnoldi/nonreg_tes */
 
-                if (drv) {FREE(drv); drv = NULL;}
-                if (dir) {FREE(dir); dir = NULL;}
-                if (name) {FREE(name); name = NULL;}
-                if (ext) {FREE(ext); ext = NULL;}
+                if (drv)
+                {
+                    FREE(drv);
+                    drv = NULL;
+                }
+                if (dir)
+                {
+                    FREE(dir);
+                    dir = NULL;
+                }
+                if (name)
+                {
+                    FREE(name);
+                    name = NULL;
+                }
+                if (ext)
+                {
+                    FREE(ext);
+                    ext = NULL;
+                }
 
                 lengthNewLine = lencurrentline + lenstringToAddAtTheEnd;
                 new_line = (char*)MALLOC(sizeof(char) * (lengthNewLine + 1));
@@ -174,45 +194,68 @@ char *completeLine(char *currentline,char *stringToAdd,char *filePattern,
                     strcat(new_line, stringToAddAtTheEnd);
                 }
 
-                if (stringToAddAtTheEnd) {FREE(stringToAddAtTheEnd); stringToAddAtTheEnd = NULL;}
+                if (stringToAddAtTheEnd)
+                {
+                    FREE(stringToAddAtTheEnd);
+                    stringToAddAtTheEnd = NULL;
+                }
 
                 return new_line;
             }
 
-            if (drv) {FREE(drv); drv = NULL;}
-            if (dir) {FREE(dir); dir = NULL;}
-            if (name) {FREE(name); name = NULL;}
-            if (ext) {FREE(ext); ext = NULL;}
+            if (drv)
+            {
+                FREE(drv);
+                drv = NULL;
+            }
+            if (dir)
+            {
+                FREE(dir);
+                dir = NULL;
+            }
+            if (name)
+            {
+                FREE(name);
+                name = NULL;
+            }
+            if (ext)
+            {
+                FREE(ext);
+                ext = NULL;
+            }
         }
     }
 
+    // Does the end of line (currentLine) matches the beginning of completed word (stringToAdd) ?
     iposInsert = findMatchingPrefixSuffix(currentline, stringToAdd, stringToAddIsPath);
     res = stristr(stringToAdd, &currentline[iposInsert]);
 
     if (res == NULL)
     {
-        // We found "pattern"
-        // if it is a path, we add at the end
-        if ((currentline[lencurrentline - 1] == '/') || (currentline[lencurrentline - 1] == '\\'))
+        // No, find the last occurence of completed word word in line
+        char* foundCompletedWordPtr = NULL;
+        char* nextFoundCompletedWordPtr = stristr(currentline, stringToAdd);
+        while (nextFoundCompletedWordPtr)
         {
-            iposInsert = lencurrentline;
+            foundCompletedWordPtr = nextFoundCompletedWordPtr;
+            nextFoundCompletedWordPtr =
+                stristr(foundCompletedWordPtr + strlen(foundCompletedWordPtr), stringToAdd);
         }
-        else
+
+        if (foundCompletedWordPtr)
         {
-            iposInsert = lencurrentline - 1;
+            iposInsert = (int) (foundCompletedWordPtr - currentline);
         }
     }
-    else
+
+    // if it is a path, we add at the end
+    if ((currentline[lencurrentline - 1] == '/') || (currentline[lencurrentline - 1] == '\\'))
     {
-        // if it is a path, we add at the end
-        if ((currentline[lencurrentline - 1] == '/') || (currentline[lencurrentline - 1] == '\\'))
-        {
-            iposInsert = lencurrentline;
-        }
+        iposInsert = lencurrentline;
     }
 
-    lengthNewLine = (int)(strlen(currentline)+ strlen(stringToAdd) + lenstringToAddAtTheEnd);
-    new_line = (char*)MALLOC(sizeof(char)*(lengthNewLine + 1));
+    lengthNewLine = (int)(strlen(currentline) + strlen(stringToAdd) + lenstringToAddAtTheEnd);
+    new_line = (char*)MALLOC(sizeof(char) * (lengthNewLine + 1));
     if (new_line)
     {
         strcpy(new_line, currentline);
@@ -222,7 +265,11 @@ char *completeLine(char *currentline,char *stringToAdd,char *filePattern,
         strcat(new_line, stringToAddAtTheEnd);
     }
 
-    if (stringToAddAtTheEnd) {FREE(stringToAddAtTheEnd); stringToAddAtTheEnd = NULL;}
+    if (stringToAddAtTheEnd)
+    {
+        FREE(stringToAddAtTheEnd);
+        stringToAddAtTheEnd = NULL;
+    }
 
     return new_line;
 }
diff --git a/scilab/modules/completion/tests/nonreg_tests/bug_12393.dia.ref b/scilab/modules/completion/tests/nonreg_tests/bug_12393.dia.ref
new file mode 100644 (file)
index 0000000..dfdf713
--- /dev/null
@@ -0,0 +1,28 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2013 - Scilab Enterprises - Simon MARCHETTO
+//
+//  This file is distributed under the same license as the Scilab package.
+// =============================================================================
+//
+// <-- JAVA NOT MANDATORY -->
+//
+// <-- Non-regression test for bug 10393 -->
+//
+// <-- Bugzilla URL -->
+// http://bugzilla.scilab.org/show_bug.cgi?id=12393
+//
+// <-- Short Description -->
+// Replacment by common part during completion does not work
+ilib_verbose(0);
+ierr = exec(SCI + "/modules/completion/tests/utilities/build_primitives.sce", ..
+    "errcatch", -1);
+assert_checkequal(ierr, 0);
+ierr = exec(TMPDIR + "/completion/loader.sce", "errcatch", -1);
+assert_checkequal(ierr, 0);
+// Suggested words for 'setdefau' are 'setdefaultlanguage', 'setDefaultColor'
+// We must replace 'setdefau' by common part of this words: 'set'
+currentline = 'setdefau';
+r = completeline('setdefau', 'set', getfilepartlevel(currentline), ..
+    getpartlevel(currentline), %t);
+assert_checkequal(r, 'set');
diff --git a/scilab/modules/completion/tests/nonreg_tests/bug_12393.tst b/scilab/modules/completion/tests/nonreg_tests/bug_12393.tst
new file mode 100644 (file)
index 0000000..410743f
--- /dev/null
@@ -0,0 +1,30 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2013 - Scilab Enterprises - Simon MARCHETTO
+//
+//  This file is distributed under the same license as the Scilab package.
+// =============================================================================
+//
+// <-- JAVA NOT MANDATORY -->
+//
+// <-- Non-regression test for bug 10393 -->
+//
+// <-- Bugzilla URL -->
+// http://bugzilla.scilab.org/show_bug.cgi?id=12393
+//
+// <-- Short Description -->
+// During completion replacment by common part of suggested words does not work
+
+ilib_verbose(0);
+ierr = exec(SCI + "/modules/completion/tests/utilities/build_primitives.sce", ..
+    "errcatch", -1);
+assert_checkequal(ierr, 0);
+ierr = exec(TMPDIR + "/completion/loader.sce", "errcatch", -1);
+assert_checkequal(ierr, 0);
+
+// Suggested words for 'setdefau' are 'setdefaultlanguage', 'setDefaultColor'
+// We must replace 'setdefau' by common part of this words: 'set'
+currentline = 'setdefau';
+r = completeline('setdefau', 'set', getfilepartlevel(currentline), ..
+    getpartlevel(currentline), %t);
+assert_checkequal(r, 'set');
index feac990..b16eb17 100644 (file)
@@ -72,4 +72,9 @@ assert_checkequal(r, 't.field1');
 currentline = 't.field1 = t.fie';
 r = completeline(currentline,'field2',getfilepartlevel(currentline),getpartlevel(currentline),%t);
 assert_checkequal(r, 't.field1 = t.field2');
-
+currentline = 'totoa';
+r = completeline(currentline,'toto',getfilepartlevel(currentline),getpartlevel(currentline),%t);
+assert_checkequal(r, 'toto');
+currentline = 'totototoa';
+r = completeline(currentline,'toto',getfilepartlevel(currentline),getpartlevel(currentline),%t);
+assert_checkequal(r, 'toto');
index 8b9ba62..634c6ae 100644 (file)
@@ -96,3 +96,11 @@ assert_checkequal(r, 't.field1');
 currentline = 't.field1 = t.fie';
 r = completeline(currentline,'field2',getfilepartlevel(currentline),getpartlevel(currentline),%t);
 assert_checkequal(r, 't.field1 = t.field2');
+
+currentline = 'totoa';
+r = completeline(currentline,'toto',getfilepartlevel(currentline),getpartlevel(currentline),%t);
+assert_checkequal(r, 'toto');
+
+currentline = 'totototoa';
+r = completeline(currentline,'toto',getfilepartlevel(currentline),getpartlevel(currentline),%t);
+assert_checkequal(r, 'toto');
index fc74480..e5fcf12 100644 (file)
@@ -50,39 +50,46 @@ public class CompletionAction extends AbstractConsoleAction {
             return;
         }
 
-        Point location = configuration.getInputParsingManager().getWindowCompletionLocation();
         List<CompletionItem> completionItems = configuration.getCompletionManager().getCompletionItems();
-        AbstractSciCompletionWindow win = (AbstractSciCompletionWindow) configuration.getCompletionWindow();
-
-        if (completionItems != null && completionItems.size() == 1) {
-            /* Only one item returned, autoselected and appended to command line */
-            configuration.getCompletionWindow().show(completionItems, location);
-            win.addCompletedWord(caretPosition);
-
-            ((AbstractSciCompletionWindow) configuration.getCompletionWindow()).setVisible(false);
-        } else if (completionItems != null && completionItems.size() != 0) {
-            String [] completionArray = new String [completionItems.size()];
-
-            int i = 0;
-            Iterator < CompletionItem >  it = completionItems.iterator();
-            while  (it.hasNext()) {
-                CompletionItem currentItem = it.next();
-                completionArray[i] = currentItem.getReturnValue();
-                i++;
-            }
 
-            java.util.Arrays.sort(completionArray);
-            String commonPartOfWord = Completion.getCommonPart(completionArray, completionItems.size());
+        if ((completionItems != null) && (completionItems.size() > 0)) {
+            AbstractSciCompletionWindow win = (AbstractSciCompletionWindow) configuration.getCompletionWindow();
+
+            if (completionItems.size() == 1) {
+                Point location = configuration.getInputParsingManager().getWindowCompletionLocation();
+                /* Only one item returned, autoselected and appended to command line */
+                configuration.getCompletionWindow().show(completionItems, location);
+                win.addCompletedWord(caretPosition);
 
-            caretPosition = configuration.getInputParsingManager().getCaretPosition();
-            String currentLine = configuration.getInputParsingManager().getCommandLine();
+                ((AbstractSciCompletionWindow) configuration.getCompletionWindow()).setVisible(false);
+            } else {
+                String [] completionArray = new String [completionItems.size()];
 
-            if ((commonPartOfWord.length() != 0) && (caretPosition == currentLine.length())) {
-                if (configuration.getInputParsingManager().getPartLevel(0).length() != 0) {
-                    win.addCompletedWord(commonPartOfWord, caretPosition);
+                int i = 0;
+                Iterator < CompletionItem >  it = completionItems.iterator();
+                while  (it.hasNext()) {
+                    CompletionItem currentItem = it.next();
+                    completionArray[i] = currentItem.getReturnValue();
+                    i++;
                 }
+
+                // First autocmplete by the common prefix of suggested words, if exist
+                java.util.Arrays.sort(completionArray);
+                String commonPartOfWord = Completion.getCommonPart(completionArray, completionItems.size());
+
+                caretPosition = configuration.getInputParsingManager().getCaretPosition();
+                String currentLine = configuration.getInputParsingManager().getCommandLine();
+
+                if ((commonPartOfWord.length() != 0) && (caretPosition == currentLine.length())) {
+                    if (configuration.getInputParsingManager().getPartLevel(0).length() != 0) {
+                        win.addCompletedWord(commonPartOfWord, caretPosition);
+                    }
+                }
+
+                // Display suggested word window at caret position
+                Point location = configuration.getInputParsingManager().getWindowCompletionLocation();
+                configuration.getCompletionWindow().show(completionItems, location);
             }
-            configuration.getCompletionWindow().show(completionItems, location);
         }
     }
 }
index c83b9a2..5413b45 100644 (file)
@@ -21,32 +21,35 @@ char *stristr(const char *psz, const char *tofind)
     const char *ptr = psz;
     const char *ptr2;
 
-    while (1)
+    if (strlen(tofind) > 0)
     {
-        ptr = strchr(psz, toupper(*tofind));
-        ptr2 = strchr(psz, tolower(*tofind));
-
-        if (!ptr)
+        while (1)
         {
-            ptr = ptr2; /* was ptr2 = ptr.  Big bug fixed 10/22/99 */
-        }
+            ptr = strchr(psz, toupper(*tofind));
+            ptr2 = strchr(psz, tolower(*tofind));
 
-        if (!ptr)
-        {
-            break;
-        }
+            if (!ptr)
+            {
+                ptr = ptr2; /* was ptr2 = ptr.  Big bug fixed 10/22/99 */
+            }
 
-        if (ptr2 && (ptr2 < ptr))
-        {
-            ptr = ptr2;
-        }
+            if (!ptr)
+            {
+                break;
+            }
 
-        if (!strnicmp(ptr, tofind, strlen(tofind)))
-        {
-            return (char *) ptr;
-        }
+            if (ptr2 && (ptr2 < ptr))
+            {
+                ptr = ptr2;
+            }
 
-        psz = ptr + 1;
+            if (!strnicmp(ptr, tofind, strlen(tofind)))
+            {
+                return (char *) ptr;
+            }
+
+            psz = ptr + 1;
+        }
     }
     return 0;
 }