generalized commenting/uncommenting.
Can now define the set of comment-strings
(for non-smalltalk language editing)
--- a/CodeView.st Sun Jan 05 02:17:32 1997 +0100
+++ b/CodeView.st Tue Jan 07 20:19:13 1997 +0100
@@ -11,7 +11,7 @@
"
Workspace subclass:#CodeView
- instanceVariableNames:'explainAction'
+ instanceVariableNames:'explainAction commentStrings'
classVariableNames:''
poolDictionaries:''
category:'Interface-Workspace'
@@ -40,7 +40,7 @@
explainAction to be performed for explain.
This action is to be defined by the user of this view
- (i.e. ususally the owning browser)
+ (i.e. usually the owning browser)
In addition, uncomment/comment are added to the controlMenu.
These are smalltalk specific - if you plan to edit other language code,
@@ -57,6 +57,15 @@
If required, simulate this by setting the doItAction and
explainAction, to notify the model manually about whats going on.
+ [instance variables:]
+ commentStrings <Array> an array with 2 entries;
+ the first defining the EOL-comment string,
+ the 2nd (another array) defining opening
+ and closing comment strings.
+ Default to ST/X comments,
+ can be changed in an instance for other
+ programming languages.
+
[author:]
Claus Gittinger
@@ -80,14 +89,60 @@
All lines from line1 to line2 get an end-of-line comment
in the first col."
- line1 to:line2 do:[:lineNr |
- |l|
+ |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].
+ ].
- l := self listAt:lineNr.
- l isNil ifTrue:[l := ''].
- self at:lineNr put:(('"' , '/') , l)
+ 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 at:lineNr put:l
].
self textChanged.
+
+ "Modified: 7.1.1997 / 20:15:06 / cg"
+!
+
+commentSelection
+ "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|
+
+ selectionStartLine notNil ifTrue:[
+ (selectionStartCol == 1 and:[selectionEndCol == 0]) ifTrue:[
+ self commentFrom:selectionStartLine to:selectionEndLine-1
+ ] ifFalse:[
+ commentPair := commentStrings at:2.
+ 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.
+ ]
+ ]
+
+ "Modified: 7.1.1997 / 20:13:25 / cg"
!
uncommentFrom:line1 to:line2
@@ -95,15 +150,81 @@
All lines from line1 to line2 get an end-of-line comment
in the first col."
+ |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|
- l := self listAt:lineNr.
- (l notNil and:[l startsWith:('"' ,'/')]) ifTrue:[
- self at:lineNr put:(l copyFrom:3)
- ]
+ l := self listAt:lineNr.
+ l notNil ifTrue:[
+ eolComment notNil ifTrue:[
+ (l startsWith:eolComment) ifTrue:[
+ l := l copyFrom:rest
+ ]
+ ] ifFalse:[
+ ((l startsWith:opening)
+ and:[l endsWith:closing]) ifTrue:[
+ l := l copyFrom:opening size + 1.
+ l := l copyWithoutLast:closing size.
+ ]
+ ].
+ self at:lineNr put:l
+ ]
].
self textChanged.
+
+ "Modified: 7.1.1997 / 20:17:44 / cg"
+!
+
+uncommentSelection
+ "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|
+
+ selectionStartLine notNil ifTrue:[
+ (selectionStartCol == 1 and:[selectionEndCol == 0]) ifTrue:[
+ self uncommentFrom:selectionStartLine to:selectionEndLine-1
+ ] 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:[
+
+ self deleteCharsAtLine:selectionEndLine fromCol:selectionEndCol-sz2+1 toCol:selectionEndCol.
+ self deleteCharsAtLine:selectionStartLine fromCol:selectionStartCol toCol:selectionStartCol+sz1-1.
+
+ e := selectionEndCol - sz2.
+ selectionStartLine == selectionEndLine ifTrue:[e := e - sz1].
+ self selectFromLine:selectionStartLine col:selectionStartCol
+ toLine:selectionEndLine col:e.
+ ]
+ ]
+ ]
+
+ "Modified: 7.1.1997 / 20:13:32 / cg"
! !
!CodeView methodsFor:'event handling'!
@@ -122,6 +243,17 @@
super keyPress:key x:x y:y
! !
+!CodeView methodsFor:'initialization'!
+
+initialize
+ super initialize.
+
+ commentStrings := #('"/'
+ ('"' '"'))
+
+ "Created: 7.1.1997 / 19:47:13 / cg"
+! !
+
!CodeView methodsFor:'menu & menu actions'!
accept
@@ -146,29 +278,6 @@
]
!
-commentSelection
- "convenient function to comment out a block.
- All lines from line1 to line2 get an end-of-line comment
- in the first col."
-
- |e|
-
- selectionStartLine notNil ifTrue:[
- (selectionStartCol == 1 and:[selectionEndCol == 0]) ifTrue:[
- self commentFrom:selectionStartLine to:selectionEndLine-1
- ] ifFalse:[
- self insert:$" atLine:selectionStartLine col:selectionStartCol.
- e := selectionEndCol + 1.
- selectionStartLine == selectionEndLine ifTrue:[e := e + 1].
- self insert:$" atLine:selectionEndLine col:e.
- self selectFromLine:selectionStartLine col:selectionStartCol
- toLine:selectionEndLine col:e.
- ]
- ]
-
- "Modified: 12.11.1996 / 11:32:40 / cg"
-!
-
editMenu
"return the popUpMenu;
to make this independent from what is defined in superclasses,
@@ -234,36 +343,10 @@
explainAction value:(self contents) value:(text asString)
]
]
-!
-
-uncommentSelection
- "convenient function to comment out a block.
- All lines from line1 to line2 get an end-of-line comment
- in the first col."
-
- |e|
-
- selectionStartLine notNil ifTrue:[
- (selectionStartCol == 1 and:[selectionEndCol == 0]) ifTrue:[
- self uncommentFrom:selectionStartLine to:selectionEndLine-1
- ] ifFalse:[
- ((self characterAtLine:selectionStartLine col:selectionStartCol) == $"
- and:[(self characterAtLine:selectionEndLine col:selectionEndCol) == $"]) ifTrue:[
- self deleteCharAtLine:selectionEndLine col:selectionEndCol.
- self deleteCharAtLine:selectionStartLine col:selectionStartCol.
- e := selectionEndCol - 1.
- selectionStartLine == selectionEndLine ifTrue:[e := e - 1].
- self selectFromLine:selectionStartLine col:selectionStartCol
- toLine:selectionEndLine col:e.
- ]
- ]
- ]
-
- "Modified: 12.11.1996 / 11:32:57 / cg"
! !
!CodeView class methodsFor:'documentation'!
version
- ^ '$Header: /cvs/stx/stx/libwidg/CodeView.st,v 1.31 1996-11-12 10:33:36 cg Exp $'
+ ^ '$Header: /cvs/stx/stx/libwidg/CodeView.st,v 1.32 1997-01-07 19:19:13 cg Exp $'
! !