SmallSense__CompletionController.st
changeset 185 75738108cc3f
parent 178 f98d96568600
child 187 7baeeea7d8ae
--- a/SmallSense__CompletionController.st	Mon Mar 31 22:31:38 2014 +0200
+++ b/SmallSense__CompletionController.st	Mon Mar 31 23:43:25 2014 +0200
@@ -67,6 +67,10 @@
             self complete.
             ^ true.
         ].
+        key == #Tab ifTrue:[ 
+            self handleKeyPressTab.  
+            ^ true
+        ].
         key isCharacter ifTrue:[
             self updateSelection.
         ].
@@ -74,7 +78,59 @@
     ^ super handleKeyPress:key x:x y:y
 
     "Created: / 27-09-2013 / 15:38:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 30-09-2013 / 15:00:49 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 31-03-2014 / 22:55:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+handleKeyPressTab
+    "Tab has been pressed, try to complete longest common prefix"
+
+    | prefix matching longest minlen |
+
+    prefix := support wordBeforeCursor string.                                        
+    matching := OrderedCollection new.
+    minlen := SmallInteger maxVal.
+    completionView list do:[:po |
+        | s |
+
+        s := po stringToComplete.
+        (s startsWith: prefix) ifTrue:[
+            matching add: po -> s.
+            minlen := minlen min: s size.
+        ].
+    ].
+    matching isEmpty ifTrue:[
+        completionView flash.
+        ^self.
+    ].
+    matching size == 1 ifTrue:[
+        self complete: matching first key.
+    ].
+
+    longest := String streamContents:[:s |
+        | i |
+
+        s nextPutAll: prefix.
+        i := prefix size + 1.
+        [ i <= minlen ] whileTrue:[
+            | c |
+
+            c := matching first value at: i.
+            (matching allSatisfy:[:e|(e value at: i) == c]) ifTrue:[
+                s nextPut:c.
+                i := i + 1.
+            ] ifFalse:[
+                "/ terminate the loop    
+                i := minlen + 2.
+            ]
+        ]
+    ].
+    longest size = prefix size ifTrue:[
+        completionView flash.
+        ^self.
+    ].
+    editView insertStringAtCursor:(longest copyFrom: prefix size + 1).
+
+    "Created: / 31-03-2014 / 22:55:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 postKeyPress:key
@@ -133,14 +189,17 @@
 !CompletionController methodsFor:'private'!
 
 complete
-    | selection |
-
-    selection := completionView selection.
-    self closeCompletionView.
-    selection insert
+    self complete: completionView selection.
 
     "Created: / 27-09-2013 / 15:38:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 17-10-2013 / 01:08:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 31-03-2014 / 23:22:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+complete: item
+    self closeCompletionView.
+    item insert
+
+    "Created: / 31-03-2014 / 23:21:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 stopCompletionProcess