Initial support for ignoring keystrokes to avoid duplicate text when electric insert is active.
authorJan Vrany <jan.vrany@fit.cvut.cz>
Mon, 20 Jan 2014 09:34:34 +0000
changeset 155 d792aed09149
parent 154 b96fbde91144
child 156 9b02027653ed
Initial support for ignoring keystrokes to avoid duplicate text when electric insert is active.
SmallSense__EditSupport.st
SmallSense__JavaEditSupport.st
SmallSense__SettingsAppl.st
SmallSense__SmalltalkEditSupport.st
smallsense.rc
--- a/SmallSense__EditSupport.st	Sat Jan 18 23:41:04 2014 +0000
+++ b/SmallSense__EditSupport.st	Mon Jan 20 09:34:34 2014 +0000
@@ -3,7 +3,8 @@
 "{ NameSpace: SmallSense }"
 
 Object subclass:#EditSupport
-	instanceVariableNames:'service textView backspaceIsUndo completionController snippets'
+	instanceVariableNames:'service textView backspaceIsUndo completionController snippets
+		ignoreKeystrokes ignoreKeystrokesPosition'
 	classVariableNames:''
 	poolDictionaries:''
 	category:'SmallSense-Core-Services'
@@ -92,9 +93,20 @@
 !
 
 insertElectric:stringOrLines advanceCursorBy:offsetOrNil 
+    ^ self insertElectric:stringOrLines advanceCursorBy:offsetOrNil ignoreKeystrokes: nil
+
+    "Created: / 22-10-2013 / 11:56:43 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 19-01-2014 / 20:29:37 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+insertElectric:stringOrLines advanceCursorBy:offsetOrNil ignoreKeystrokes: ignoreKeystrokeSequence 
     "Insert given stringOrLines. If offsetOrNil is not nil, then
-     move cursor by `offsetOrNil` character after **begining** of
-     inserted text.
+     move cursor by `offsetOrNil` after the **begining** of
+     inserted text. If `ignoreKeystrokeSequence` is not nil and not empty, then if
+     subsequent key strokes are ignored (i.e, does nothing) if matches
+     the given sequence. This is used to avoid duplication if user is not
+     aware of electric insertion and types whole text that has been
+     (electrically) inserted).
 
      `stringOrLines` could be either string or collection of strings (lines)
      `offsetOrNil` could be either integer (cursor is then advanced by
@@ -102,6 +114,8 @@
             or point (x,y, cursor is then advanced by x lines after current
             line and by y characters after beggining of the inserted text
             (if x == 0) or at set at column y (if x ~~ 0)
+     `ignoreKeystrokeSequence` a sequenceable collection of keys (in a form
+            as passed to #keyPress:x:y: method.
     "
 
     | lineOffset colOffset newCursorCol newCursorLine advanceCursor |
@@ -134,12 +148,14 @@
                     textView cursorLine: newCursorLine col: newCursorCol.
                 ].
             ].
-
+        ].
+    ignoreKeystrokeSequence notEmptyOrNil ifTrue:[
+        ignoreKeystrokes := ignoreKeystrokeSequence.
+        ignoreKeystrokesPosition := 1.
+    ].
 
-        ].
-
-    "Created: / 22-10-2013 / 11:56:43 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 19-11-2013 / 12:30:52 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Created: / 19-01-2014 / 20:29:00 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 20-01-2014 / 09:24:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 insertElectricBlockOpenedBy: openText closedBy: closeText
@@ -187,7 +203,18 @@
 keyPress: key x:x y:y in: view
     "Handles an event in given view (a subview of codeView).
      If the method returns true, the event will not be processed
-     by the view."
+     by the view.
+
+     IMPORTANT: Never ever call `^ super keyPress: key x:x y:y in: view`,
+     as keyPresIgnore... advances position and calling keyPressIgnore here
+     and calling super would advance it twice!!
+     "
+
+    view ~~ textView ifTrue:[ ^ false ].
+
+    (self keyPressIgnored: key) ifTrue:[
+        ^ true.
+    ].
 
     key == Character space ifTrue:[
         ^ self insertElectricSnippet
@@ -196,7 +223,30 @@
     ^false
 
     "Created: / 24-07-2013 / 23:31:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 22-10-2013 / 01:56:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified (format): / 20-01-2014 / 09:20:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+keyPressIgnored: key
+    "raise an error: this method should be implemented (TODO)"
+
+    ignoreKeystrokes notNil ifTrue:[
+        ^ (ignoreKeystrokes at: ignoreKeystrokesPosition) == key ifTrue:[
+            "/ Key stroke should be ignored...
+            ignoreKeystrokesPosition := ignoreKeystrokesPosition + 1.
+            ignoreKeystrokesPosition > ignoreKeystrokes size ifTrue:[
+                "/ Nil out instvars if there's no more keys to ignore.
+                ignoreKeystrokes := ignoreKeystrokesPosition := nil.
+            ].
+            true.
+        ] ifFalse:[
+            "/ Nil out instvars, user typed something else!!
+            ignoreKeystrokes := ignoreKeystrokesPosition := nil.
+            false.
+        ].
+    ].
+    ^ false.
+
+    "Created: / 20-01-2014 / 09:11:17 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 keyPressSpace
--- a/SmallSense__JavaEditSupport.st	Sat Jan 18 23:41:04 2014 +0000
+++ b/SmallSense__JavaEditSupport.st	Mon Jan 20 09:34:34 2014 +0000
@@ -125,6 +125,10 @@
 
     view ~~ textView ifTrue:[ ^ false ].
 
+    (self keyPressIgnored: key) ifTrue:[
+        ^ true.
+    ]. 
+
     lastTypedKey3 := lastTypedKey2.
     lastTypedKey2 := lastTypedKey1.
     lastTypedKey1 := lastTypedKey0.
@@ -134,10 +138,14 @@
         ^ self keyPressOpenCurly
     ].
 
-    ^ super keyPress: key x:x y:y in: view
+    key == Character space ifTrue:[
+        ^ self insertElectricSnippet
+    ].
+
+    ^ false
 
     "Created: / 07-03-2010 / 09:36:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 22-10-2013 / 01:55:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 20-01-2014 / 09:20:28 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 keyPressOpenCurly
--- a/SmallSense__SettingsAppl.st	Sat Jan 18 23:41:04 2014 +0000
+++ b/SmallSense__SettingsAppl.st	Mon Jan 20 09:34:34 2014 +0000
@@ -269,11 +269,11 @@
        smallSenseBackgroundLintEnabled
        smallSenseBackgroundTypingEnabled
        smallSenseElectricEditSupportEnabled
-       smallSenseCompleteIfUnambiguousa
+       smallSenseCompleteIfUnambiguous
 
     )
 
-    "Modified: / 18-01-2014 / 23:36:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 20-01-2014 / 09:23:50 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 basicReadSettings
--- a/SmallSense__SmalltalkEditSupport.st	Sat Jan 18 23:41:04 2014 +0000
+++ b/SmallSense__SmalltalkEditSupport.st	Mon Jan 20 09:34:34 2014 +0000
@@ -39,6 +39,17 @@
 
 !SmalltalkEditSupport methodsFor:'editing'!
 
+insertElectric:stringOrLines advanceCursorBy:offsetOrNil
+    | ignore |
+
+    (stringOrLines isString and:[stringOrLines first == lastTypedKey0] ) ifTrue:[
+        ignore := stringOrLines copyFrom: 2.
+    ].
+    ^ self insertElectric:stringOrLines advanceCursorBy:offsetOrNil ignoreKeystrokes: ignore
+
+    "Created: / 20-01-2014 / 09:27:00 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
 insertElectricSnippet
     lastTypedKey0 == Character space ifTrue:[
         ^ self insertElectricSnippetAfterSpace
@@ -144,6 +155,10 @@
 
     view ~~ textView ifTrue:[ ^ false ].
 
+    (self keyPressIgnored: key) ifTrue:[
+        ^ true.
+    ]. 
+
     lastTypedKey3 := lastTypedKey2.
     lastTypedKey2 := lastTypedKey1.
     lastTypedKey1 := lastTypedKey0.
@@ -174,10 +189,14 @@
         ^ self keyPressEqual
     ].
 
-    ^ super keyPress: key x:x y:y in: view
+    key == Character space ifTrue:[
+        ^ self insertElectricSnippet
+    ].
+
+    ^ false.
 
     "Created: / 07-03-2010 / 09:36:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 22-10-2013 / 11:09:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 20-01-2014 / 09:20:33 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 keyPressDoubleColon
--- a/smallsense.rc	Sat Jan 18 23:41:04 2014 +0000
+++ b/smallsense.rc	Mon Jan 20 09:34:34 2014 +0000
@@ -25,7 +25,7 @@
       VALUE "LegalCopyright", "Copyright Claus Gittinger 1988-2011\nCopyright eXept Software AG 1998-2011\0"
       VALUE "ProductName", "Smalltalk/X\0"
       VALUE "ProductVersion", "6.2.3.0\0"
-      VALUE "ProductDate", "Sat, 18 Jan 2014 23:39:25 GMT\0"
+      VALUE "ProductDate", "Mon, 20 Jan 2014 09:32:44 GMT\0"
     END
 
   END