author | Claus Gittinger <cg@exept.de> |
Sat, 11 Nov 1995 17:23:54 +0100 | |
changeset 174 | d80a6cc3f9b2 |
parent 136 | 80d2240b2cdc |
child 201 | d2888811c664 |
permissions | -rw-r--r-- |
0 | 1 |
" |
4 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
70 | 3 |
All Rights Reserved |
0 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
13 |
Workspace subclass:#CodeView |
|
125 | 14 |
instanceVariableNames:'explainAction' |
0 | 15 |
classVariableNames:'' |
16 |
poolDictionaries:'' |
|
17 |
category:'Interface-Workspace' |
|
18 |
! |
|
19 |
||
32 | 20 |
!CodeView class methodsFor:'documentation'! |
21 |
||
38 | 22 |
copyright |
23 |
" |
|
24 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
70 | 25 |
All Rights Reserved |
38 | 26 |
|
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
36 |
version |
|
174
d80a6cc3f9b2
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
136
diff
changeset
|
37 |
^ '$Header: /cvs/stx/stx/libwidg/CodeView.st,v 1.21 1995-11-11 16:19:21 cg Exp $' |
38 | 38 |
! |
39 |
||
32 | 40 |
documentation |
41 |
" |
|
125 | 42 |
a view for text which is known to be smalltalk code. |
43 |
It adds explain to the menu, and defines another action: |
|
44 |
explainAction to be performed for explain. |
|
32 | 45 |
|
125 | 46 |
This action is to be defined by the user of this view |
38 | 47 |
(i.e. ususally the owning browser) |
121 | 48 |
|
125 | 49 |
If used with a model, accept sends the changeMsg to it (as defined in EditTextView). |
121 | 50 |
(however, it is possible to define moth changeMsg and acceptAction) |
123 | 51 |
|
52 |
||
53 |
Caveat: |
|
54 |
in this version, CodeView does not yet support MVC setups for doIt |
|
55 |
and explain. |
|
56 |
If required, simulate this by setting the doItAction and |
|
57 |
explainAction, to notify the model manually about whats going on. |
|
32 | 58 |
" |
59 |
! ! |
|
60 |
||
0 | 61 |
!CodeView methodsFor:'initialization'! |
62 |
||
97 | 63 |
editMenu |
121 | 64 |
"return the popUpMenu; |
65 |
to make this independent from what is defined in superclasses, |
|
66 |
get the superclass menu and add my functions." |
|
131 | 67 |
|
97 | 68 |
|m sub idx| |
38 | 69 |
|
97 | 70 |
m := super editMenu. |
0 | 71 |
|
131 | 72 |
self sensor ctrlDown ifTrue:[ |
73 |
m addLabels:(resources array:#('-' 'commentIt' 'uncommentIt')) |
|
74 |
selectors:#(nil commentSelection uncommentSelection) |
|
75 |
after:(m labels size). |
|
76 |
self hasSelection ifFalse:[ |
|
77 |
m disableAll:#(commentSelection uncommentSelection) |
|
78 |
]. |
|
79 |
] ifFalse:[ |
|
38 | 80 |
|
131 | 81 |
" |
82 |
codeViews do support #accept |
|
83 |
... add it after #inspectIt |
|
84 |
" |
|
85 |
idx := m indexOf:#inspectIt. |
|
86 |
idx ~~ 0 ifTrue:[ |
|
87 |
m addLabels:(resources array:#('-' 'accept')) |
|
88 |
selectors:#(nil accept) |
|
89 |
after:idx. |
|
90 |
]. |
|
91 |
||
92 |
" |
|
93 |
and add #explain after $gotoLine in the extra menu |
|
94 |
" |
|
95 |
sub := m subMenuAt:#others. |
|
96 |
sub notNil ifTrue:[ |
|
97 |
idx := sub indexOf:#gotoLine. |
|
98 |
sub addLabels:(resources array:#('-' 'explain')) |
|
99 |
selectors:#(nil explain) |
|
100 |
after:idx. |
|
101 |
self hasSelection ifFalse:[ |
|
102 |
sub disable:#explain |
|
103 |
]. |
|
97 | 104 |
]. |
59 | 105 |
]. |
131 | 106 |
^ m. |
107 |
! ! |
|
97 | 108 |
|
131 | 109 |
!CodeView methodsFor:'editing'! |
110 |
||
111 |
uncommentFrom:line1 to:line2 |
|
112 |
"convenient function to comment out a block. |
|
113 |
All lines from line1 to line2 get an end-of-line comment |
|
114 |
in the first col." |
|
115 |
||
116 |
line1 to:line2 do:[:lineNr | |
|
117 |
|l| |
|
118 |
||
119 |
l := self listAt:lineNr. |
|
120 |
(l notNil and:[l startsWith:('"' ,'/')]) ifTrue:[ |
|
121 |
self at:lineNr put:(l copyFrom:3) |
|
122 |
] |
|
123 |
]. |
|
124 |
self textChanged. |
|
125 |
! |
|
126 |
||
127 |
commentFrom:line1 to:line2 |
|
128 |
"convenient function to comment out a block. |
|
129 |
All lines from line1 to line2 get an end-of-line comment |
|
130 |
in the first col." |
|
131 |
||
132 |
line1 to:line2 do:[:lineNr | |
|
133 |
|l| |
|
134 |
||
135 |
l := self listAt:lineNr. |
|
136 |
l isNil ifTrue:[l := '']. |
|
137 |
self at:lineNr put:(('"' , '/') , l) |
|
138 |
]. |
|
139 |
self textChanged. |
|
0 | 140 |
! ! |
141 |
||
142 |
!CodeView methodsFor:'accessing'! |
|
143 |
||
144 |
explainAction:aBlock |
|
145 |
"set the action to be performed on explain" |
|
146 |
||
147 |
explainAction := aBlock |
|
148 |
! ! |
|
149 |
||
150 |
!CodeView methodsFor:'user actions'! |
|
151 |
||
152 |
accept |
|
125 | 153 |
"redefined accept action; |
154 |
save cursor and selection to allow restore in case of an error |
|
155 |
(we are typically compiling here ... and the compiler may show |
|
156 |
errors by highlighting them)" |
|
95 | 157 |
|
158 |
codeStartPosition := 1. |
|
159 |
[ |
|
160 |
Object abortSignal handle:[:ex | |
|
161 |
self cursor:Cursor normal. |
|
162 |
"redraw selection in normal color" |
|
163 |
self selectFromLine:selectionStartLine col:selectionStartCol |
|
164 |
toLine:selectionEndLine col:selectionEndCol. |
|
165 |
ex return |
|
166 |
] do:[ |
|
125 | 167 |
super accept. |
70 | 168 |
] |
95 | 169 |
] valueNowOrOnUnwindDo:[ |
170 |
self unselect. |
|
0 | 171 |
] |
172 |
! |
|
173 |
||
174 |
explain |
|
175 |
"explain action; |
|
176 |
evaluate the explainBlock passing whole contents and |
|
177 |
selection as arguments." |
|
178 |
||
179 |
|text| |
|
180 |
||
181 |
explainAction notNil ifTrue:[ |
|
70 | 182 |
text := self selection. |
183 |
text notNil ifTrue:[ |
|
121 | 184 |
explainAction value:(self contents) value:(text asString) |
70 | 185 |
] |
0 | 186 |
] |
136 | 187 |
! |
188 |
||
189 |
commentSelection |
|
190 |
"convenient function to comment out a block. |
|
191 |
All lines from line1 to line2 get an end-of-line comment |
|
192 |
in the first col." |
|
193 |
||
194 |
|e| |
|
195 |
||
196 |
(selectionStartCol == 1 and:[selectionEndCol == 0]) ifTrue:[ |
|
197 |
self commentFrom:selectionStartLine to:selectionEndLine-1 |
|
198 |
] ifFalse:[ |
|
199 |
self insert:$" atLine:selectionStartLine col:selectionStartCol. |
|
200 |
e := selectionEndCol + 1. |
|
201 |
selectionStartLine == selectionEndLine ifTrue:[e := e + 1]. |
|
202 |
self insert:$" atLine:selectionEndLine col:e. |
|
203 |
self selectFromLine:selectionStartLine col:selectionStartCol |
|
204 |
toLine:selectionEndLine col:e. |
|
205 |
] |
|
206 |
! |
|
207 |
||
208 |
uncommentSelection |
|
209 |
"convenient function to comment out a block. |
|
210 |
All lines from line1 to line2 get an end-of-line comment |
|
211 |
in the first col." |
|
212 |
||
213 |
|e| |
|
214 |
||
215 |
(selectionStartCol == 1 and:[selectionEndCol == 0]) ifTrue:[ |
|
216 |
self uncommentFrom:selectionStartLine to:selectionEndLine-1 |
|
217 |
] ifFalse:[ |
|
218 |
((self characterAtLine:selectionStartLine col:selectionStartCol) == $" |
|
219 |
and:[(self characterAtLine:selectionEndLine col:selectionEndCol) == $"]) ifTrue:[ |
|
220 |
self deleteCharAtLine:selectionEndLine col:selectionEndCol. |
|
221 |
self deleteCharAtLine:selectionStartLine col:selectionStartCol. |
|
222 |
e := selectionEndCol - 1. |
|
223 |
selectionStartLine == selectionEndLine ifTrue:[e := e - 1]. |
|
224 |
self selectFromLine:selectionStartLine col:selectionStartCol |
|
225 |
toLine:selectionEndLine col:e. |
|
226 |
] |
|
227 |
] |
|
0 | 228 |
! ! |
229 |
||
70 | 230 |
!CodeView methodsFor:'event handling'! |
0 | 231 |
|
232 |
keyPress:key x:x y:y |
|
32 | 233 |
"catch keyboard shortcuts" |
0 | 234 |
|
132 | 235 |
<resource: #keyboard (#Accept #Explain #Help |
236 |
#CommentSelection #UncommentSelection)> |
|
237 |
||
32 | 238 |
(key == #Accept) ifTrue:[^ self accept]. |
10 | 239 |
(key == #Explain) ifTrue:[^ self explain]. |
32 | 240 |
(key == #Help) ifTrue:[^ self explain]. |
132 | 241 |
(key == #CommentSelection) ifTrue:[^ self commentSelection]. |
242 |
(key == #UncommentSelection) ifTrue:[^ self uncommentSelection]. |
|
121 | 243 |
|
0 | 244 |
super keyPress:key x:x y:y |
245 |
! ! |