EditTextView.st
changeset 6015 9984d88f5e98
parent 5988 90a6f9e83f75
child 6023 1df17efc16f7
child 6031 01fbf7320331
--- a/EditTextView.st	Sun Dec 18 11:55:51 2016 +0100
+++ b/EditTextView.st	Fri Dec 23 10:04:14 2016 +0100
@@ -1725,6 +1725,192 @@
     "Modified: / 1.2.1998 / 13:15:55 / cg"
 ! !
 
+!EditTextView methodsFor:'commenting'!
+
+commentFrom:line1 to:line2 commentStrings:commentStrings
+    "helper function to comment out a block.
+     All lines from line1 to line2 get an end-of-line comment
+     in the first col
+     (if no eol comment is available, a bracketing comment is used)."
+
+    |eolComment opening closing|
+
+    eolComment := commentStrings at:1.
+    eolComment isNil ifTrue:[
+        opening := (commentStrings at:2) at:1.
+        closing := (commentStrings at:2) at:2.
+        (opening isNil or:[closing isNil]) ifTrue:[^ self].
+    ].
+
+    line1 to:line2 do:[:lineNr |
+        |l|
+
+        l := self listAt:lineNr.
+        l isNil ifTrue:[l := ''].
+        eolComment notNil ifTrue:[
+            l := eolComment , l
+        ] ifFalse:[
+            l := opening , l , closing
+        ].
+        self replaceLine:lineNr with:l.
+        widthOfWidestLine notNil ifTrue:[
+            widthOfWidestLine := widthOfWidestLine max:(self widthOfLineString:l).
+        ].
+    ].
+    self textChanged.
+
+    "Created: / 09-11-1997 / 01:05:35 / cg"
+    "Modified: / 09-10-2006 / 10:46:44 / cg"
+!
+
+commentSelection:commentStrings
+    "convenient function to comment out a block.
+     All lines from line1 to line2 get an end-of-line comment
+     in the first col."
+
+    |e commentPair opening closing|
+
+    (self checkModificationsAllowed) ifFalse:[ ^ self].
+    commentStrings isNil ifTrue:[ self beep. ^ self].
+
+    self
+        undoableDo:[ 
+            selectionStartLine isNil ifTrue:[
+                self commentFrom:cursorLine to:cursorLine commentStrings:commentStrings
+            ] ifFalse:[
+                (selectionStartCol == 1 and:[selectionEndCol == 0]) ifTrue:[
+                    self commentFrom:selectionStartLine to:selectionEndLine-1 commentStrings:commentStrings
+                ] ifFalse:[
+                    commentPair := commentStrings at:2 ifAbsent:nil.
+                    commentPair isNil ifTrue:[
+                        self beep.
+                    ] ifFalse:[
+                        opening := commentPair at:1.
+                        closing := commentPair at:2.
+                        (opening isNil or:[closing isNil]) ifTrue:[^ self].
+
+                        e := selectionEndCol.
+
+                        self insertString:closing atLine:selectionEndLine col:e+1.
+                        self insertString:opening atLine:selectionStartLine col:selectionStartCol.
+
+                        selectionStartLine == selectionEndLine ifTrue:[e := e + opening size].
+                        self selectFromLine:selectionStartLine col:selectionStartCol
+                                     toLine:selectionEndLine col:e+closing size.
+                    ]
+                ]
+            ].    
+        ]    
+        info:'Comment'.
+
+    "Created: / 9.11.1997 / 01:05:40 / cg"
+    "Modified: / 5.4.1998 / 16:52:23 / cg"
+!
+
+uncommentFrom:line1 to:line2 commentStrings:commentStrings
+    "helper function to comment out a block.
+     All lines from line1 to line2 get an end-of-line comment
+     in the first col.
+     (if no eol comment is available, a bracketing comment is removed)."
+
+    |eolComment opening closing rest|
+
+    eolComment := commentStrings at:1.
+    eolComment isNil ifTrue:[
+        opening := (commentStrings at:2) at:1.
+        closing := (commentStrings at:2) at:2.
+        (opening isNil or:[closing isNil]) ifTrue:[^ self].
+    ] ifFalse:[
+        rest := eolComment size + 1.
+    ].
+
+    line1 to:line2 do:[:lineNr |
+        |l|
+
+        l := self listAt:lineNr.
+        l notNil ifTrue:[
+            eolComment notNil ifTrue:[
+                (l startsWith:eolComment) ifTrue:[
+                    l := l copyFrom:rest
+                ] ifFalse:[
+                    (l withoutLeadingSeparators startsWith:eolComment) ifTrue:[
+                        "/ only for single lines?
+                        true "line1 = line2" ifTrue:[
+                            |numSpaces|
+
+                            numSpaces := l indexOfNonSeparator - 1.
+                            l := l copyFrom:numSpaces+1+rest.
+                            l := (String new:numSpaces),l
+                        ].    
+                    ].    
+                ]
+            ] ifFalse:[
+                ((l startsWith:opening) and:[l endsWith:closing]) ifTrue:[
+                    l := l copyFrom:opening size + 1.
+                    l := l copyButLast:closing size.
+                ].    
+            ].
+            self replaceLine:lineNr with:l.
+        ]
+    ].
+    
+    widthOfWidestLine := nil. "/ i.e. unknown
+    self textChanged.
+!
+
+uncommentSelection:commentStrings
+    "convenient function to comment out a block.
+     All lines from line1 to line2 get an end-of-line comment
+     in the first col."
+
+    |e commentPair opening closing sz1 sz2 l1 l2 c1 c2|
+
+    (self checkModificationsAllowed) ifFalse:[ ^ self].
+    commentStrings isNil ifTrue:[ self beep. ^ self].
+
+    self
+        undoableDo:[
+            selectionStartLine isNil ifTrue:[
+                self uncommentFrom:cursorLine to:cursorLine commentStrings:commentStrings
+            ] ifFalse:[
+                (selectionStartCol == 1 and:[selectionEndCol == 0]) ifTrue:[
+                    self uncommentFrom:selectionStartLine to:selectionEndLine-1 commentStrings:commentStrings
+                ] ifFalse:[
+                    commentPair := commentStrings at:2.
+                    opening := commentPair at:1.
+                    closing := commentPair at:2.
+                    (opening isNil or:[closing isNil]) ifTrue:[^ self].
+
+                    sz1 := opening size.
+                    sz2 := closing size.
+
+                    ((self
+                        stringAtLine:selectionStartLine
+                        from:selectionStartCol
+                        to:selectionStartCol+sz1 - 1) = opening
+                    and:[(self
+                        stringAtLine:selectionEndLine
+                        from:selectionEndCol - sz2 + 1
+                        to:selectionEndCol) = closing ]) ifTrue:[
+
+                        l2 := selectionEndLine.   c2 := selectionEndCol.
+                        l1 := selectionStartLine. c1 := selectionStartCol.
+                        self deleteCharsAtLine:l2 fromCol:c2-sz2+1 toCol:c2.
+                        self deleteCharsAtLine:l1 fromCol:c1 toCol:c1+sz1-1.
+
+                        e := c2 - sz2.
+                        l1 == l2 ifTrue:[e := e - sz1].
+                        self selectFromLine:l1 col:c1 toLine:l2 col:e.
+                    ]
+                ]
+            ].
+        ]   
+        info:'Uncomment'.
+
+    "Modified: / 7.1.1997 / 20:13:32 / cg"
+    "Created: / 9.11.1997 / 01:05:46 / cg"
+! !
+
 !EditTextView methodsFor:'cursor handling'!
 
 basicCursorReturn