SmallSense__CompletionController.st
changeset 174 3e08d765d86f
parent 143 038fdc3940f3
child 176 df6d3225d1e4
--- a/SmallSense__CompletionController.st	Tue Nov 19 13:02:56 2013 +0000
+++ b/SmallSense__CompletionController.st	Wed Feb 26 19:06:00 2014 +0100
@@ -3,7 +3,7 @@
 "{ NameSpace: SmallSense }"
 
 EditTextViewCompletionSupport subclass:#CompletionController
-	instanceVariableNames:'support seqno'
+	instanceVariableNames:'support seqno completeIfUnambiguous'
 	classVariableNames:''
 	poolDictionaries:''
 	category:'SmallSense-Core'
@@ -123,10 +123,11 @@
     "/ please change as required (and remove this comment)
     "/ support := nil.
     seqno := 0.
+    completeIfUnambiguous := UserPreferences current smallSenseCompleteIfUnambiguous.
 
     "/ super initialize.   -- commented since inherited method does nothing
 
-    "Modified: / 03-10-2013 / 07:11:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 18-01-2014 / 23:10:49 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !CompletionController methodsFor:'private'!
@@ -142,44 +143,6 @@
     "Modified: / 17-10-2013 / 01:08:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
-startCompletionProcess
-    "start the code completion process in the background"
-
-    |initialList cursorX cursorY|
-
-    "/ terminate any previous process
-    self stopCompletionProcess.
-
-    (editView sensor hasKeyPressEventFor:nil) ifTrue:[ 
-        "/ 'cl' printCR.
-        self closeCompletionView. 
-        ^ self
-    ].
-    ((cursorX := editView xOfCursor) isNil
-    or:[ (cursorY := editView yOfCursor) isNil ]) ifTrue:[
-        "/ no cursor - user is selecting, or cursor has been scrolled out of sight.
-        "/ 'cl2' printCR.
-        self closeCompletionView. 
-        ^ self
-    ].
-
-    completionView isNil ifTrue:[
-        initialList := #( ).
-        "/ 'op1' printCR.
-    ] ifFalse:[
-        initialList := completionView list.
-        "/ 'op2' printCR.
-    ].
-    self openCompletionView:initialList.
-
-    completionProcess := 
-        [
-            self computeCompletions.
-        ] forkAt:(Processor activePriority - 1).
-
-    "Created: / 04-11-2013 / 11:55:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-!
-
 stopCompletionProcess
     "kill any background completion process"
 
@@ -204,22 +167,39 @@
 !
 
 updateSelection
+    "Updates selection in completion view based on
+     currently typed partial text. Return true if
+     the complection window should be closed or false
+     if it shall be kept open."
 
     | matches word |
 
     word := support wordBeforeCursor.
     matches := completionView list select:[:po | po stringToComplete startsWith: word ].
     matches notEmptyOrNil ifTrue:[
-        completionView selection: (matches inject: matches anElement into:[:shortest :each |
-            each stringToComplete size < shortest stringToComplete size 
-                ifTrue:[each]
-                ifFalse:[shortest]
-        ]).
+        matches size == 1 ifTrue:[
+            completionView selection:  matches anElement.
+            completeIfUnambiguous ifTrue:[
+                self complete.
+                ^ true
+            ]
+        ] ifFalse:[
+            | selection |
+
+            selection := matches inject: matches anElement into:[:mostrelevant :each |
+                each relevance > mostrelevant relevance 
+                    ifTrue:[each]
+                    ifFalse:[mostrelevant]
+            ].
+            completionView selection: selection.
+        ]
     ] ifFalse:[
         completionView selection: nil.
-    ]
+    ].
+    ^ false.
 
     "Created: / 27-09-2013 / 16:16:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified (format): / 18-01-2014 / 23:24:59 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !CompletionController methodsFor:'private-API'!
@@ -274,6 +254,7 @@
     "/ move the window
 
     list isEmpty ifTrue:[ ^ self ].
+    list = #( 'Busy...' ) ifTrue:[ ^ self ].  
 
     x := (editView xOfCol:editView cursorCol  inVisibleLine:editView cursorLine)
             - 16"icon" - (editView widthOfString:  support wordBeforeCursor) - 5"magic constant".
@@ -294,8 +275,9 @@
         ].
         topView origin:movePos.
 "/        topView resizeToFit.
-        self updateSelection.
-        topView open.
+        self updateSelection ifFalse:[
+            topView open.
+        ].
     ] ifFalse:[
         completionView list:list.
         self updateSelection.
@@ -307,7 +289,7 @@
     ].
 
     "Created: / 27-09-2013 / 14:01:27 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 03-10-2013 / 07:10:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 18-01-2014 / 23:24:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 updateCompletions: completionResult sequence: sequence