Indentation fixes for electric-inserted code.
authorJan Vrany <jan.vrany@fit.cvut.cz>
Tue, 19 Nov 2013 12:48:26 +0000
changeset 144 a43236d0c411
parent 143 038fdc3940f3
child 145 94e4ee54e364
Indentation fixes for electric-inserted code.
SmallSense__EditSupport.st
SmallSense__SmalltalkEditSupport.st
--- a/SmallSense__EditSupport.st	Mon Nov 04 13:48:49 2013 -0300
+++ b/SmallSense__EditSupport.st	Tue Nov 19 12:48:26 2013 +0000
@@ -91,29 +91,59 @@
     "Created: / 22-10-2013 / 11:08:19 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
-insertElectric:text advanceCursorBy:offsetOrNil 
-    "Insert given text. If offsetOrNil is not nil, then
-     move cursor by `offsetOrNil` character after **beggining** of
-     inserted text."
+insertElectric:stringOrLines advanceCursorBy:offsetOrNil 
+    "Insert given stringOrLines. If offsetOrNil is not nil, then
+     move cursor by `offsetOrNil` character after **begining** of
+     inserted text.
+
+     `stringOrLines` could be either string or collection of strings (lines)
+     `offsetOrNil` could be either integer (cursor is then advanced by
+            offsetOrNil characters after **begining** of inserted text)
+            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)
+    "
+
+    | lineOffset colOffset newCursorCol newCursorLine advanceCursor |
+
+    advanceCursor := false.
+    offsetOrNil notNil ifTrue:[
+        lineOffset := offsetOrNil isPoint ifTrue:[offsetOrNil x] ifFalse:[0].
+        colOffset := offsetOrNil isPoint ifTrue:[offsetOrNil y] ifFalse:[offsetOrNil].
+
+        newCursorLine := textView cursorLine + lineOffset.
+        newCursorCol := (lineOffset == 0 ifTrue:[textView cursorCol] ifFalse:[0]) + colOffset.
+
+        advanceCursor := true.
+    ].
+
     
     self 
         insertDo:[
-            offsetOrNil isNil ifTrue:[
-                textView insertStringAtCursor:text
+            stringOrLines isString ifTrue:[
+                "/ Simple string...    
+                textView insertStringAtCursor:stringOrLines.
             ] ifFalse:[
-                | col |
+                "/ Couple lines...
+                textView 
+                    insertLines: stringOrLines
+                    withCR: false.   
+            ].
+            advanceCursor ifTrue:[
+                (textView cursorLine ~~ newCursorLine or:[textView cursorCol ~~ newCursorCol]) ifTrue:[
+                    textView cursorLine: newCursorLine col: newCursorCol.
+                ].
+            ].
 
-                col := textView cursorCol.
-                textView insertStringAtCursor:text.
-                textView cursorCol:col + offsetOrNil.
-            ].
+
         ].
 
     "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>"
 !
 
 insertElectricBlockOpenedBy: openText closedBy: closeText
-    | lineNr indent lines autoIndent |
+    | indent lines autoIndent |
 
     textView completionSupport notNil ifTrue:[
         textView completionSupport 
@@ -127,15 +157,11 @@
     textView autoIndent: false. 
     [
         textView undoableDo:[
-            lineNr :=  textView cursorLine.
             lines := Array 
                         with: openText ? ''
                         with: '' 
                         with: ((String new:indent withAll:Character space) , closeText ).
-            textView 
-                insertLines: lines
-                withCR: false.
-            textView cursorLine: lineNr + 1 col: indent + 5. 
+            self insertElectric: lines advanceCursorBy:  1 @ (indent + 5)
 
     "/        textView insertStringAtCursor: (openText ? '') , Character cr , Character cr,  , closeText , Character cr.
     "/        line := textView cursorLine - 1.
@@ -147,7 +173,7 @@
     ].
 
     "Created: / 25-07-2013 / 10:41:50 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 22-10-2013 / 17:10:33 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 19-11-2013 / 12:29:49 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 insertElectricSnippet    
--- a/SmallSense__SmalltalkEditSupport.st	Mon Nov 04 13:48:49 2013 -0300
+++ b/SmallSense__SmalltalkEditSupport.st	Tue Nov 19 12:48:26 2013 +0000
@@ -204,7 +204,7 @@
 !
 
 keyPressReturn
-    | line tokens i t |
+    | line tokens i t currentLineIndent |
 
     line := textView listAt: textView cursorLine.
     line isNil ifTrue:[ ^ false ].
@@ -213,15 +213,12 @@
     (line indexOfAny:'[|/') == 0 ifTrue:[ ^ false ].
 
     "/ Insert "/ at the beggining of the line if current line starts with "/
-    i := line indexOfNonSeparator.
+    i := currentLineIndent := line indexOfNonSeparator.
     (i ~~ 0 and:[ i < line size and:[(line at:i) == $" and:[(line at:i + 1) == $/]]]) ifTrue:[
-        "/ OK, current line contains eol-comment
-        self insertDo:[
-             textView insertCharAtCursor: Character cr. 
-        ].
-        self insertDo:[
-            textView insertStringAtCursor: '"/ '
-        ].
+        "/ OK, current line contains eol-comment. Split into
+        "/ two actions so backspace deletes only the inserted '"/ ' text
+        self insertElectric: #('' '') advanceCursorBy: (1 @ i).
+        self insertElectric: '"/ '.
         ^ true   
     ].
 
@@ -253,9 +250,7 @@
         [ i > 0 and:[ (tokens at: i) == #Identifier ] ] whileTrue:[ i := i - 1 ].
         (i ~~ 0 and: [(tokens at: i) == $|]) ifTrue:[
             RBFormatter emptyLineAfterTemporaries ifTrue:[
-                textView undoableDo:[
-                    textView insertStringAtCursor: (Character cr asString , Character cr , Character cr)
-                ].
+                self insertElectric:#('' '' '')  advanceCursorBy: 2 @ currentLineIndent.
                 ^ true
             ]
         ]
@@ -263,7 +258,7 @@
     ^ false.
 
     "Created: / 25-07-2013 / 00:02:56 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 04-10-2013 / 08:03:50 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified (comment): / 19-11-2013 / 12:41:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 keyPressReturnToken