class: DoWhatIMeanSupport
authorClaus Gittinger <cg@exept.de>
Wed, 06 Nov 2013 16:41:48 +0100
changeset 4444 17e98954dad5
parent 4443 c7f5e10435e9
child 4445 c9eea14d6956
class: DoWhatIMeanSupport changed: #codeCompletionForMessage:into: cursor positioning after completion.
DoWhatIMeanSupport.st
--- a/DoWhatIMeanSupport.st	Tue Nov 05 12:32:42 2013 +0100
+++ b/DoWhatIMeanSupport.st	Wed Nov 06 16:41:48 2013 +0100
@@ -2312,8 +2312,9 @@
 
     editAction := 
         [:index |
-            |chosen parentsToInsert|
-
+            |crsrPos chosen parentsToInsert|
+
+            crsrPos := codeView characterPositionOfCursor.
             chosen := allBest at:index.
 
             chosen ~= selector ifTrue:[
@@ -2335,22 +2336,21 @@
 
                 codeView
                     undoableDo:[
-                        |newCursorPosition stop checkForArgumentTemplates|
-
-                        checkForArgumentTemplates := false.
-                        (selector isUnarySelector and:[chosen isKeywordSelector]) ifTrue:[ checkForArgumentTemplates := true ].
+                        |positionOfFirstArg newCursorPosition stop checkForArgumentTemplates
+                         newPart oldPartialToken start|
+
+                        checkForArgumentTemplates := (selector isUnarySelector and:[chosen isKeywordSelector]).
                         numArgs > nSelParts ifTrue:[
                             "/ new selector has more arguments; append them
                             stop := selectorParts last stop.
+                            codeView deleteFromCharacterPosition:stop+1 to:crsrPos-1.
 
                             "/ append the rest ...
                             numArgs downTo:nSelParts+1 do:[:idx |
                                 |newPart|
 
                                 newPart := newParts at:idx.
-                                (chosen endsWith:$:) ifTrue:[
-                                    newPart := newPart , ':'
-                                ].
+                                newPart := newPart , ':'.
 
                                 (codeView characterAtCharacterPosition:stop) == $: ifFalse:[
                                     newPart := ':' , newPart.
@@ -2358,16 +2358,17 @@
                                 newPart := (codeView characterAtCharacterPosition:stop) asString , newPart.
 
                                 codeView replaceFromCharacterPosition:stop to:stop with:newPart.
-                                "/ remember the last replacement's end as new cursor position
-                                newCursorPosition isNil ifTrue:[ newCursorPosition := stop + newPart size ].
+                                "/ remember the leftMost replacement's end as new cursor position
+                                newCursorPosition := stop + newPart size
                             ].
                             checkForArgumentTemplates := true.
                         ].
 
                         "/ replace existing parts
                         (nSelParts min:newParts size) downTo:1 do:[:idx |
-                            |newPart oldPartialToken start stop|
-
+                            |skipColon|
+
+                            skipColon := 0.
                             newPart := newParts at:idx.
                             oldPartialToken := selectorParts at:idx.
                             start := oldPartialToken start.
@@ -2375,7 +2376,9 @@
 
                             (chosen endsWith:$:) ifTrue:[
                                 (codeView characterAtCharacterPosition:stop+1) == $: ifFalse:[
-                                    newPart := newPart , ':'
+                                    newPart := newPart , ':'.
+                                ] ifTrue:[
+                                    skipColon := 1.
                                 ]
                             ] ifFalse:[
                                 (codeView characterAtCharacterPosition:stop) == $: ifTrue:[
@@ -2395,16 +2398,19 @@
         "/                            codeView replaceFromCharacterPosition:start to:stop with:newPart.
                             ].
 
-                            codeView replaceFromCharacterPosition:start to:stop with:newPart.
-
-                            oldLen := stop - start + 1.
-                            newLen := newPart size.
-
-                            "/ codeView selectFromCharacterPosition:start+oldLen to:start+newLen-1.
-                            "/ remember the last replacement's end as new cursor position
-                            newCursorPosition isNil ifTrue:[ newCursorPosition := stop + (newLen-oldLen) ].
+                            oldPartialToken value ~= newPart ifTrue:[
+                                codeView replaceFromCharacterPosition:start to:stop with:newPart.
+
+                                oldLen := stop - start + 1.
+                                newLen := newPart size.
+
+                                "/ codeView selectFromCharacterPosition:start+oldLen to:start+newLen-1.
+                                "/ remember the leftMost replacement's end as new cursor position
+                                newCursorPosition := start + newPart size + skipColon. "/ (newLen-oldLen) + 1.
+                                "/ codeView cursorToCharacterPosition:newCursorPosition.
+                            ].
                         ].
-                        codeView cursorToCharacterPosition:newCursorPosition.
+                        codeView cursorToCharacterPosition:newCursorPosition-1.
                         codeView cursorRight.  "/ avoid going to the next line !!
                         codeView dontReplaceSelectionOnInput.
 
@@ -2429,7 +2435,7 @@
 
     "Created: / 10-11-2006 / 13:18:27 / cg"
     "Modified: / 16-02-2010 / 10:33:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 27-07-2013 / 16:34:10 / cg"
+    "Modified: / 06-11-2013 / 16:40:51 / cg"
 !
 
 codeCompletionForMethodSpec:node 
@@ -4617,10 +4623,10 @@
 !DoWhatIMeanSupport class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libwidg2/DoWhatIMeanSupport.st,v 1.190 2013-10-28 14:16:03 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libwidg2/DoWhatIMeanSupport.st,v 1.191 2013-11-06 15:41:48 cg Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libwidg2/DoWhatIMeanSupport.st,v 1.190 2013-10-28 14:16:03 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libwidg2/DoWhatIMeanSupport.st,v 1.191 2013-11-06 15:41:48 cg Exp $'
 ! !