CodeView.st
changeset 1396 4e019d312d6f
parent 1293 c0806b73dc48
child 1516 677fbbd9b32f
equal deleted inserted replaced
1395:cbadcf93c173 1396:4e019d312d6f
     9  other person.  No title to or ownership of the software is
     9  other person.  No title to or ownership of the software is
    10  hereby transferred.
    10  hereby transferred.
    11 "
    11 "
    12 
    12 
    13 Workspace subclass:#CodeView
    13 Workspace subclass:#CodeView
    14 	instanceVariableNames:'explainAction commentStrings'
    14 	instanceVariableNames:'explainAction'
    15 	classVariableNames:''
    15 	classVariableNames:''
    16 	poolDictionaries:''
    16 	poolDictionaries:''
    17 	category:'Interface-Workspace'
    17 	category:'Interface-Workspace'
    18 !
    18 !
    19 
    19 
    74 "
    74 "
    75 ! !
    75 ! !
    76 
    76 
    77 !CodeView methodsFor:'accessing'!
    77 !CodeView methodsFor:'accessing'!
    78 
    78 
    79 commentStrings:anArrayOfCommentStrings
       
    80     "define the comment strings"
       
    81 
       
    82     commentStrings := anArrayOfCommentStrings
       
    83 
       
    84     "Created: 7.1.1997 / 20:25:06 / cg"
       
    85 !
       
    86 
       
    87 explainAction:aBlock
    79 explainAction:aBlock
    88     "set the action to be performed on explain"
    80     "set the action to be performed on explain"
    89 
    81 
    90     explainAction := aBlock
    82     explainAction := aBlock
    91 ! !
       
    92 
       
    93 !CodeView methodsFor:'editing'!
       
    94 
       
    95 commentFrom:line1 to:line2
       
    96     "convenient function to comment out a block.
       
    97      All lines from line1 to line2 get an end-of-line comment
       
    98      in the first col."
       
    99 
       
   100     |eolComment opening closing|
       
   101 
       
   102     eolComment := commentStrings at:1.
       
   103     eolComment isNil ifTrue:[
       
   104         opening := (commentStrings at:2) at:1.
       
   105         closing := (commentStrings at:2) at:2.
       
   106         (opening isNil or:[closing isNil]) ifTrue:[^ self].
       
   107     ].
       
   108 
       
   109     line1 to:line2 do:[:lineNr |
       
   110         |l|
       
   111 
       
   112         l := self listAt:lineNr.
       
   113         l isNil ifTrue:[l := ''].
       
   114         eolComment notNil ifTrue:[
       
   115             l := eolComment , l
       
   116         ] ifFalse:[
       
   117             l := opening , l , closing
       
   118         ].
       
   119         self at:lineNr put:l
       
   120     ].
       
   121     self textChanged.
       
   122 
       
   123     "Modified: 7.1.1997 / 20:15:06 / cg"
       
   124 !
       
   125 
       
   126 commentSelection
       
   127     "convenient function to comment out a block.
       
   128      All lines from line1 to line2 get an end-of-line comment
       
   129      in the first col."
       
   130 
       
   131     |e commentPair opening closing|
       
   132 
       
   133     selectionStartLine notNil ifTrue:[
       
   134         (selectionStartCol == 1 and:[selectionEndCol == 0]) ifTrue:[
       
   135             self commentFrom:selectionStartLine to:selectionEndLine-1
       
   136         ] ifFalse:[
       
   137             commentPair := commentStrings at:2.
       
   138             opening := commentPair at:1.
       
   139             closing := commentPair at:2.
       
   140             (opening isNil or:[closing isNil]) ifTrue:[^ self].
       
   141 
       
   142             e := selectionEndCol.
       
   143 
       
   144             self insertString:closing atLine:selectionEndLine col:e+1.
       
   145             self insertString:opening atLine:selectionStartLine col:selectionStartCol.
       
   146 
       
   147             selectionStartLine == selectionEndLine ifTrue:[e := e + opening size].
       
   148             self selectFromLine:selectionStartLine col:selectionStartCol
       
   149                          toLine:selectionEndLine col:e+closing size.
       
   150         ]
       
   151     ]
       
   152 
       
   153     "Modified: 7.1.1997 / 20:13:25 / cg"
       
   154 !
       
   155 
       
   156 uncommentFrom:line1 to:line2
       
   157     "convenient function to comment out a block.
       
   158      All lines from line1 to line2 get an end-of-line comment
       
   159      in the first col."
       
   160 
       
   161     |eolComment opening closing rest|
       
   162 
       
   163     eolComment := commentStrings at:1.
       
   164     eolComment isNil ifTrue:[
       
   165         opening := (commentStrings at:2) at:1.
       
   166         closing := (commentStrings at:2) at:2.
       
   167         (opening isNil or:[closing isNil]) ifTrue:[^ self].
       
   168     ] ifFalse:[
       
   169         rest := eolComment size + 1.
       
   170     ].
       
   171 
       
   172     line1 to:line2 do:[:lineNr |
       
   173         |l|
       
   174 
       
   175         l := self listAt:lineNr.
       
   176         l notNil ifTrue:[
       
   177             eolComment notNil ifTrue:[
       
   178                 (l startsWith:eolComment) ifTrue:[
       
   179                     l := l copyFrom:rest
       
   180                 ]
       
   181             ] ifFalse:[
       
   182                 ((l startsWith:opening)
       
   183                 and:[l endsWith:closing]) ifTrue:[
       
   184                     l := l copyFrom:opening size + 1.
       
   185                     l := l copyWithoutLast:closing size.
       
   186                 ]
       
   187             ].
       
   188             self at:lineNr put:l
       
   189         ]
       
   190     ].
       
   191     self textChanged.
       
   192 
       
   193     "Modified: 7.1.1997 / 20:17:44 / cg"
       
   194 !
       
   195 
       
   196 uncommentSelection
       
   197     "convenient function to comment out a block.
       
   198      All lines from line1 to line2 get an end-of-line comment
       
   199      in the first col."
       
   200 
       
   201     |e commentPair opening closing sz1 sz2|
       
   202 
       
   203     selectionStartLine notNil ifTrue:[
       
   204         (selectionStartCol == 1 and:[selectionEndCol == 0]) ifTrue:[
       
   205             self uncommentFrom:selectionStartLine to:selectionEndLine-1
       
   206         ] ifFalse:[
       
   207             commentPair := commentStrings at:2.
       
   208             opening := commentPair at:1.
       
   209             closing := commentPair at:2.
       
   210             (opening isNil or:[closing isNil]) ifTrue:[^ self].
       
   211 
       
   212             sz1 := opening size.
       
   213             sz2 := closing size.
       
   214 
       
   215             ((self 
       
   216                 stringAtLine:selectionStartLine 
       
   217                 from:selectionStartCol
       
   218                 to:selectionStartCol+sz1 - 1) = opening
       
   219             and:[(self 
       
   220                 stringAtLine:selectionEndLine 
       
   221                 from:selectionEndCol - sz2 + 1
       
   222                 to:selectionEndCol) = closing ]) ifTrue:[
       
   223 
       
   224                 self deleteCharsAtLine:selectionEndLine fromCol:selectionEndCol-sz2+1 toCol:selectionEndCol.
       
   225                 self deleteCharsAtLine:selectionStartLine fromCol:selectionStartCol toCol:selectionStartCol+sz1-1.
       
   226 
       
   227                 e := selectionEndCol - sz2.
       
   228                 selectionStartLine == selectionEndLine ifTrue:[e := e - sz1].
       
   229                 self selectFromLine:selectionStartLine col:selectionStartCol
       
   230                              toLine:selectionEndLine col:e.
       
   231             ]
       
   232         ]
       
   233     ]
       
   234 
       
   235     "Modified: 7.1.1997 / 20:13:32 / cg"
       
   236 ! !
    83 ! !
   237 
    84 
   238 !CodeView methodsFor:'event handling'!
    85 !CodeView methodsFor:'event handling'!
   239 
    86 
   240 keyPress:key x:x y:y
    87 keyPress:key x:x y:y
   249     (key == #UncommentSelection)  ifTrue:[^ self uncommentSelection].
    96     (key == #UncommentSelection)  ifTrue:[^ self uncommentSelection].
   250 
    97 
   251     super keyPress:key x:x y:y
    98     super keyPress:key x:x y:y
   252 
    99 
   253     "Modified: 9.1.1997 / 12:14:00 / cg"
   100     "Modified: 9.1.1997 / 12:14:00 / cg"
   254 ! !
       
   255 
       
   256 !CodeView methodsFor:'initialization'!
       
   257 
       
   258 initialize
       
   259     super initialize.
       
   260 
       
   261     commentStrings := #('"/'
       
   262                         ('"' '"'))
       
   263 
       
   264     "Created: 7.1.1997 / 19:47:13 / cg"
       
   265 ! !
   101 ! !
   266 
   102 
   267 !CodeView methodsFor:'menu & menu actions'!
   103 !CodeView methodsFor:'menu & menu actions'!
   268 
   104 
   269 accept
   105 accept
   306 
   142 
   307     |m sub|
   143     |m sub|
   308 
   144 
   309     m := super editMenu.
   145     m := super editMenu.
   310 
   146 
   311     self sensor ctrlDown ifTrue:[
   147     self sensor ctrlDown ifFalse:[
   312         m addLabels:(resources array:#('-' 'commentIt' 'uncommentIt'))
       
   313           selectors:#(nil commentSelection uncommentSelection)
       
   314           accelerators:#(nil CommentSelection UncommentSelection)
       
   315           after:#again.
       
   316 
       
   317         self hasSelection ifFalse:[
       
   318             m disableAll:#(commentSelection uncommentSelection) 
       
   319         ].
       
   320     ] ifFalse:[
       
   321 
       
   322         "
   148         "
   323          codeViews do support #accept
   149          codeViews do support #accept
   324          ... add it after #inspectIt
   150          ... add it after #inspectIt
   325         "
   151         "
   326         m addLabels:(resources array:#('-' 'accept'))
   152         m addLabels:(resources array:#('-' 'accept'))
   348             ].
   174             ].
   349         ].
   175         ].
   350     ].
   176     ].
   351     ^ m.
   177     ^ m.
   352 
   178 
   353     "Modified: 1.8.1997 / 21:47:55 / cg"
   179     "Modified: / 9.11.1997 / 01:07:03 / cg"
   354 !
   180 !
   355 
   181 
   356 explain
   182 explain
   357     "explain action;
   183     "explain action;
   358      evaluate the explainBlock passing whole contents and 
   184      evaluate the explainBlock passing whole contents and 
   369 ! !
   195 ! !
   370 
   196 
   371 !CodeView class methodsFor:'documentation'!
   197 !CodeView class methodsFor:'documentation'!
   372 
   198 
   373 version
   199 version
   374     ^ '$Header: /cvs/stx/stx/libwidg/CodeView.st,v 1.37 1997-08-01 19:48:27 cg Exp $'
   200     ^ '$Header: /cvs/stx/stx/libwidg/CodeView.st,v 1.38 1997-11-11 13:52:51 cg Exp $'
   375 ! !
   201 ! !