author | Claus Gittinger <cg@exept.de> |
Wed, 20 Jul 2011 15:24:30 +0200 | |
changeset 10353 | 92b1359dd74a |
parent 10275 | 1151182dcaae |
child 10410 | 0d8eaf28b7a9 |
permissions | -rw-r--r-- |
9980 | 1 |
" |
10077 | 2 |
COPYRIGHT (c) 2010 by Jan Vrany, SWING Research Group. CTU in Prague |
3 |
All Rights Reserved |
|
9980 | 4 |
|
10077 | 5 |
Permission is hereby granted, free of charge, to any person |
6 |
obtaining a copy of this software and associated documentation |
|
7 |
files (the 'Software'), to deal in the Software without |
|
8 |
restriction, including without limitation the rights to use, |
|
9 |
copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
10 |
copies of the Software, and to permit persons to whom the |
|
11 |
Software is furnished to do so, subject to the following |
|
12 |
conditions: |
|
13 |
||
14 |
The above copyright notice and this permission notice shall be |
|
15 |
included in all copies or substantial portions of the Software. |
|
16 |
||
17 |
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, |
|
18 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
|
19 |
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
20 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
21 |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|
22 |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
23 |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|
24 |
OTHER DEALINGS IN THE SOFTWARE. |
|
9980 | 25 |
" |
26 |
"{ Package: 'stx:libtool' }" |
|
27 |
||
28 |
"{ NameSpace: Tools }" |
|
29 |
||
30 |
CodeViewService subclass:#CodeHighlightingService |
|
10275
1151182dcaae
changed: #startSyntaxHighlightProcess
Claus Gittinger <cg@exept.de>
parents:
10272
diff
changeset
|
31 |
instanceVariableNames:'worker workerRunning' |
1151182dcaae
changed: #startSyntaxHighlightProcess
Claus Gittinger <cg@exept.de>
parents:
10272
diff
changeset
|
32 |
classVariableNames:'' |
1151182dcaae
changed: #startSyntaxHighlightProcess
Claus Gittinger <cg@exept.de>
parents:
10272
diff
changeset
|
33 |
poolDictionaries:'' |
1151182dcaae
changed: #startSyntaxHighlightProcess
Claus Gittinger <cg@exept.de>
parents:
10272
diff
changeset
|
34 |
category:'Interface-CodeView' |
9980 | 35 |
! |
36 |
||
37 |
!CodeHighlightingService class methodsFor:'documentation'! |
|
38 |
||
39 |
copyright |
|
40 |
" |
|
10077 | 41 |
COPYRIGHT (c) 2010 by Jan Vrany, SWING Research Group. CTU in Prague |
42 |
All Rights Reserved |
|
43 |
||
44 |
Permission is hereby granted, free of charge, to any person |
|
45 |
obtaining a copy of this software and associated documentation |
|
46 |
files (the 'Software'), to deal in the Software without |
|
47 |
restriction, including without limitation the rights to use, |
|
48 |
copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
49 |
copies of the Software, and to permit persons to whom the |
|
50 |
Software is furnished to do so, subject to the following |
|
51 |
conditions: |
|
9980 | 52 |
|
10077 | 53 |
The above copyright notice and this permission notice shall be |
54 |
included in all copies or substantial portions of the Software. |
|
55 |
||
56 |
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, |
|
57 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
|
58 |
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
59 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
60 |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|
61 |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
62 |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|
63 |
OTHER DEALINGS IN THE SOFTWARE. |
|
9980 | 64 |
" |
65 |
! ! |
|
66 |
||
67 |
!CodeHighlightingService class methodsFor:'accessing'! |
|
68 |
||
69 |
label |
|
70 |
||
71 |
"Answers short label - for UI" |
|
72 |
||
73 |
^'Syntax Highlighting' |
|
74 |
||
75 |
"Created: / 07-03-2010 / 14:00:21 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
76 |
! ! |
|
77 |
||
78 |
!CodeHighlightingService methodsFor:'change & update'! |
|
79 |
||
80 |
update: aspect with: param from: sender |
|
81 |
||
10149 | 82 |
sender == textView modifiedChannel ifTrue:[^self codeChanged: false]. |
83 |
sender == textView model ifTrue:[^self codeChanged: true]. |
|
9980 | 84 |
sender == codeView languageHolder ifTrue:[^self codeChanged: true]. |
85 |
sender == codeView classHolder ifTrue:[^self codeChanged: true]. |
|
86 |
||
87 |
super update: aspect with: param from: sender |
|
88 |
||
89 |
"Created: / 06-03-2010 / 19:38:07 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
10149 | 90 |
"Modified: / 05-07-2011 / 10:18:08 / cg" |
9980 | 91 |
! ! |
92 |
||
93 |
!CodeHighlightingService methodsFor:'private'! |
|
94 |
||
95 |
codeChanged: force |
|
96 |
||
97 |
(force or:[codeView textView modified]) ifTrue: |
|
98 |
[self startSyntaxHighlightProcess]. |
|
99 |
||
100 |
"Modified: / 06-03-2010 / 19:32:29 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
101 |
! |
|
102 |
||
103 |
setHighlightedCode:newCode elements: elements |
|
104 |
"the background synhighlighter has generated new colored text, |
|
105 |
with highlighted syntax. |
|
106 |
If there have been no modifications in the meantime, install it." |
|
107 |
||
108 |
|firstShown lastShown cursorWasOn anyChange newLines l replaceAction textView list| |
|
109 |
||
110 |
textView := codeView textView. |
|
111 |
textView modified ifTrue:[ |
|
112 |
"/ new input arrived in the meantime |
|
113 |
||
114 |
^ self |
|
115 |
]. |
|
116 |
worker notNil ifTrue:[ |
|
117 |
"/ another coloring process has already been started. |
|
118 |
"/ ignore this (leftover) code. |
|
119 |
||
120 |
^ self |
|
121 |
]. |
|
122 |
firstShown := textView firstLineShown. |
|
123 |
lastShown := textView lastLineShown. |
|
124 |
replaceAction := [:lNr :line | |
|
125 |
|oldLine| |
|
126 |
||
127 |
oldLine := list at:lNr ifAbsent:nil. |
|
128 |
oldLine notNil ifTrue:[ |
|
129 |
line notNil ifTrue:[ |
|
130 |
"/ this check is needed - there is a race |
|
131 |
"/ when the text is converted. This detects the |
|
132 |
"/ resulting error. |
|
133 |
"/ Certainly a kludge. |
|
134 |
||
135 |
oldLine string = line string ifTrue:[ |
|
136 |
oldLine emphasis ~= line emphasis ifTrue:[ |
|
137 |
textView modifiedChannel removeDependent:self. |
|
138 |
list at:lNr put:line. |
|
139 |
textView modifiedChannel addDependent:self. |
|
140 |
(lNr between:firstShown and:lastShown) ifTrue:[ |
|
141 |
anyChange ifFalse:[ |
|
142 |
anyChange := true. |
|
143 |
cursorWasOn := textView hideCursor |
|
144 |
]. |
|
145 |
textView redrawLine:lNr |
|
146 |
] |
|
147 |
] |
|
148 |
] |
|
149 |
] |
|
150 |
] |
|
151 |
]. |
|
10234
7cdbded1e728
comment/format in: #setHighlightedCode:elements:
Claus Gittinger <cg@exept.de>
parents:
10151
diff
changeset
|
152 |
|
9980 | 153 |
anyChange := false. |
154 |
newLines := newCode asStringCollection. |
|
155 |
list := textView list. |
|
156 |
list isNil ifTrue:[ |
|
157 |
textView list:newLines. |
|
158 |
] ifFalse:[ |
|
10234
7cdbded1e728
comment/format in: #setHighlightedCode:elements:
Claus Gittinger <cg@exept.de>
parents:
10151
diff
changeset
|
159 |
"/ the cursor line first - that's where your eyes are ... |
9980 | 160 |
(l := textView cursorLine) notNil ifTrue:[ |
161 |
l <= newLines size ifTrue:[ |
|
162 |
replaceAction value:l value:(newLines at:l) |
|
163 |
] |
|
164 |
]. |
|
165 |
newLines keysAndValuesDo:replaceAction. |
|
166 |
anyChange ifTrue:[ |
|
167 |
cursorWasOn ifTrue:[ |
|
168 |
textView showCursor |
|
169 |
] |
|
170 |
] |
|
171 |
]. |
|
172 |
codeView syntaxElements: elements |
|
173 |
||
174 |
"Modified: / 09-10-2006 / 11:50:17 / cg" |
|
175 |
"Created: / 14-02-2010 / 16:10:21 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
176 |
"Modified: / 06-03-2010 / 19:58:42 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
10234
7cdbded1e728
comment/format in: #setHighlightedCode:elements:
Claus Gittinger <cg@exept.de>
parents:
10151
diff
changeset
|
177 |
"Modified (format): / 06-07-2011 / 18:21:12 / cg" |
9980 | 178 |
! |
179 |
||
180 |
showInfo: aString |
|
181 |
||
182 |
codeView showInfo: aString |
|
183 |
||
184 |
"Created: / 06-03-2010 / 19:34:51 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
185 |
! |
|
186 |
||
187 |
startSyntaxHighlightProcess |
|
188 |
"start a background process, which does the syntax coloring. |
|
189 |
When it finishes, it pushes a user event to show the new text in the codeView. |
|
190 |
(This is done as an event to synchronize the coloring with modifications |
|
191 |
done to the text - the colored text will discarded, if there were |
|
192 |
any new modifications in the meanwhile)" |
|
193 |
||
194 |
|dontDoIt oldCodeList highlighterClass prio textView| |
|
195 |
||
196 |
textView := codeView textView. |
|
197 |
||
198 |
highlighterClass := self syntaxHighlighterClass. |
|
199 |
||
200 |
dontDoIt := highlighterClass isNil. |
|
201 |
"dontDoIt := dontDoIt |
|
202 |
or:[self doSyntaxColoring value ~~ true |
|
203 |
or:[(self doImmediateSyntaxColoring) value ~~ true]]." |
|
204 |
dontDoIt ifTrue:[^self]. |
|
205 |
||
206 |
"/ this clobbers the codeViews modified state; therefore, we have to remember |
|
207 |
"/ this info somewhere ... |
|
10244 | 208 |
codeView browser ifNotNil:[ |
209 |
textView modified ifTrue:[ |
|
210 |
codeView browser navigationState realModifiedState: true |
|
211 |
]. |
|
212 |
textView modifiedChannel setValue:false. |
|
9980 | 213 |
]. |
214 |
||
215 |
worker notNil ifTrue:[ |
|
216 |
workerRunning ~~ true ifTrue:[ |
|
217 |
"/ process already created, but did not get a change to start yet; |
|
218 |
^ self |
|
219 |
]. |
|
220 |
self stopSyntaxHighlightProcess |
|
221 |
]. |
|
222 |
prio := Processor userBackgroundPriority - 1. |
|
223 |
textView shown ifFalse:[ |
|
224 |
prio := prio - 1 max:1 |
|
225 |
]. |
|
10149 | 226 |
|
9980 | 227 |
worker := [ |
228 |
[ |
|
10149 | 229 |
|oldCode newCode elements cls sensor mthd| |
230 |
||
9980 | 231 |
workerRunning := true. |
232 |
codeView syntaxElements: nil. |
|
233 |
codeView syntaxElementSelection: nil. |
|
234 |
cls := codeView classHolder value. |
|
235 |
(cls notNil and:[cls isObsolete]) ifTrue:[ |
|
236 |
cls isMeta ifTrue:[ |
|
237 |
cls := (Smalltalk at:cls theNonMetaclass name) class |
|
238 |
] ifFalse:[ |
|
239 |
cls := Smalltalk at:cls name |
|
240 |
]. |
|
241 |
]. |
|
10149 | 242 |
mthd := codeView methodHolder value. |
243 |
||
9980 | 244 |
textView modified ifFalse:[ |
245 |
oldCodeList := textView list copy. |
|
246 |
textView modified ifFalse:[ |
|
247 |
oldCodeList isNil ifFalse:[ |
|
248 |
oldCode := oldCodeList asStringWithoutEmphasis. |
|
249 |
textView modified ifFalse:[ |
|
250 |
Screen currentScreenQuerySignal answer:codeView device |
|
251 |
do:[ |
|
252 |
Parser::ParseError handle:[:ex | |
|
253 |
|errMsg| |
|
254 |
||
255 |
errMsg := ex description asStringCollection first asString. |
|
256 |
||
257 |
"/ Transcript topView raiseDeiconified. |
|
258 |
"/ Transcript showCR:'ParseError: ', ex description. |
|
259 |
"/ self halt. |
|
10275
1151182dcaae
changed: #startSyntaxHighlightProcess
Claus Gittinger <cg@exept.de>
parents:
10272
diff
changeset
|
260 |
"/ self showInfo:(errMsg colorizeAllWith:Color red). |
9980 | 261 |
newCode := nil. |
262 |
] do:[ |
|
263 |
elements := SortedCollection new. |
|
264 |
codeView codeAspect == #method ifTrue:[ |
|
10149 | 265 |
newCode := highlighterClass formatMethod:mthd source:oldCode in:cls using: nil elementsInto: elements. |
9980 | 266 |
] ifFalse:[ |
267 |
codeView codeAspect == #expression ifTrue:[ |
|
268 |
newCode := highlighterClass formatExpression:oldCode in:cls elementsInto: elements. |
|
269 |
] ifFalse:[ |
|
270 |
codeView codeAspect == #classDefinition ifTrue:[ |
|
271 |
newCode := highlighterClass formatClassDefinition:oldCode in:cls elementsInto: elements. |
|
272 |
] |
|
273 |
]. |
|
274 |
]. |
|
275 |
] |
|
276 |
]. |
|
277 |
newCode notNil ifTrue:[ |
|
278 |
textView modified ifFalse:[ |
|
10353
92b1359dd74a
changed: #startSyntaxHighlightProcess
Claus Gittinger <cg@exept.de>
parents:
10275
diff
changeset
|
279 |
newCode ~= oldCodeList ifTrue:[ |
9980 | 280 |
newCode := newCode asStringCollection. |
281 |
textView modified ifFalse:[ |
|
282 |
worker := nil. |
|
283 |
(textView := codeView textView) notNil ifTrue:[ |
|
284 |
"/ must add this event - and not been interrupted |
|
285 |
"/ by any arriving key-event. |
|
10272
3f3652010960
changed: #startSyntaxHighlightProcess
Claus Gittinger <cg@exept.de>
parents:
10244
diff
changeset
|
286 |
"/ self showInfo:nil. |
10353
92b1359dd74a
changed: #startSyntaxHighlightProcess
Claus Gittinger <cg@exept.de>
parents:
10275
diff
changeset
|
287 |
|
9980 | 288 |
codeView sensor |
289 |
pushUserEvent:#setHighlightedCode:elements: |
|
290 |
for:self |
|
291 |
withArguments:(Array with:newCode with: elements). |
|
292 |
"/self delayedUpdateBufferLabelWithCheckIfModified |
|
293 |
] |
|
294 |
] |
|
10353
92b1359dd74a
changed: #startSyntaxHighlightProcess
Claus Gittinger <cg@exept.de>
parents:
10275
diff
changeset
|
295 |
]. |
9980 | 296 |
] |
297 |
] |
|
298 |
] |
|
299 |
] |
|
300 |
] |
|
301 |
] |
|
302 |
] ensure:[ |
|
303 |
workerRunning := false. |
|
304 |
worker := nil |
|
305 |
] |
|
306 |
] forkAt:prio |
|
307 |
||
308 |
"Modified: / 10-04-2011 / 18:21:32 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
10244 | 309 |
"Modified: / 07-07-2011 / 12:26:12 / Jan Vrany <jan.vrant@fit.cvut,cz>" |
10353
92b1359dd74a
changed: #startSyntaxHighlightProcess
Claus Gittinger <cg@exept.de>
parents:
10275
diff
changeset
|
310 |
"Modified: / 20-07-2011 / 15:14:14 / cg" |
9980 | 311 |
! |
312 |
||
313 |
stopSyntaxHighlightProcess |
|
314 |
"stop any syntax coloring background process." |
|
315 |
||
316 |
|p| |
|
317 |
||
318 |
(p := worker) ifNil:[^self]. |
|
319 |
worker := nil. |
|
320 |
p terminate. |
|
321 |
"/ raise its prio to make it terminate quickly |
|
322 |
p priority:(Processor userSchedulingPriority + 1) |
|
323 |
||
324 |
"Modified: / 03-09-2010 / 22:27:19 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
325 |
! |
|
326 |
||
327 |
syntaxHighlighterClass |
|
10149 | 328 |
| lang cls mthd | |
9980 | 329 |
|
330 |
cls := (lang := codeView languageHolder value) |
|
331 |
ifNil:[nil] |
|
332 |
ifNotNil:[lang syntaxHighlighterClass]. |
|
10149 | 333 |
|
9980 | 334 |
"Ugly hack because I don't want to branch libcomp :-)" |
10149 | 335 |
cls == SyntaxHighlighter ifTrue:[ |
336 |
"/ hack |
|
337 |
mthd := codeView methodHolder value. |
|
338 |
"/ what about method-language ? |
|
339 |
(mthd notNil and:[codeView browserHolder value notNil]) ifTrue:[ |
|
340 |
cls := codeView browserHolder value syntaxHighlighterForMethod:mthd. |
|
341 |
]. |
|
342 |
cls == SyntaxHighlighter ifTrue:[ |
|
343 |
cls := SyntaxHighlighter2 |
|
344 |
] |
|
345 |
]. |
|
346 |
^ cls |
|
9980 | 347 |
|
348 |
"Created: / 14-02-2010 / 12:39:15 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
349 |
"Modified: / 06-03-2010 / 19:39:05 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
10151
8d04d7db6448
comment/format in: #startSyntaxHighlightProcess
Claus Gittinger <cg@exept.de>
parents:
10149
diff
changeset
|
350 |
"Modified: / 05-07-2011 / 11:10:11 / cg" |
9980 | 351 |
! ! |
352 |
||
353 |
!CodeHighlightingService class methodsFor:'documentation'! |
|
354 |
||
355 |
version_CVS |
|
10353
92b1359dd74a
changed: #startSyntaxHighlightProcess
Claus Gittinger <cg@exept.de>
parents:
10275
diff
changeset
|
356 |
^ '$Header: /cvs/stx/stx/libtool/Tools__CodeHighlightingService.st,v 1.9 2011-07-20 13:24:30 cg Exp $' |
9980 | 357 |
! |
358 |
||
359 |
version_SVN |
|
360 |
^ '§Id: Tools__CodeHighlightingService.st 7715 2011-04-10 16:32:58Z vranyj1 §' |
|
361 |
! ! |