SmallSense__CompletionController.st
changeset 118 88e6fd734a11
parent 117 441529422c2f
child 123 1b949542c4b2
--- a/SmallSense__CompletionController.st	Wed Oct 02 13:37:01 2013 +0100
+++ b/SmallSense__CompletionController.st	Thu Oct 03 11:41:07 2013 +0100
@@ -3,12 +3,21 @@
 "{ NameSpace: SmallSense }"
 
 EditTextViewCompletionSupport subclass:#CompletionController
-	instanceVariableNames:'support'
+	instanceVariableNames:'support seqno'
 	classVariableNames:''
 	poolDictionaries:''
 	category:'SmallSense-Core'
 !
 
+
+!CompletionController class methodsFor:'instance creation'!
+
+new
+    "return an initialized instance"
+
+    ^ self basicNew initialize.
+! !
+
 !CompletionController methodsFor:'accessing'!
 
 support
@@ -61,21 +70,18 @@
 !
 
 postKeyPress:key
+    seqno := seqno + 1.
+    seqno == SmallInteger maxVal ifTrue:[
+        seqno := 0.
+    ].
+
     UserPreferences current immediateCodeCompletion ifFalse:[
         "/ only update, if already open
         completionView isNil ifTrue:[^ self].
     ].
 
     (key == #BackSpace or:[key == #BasicBackspace]) ifTrue:[
-        | c |
-
-        c := editView characterBeforeCursor.
-        (c notNil and:[c isLetterOrDigit]) ifTrue:[
-            autoSelect := false.
-            self updateCompletionList.
-        ] ifFalse:[
-             self closeCompletionView.
-        ].
+        self closeCompletionView.
         ^ self
     ].
 
@@ -83,14 +89,36 @@
         key isLetterOrDigit not ifTrue:[
             self closeCompletionView
         ] ifFalse:[
-            autoSelect := false.
-            self updateCompletionList.
+            | c |
+
+            c := editView characterBeforeCursor.
+            (c notNil and:[c isLetterOrDigit]) ifTrue:[
+                c := editView characterUnderCursor.
+                c isSeparator ifTrue:[
+                    autoSelect := false.
+                    self updateCompletionList.
+                ].
+            ]
         ].
         ^ self
     ].
 
     "Created: / 28-09-2013 / 00:21:00 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 30-09-2013 / 14:55:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 03-10-2013 / 11:01:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!CompletionController methodsFor:'initialization'!
+
+initialize
+    "Invoked when a new instance is created."
+
+    "/ please change as required (and remove this comment)
+    "/ support := nil.
+    seqno := 0.
+
+    "/ super initialize.   -- commented since inherited method does nothing
+
+    "Modified: / 03-10-2013 / 07:11:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !CompletionController methodsFor:'private'!
@@ -102,6 +130,22 @@
     "Created: / 27-09-2013 / 15:38:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
+startCompletionProcess
+    super startCompletionProcess
+
+    "Created: / 03-10-2013 / 10:57:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+stopCompletionProcess
+    "kill any background completion process"
+
+    editView sensor flushUserEventsFor: self.     
+    super stopCompletionProcess
+
+    "Created: / 02-10-2013 / 15:09:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 03-10-2013 / 11:03:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
 updateCompletionList
     "called for keypress events"
 
@@ -136,22 +180,40 @@
 
 !CompletionController methodsFor:'private-API'!
 
+closeCompletionView
+    |v|
+
+    (v := completionView) notNil ifTrue:[
+        completionView := nil.
+        "/ let it close itself - avoids synchronization problems
+        v sensor
+            pushUserEvent:#value
+            for:[ v topView destroy ].
+        v sensor flushEventsFor: self. 
+    ].
+
+    "Created: / 02-10-2013 / 13:57:27 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
 computeCompletions
     "Actually compute the completions and update the completion view."  
 
     | completions |
 
+    editView sensor flushUserEventsFor: self.
+
     "/ Wait a while to give user chance finish typing.
     "/ This also reduces CPU consumption by avoiding
     "/ useless computation
     Delay waitForMilliseconds: 200. 
+"/    self updateCompletions: support computeCompletion
     completions := support computeCompletion.
     completions notEmptyOrNil ifTrue:[
-        editView sensor pushUserEvent: #updateCompletions: for: self withArgument: completions.
+        editView sensor pushUserEvent: #updateCompletions:sequence: for: self withArguments: (Array with: completions with: seqno)
     ].
 
     "Created: / 27-09-2013 / 13:12:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 30-09-2013 / 11:53:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 03-10-2013 / 07:17:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 openCompletionView
@@ -163,7 +225,7 @@
 openCompletionView: list
     "Makes sure the completion view is opened and with given `list`."
     
-    | movePos topView x y|
+    | movePos topView x y  windowExtent screenExtent |
     "/ move the window
 
     list isEmpty ifTrue:[ ^ self ].
@@ -171,13 +233,20 @@
     x := (editView xOfCol:editView cursorCol  inVisibleLine:editView cursorLine)
             - 16"icon" - (editView widthOfString:  support wordBeforeCursor) - 5"magic constant".
     y := editView yOfCursor + editView font maxHeight + 3.
-    movePos := (editView originRelativeTo: nil) + (x @ y).           
+    movePos := (editView originRelativeTo: nil) + (x @ y).
 
     completionView isNil ifTrue:[
+
         completionView := CompletionView new.
         completionView list:list.
         completionView font: editView font.
         topView := completionView.
+
+        windowExtent := completionView extent copy.
+        screenExtent := Screen current monitorBoundsAt: movePos.
+        (screenExtent height) < (movePos y + windowExtent y) ifTrue:[
+            movePos y: (movePos y - windowExtent y - editView font maxHeight - 5).
+        ].
         topView origin:movePos.
 "/        topView resizeToFit.
         self updateSelection.
@@ -193,12 +262,22 @@
     ].
 
     "Created: / 27-09-2013 / 14:01:27 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 27-09-2013 / 23:54:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 03-10-2013 / 07:10:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
-updateCompletions: completionResult
-    self openCompletionView: completionResult
+updateCompletions: completionResult sequence: sequence
+    seqno == sequence ifTrue:[
+        self openCompletionView: completionResult 
+    ].
 
-    "Created: / 27-09-2013 / 13:52:43 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Created: / 03-10-2013 / 07:14:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 03-10-2013 / 11:02:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
+!CompletionController class methodsFor:'documentation'!
+
+version_HG
+
+    ^ '$Changeset: <not expanded> $'
+! !
+