Helios-Workspace.st 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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. !HLCodeModel methodsFor: 'defaults'!
  26. defaultReceiver
  27. ^ DoIt new
  28. ! !
  29. !HLCodeModel class methodsFor: 'actions'!
  30. on: anEnvironment
  31. ^ self new
  32. environment: anEnvironment;
  33. yourself
  34. ! !
  35. HLWidget subclass: #HLCodeWidget
  36. instanceVariableNames: 'model wrapper code editor state'
  37. package: 'Helios-Workspace'!
  38. !HLCodeWidget methodsFor: 'accessing'!
  39. announcer
  40. ^ self model announcer
  41. !
  42. contents
  43. ^ editor getValue
  44. !
  45. contents: aString
  46. editor setValue: aString.
  47. state ifNotNil: [ self updateState ]
  48. !
  49. currentLine
  50. ^editor getLine: (editor getCursor line)
  51. !
  52. currentLineOrSelection
  53. ^editor somethingSelected
  54. ifFalse: [ self currentLine ]
  55. ifTrue: [ self selection ]
  56. !
  57. model
  58. ^ model ifNil: [ model := HLCodeModel new ]
  59. !
  60. model: aModel
  61. model := aModel
  62. !
  63. receiver
  64. ^ self model receiver
  65. !
  66. receiver: anObject
  67. self model receiver: anObject
  68. !
  69. selection
  70. ^editor getSelection
  71. !
  72. selectionEnd
  73. ^code element selectionEnd
  74. !
  75. selectionEnd: anInteger
  76. code element selectionEnd: anInteger
  77. !
  78. selectionStart
  79. ^code element selectionStart
  80. !
  81. selectionStart: anInteger
  82. code element selectionStart: anInteger
  83. ! !
  84. !HLCodeWidget methodsFor: 'actions'!
  85. clear
  86. self contents: ''
  87. !
  88. configureEditor
  89. self editor at: 'amberCodeWidget' put: self.
  90. self editor on: 'change' do: [ self onChange ]
  91. !
  92. doIt
  93. | result |
  94. self model announcer announce: (HLDoItRequested on: model).
  95. result := model doIt: self currentLineOrSelection.
  96. self model announcer announce: (HLDoItExecuted on: model).
  97. ^ result
  98. !
  99. editor
  100. ^ editor
  101. !
  102. focus
  103. editor focus
  104. !
  105. inspectIt
  106. | newInspector |
  107. self model announcer announce: (HLInspectItRequested on: model).
  108. newInspector := self makeInspectorOn: self doIt.
  109. newInspector open
  110. !
  111. makeInspectorOn: anObject
  112. ^ HLInspector new
  113. inspect: anObject;
  114. yourself
  115. !
  116. print: aString
  117. | start stop currentLine |
  118. currentLine := (editor getCursor: false) line.
  119. start := HashedCollection new.
  120. start at: 'line' put: currentLine.
  121. start at: 'ch' put: (editor getCursor: false) ch.
  122. (editor getSelection) ifEmpty: [
  123. "select current line if selection is empty"
  124. start at: 'ch' put: (editor getLine: currentLine) size.
  125. editor setSelection: #{'line' -> currentLine. 'ch' -> 0} end: start.
  126. ].
  127. stop := HashedCollection new.
  128. stop at: 'line' put: currentLine.
  129. stop at: 'ch' put: ((start at: 'ch') + aString size + 2).
  130. editor replaceSelection: (editor getSelection, ' ', aString, ' ').
  131. editor setCursor: (editor getCursor: true).
  132. editor setSelection: stop end: start
  133. !
  134. printIt
  135. | result |
  136. result:= self doIt.
  137. self model announcer announce: (HLPrintItRequested on: model).
  138. self print: result printString.
  139. self focus.
  140. !
  141. saveIt
  142. "I do not do anything"
  143. !
  144. setEditorOn: aTextarea
  145. <self['@editor'] = CodeMirror.fromTextArea(aTextarea, {
  146. theme: 'amber',
  147. lineNumbers: true,
  148. enterMode: 'flat',
  149. indentWithTabs: true,
  150. indentUnit: 4,
  151. matchBrackets: true,
  152. electricChars: true,
  153. keyMap: 'Amber',
  154. extraKeys: {"Shift-Space": "autocomplete"}
  155. })>
  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: 'PintIt';
  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: 'Do you want to cancel 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'
  563. package: 'Helios-Workspace'!
  564. !HLWorkspace methodsFor: 'accessing'!
  565. codeWidget
  566. ^ codeWidget ifNil: [ codeWidget := HLCodeWidget new ]
  567. ! !
  568. !HLWorkspace methodsFor: 'actions'!
  569. focus
  570. ^ self codeWidget focus
  571. ! !
  572. !HLWorkspace methodsFor: 'rendering'!
  573. renderContentOn: html
  574. html with: (HLContainer with: self codeWidget)
  575. ! !
  576. !HLWorkspace methodsFor: 'testing'!
  577. canHaveFocus
  578. ^ true
  579. ! !
  580. !HLWorkspace class methodsFor: 'accessing'!
  581. tabLabel
  582. ^ 'Workspace'
  583. !
  584. tabPriority
  585. ^ 10
  586. ! !
  587. !HLWorkspace class methodsFor: 'testing'!
  588. canBeOpenAsTab
  589. ^ true
  590. ! !