1
0

Helios-Workspace.st 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. Smalltalk current createPackage: 'Helios-Workspace' properties: #{}!
  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: someCode
  23. ^ self environment eval: someCode on: self receiver
  24. !
  25. subscribe: aWidget
  26. aWidget subscribeTo: self announcer
  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'
  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. !
  51. currentLine
  52. ^editor getLine: (editor getCursor line)
  53. !
  54. currentLineOrSelection
  55. ^editor somethingSelected
  56. ifFalse: [self currentLine]
  57. ifTrue: [self selection]
  58. !
  59. model
  60. ^ model ifNil: [
  61. self model: HLCodeModel new.
  62. model ]
  63. !
  64. model: aModel
  65. model := aModel
  66. !
  67. receiver
  68. ^ self model receiver
  69. !
  70. receiver: anObject
  71. self model receiver: anObject
  72. !
  73. selection
  74. ^editor getSelection
  75. !
  76. selectionEnd
  77. ^code element selectionEnd
  78. !
  79. selectionEnd: anInteger
  80. code element selectionEnd: anInteger
  81. !
  82. selectionStart
  83. ^code element selectionStart
  84. !
  85. selectionStart: anInteger
  86. code element selectionStart: anInteger
  87. ! !
  88. !HLCodeWidget methodsFor: 'actions'!
  89. clear
  90. self contents: ''
  91. !
  92. doIt
  93. | result |
  94. self announcer announce: (HLDoItRequested on: model).
  95. result:= model doIt: self currentLineOrSelection.
  96. self announcer announce: (HLDoItExecuted on: model).
  97. ^ result
  98. !
  99. editor
  100. ^editor
  101. !
  102. focus
  103. self editor focus
  104. !
  105. inspectIt
  106. | newInspector |
  107. self 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. observeWrapper
  117. wrapper onKeyDown: [ :e | self onKeyDown: e ]
  118. !
  119. print: aString
  120. | start stop |
  121. start := HashedCollection new.
  122. stop := HashedCollection new.
  123. start at: 'line' put: (editor getCursor: false) line.
  124. start at: 'ch' put: (editor getCursor: false) ch.
  125. stop at: 'line' put: (start at: 'line').
  126. stop at: 'ch' put: ((start at: 'ch') + aString size + 2).
  127. editor replaceSelection: (editor getSelection, ' ', aString, ' ').
  128. editor setCursor: (editor getCursor: true).
  129. editor setSelection: stop end: start
  130. !
  131. printIt
  132. | result |
  133. result:= self doIt.
  134. self announcer announce: (HLPrintItRequested on: model).
  135. self print: result printString.
  136. self focus.
  137. !
  138. setEditorOn: aTextarea
  139. <self['@editor'] = CodeMirror.fromTextArea(aTextarea, {
  140. theme: 'amber',
  141. lineNumbers: true,
  142. enterMode: 'flat',
  143. matchBrackets: true,
  144. electricChars: false
  145. })>
  146. ! !
  147. !HLCodeWidget methodsFor: 'reactions'!
  148. onDoIt
  149. self doIt
  150. !
  151. onInspectIt
  152. self inspectIt
  153. !
  154. onKeyDown: anEvent
  155. <if(anEvent.ctrlKey) {
  156. if(anEvent.keyCode === 80) { //ctrl+p
  157. self._onPrintIt();
  158. anEvent.preventDefault();
  159. return false;
  160. }
  161. if(anEvent.keyCode === 68) { //ctrl+d
  162. self._onDoIt();
  163. anEvent.preventDefault();
  164. return false;
  165. }
  166. if(anEvent.keyCode === 73) { //ctrl+i
  167. self._onInspectIt();
  168. anEvent.preventDefault();
  169. return false;
  170. }
  171. }>
  172. !
  173. onPrintIt
  174. self printIt
  175. ! !
  176. !HLCodeWidget methodsFor: 'rendering'!
  177. renderContentOn: html
  178. code := html textarea.
  179. self setEditorOn: code element.
  180. self observeWrapper
  181. ! !
  182. HLWidget subclass: #HLWorkspace
  183. instanceVariableNames: 'model codeWidget'
  184. package: 'Helios-Workspace'!
  185. !HLWorkspace methodsFor: 'accessing'!
  186. codeWidget
  187. ^ codeWidget ifNil: [
  188. codeWidget := HLCodeWidget new
  189. model: self model code;
  190. yourself ]
  191. !
  192. model
  193. ^ model ifNil: [
  194. self model: HLWorkspaceModel new.
  195. model ]
  196. !
  197. model: aModel
  198. model := aModel.
  199. self codeWidget model: aModel code.
  200. self observeCodeWidget.
  201. ! !
  202. !HLWorkspace methodsFor: 'actions'!
  203. observeCodeWidget
  204. ! !
  205. !HLWorkspace methodsFor: 'reactions'!
  206. onDoIt
  207. !
  208. onInspectIt
  209. !
  210. onPrintIt
  211. ! !
  212. !HLWorkspace methodsFor: 'rendering'!
  213. renderContentOn: html
  214. html with: self codeWidget
  215. ! !
  216. !HLWorkspace class methodsFor: 'accessing'!
  217. tabLabel
  218. ^ 'Workspace'
  219. !
  220. tabPriority
  221. ^ 10
  222. ! !
  223. !HLWorkspace class methodsFor: 'testing'!
  224. canBeOpenAsTab
  225. ^ true
  226. ! !
  227. Object subclass: #HLWorkspaceModel
  228. instanceVariableNames: 'announcer environment code'
  229. package: 'Helios-Workspace'!
  230. !HLWorkspaceModel methodsFor: 'accessing'!
  231. announcer
  232. ^ announcer ifNil: [ announcer := Announcer new ]
  233. !
  234. code
  235. "Answers the code model working for this workspace model"
  236. ^ code ifNil:[ HLCodeModel on: self environment ]
  237. !
  238. environment
  239. ^ environment ifNil: [ HLManager current environment ]
  240. !
  241. environment: anEnvironment
  242. environment := anEnvironment
  243. ! !
  244. !HLWorkspaceModel methodsFor: 'reactions'!
  245. onKeyDown: anEvent
  246. <if(anEvent.ctrlKey) {
  247. if(anEvent.keyCode === 80) { //ctrl+p
  248. self._printIt();
  249. anEvent.preventDefault();
  250. return false;
  251. }
  252. if(anEvent.keyCode === 68) { //ctrl+d
  253. self._doIt();
  254. anEvent.preventDefault();
  255. return false;
  256. }
  257. if(anEvent.keyCode === 73) { //ctrl+i
  258. self._inspectIt();
  259. anEvent.preventDefault();
  260. return false;
  261. }
  262. }>
  263. ! !
  264. !HLWorkspaceModel class methodsFor: 'actions'!
  265. on: anEnvironment
  266. ^ self new
  267. environment: anEnvironment;
  268. yourself
  269. ! !