-
-
Notifications
You must be signed in to change notification settings - Fork 343
/
SpCodePresenter.class.st
468 lines (344 loc) · 10.9 KB
/
SpCodePresenter.class.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
"
I'm a code (Pharo code) presenter.
I will style and provide completion according to the behaviour being edited.
@@TODO
!! Transmissions
- #inputTextPort (default)
- #outputTextChangedPort (default)
"
Class {
#name : #SpCodePresenter,
#superclass : #SpTextPresenter,
#instVars : [
'#syntaxHighlight => SpObservableSlot',
'#lineNumbers => SpObservableSlot',
'#interactionModel => SpObservableSlot',
'#overrideContextMenu'
],
#category : #'Spec2-Code'
}
{ #category : #specs }
SpCodePresenter class >> adapterName [
^ #CodeAdapter
]
{ #category : #commands }
SpCodePresenter class >> buildCommandsGroupWith: presenter forRoot: aCmCommandsGroup [
aCmCommandsGroup beDisplayedAsGroup.
aCmCommandsGroup register: (self codeSelectionActionCommandsGroupWith: presenter).
aCmCommandsGroup register: (self codeNavigationCommandsGroupWith: presenter)
]
{ #category : #commands }
SpCodePresenter class >> buildEditionCommandsGroupWith: presenter forRoot: aCmCommandsGroup [
super buildEditionCommandsGroupWith: presenter forRoot: aCmCommandsGroup.
aCmCommandsGroup register: (self codeAcceptCommandsGroupWith: presenter)
]
{ #category : #commands }
SpCodePresenter class >> codeAcceptCommandsGroupWith: aPresenter [
^ (CmCommandGroup named: 'Accept commands') asSpecGroup
description: 'Accept or cancel changes.';
beDisplayedAsGroup;
register: (SpAcceptChangesCommand forSpecContext: aPresenter);
register: (SpCancelChangesCommand forSpecContext: aPresenter);
yourself
]
{ #category : #commands }
SpCodePresenter class >> codeNavigationCommandsGroupWith: aPresenter [
| group |
group := (CmCommandGroup named: 'Selection search commands') asSpecGroup.
group
description: 'Search the system from code selection.';
beDisplayedAsGroup.
group register: (SpBrowseClassCommand new
transform: [ :aContext | aContext findClassFromSelection ];
asSpecCommand).
self flag: #TODO. "This looks like a very bad way to do this :P"
{
SpBrowseImplementorsCommand.
SpBrowseSendersCommand.
SpBrowseMethodReferencesCommand.
SpBrowseMethodsContainingStringCommand.
}
do:[ :commandClass |
group register: (commandClass new
transform: [ :aContext | aContext selectedTextOrLine ];
asSpecCommand) ].
^ group
]
{ #category : #commands }
SpCodePresenter class >> codeSelectionActionCommandsGroupWith: presenter [
| group |
group := (CmCommandGroup named: 'Selection action commands') asSpecGroup.
group
description: 'Actions on code selection.';
beDisplayedAsGroup.
SpCodeSelectionCommand allSubclassesDo: [ :cmdClass |
group register: (cmdClass forSpecContext: presenter) ].
^ group
]
{ #category : #examples }
SpCodePresenter class >> example [
<sampleInstance>
^ self new
openWithSpec;
text:
'some method: 42'
]
{ #category : #specs }
SpCodePresenter class >> title [
^ 'Code'
]
{ #category : #private }
SpCodePresenter >> basicInteractionModel: anObject [
interactionModel := anObject
]
{ #category : #'api-model' }
SpCodePresenter >> beForBehavior: aBehavior [
"used when browsing classes or alike"
self interactionModel: (SpCodeBehaviorInteractionModel on: aBehavior)
]
{ #category : #'api-model' }
SpCodePresenter >> beForMethod: aMethod [
"used when browsing methods"
self interactionModel: (SpCodeMethodInteractionModel on: aMethod)
]
{ #category : #'api-model' }
SpCodePresenter >> beForObject: anObject [
"Used when inspecting objects"
self interactionModel: (SpCodeObjectInteractionModel on: anObject)
]
{ #category : #'api-model' }
SpCodePresenter >> beForScripting [
"Use in playgrounds"
self interactionModel: SpCodeScriptingInteractionModel new
]
{ #category : #'private bindings' }
SpCodePresenter >> behavior [
"Return the class corresponding to the method class of the source code you are editing"
^ self interactionModel ifNotNil: [ :im | im behavior ]
]
{ #category : #'private bindings' }
SpCodePresenter >> behavior: aClass [
"Set the class corresponding to the method class of the source code you are editing"
]
{ #category : #'private bindings' }
SpCodePresenter >> bindingOf: aString [
self interactionModel ifNotNil: [ :im | ^ im bindingOf: aString ].
^ nil
]
{ #category : #private }
SpCodePresenter >> buildContextKeyBindingsWith: aCategory [
| category |
category := self newContextMenuKeyBindings.
aCategory ifNotNil: [
aCategory allEntries keymaps do: [ :each |
category addKeymapEntry: each ] ].
^ category
]
{ #category : #private }
SpCodePresenter >> buildContextMenuWith: aValuable [
| menuPresenter |
overrideContextMenu ifTrue: [ ^ aValuable ].
menuPresenter := self newContextMenu.
aValuable ifNotNil: [
aValuable value ifNotNil: [ :menu |
menu menuGroups do: [ :aGroup |
menuPresenter addMenuGroup: aGroup ] ] ].
^ menuPresenter
]
{ #category : #api }
SpCodePresenter >> clearInteractionModel [
self interactionModel: SpCodeNullInteractionModel new
]
{ #category : #'api-shortcuts' }
SpCodePresenter >> contextKeyBindings [
^ self buildContextKeyBindingsWith: super contextKeyBindings
]
{ #category : #accessing }
SpCodePresenter >> contextMenu [
"The code presenter context menu is a compound of two menus:
1) the basic interoperation menu (that includes operations like cut&paste but also do it,
print it, etc.
2) the user defined context menu.
If you want to override completely this menu you need to set the 'overrideMenu' property
to true"
^ [ self buildContextMenuWith: super contextMenu ]
]
{ #category : #'private bindings' }
SpCodePresenter >> doItContext [
^ self interactionModel ifNotNil: [ :im | im doItContext ]
]
{ #category : #'private bindings' }
SpCodePresenter >> doItContext: anObject [
]
{ #category : #'private bindings' }
SpCodePresenter >> doItReceiver [
^ self interactionModel ifNotNil: [ :im | im doItReceiver ]
]
{ #category : #'private bindings' }
SpCodePresenter >> doItReceiver: anObject [
]
{ #category : #accessing }
SpCodePresenter >> findClassFrom: aString [
| ast |
self flag: #TODO. "Does this really has to be part of the code api?"
ast := RBParser parseExpression: aString onError: [ ^ nil ].
ast nodesDo: [ :node |
(node isVariable and: [ node name first isUppercase ])
ifTrue: [
(self class environment classNamed: node name)
ifNotNil: [ :aClass | ^ aClass ] ] ].
^ nil
]
{ #category : #accessing }
SpCodePresenter >> findClassFromSelection [
self flag: #TODO. "Does this really has to be part of the text api?"
^ self findClassFrom: self selectedTextOrLine trimmed
]
{ #category : #'private bindings' }
SpCodePresenter >> hasBindingOf: aString [
self interactionModel ifNotNil: [ :im | ^ im hasBindingOf: aString ].
^ false
]
{ #category : #'private bindings' }
SpCodePresenter >> hasBindingThatBeginsWith: aString [
^ self interactionModel
ifNotNil: [ :im | im hasBindingThatBeginsWith: aString ]
ifNil: [ false ]
]
{ #category : #testing }
SpCodePresenter >> hasLineNumbers [
^ lineNumbers
]
{ #category : #testing }
SpCodePresenter >> hasSyntaxHighlight [
^ syntaxHighlight
]
{ #category : #initialization }
SpCodePresenter >> initialize [
super initialize.
"Use the default menu"
overrideContextMenu := false.
self withSyntaxHighlight.
self withLineNumbers.
self registerEventsForStyling
]
{ #category : #api }
SpCodePresenter >> insertPopoverAfterCurrentSelection: aPresenter [
self withAdapterDo: [ :anAdapter |
anAdapter insertPopoverAfterCurrentSelection: aPresenter ]
]
{ #category : #accessing }
SpCodePresenter >> insertPrintPopoverAfterCurrentSelection: anObject [
"Inserts a print-it result popover.
This is for use of SpCodePrintItCommand"
self insertPopoverAfterCurrentSelection: (SpCodePopoverPrintPresenter
newCode: self
object: anObject)
]
{ #category : #api }
SpCodePresenter >> interactionModel [
^ interactionModel
]
{ #category : #api }
SpCodePresenter >> interactionModel: anInteractionModel [
anInteractionModel owner: self.
self basicInteractionModel: anInteractionModel
]
{ #category : #'private bindings' }
SpCodePresenter >> isForScripting [
^ self interactionModel isForScripting
]
{ #category : #accessing }
SpCodePresenter >> lineNumbers: aBoolean [
lineNumbers := aBoolean
]
{ #category : #private }
SpCodePresenter >> newContextMenu [
^ self rootCommandsGroup asMenuPresenter
]
{ #category : #private }
SpCodePresenter >> newContextMenuKeyBindings [
^ self rootCommandsGroup asKMCategory
]
{ #category : #accessing }
SpCodePresenter >> overrideContextMenu: aBoolean [
"This property set the type of menu to use: if #overrideContextMenu is false (the default),
the contextMenu will include code presenter default entries. If #overrideContextMenu is true,
just what is defined in #contextMenu will be displayed"
overrideContextMenu := aBoolean
]
{ #category : #initialization }
SpCodePresenter >> registerEventsForStyling [
self whenInteractionModelChangedDo: [ :im |
self withAdapterDo: [ :anAdapter |
anAdapter interactionModel: im ] ]
]
{ #category : #api }
SpCodePresenter >> selectedBehavior [
^ self behavior
]
{ #category : #'private bindings' }
SpCodePresenter >> selectedClassOrMetaClass [
^ self interactionModel ifNotNil: [ :im | im selectedClassOrMetaClass ]
]
{ #category : #api }
SpCodePresenter >> selectedTextOrLine [
self selectedText ifNotEmpty: [ :aString | ^ aString ].
^ self
selectLine;
selectedText
]
{ #category : #accessing }
SpCodePresenter >> syntaxHighlight: aBoolean [
syntaxHighlight := aBoolean
]
{ #category : #api }
SpCodePresenter >> userActionDuring: aBlock [
"There are a buch of operations that will all trigger a backend update. A tipical case,
changing text and interactionModel will trigger the text-changed event 3 times and that
may cause an excesive compute that we want to avoid.
We use #userActionDuring: to avoid that.
[[[language=Smalltalk
aCodePresenter userActionDuring: [
aCodePresenter
beForScription;
text: '42 factorial' ]
]]]."
self withAdapterDo: [ :anAdapter |
anAdapter userActionDuring: aBlock.
^ self ].
aBlock value
]
{ #category : #'api-events' }
SpCodePresenter >> whenInteractionModelChangedDo: aBlock [
"Set a block to perform when the behavior class changed"
self
property: #interactionModel
whenChangedDo: aBlock
]
{ #category : #'api-events' }
SpCodePresenter >> whenLineNumbersChangedDo: aBlock [
"Set a block to perform when the syntax highlight is enabled/disabled"
self property: #lineNumbers whenChangedDo: aBlock
]
{ #category : #'api-events' }
SpCodePresenter >> whenSyntaxHighlightChangedDo: aBlock [
"Set a block to perform when the syntax highlight is enabled/disabled"
self property: #syntaxHighlight whenChangedDo: aBlock
]
{ #category : #api }
SpCodePresenter >> withLineNumbers [
self lineNumbers: true
]
{ #category : #api }
SpCodePresenter >> withSyntaxHighlight [
self syntaxHighlight: true
]
{ #category : #api }
SpCodePresenter >> withoutLineNumbers [
self lineNumbers: false
]
{ #category : #api }
SpCodePresenter >> withoutSyntaxHighlight [
self syntaxHighlight: false
]