Helios-Workspace.st 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. Smalltalk current createPackage: 'Helios-Workspace'!
  2. Object subclass: #HLCodeModel
  3. instanceVariableNames: 'announcer environment receiver'
  4. package: 'Helios-Workspace'!
  5. !HLCodeModel methodsFor: 'accessing'!
  6. announcer
  7. ^ announcer ifNil: [ announcer := Announcer new ]
  8. !
  9. environment
  10. ^ environment ifNil: [ HLManager current environment ]
  11. !
  12. environment: anEnvironment
  13. environment := anEnvironment
  14. !
  15. receiver
  16. ^ receiver ifNil: [ receiver := self defaultReceiver ]
  17. !
  18. receiver: anObject
  19. receiver := anObject
  20. ! !
  21. !HLCodeModel methodsFor: 'actions'!
  22. doIt: aString
  23. ^ self environment eval: aString on: self receiver
  24. !
  25. inspect: anObject
  26. self environment inspect: anObject
  27. ! !
  28. !HLCodeModel methodsFor: 'defaults'!
  29. defaultReceiver
  30. ^ DoIt new
  31. ! !
  32. !HLCodeModel class methodsFor: 'actions'!
  33. on: anEnvironment
  34. ^ self new
  35. environment: anEnvironment;
  36. yourself
  37. ! !
  38. HLWidget subclass: #HLCodeWidget
  39. instanceVariableNames: 'model wrapper code editor state'
  40. package: 'Helios-Workspace'!
  41. !HLCodeWidget methodsFor: 'accessing'!
  42. announcer
  43. ^ self model announcer
  44. !
  45. contents
  46. ^ editor getValue
  47. !
  48. contents: aString
  49. editor setValue: aString.
  50. state ifNotNil: [ self updateState ]
  51. !
  52. currentLine
  53. ^editor getLine: (editor getCursor line)
  54. !
  55. currentLineOrSelection
  56. ^editor somethingSelected
  57. ifFalse: [ self currentLine ]
  58. ifTrue: [ self selection ]
  59. !
  60. editorOptions
  61. ^ #{
  62. 'theme' -> 'amber'.
  63. 'lineNumbers' -> true.
  64. 'enterMode' -> 'flat'.
  65. 'indentWithTabs' -> true.
  66. 'indentUnit' -> 4.
  67. 'matchBrackets' -> true.
  68. 'electricChars' -> false.
  69. 'keyMap' -> 'Amber'.
  70. 'extraKeys' -> #{'Shift-Space' -> 'autocomplete'}
  71. }
  72. !
  73. model
  74. ^ model ifNil: [ model := HLCodeModel new ]
  75. !
  76. model: aModel
  77. model := aModel
  78. !
  79. receiver
  80. ^ self model receiver
  81. !
  82. receiver: anObject
  83. self model receiver: anObject
  84. !
  85. selection
  86. ^editor getSelection
  87. !
  88. selectionEnd
  89. ^code element selectionEnd
  90. !
  91. selectionEnd: anInteger
  92. code element selectionEnd: anInteger
  93. !
  94. selectionStart
  95. ^code element selectionStart
  96. !
  97. selectionStart: anInteger
  98. code element selectionStart: anInteger
  99. ! !
  100. !HLCodeWidget methodsFor: 'actions'!
  101. clear
  102. self contents: ''
  103. !
  104. configureEditor
  105. self editor at: 'amberCodeWidget' put: self.
  106. self editor on: 'change' do: [ self onChange ]
  107. !
  108. doIt
  109. | result |
  110. self model announcer announce: (HLDoItRequested on: model).
  111. result := model doIt: self currentLineOrSelection.
  112. self model announcer announce: (HLDoItExecuted on: model).
  113. ^ result
  114. !
  115. editor
  116. ^ editor
  117. !
  118. focus
  119. editor focus
  120. !
  121. inspectIt
  122. | newInspector |
  123. self model announcer announce: (HLInspectItRequested on: model).
  124. self model inspect: self doIt
  125. !
  126. print: aString
  127. | start stop currentLine |
  128. currentLine := (editor getCursor: false) line.
  129. start := HashedCollection new.
  130. start at: 'line' put: currentLine.
  131. start at: 'ch' put: (editor getCursor: false) ch.
  132. (editor getSelection) ifEmpty: [
  133. "select current line if selection is empty"
  134. start at: 'ch' put: (editor getLine: currentLine) size.
  135. editor setSelection: #{'line' -> currentLine. 'ch' -> 0} end: start.
  136. ].
  137. stop := HashedCollection new.
  138. stop at: 'line' put: currentLine.
  139. stop at: 'ch' put: ((start at: 'ch') + aString size + 2).
  140. editor replaceSelection: (editor getSelection, ' ', aString, ' ').
  141. editor setCursor: (editor getCursor: true).
  142. editor setSelection: stop end: start
  143. !
  144. printIt
  145. | result |
  146. result:= self doIt.
  147. self model announcer announce: (HLPrintItRequested on: model).
  148. self print: result printString.
  149. self focus.
  150. !
  151. saveIt
  152. "I do not do anything"
  153. !
  154. setEditorOn: aTextarea
  155. <self['@editor'] = CodeMirror.fromTextArea(aTextarea, self._editorOptions())>
  156. ! !
  157. !HLCodeWidget methodsFor: 'hints'!
  158. messageHintFor: anEditor token: aToken
  159. ^ ((Smalltalk current at: 'allSelectors') value asSet asArray
  160. select: [ :each | each includesSubString: aToken string ])
  161. reject: [ :each | each = aToken string ]
  162. !
  163. variableHintFor: anEditor token: aToken
  164. | variables classNames pseudoVariables |
  165. variables := ((window jQuery: anEditor display wrapper) find: 'span.cm-variable') get
  166. collect: [ :each | (window jQuery: each) html ].
  167. classNames := Smalltalk current classes collect: [ :each | each name ].
  168. pseudoVariables := Smalltalk current pseudoVariableNames.
  169. ^ ((variables, classNames, pseudoVariables) asSet asArray
  170. select: [ :each | each includesSubString: aToken string ])
  171. reject: [ :each | each = aToken string ]
  172. ! !
  173. !HLCodeWidget methodsFor: 'reactions'!
  174. onChange
  175. self updateState
  176. !
  177. onDoIt
  178. self doIt
  179. !
  180. onInspectIt
  181. self inspectIt
  182. !
  183. onPrintIt
  184. self printIt
  185. !
  186. onSaveIt
  187. "I do not do anything"
  188. ! !
  189. !HLCodeWidget methodsFor: 'rendering'!
  190. renderButtonsOn: html
  191. html button
  192. class: 'button';
  193. with: 'DoIt';
  194. onClick: [ self doIt ].
  195. html button
  196. class: 'button';
  197. with: 'PrintIt';
  198. onClick: [ self printIt ].
  199. html button
  200. class: 'button';
  201. with: 'InspectIt';
  202. onClick: [ self inspectIt ]
  203. !
  204. renderContentOn: html
  205. code := html textarea.
  206. state := html div class: 'state'.
  207. html div
  208. class: 'buttons_bar';
  209. with: [ self renderButtonsOn: html ].
  210. self
  211. setEditorOn: code element;
  212. configureEditor;
  213. updateState
  214. ! !
  215. !HLCodeWidget methodsFor: 'testing'!
  216. canHaveFocus
  217. ^ true
  218. !
  219. hasFocus
  220. ^ code asJQuery is: ':active'
  221. !
  222. hasModification
  223. ^ false
  224. ! !
  225. !HLCodeWidget methodsFor: 'updating'!
  226. updateState
  227. self hasModification
  228. ifTrue: [ state asJQuery addClass: 'modified' ]
  229. ifFalse: [ state asJQuery removeClass: 'modified' ]
  230. ! !
  231. !HLCodeWidget class methodsFor: 'accessing'!
  232. keyMap
  233. ^ HLManager current keyBinder systemIsMac
  234. ifTrue: [ self macKeyMap ]
  235. ifFalse: [ self pcKeyMap ]
  236. !
  237. macKeyMap
  238. ^ #{
  239. 'Alt-Backspace' -> 'delWordBefore'.
  240. 'Alt-Delete' -> 'delWordAfter'.
  241. 'Alt-Left' -> 'goWordBoundaryLeft'.
  242. 'Alt-Right' -> 'goWordBoundaryRight'.
  243. 'Cmd-A' -> 'selectAll'.
  244. 'Cmd-Alt-F' -> 'replace'.
  245. 'Cmd-D' -> 'doIt'.
  246. 'Cmd-Down' -> 'goDocEnd'.
  247. 'Cmd-End' -> 'goDocEnd'.
  248. 'Cmd-F' -> 'find'.
  249. 'Cmd-G' -> 'findNext'.
  250. 'Cmd-I' -> 'inspectIt'.
  251. 'Cmd-Left' -> 'goLineStart'.
  252. 'Cmd-P' -> 'printIt'.
  253. 'Cmd-Right' -> 'goLineEnd'.
  254. 'Cmd-S' -> 'saveIt'.
  255. 'Cmd-Up' -> 'goDocStart'.
  256. 'Cmd-Y' -> 'redo'.
  257. 'Cmd-Z' -> 'undo'.
  258. 'Cmd-[' -> 'indentLess'.
  259. 'Cmd-]' -> 'indentMore'.
  260. 'Ctrl-Alt-Backspace' -> 'delWordAfter'.
  261. 'Shift-Cmd-Alt-F' -> 'replaceAll'.
  262. 'Shift-Cmd-G' -> 'findPrev'.
  263. 'Shift-Cmd-Z' -> 'redo'.
  264. 'fallthrough' -> { 'basic'. 'emacsy' }
  265. }
  266. !
  267. pcKeyMap
  268. ^ {
  269. 'Alt-Left' -> 'goLineStart'.
  270. 'Alt-Right' -> 'goLineEnd'.
  271. 'Alt-Up' -> 'goDocStart'.
  272. 'Ctrl-A' -> 'selectAll'.
  273. 'Ctrl-Backspace' -> 'delWordBefore'.
  274. 'Ctrl-D' -> 'doIt'.
  275. 'Ctrl-Delete' -> 'delWordAfter'.
  276. 'Ctrl-Down' -> 'goDocEnd'.
  277. 'Ctrl-End' -> 'goDocEnd'.
  278. 'Ctrl-F' -> 'find'.
  279. 'Ctrl-G' -> 'findNext'.
  280. 'Ctrl-I' -> 'inspectIt'.
  281. 'Ctrl-Home' -> 'goDocStart'.
  282. 'Ctrl-Left' -> 'goWordBoundaryLeft'.
  283. 'Ctrl-P' -> 'printIt'.
  284. 'Ctrl-Right' -> 'goWordBoundaryRight'.
  285. 'Ctrl-S' -> 'saveIt'.
  286. 'Ctrl-Y' -> 'redo'.
  287. 'Ctrl-Z' -> 'undo'.
  288. 'Ctrl-[' -> 'indentLess'.
  289. 'Ctrl-]' -> 'indentMore'.
  290. 'Shift-Ctrl-F' -> 'replace'.
  291. 'Shift-Ctrl-G' -> 'findPrev'.
  292. 'Shift-Ctrl-R' -> 'replaceAll'.
  293. 'Shift-Ctrl-Z' -> 'redo'.
  294. 'fallthrough' -> #('basic')
  295. }
  296. ! !
  297. !HLCodeWidget class methodsFor: 'hints'!
  298. hintFor: anEditor options: options
  299. | cursor token completions |
  300. cursor := anEditor getCursor.
  301. token := anEditor getTokenAt: cursor.
  302. token at: 'state' put: ((CodeMirror basicAt: 'innerMode')
  303. value: anEditor getMode value: (token at: 'state')) state.
  304. completions := token type = 'variable'
  305. ifTrue: [ HLCodeWidget variableHintFor: anEditor token: token ]
  306. ifFalse: [ HLCodeWidget messageHintFor: anEditor token: token ].
  307. ^ #{
  308. 'list' -> completions.
  309. 'from' -> ((CodeMirror basicAt: 'Pos') value: cursor line value: token end).
  310. 'to' -> ((CodeMirror basicAt: 'Pos') value: cursor line value: token start)
  311. }
  312. !
  313. messageHintFor: anEditor token: aToken
  314. ^ (anEditor at: 'amberCodeWidget')
  315. messageHintFor: anEditor token: aToken
  316. !
  317. variableHintFor: anEditor token: aToken
  318. ^ (anEditor at: 'amberCodeWidget')
  319. variableHintFor: anEditor token: aToken
  320. ! !
  321. !HLCodeWidget class methodsFor: 'initialization'!
  322. initialize
  323. super initialize.
  324. self
  325. setupCodeMirror;
  326. setupCommands;
  327. setupKeyMaps.
  328. !
  329. setupCodeMirror
  330. <
  331. CodeMirror.keyMap.default.fallthrough = ["basic"];
  332. CodeMirror.commands.autocomplete = function(cm) {
  333. CodeMirror.showHint(cm, self._hintFor_options_);
  334. }
  335. >
  336. !
  337. setupCommands
  338. (CodeMirror basicAt: 'commands')
  339. at: 'doIt' put: [ :cm | cm amberCodeWidget doIt ];
  340. at: 'inspectIt' put: [ :cm | cm amberCodeWidget inspectIt ];
  341. at: 'printIt' put: [ :cm | cm amberCodeWidget printIt ];
  342. at: 'saveIt' put: [ :cm | cm amberCodeWidget saveIt ]
  343. !
  344. setupKeyMaps
  345. <CodeMirror.keyMap['Amber'] = self._keyMap()>
  346. ! !
  347. HLCodeWidget subclass: #HLNavigationCodeWidget
  348. instanceVariableNames: 'methodContents'
  349. package: 'Helios-Workspace'!
  350. !HLNavigationCodeWidget methodsFor: 'accessing'!
  351. configureEditor
  352. super configureEditor.
  353. self contents: self methodContents
  354. !
  355. contents: aString
  356. self methodContents: aString.
  357. super contents: aString
  358. !
  359. methodContents
  360. ^ methodContents ifNil: [ '' ]
  361. !
  362. methodContents: aString
  363. ^ methodContents := aString
  364. !
  365. previous
  366. "for browser lists widget"
  367. !
  368. previous: aWidget
  369. "for browser lists widget"
  370. ! !
  371. !HLNavigationCodeWidget methodsFor: 'testing'!
  372. hasModification
  373. ^ (self methodContents = self contents) not
  374. ! !
  375. !HLNavigationCodeWidget class methodsFor: 'instance creation'!
  376. on: aBrowserModel
  377. ^ self new
  378. browserModel: aBrowserModel;
  379. yourself
  380. ! !
  381. !HLNavigationCodeWidget class methodsFor: 'testing'!
  382. canBeOpenAsTab
  383. ^ false
  384. ! !
  385. HLNavigationCodeWidget subclass: #HLBrowserCodeWidget
  386. instanceVariableNames: 'browserModel'
  387. package: 'Helios-Workspace'!
  388. !HLBrowserCodeWidget methodsFor: 'accessing'!
  389. browserModel
  390. ^ browserModel
  391. !
  392. browserModel: aBrowserModel
  393. browserModel := aBrowserModel.
  394. self
  395. observeSystem;
  396. observeBrowserModel
  397. ! !
  398. !HLBrowserCodeWidget methodsFor: 'actions'!
  399. observeBrowserModel
  400. self browserModel announcer
  401. on: HLSaveSourceCode
  402. send: #onSaveIt
  403. to: self;
  404. on: HLShowInstanceToggled
  405. send: #onShowInstanceToggled
  406. to: self;
  407. on: HLSourceCodeSaved
  408. send: #onSourceCodeSaved
  409. to: self;
  410. on: HLAboutToChange
  411. send: #onBrowserAboutToChange:
  412. to: self;
  413. on: HLParseErrorRaised
  414. send: #onParseError:
  415. to: self;
  416. on: HLCompileErrorRaised
  417. send: #onCompileError:
  418. to: self;
  419. on: HLUnknownVariableErrorRaised
  420. send: #onUnknownVariableError:
  421. to: self;
  422. on: HLInstVarAdded
  423. send: #onInstVarAdded
  424. to: self;
  425. on: HLMethodSelected
  426. send: #onMethodSelected:
  427. to: self;
  428. on: HLClassSelected
  429. send: #onClassSelected:
  430. to: self;
  431. on: HLProtocolSelected
  432. send: #onProtocolSelected:
  433. to: self;
  434. on: HLSourceCodeFocusRequested
  435. send: #onSourceCodeFocusRequested
  436. to: self
  437. !
  438. observeSystem
  439. self browserModel systemAnnouncer
  440. on: MethodModified
  441. send: #onMethodModified:
  442. to: self
  443. !
  444. refresh
  445. self hasModification ifTrue: [ ^ self ].
  446. self hasFocus ifTrue: [ ^ self ].
  447. self contents: self browserModel selectedMethod source
  448. !
  449. renderButtonsOn: html
  450. html button
  451. class: 'button';
  452. with: 'SaveIt';
  453. onClick: [ self saveIt ].
  454. super renderButtonsOn: html
  455. !
  456. saveIt
  457. self browserModel saveSourceCode
  458. !
  459. unregister
  460. super unregsiter.
  461. self browserModel announcer unsubscribe: self.
  462. self browserModel systemAnnouncer unsubscribe: self
  463. ! !
  464. !HLBrowserCodeWidget methodsFor: 'reactions'!
  465. onBrowserAboutToChange: anAnnouncement
  466. | block |
  467. block := anAnnouncement actionBlock.
  468. self hasModification
  469. ifTrue: [
  470. self
  471. confirm: 'Changes have not been saved. Do you want to discard these changes?'
  472. ifTrue: [
  473. "Don't ask twice"
  474. self methodContents: self contents.
  475. block value ].
  476. HLChangeForbidden signal ]
  477. !
  478. onClassSelected: anAnnouncement
  479. | class |
  480. class:= anAnnouncement item.
  481. class ifNil: [ ^ self contents: '' ].
  482. self contents: class definition
  483. !
  484. onCompileError: anAnnouncement
  485. self alert: anAnnouncement error messageText
  486. !
  487. onInstVarAdded
  488. self browserModel save: self contents
  489. !
  490. onMethodModified: anAnnouncement
  491. | method |
  492. method := anAnnouncement method.
  493. self browserModel selectedClass = method methodClass ifFalse: [ ^ self ].
  494. self browserModel selectedMethod ifNil: [ ^ self ].
  495. self browserModel selectedMethod selector = method selector ifFalse: [ ^ self ].
  496. self refresh
  497. !
  498. onMethodSelected: anAnnouncement
  499. | method |
  500. method := anAnnouncement item.
  501. method ifNil: [ ^ self contents: '' ].
  502. self contents: method source
  503. !
  504. onParseError: anAnnouncement
  505. | lineIndex newContents |
  506. lineIndex := 1.
  507. self contents: (String streamContents: [ :stream |
  508. self contents linesDo: [ :each |
  509. lineIndex = anAnnouncement line
  510. ifTrue: [
  511. stream
  512. nextPutAll: (each copyFrom: 1 to: anAnnouncement column);
  513. nextPutAll: '<- ';
  514. nextPutAll: anAnnouncement message;
  515. nextPutAll: ' ';
  516. nextPutAll: (each copyFrom: anAnnouncement column + 1 to: each size) ]
  517. ifFalse: [ stream nextPutAll: each ].
  518. stream nextPutAll: String cr.
  519. lineIndex := lineIndex + 1 ] ])
  520. !
  521. onProtocolSelected: anAnnouncement
  522. self browserModel selectedClass ifNil: [ ^ self contents: '' ].
  523. self contents: self browserModel selectedClass definition
  524. !
  525. onSaveIt
  526. self browserModel save: self contents
  527. !
  528. onShowInstanceToggled
  529. self browserModel selectedClass ifNil: [ ^ self contents: '' ].
  530. self contents: self browserModel selectedClass definition
  531. !
  532. onSourceCodeFocusRequested
  533. self focus
  534. !
  535. onSourceCodeSaved
  536. self methodContents: self contents.
  537. self updateState
  538. !
  539. onUnknownVariableError: anAnnouncement
  540. | error |
  541. error := anAnnouncement error.
  542. self
  543. confirm: (String streamContents: [ :stream |
  544. stream
  545. nextPutAll: error messageText;
  546. nextPutAll: String cr;
  547. nextPutAll: 'Would you like to define an instance variable?' ])
  548. ifTrue: [
  549. self browserModel addInstVarNamed: error variableName ]
  550. ! !
  551. !HLBrowserCodeWidget class methodsFor: 'instance creation'!
  552. on: aBrowserModel
  553. ^ self new
  554. browserModel: aBrowserModel;
  555. yourself
  556. ! !
  557. !HLBrowserCodeWidget class methodsFor: 'testing'!
  558. canBeOpenAsTab
  559. ^ false
  560. ! !
  561. HLWidget subclass: #HLWorkspace
  562. instanceVariableNames: 'codeWidget transcript'
  563. package: 'Helios-Workspace'!
  564. !HLWorkspace methodsFor: 'accessing'!
  565. codeWidget
  566. ^ codeWidget ifNil: [ codeWidget := HLCodeWidget new ]
  567. !
  568. transcript
  569. ^ transcript ifNil: [ transcript := HLTranscript new ]
  570. ! !
  571. !HLWorkspace methodsFor: 'actions'!
  572. focus
  573. ^ self codeWidget focus
  574. !
  575. unregister
  576. super unregister.
  577. self transcript unregister
  578. ! !
  579. !HLWorkspace methodsFor: 'rendering'!
  580. renderContentOn: html
  581. html with: (HLContainer with: (HLHorizontalSplitter
  582. with: self codeWidget
  583. with: [ :canvas | self renderTranscriptOn: canvas ]))
  584. !
  585. renderTranscriptOn: html
  586. html div
  587. class: 'transcript-container';
  588. with: [
  589. html div
  590. class: 'list-label';
  591. with: 'Transcript'.
  592. self transcript renderOn: html ]
  593. ! !
  594. !HLWorkspace methodsFor: 'testing'!
  595. canHaveFocus
  596. ^ true
  597. ! !
  598. !HLWorkspace class methodsFor: 'accessing'!
  599. tabClass
  600. ^ 'workspace'
  601. !
  602. tabLabel
  603. ^ 'Workspace'
  604. !
  605. tabPriority
  606. ^ 10
  607. ! !
  608. !HLWorkspace class methodsFor: 'testing'!
  609. canBeOpenAsTab
  610. ^ true
  611. ! !