Helios-Workspace.st 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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: 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: [ model := HLCodeModel new ]
  61. !
  62. model: aModel
  63. model := aModel
  64. !
  65. receiver
  66. ^ self model receiver
  67. !
  68. receiver: anObject
  69. self model receiver: anObject
  70. !
  71. selection
  72. ^editor getSelection
  73. !
  74. selectionEnd
  75. ^code element selectionEnd
  76. !
  77. selectionEnd: anInteger
  78. code element selectionEnd: anInteger
  79. !
  80. selectionStart
  81. ^code element selectionStart
  82. !
  83. selectionStart: anInteger
  84. code element selectionStart: anInteger
  85. ! !
  86. !HLCodeWidget methodsFor: 'actions'!
  87. clear
  88. self contents: ''
  89. !
  90. configureEditor
  91. self editor at: 'amberCodeWidget' put: self
  92. !
  93. doIt
  94. | result |
  95. self announcer announce: (HLDoItRequested on: model).
  96. result:= model doIt: self currentLineOrSelection.
  97. self announcer announce: (HLDoItExecuted on: model).
  98. ^ result
  99. !
  100. editor
  101. ^editor
  102. !
  103. focus
  104. editor focus
  105. !
  106. inspectIt
  107. | newInspector |
  108. self announcer announce: (HLInspectItRequested on: model).
  109. newInspector := self makeInspectorOn: self doIt.
  110. newInspector open
  111. !
  112. makeInspectorOn: anObject
  113. ^ HLInspector new
  114. inspect: anObject;
  115. yourself
  116. !
  117. print: aString
  118. | start stop currentLine |
  119. currentLine := (editor getCursor: false) line.
  120. start := HashedCollection new.
  121. start at: 'line' put: currentLine.
  122. start at: 'ch' put: (editor getCursor: false) ch.
  123. (editor getSelection) ifEmpty: [
  124. "select current line if selection is empty"
  125. start at: 'ch' put: (editor getLine: currentLine) size.
  126. editor setSelection: #{'line' -> currentLine. 'ch' -> 0} end: start.
  127. ].
  128. stop := HashedCollection new.
  129. stop at: 'line' put: currentLine.
  130. stop at: 'ch' put: ((start at: 'ch') + aString size + 2).
  131. editor replaceSelection: (editor getSelection, ' ', aString, ' ').
  132. editor setCursor: (editor getCursor: true).
  133. editor setSelection: stop end: start
  134. !
  135. printIt
  136. | result |
  137. result:= self doIt.
  138. self announcer announce: (HLPrintItRequested on: model).
  139. self print: result printString.
  140. self focus.
  141. !
  142. saveIt
  143. "I do not do anything"
  144. !
  145. setEditorOn: aTextarea
  146. <self['@editor'] = CodeMirror.fromTextArea(aTextarea, {
  147. theme: 'amber',
  148. lineNumbers: true,
  149. enterMode: 'flat',
  150. indentWithTabs: true,
  151. indentUnit: 4,
  152. matchBrackets: true,
  153. electricChars: false,
  154. keyMap: 'Amber'
  155. })>
  156. ! !
  157. !HLCodeWidget methodsFor: 'reactions'!
  158. onDoIt
  159. self doIt
  160. !
  161. onInspectIt
  162. self inspectIt
  163. !
  164. onPrintIt
  165. self printIt
  166. !
  167. onSaveIt
  168. "I do not do anything"
  169. ! !
  170. !HLCodeWidget methodsFor: 'rendering'!
  171. renderContentOn: html
  172. code := html textarea.
  173. self setEditorOn: code element.
  174. self configureEditor
  175. ! !
  176. !HLCodeWidget methodsFor: 'testing'!
  177. canHaveFocus
  178. ^ true
  179. !
  180. hasFocus
  181. ^ code asJQuery is: ':active'
  182. ! !
  183. !HLCodeWidget class methodsFor: 'accessing'!
  184. keyMap
  185. ^ HLManager current keyBinder systemIsMac
  186. ifTrue: [ self macKeyMap ]
  187. ifFalse: [ self pcKeyMap ]
  188. !
  189. macKeyMap
  190. ^ #{
  191. 'Alt-Backspace' -> 'delWordBefore'.
  192. 'Alt-Delete' -> 'delWordAfter'.
  193. 'Alt-Left' -> 'goWordBoundaryLeft'.
  194. 'Alt-Right' -> 'goWordBoundaryRight'.
  195. 'Cmd-A' -> 'selectAll'.
  196. 'Cmd-Alt-F' -> 'replace'.
  197. 'Cmd-D' -> 'doIt'.
  198. 'Cmd-Down' -> 'goDocEnd'.
  199. 'Cmd-End' -> 'goDocEnd'.
  200. 'Cmd-F' -> 'find'.
  201. 'Cmd-G' -> 'findNext'.
  202. 'Cmd-I' -> 'inspectIt'.
  203. 'Cmd-Left' -> 'goLineStart'.
  204. 'Cmd-P' -> 'printIt'.
  205. 'Cmd-Right' -> 'goLineEnd'.
  206. 'Cmd-S' -> 'saveIt'.
  207. 'Cmd-Up' -> 'goDocStart'.
  208. 'Cmd-Y' -> 'redo'.
  209. 'Cmd-Z' -> 'undo'.
  210. 'Cmd-[' -> 'indentLess'.
  211. 'Cmd-]' -> 'indentMore'.
  212. 'Ctrl-Alt-Backspace' -> 'delWordAfter'.
  213. 'Shift-Cmd-Alt-F' -> 'replaceAll'.
  214. 'Shift-Cmd-G' -> 'findPrev'.
  215. 'Shift-Cmd-Z' -> 'redo'.
  216. 'fallthrough' -> { 'basic' }
  217. }
  218. !
  219. pcKeyMap
  220. ^ {
  221. 'Alt-Left' -> 'goLineStart'.
  222. 'Alt-Right' -> 'goLineEnd'.
  223. 'Alt-Up' -> 'goDocStart'.
  224. 'Ctrl-A' -> 'selectAll'.
  225. 'Ctrl-Backspace' -> 'delWordBefore'.
  226. 'Ctrl-D' -> 'doIt'.
  227. 'Ctrl-Delete' -> 'delWordAfter'.
  228. 'Ctrl-Down' -> 'goDocEnd'.
  229. 'Ctrl-End' -> 'goDocEnd'.
  230. 'Ctrl-F' -> 'find'.
  231. 'Ctrl-G' -> 'findNext'.
  232. 'Ctrl-I' -> 'inspectIt'.
  233. 'Ctrl-Home' -> 'goDocStart'.
  234. 'Ctrl-Left' -> 'goWordBoundaryLeft'.
  235. 'Ctrl-P' -> 'printIt'.
  236. 'Ctrl-Right' -> 'goWordBoundaryRight'.
  237. 'Ctrl-S' -> 'saveIt'.
  238. 'Ctrl-Y' -> 'redo'.
  239. 'Ctrl-Z' -> 'undo'.
  240. 'Ctrl-[' -> 'indentLess'.
  241. 'Ctrl-]' -> 'indentMore'.
  242. 'Shift-Ctrl-F' -> 'replace'.
  243. 'Shift-Ctrl-G' -> 'findPrev'.
  244. 'Shift-Ctrl-R' -> 'replaceAll'.
  245. 'Shift-Ctrl-Z' -> 'redo'.
  246. 'fallthrough' -> #('basic')
  247. }
  248. !
  249. tabLabel
  250. ^ 'Workspace'
  251. !
  252. tabPriority
  253. ^ 10
  254. ! !
  255. !HLCodeWidget class methodsFor: 'initialization'!
  256. initialize
  257. super initialize.
  258. self
  259. setupCodeMirror;
  260. setupCommands;
  261. setupKeyMaps.
  262. !
  263. setupCodeMirror
  264. < CodeMirror.keyMap.default.fallthrough = ["basic"] >
  265. !
  266. setupCommands
  267. (CodeMirror basicAt: 'commands')
  268. at: 'doIt' put: [ :cm | cm amberCodeWidget doIt ];
  269. at: 'inspectIt' put: [ :cm | cm amberCodeWidget inspectIt ];
  270. at: 'printIt' put: [ :cm | cm amberCodeWidget printIt ];
  271. at: 'saveIt' put: [ :cm | cm amberCodeWidget saveIt ]
  272. !
  273. setupKeyMaps
  274. <CodeMirror.keyMap['Amber'] = self._keyMap()>
  275. ! !
  276. !HLCodeWidget class methodsFor: 'testing'!
  277. canBeOpenAsTab
  278. ^ true
  279. ! !
  280. HLCodeWidget subclass: #HLSourceCodeWidget
  281. instanceVariableNames: 'browserModel'
  282. package: 'Helios-Workspace'!
  283. !HLSourceCodeWidget methodsFor: 'accessing'!
  284. browserModel
  285. ^ browserModel
  286. !
  287. browserModel: aBrowserModel
  288. browserModel := aBrowserModel.
  289. self observeBrowserModel
  290. ! !
  291. !HLSourceCodeWidget methodsFor: 'actions'!
  292. observeBrowserModel
  293. self browserModel announcer
  294. on: HLSaveSourceCode
  295. do: [ :ann | self onSaveIt ];
  296. on: HLParseErrorRaised
  297. do: [ :ann | self onParseError: ann ];
  298. on: HLCompileErrorRaised
  299. do: [ :ann | self onCompileError: ann error ];
  300. on: HLUnknownVariableErrorRaised
  301. do: [ :ann | self onUnknownVariableError: ann error ];
  302. on: HLInstVarAdded
  303. do: [ :ann | self onInstVarAdded ]
  304. !
  305. saveIt
  306. self browserModel saveSourceCode
  307. ! !
  308. !HLSourceCodeWidget methodsFor: 'reactions'!
  309. onCompileError: anError
  310. self alert: anError messageText
  311. !
  312. onInstVarAdded
  313. self browserModel save: self contents
  314. !
  315. onParseError: anAnnouncement
  316. | lineIndex newContents |
  317. lineIndex := 1.
  318. self contents: (String streamContents: [ :stream |
  319. self contents linesDo: [ :each |
  320. lineIndex = anAnnouncement line
  321. ifTrue: [
  322. stream
  323. nextPutAll: (each copyFrom: 1 to: anAnnouncement column);
  324. nextPutAll: '<- ';
  325. nextPutAll: anAnnouncement message;
  326. nextPutAll: ' ';
  327. nextPutAll: (each copyFrom: anAnnouncement column + 1 to: each size) ]
  328. ifFalse: [ stream nextPutAll: each ].
  329. stream nextPutAll: String cr.
  330. lineIndex := lineIndex + 1 ] ])
  331. !
  332. onSaveIt
  333. self browserModel save: self contents
  334. !
  335. onUnknownVariableError: anError
  336. | confirm |
  337. confirm := self confirm: (String streamContents: [ :stream |
  338. stream
  339. nextPutAll: anError messageText;
  340. nextPutAll: String cr;
  341. nextPutAll: 'Would you like to define an instance variable?' ]).
  342. confirm ifFalse: [ ^ self ].
  343. self browserModel addInstVarNamed: anError variableName
  344. ! !
  345. !HLSourceCodeWidget class methodsFor: 'instance creation'!
  346. on: aBrowserModel
  347. ^ self new
  348. browserModel: aBrowserModel;
  349. yourself
  350. ! !
  351. !HLSourceCodeWidget class methodsFor: 'testing'!
  352. canBeOpenAsTab
  353. ^ false
  354. ! !