author | vrany |
Wed, 03 Aug 2011 01:33:41 +0200 | |
changeset 10434 | f3f696075c22 |
parent 10420 | ed96d3da2be7 |
child 10438 | bd3f0c062ac3 |
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 |
|
10420 | 31 |
instanceVariableNames:'job done' |
10275
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 |
||
10420 | 93 |
!CodeHighlightingService methodsFor:'initialization'! |
94 |
||
95 |
initialize |
|
96 |
||
10434
f3f696075c22
Fix after moving BackgroundJob from libtool to libbasic2
vrany
parents:
10420
diff
changeset
|
97 |
job := (Smalltalk at:#BackgroundJob) |
10420 | 98 |
named: 'Syntax Highlighting Job' |
99 |
on:[self syntaxHighlight: true]. |
|
100 |
||
101 |
"Created: / 29-07-2011 / 10:31:03 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
102 |
! ! |
|
103 |
||
9980 | 104 |
!CodeHighlightingService methodsFor:'private'! |
105 |
||
106 |
codeChanged: force |
|
107 |
||
108 |
(force or:[codeView textView modified]) ifTrue: |
|
10420 | 109 |
[self syntaxHighlight]. |
9980 | 110 |
|
10420 | 111 |
"Modified: / 29-07-2011 / 11:07:56 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
9980 | 112 |
! |
113 |
||
114 |
setHighlightedCode:newCode elements: elements |
|
115 |
"the background synhighlighter has generated new colored text, |
|
116 |
with highlighted syntax. |
|
117 |
If there have been no modifications in the meantime, install it." |
|
118 |
||
10420 | 119 |
|firstShown lastShown cursorWasOn anyChange newLines l replaceAction list| |
9980 | 120 |
|
10420 | 121 |
|
9980 | 122 |
textView modified ifTrue:[ |
123 |
"/ new input arrived in the meantime |
|
124 |
||
125 |
^ self |
|
126 |
]. |
|
10420 | 127 |
done ifFalse:[ |
9980 | 128 |
"/ another coloring process has already been started. |
129 |
"/ ignore this (leftover) code. |
|
130 |
^ self |
|
131 |
]. |
|
132 |
firstShown := textView firstLineShown. |
|
133 |
lastShown := textView lastLineShown. |
|
134 |
replaceAction := [:lNr :line | |
|
135 |
|oldLine| |
|
136 |
||
137 |
oldLine := list at:lNr ifAbsent:nil. |
|
138 |
oldLine notNil ifTrue:[ |
|
139 |
line notNil ifTrue:[ |
|
140 |
"/ this check is needed - there is a race |
|
141 |
"/ when the text is converted. This detects the |
|
142 |
"/ resulting error. |
|
143 |
"/ Certainly a kludge. |
|
144 |
||
145 |
oldLine string = line string ifTrue:[ |
|
146 |
oldLine emphasis ~= line emphasis ifTrue:[ |
|
147 |
textView modifiedChannel removeDependent:self. |
|
148 |
list at:lNr put:line. |
|
149 |
textView modifiedChannel addDependent:self. |
|
150 |
(lNr between:firstShown and:lastShown) ifTrue:[ |
|
151 |
anyChange ifFalse:[ |
|
152 |
anyChange := true. |
|
153 |
cursorWasOn := textView hideCursor |
|
154 |
]. |
|
155 |
textView redrawLine:lNr |
|
156 |
] |
|
157 |
] |
|
158 |
] |
|
159 |
] |
|
160 |
] |
|
161 |
]. |
|
10234
7cdbded1e728
comment/format in: #setHighlightedCode:elements:
Claus Gittinger <cg@exept.de>
parents:
10151
diff
changeset
|
162 |
|
9980 | 163 |
anyChange := false. |
164 |
newLines := newCode asStringCollection. |
|
165 |
list := textView list. |
|
166 |
list isNil ifTrue:[ |
|
167 |
textView list:newLines. |
|
168 |
] ifFalse:[ |
|
10234
7cdbded1e728
comment/format in: #setHighlightedCode:elements:
Claus Gittinger <cg@exept.de>
parents:
10151
diff
changeset
|
169 |
"/ the cursor line first - that's where your eyes are ... |
9980 | 170 |
(l := textView cursorLine) notNil ifTrue:[ |
171 |
l <= newLines size ifTrue:[ |
|
172 |
replaceAction value:l value:(newLines at:l) |
|
173 |
] |
|
174 |
]. |
|
175 |
newLines keysAndValuesDo:replaceAction. |
|
176 |
anyChange ifTrue:[ |
|
177 |
cursorWasOn ifTrue:[ |
|
178 |
textView showCursor |
|
179 |
] |
|
180 |
] |
|
181 |
]. |
|
182 |
codeView syntaxElements: elements |
|
183 |
||
184 |
"Modified: / 09-10-2006 / 11:50:17 / cg" |
|
185 |
"Created: / 14-02-2010 / 16:10:21 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
10234
7cdbded1e728
comment/format in: #setHighlightedCode:elements:
Claus Gittinger <cg@exept.de>
parents:
10151
diff
changeset
|
186 |
"Modified (format): / 06-07-2011 / 18:21:12 / cg" |
10420 | 187 |
"Modified: / 29-07-2011 / 11:10:32 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
9980 | 188 |
! |
189 |
||
190 |
showInfo: aString |
|
191 |
||
192 |
codeView showInfo: aString |
|
193 |
||
194 |
"Created: / 06-03-2010 / 19:34:51 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
195 |
! |
|
196 |
||
10420 | 197 |
syntaxHighlight |
9980 | 198 |
|
10420 | 199 |
|highlighterClass prio | |
9980 | 200 |
|
201 |
highlighterClass := self syntaxHighlighterClass. |
|
202 |
||
10420 | 203 |
highlighterClass isNil ifTrue:[ |
204 |
"No higlighter, nothing to do" |
|
205 |
^self |
|
206 |
]. |
|
9980 | 207 |
|
208 |
"/ this clobbers the codeViews modified state; therefore, we have to remember |
|
209 |
"/ this info somewhere ... |
|
10244 | 210 |
codeView browser ifNotNil:[ |
211 |
textView modified ifTrue:[ |
|
212 |
codeView browser navigationState realModifiedState: true |
|
213 |
]. |
|
214 |
textView modifiedChannel setValue:false. |
|
9980 | 215 |
]. |
216 |
||
10420 | 217 |
job scheduled ifTrue:[ |
218 |
job running ifFalse:[ |
|
9980 | 219 |
"/ process already created, but did not get a change to start yet; |
220 |
^ self |
|
10420 | 221 |
] ifTrue:[ |
222 |
job stop. |
|
223 |
] |
|
9980 | 224 |
]. |
225 |
prio := Processor userBackgroundPriority - 1. |
|
226 |
textView shown ifFalse:[ |
|
227 |
prio := prio - 1 max:1 |
|
228 |
]. |
|
10149 | 229 |
|
10420 | 230 |
job startWithPriority: prio. |
231 |
||
232 |
"Modified: / 07-07-2011 / 12:26:12 / Jan Vrany <jan.vrant@fit.cvut,cz>" |
|
233 |
"Modified: / 20-07-2011 / 15:14:14 / cg" |
|
234 |
"Created: / 29-07-2011 / 11:07:42 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
235 |
! |
|
236 |
||
237 |
syntaxHighlight: delayed |
|
238 |
||
239 |
|oldCode oldCodeList newCode elements cls mthd highlighterClass| |
|
240 |
||
241 |
done := false. |
|
242 |
codeView syntaxElements: nil. |
|
243 |
codeView syntaxElementSelection: nil. |
|
244 |
||
245 |
highlighterClass := self syntaxHighlighterClass. |
|
10149 | 246 |
|
10420 | 247 |
cls := codeView klass. |
248 |
(cls notNil and:[cls isObsolete]) ifTrue:[ |
|
249 |
cls isMeta ifTrue:[ |
|
250 |
cls := (Smalltalk at:cls theNonMetaclass name) class |
|
251 |
] ifFalse:[ |
|
252 |
cls := Smalltalk at:cls name |
|
253 |
]. |
|
254 |
]. |
|
255 |
mthd := codeView methodHolder value. |
|
10149 | 256 |
|
10420 | 257 |
textView modified ifFalse:[ |
258 |
oldCodeList := textView list copy. |
|
259 |
textView modified ifFalse:[ |
|
260 |
oldCodeList isNil ifFalse:[ |
|
261 |
oldCode := oldCodeList asStringWithoutEmphasis. |
|
262 |
textView modified ifFalse:[ |
|
263 |
Screen currentScreenQuerySignal answer:codeView device |
|
264 |
do:[ |
|
265 |
Parser::ParseError handle:[:ex | |
|
266 |
|errMsg| |
|
9980 | 267 |
|
10420 | 268 |
errMsg := ex description asStringCollection first asString. |
9980 | 269 |
|
10420 | 270 |
"/ Transcript topView raiseDeiconified. |
271 |
"/ Transcript showCR:'ParseError: ', ex description. |
|
9980 | 272 |
"/ self halt. |
10420 | 273 |
"/ self showInfo:(errMsg colorizeAllWith:Color red). |
274 |
newCode := nil. |
|
275 |
] do:[ |
|
276 |
elements := SortedCollection new. |
|
277 |
codeView codeAspect == #method ifTrue:[ |
|
278 |
newCode := highlighterClass formatMethod:mthd source:oldCode in:cls using: nil elementsInto: elements. |
|
279 |
] ifFalse:[ |
|
280 |
codeView codeAspect == #expression ifTrue:[ |
|
281 |
newCode := highlighterClass formatExpression:oldCode in:cls elementsInto: elements. |
|
282 |
] ifFalse:[ |
|
283 |
codeView codeAspect == #classDefinition ifTrue:[ |
|
284 |
newCode := highlighterClass formatClassDefinition:oldCode in:cls elementsInto: elements. |
|
285 |
] |
|
286 |
]. |
|
287 |
]. |
|
288 |
] |
|
289 |
]. |
|
290 |
newCode notNil ifTrue:[ |
|
291 |
textView modified ifFalse:[ |
|
292 |
newCode ~= oldCodeList ifTrue:[ |
|
293 |
newCode := newCode asStringCollection. |
|
294 |
textView modified ifFalse:[ |
|
295 |
done := true. |
|
296 |
textView notNil ifTrue:[ |
|
297 |
"/ must add this event - and not been interrupted |
|
298 |
"/ by any arriving key-event. |
|
299 |
"/ self showInfo:nil. |
|
300 |
delayed ifTrue:[ |
|
301 |
codeView sensor |
|
302 |
pushUserEvent:#setHighlightedCode:elements: |
|
303 |
for:self |
|
304 |
withArguments:(Array with:newCode with: elements). |
|
305 |
"/self delayedUpdateBufferLabelWithCheckIfModified |
|
306 |
] ifFalse:[ |
|
307 |
textView contents: newCode. |
|
9980 | 308 |
] |
309 |
] |
|
310 |
] |
|
10420 | 311 |
]. |
9980 | 312 |
] |
313 |
] |
|
314 |
] |
|
10420 | 315 |
] |
316 |
] |
|
317 |
] |
|
9980 | 318 |
|
10420 | 319 |
"Created: / 29-07-2011 / 11:01:53 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
9980 | 320 |
! |
321 |
||
322 |
syntaxHighlighterClass |
|
10149 | 323 |
| lang cls mthd | |
9980 | 324 |
|
325 |
cls := (lang := codeView languageHolder value) |
|
326 |
ifNil:[nil] |
|
327 |
ifNotNil:[lang syntaxHighlighterClass]. |
|
10149 | 328 |
|
9980 | 329 |
"Ugly hack because I don't want to branch libcomp :-)" |
10149 | 330 |
cls == SyntaxHighlighter ifTrue:[ |
331 |
"/ hack |
|
332 |
mthd := codeView methodHolder value. |
|
333 |
"/ what about method-language ? |
|
334 |
(mthd notNil and:[codeView browserHolder value notNil]) ifTrue:[ |
|
335 |
cls := codeView browserHolder value syntaxHighlighterForMethod:mthd. |
|
336 |
]. |
|
337 |
cls == SyntaxHighlighter ifTrue:[ |
|
338 |
cls := SyntaxHighlighter2 |
|
339 |
] |
|
340 |
]. |
|
341 |
^ cls |
|
9980 | 342 |
|
343 |
"Created: / 14-02-2010 / 12:39:15 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
344 |
"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
|
345 |
"Modified: / 05-07-2011 / 11:10:11 / cg" |
9980 | 346 |
! ! |
347 |
||
10410 | 348 |
!CodeHighlightingService methodsFor:'registering'! |
349 |
||
350 |
registerIn: aCodeView |
|
351 |
||
352 |
super registerIn: aCodeView. |
|
10420 | 353 |
self syntaxHighlight. |
10410 | 354 |
|
355 |
"Created: / 27-07-2011 / 13:25:03 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
356 |
! ! |
|
357 |
||
9980 | 358 |
!CodeHighlightingService class methodsFor:'documentation'! |
359 |
||
360 |
version_CVS |
|
10434
f3f696075c22
Fix after moving BackgroundJob from libtool to libbasic2
vrany
parents:
10420
diff
changeset
|
361 |
^ '$Header: /cvs/stx/stx/libtool/Tools__CodeHighlightingService.st,v 1.12 2011-08-02 23:33:41 vrany Exp $' |
9980 | 362 |
! |
363 |
||
364 |
version_SVN |
|
365 |
^ '§Id: Tools__CodeHighlightingService.st 7715 2011-04-10 16:32:58Z vranyj1 §' |
|
366 |
! ! |