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
|
|
31 |
instanceVariableNames:'worker workerRunning'
|
|
32 |
classVariableNames:''
|
|
33 |
poolDictionaries:''
|
|
34 |
category:'Interface-CodeView'
|
|
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 |
|
|
82 |
sender == codeView textView modifiedChannel ifTrue:[^self codeChanged: false].
|
|
83 |
sender == codeView textView model ifTrue:[^self codeChanged: true].
|
|
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>"
|
|
90 |
! !
|
|
91 |
|
|
92 |
!CodeHighlightingService methodsFor:'private'!
|
|
93 |
|
|
94 |
codeChanged: force
|
|
95 |
|
|
96 |
(force or:[codeView textView modified]) ifTrue:
|
|
97 |
[self startSyntaxHighlightProcess].
|
|
98 |
|
|
99 |
"Modified: / 06-03-2010 / 19:32:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
100 |
!
|
|
101 |
|
|
102 |
setHighlightedCode:newCode elements: elements
|
|
103 |
"the background synhighlighter has generated new colored text,
|
|
104 |
with highlighted syntax.
|
|
105 |
If there have been no modifications in the meantime, install it."
|
|
106 |
|
|
107 |
|firstShown lastShown cursorWasOn anyChange newLines l replaceAction textView list|
|
|
108 |
|
|
109 |
textView := codeView textView.
|
|
110 |
textView modified ifTrue:[
|
|
111 |
"/ new input arrived in the meantime
|
|
112 |
|
|
113 |
^ self
|
|
114 |
].
|
|
115 |
worker notNil ifTrue:[
|
|
116 |
"/ another coloring process has already been started.
|
|
117 |
"/ ignore this (leftover) code.
|
|
118 |
|
|
119 |
^ self
|
|
120 |
].
|
|
121 |
firstShown := textView firstLineShown.
|
|
122 |
lastShown := textView lastLineShown.
|
|
123 |
replaceAction := [:lNr :line |
|
|
124 |
|oldLine|
|
|
125 |
|
|
126 |
oldLine := list at:lNr ifAbsent:nil.
|
|
127 |
oldLine notNil ifTrue:[
|
|
128 |
line notNil ifTrue:[
|
|
129 |
"/ this check is needed - there is a race
|
|
130 |
"/ when the text is converted. This detects the
|
|
131 |
"/ resulting error.
|
|
132 |
"/ Certainly a kludge.
|
|
133 |
|
|
134 |
oldLine string = line string ifTrue:[
|
|
135 |
oldLine emphasis ~= line emphasis ifTrue:[
|
|
136 |
textView modifiedChannel removeDependent:self.
|
|
137 |
list at:lNr put:line.
|
|
138 |
textView modifiedChannel addDependent:self.
|
|
139 |
(lNr between:firstShown and:lastShown) ifTrue:[
|
|
140 |
anyChange ifFalse:[
|
|
141 |
anyChange := true.
|
|
142 |
cursorWasOn := textView hideCursor
|
|
143 |
].
|
|
144 |
textView redrawLine:lNr
|
|
145 |
]
|
|
146 |
]
|
|
147 |
]
|
|
148 |
]
|
|
149 |
]
|
|
150 |
].
|
|
151 |
anyChange := false.
|
|
152 |
newLines := newCode asStringCollection.
|
|
153 |
list := textView list.
|
|
154 |
list isNil ifTrue:[
|
|
155 |
textView list:newLines.
|
|
156 |
] ifFalse:[
|
|
157 |
"/ the cursor line first - thats where your eyes are ...
|
|
158 |
(l := textView cursorLine) notNil ifTrue:[
|
|
159 |
l <= newLines size ifTrue:[
|
|
160 |
replaceAction value:l value:(newLines at:l)
|
|
161 |
]
|
|
162 |
].
|
|
163 |
newLines keysAndValuesDo:replaceAction.
|
|
164 |
anyChange ifTrue:[
|
|
165 |
cursorWasOn ifTrue:[
|
|
166 |
textView showCursor
|
|
167 |
]
|
|
168 |
]
|
|
169 |
].
|
|
170 |
codeView syntaxElements: elements
|
|
171 |
|
|
172 |
"Modified: / 09-10-2006 / 11:50:17 / cg"
|
|
173 |
"Created: / 14-02-2010 / 16:10:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
174 |
"Modified: / 06-03-2010 / 19:58:42 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
175 |
!
|
|
176 |
|
|
177 |
showInfo: aString
|
|
178 |
|
|
179 |
codeView showInfo: aString
|
|
180 |
|
|
181 |
"Created: / 06-03-2010 / 19:34:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
182 |
!
|
|
183 |
|
|
184 |
startSyntaxHighlightProcess
|
|
185 |
"start a background process, which does the syntax coloring.
|
|
186 |
When it finishes, it pushes a user event to show the new text in the codeView.
|
|
187 |
(This is done as an event to synchronize the coloring with modifications
|
|
188 |
done to the text - the colored text will discarded, if there were
|
|
189 |
any new modifications in the meanwhile)"
|
|
190 |
|
|
191 |
|dontDoIt oldCodeList highlighterClass prio textView|
|
|
192 |
|
|
193 |
textView := codeView textView.
|
|
194 |
|
|
195 |
highlighterClass := self syntaxHighlighterClass.
|
|
196 |
|
|
197 |
dontDoIt := highlighterClass isNil.
|
|
198 |
"dontDoIt := dontDoIt
|
|
199 |
or:[self doSyntaxColoring value ~~ true
|
|
200 |
or:[(self doImmediateSyntaxColoring) value ~~ true]]."
|
|
201 |
dontDoIt ifTrue:[^self].
|
|
202 |
|
|
203 |
|
|
204 |
|
|
205 |
|
|
206 |
"/ this clobbers the codeViews modified state; therefore, we have to remember
|
|
207 |
"/ this info somewhere ...
|
|
208 |
textView modified ifTrue:[
|
|
209 |
codeView modifiedChannel value:true
|
|
210 |
].
|
|
211 |
textView modifiedChannel setValue:false.
|
|
212 |
|
|
213 |
worker notNil ifTrue:[
|
|
214 |
workerRunning ~~ true ifTrue:[
|
|
215 |
"/ process already created, but did not get a change to start yet;
|
|
216 |
^ self
|
|
217 |
].
|
|
218 |
self stopSyntaxHighlightProcess
|
|
219 |
].
|
|
220 |
prio := Processor userBackgroundPriority - 1.
|
|
221 |
textView shown ifFalse:[
|
|
222 |
prio := prio - 1 max:1
|
|
223 |
].
|
|
224 |
worker := [
|
|
225 |
|oldCode newCode elements cls sensor |
|
|
226 |
[
|
|
227 |
workerRunning := true.
|
|
228 |
codeView syntaxElements: nil.
|
|
229 |
codeView syntaxElementSelection: nil.
|
|
230 |
cls := codeView classHolder value.
|
|
231 |
(cls notNil and:[cls isObsolete]) ifTrue:[
|
|
232 |
cls isMeta ifTrue:[
|
|
233 |
cls := (Smalltalk at:cls theNonMetaclass name) class
|
|
234 |
] ifFalse:[
|
|
235 |
cls := Smalltalk at:cls name
|
|
236 |
].
|
|
237 |
].
|
|
238 |
textView modified ifFalse:[
|
|
239 |
oldCodeList := textView list copy.
|
|
240 |
textView modified ifFalse:[
|
|
241 |
oldCodeList isNil ifFalse:[
|
|
242 |
oldCode := oldCodeList asStringWithoutEmphasis.
|
|
243 |
textView modified ifFalse:[
|
|
244 |
Screen currentScreenQuerySignal answer:codeView device
|
|
245 |
do:[
|
|
246 |
Parser::ParseError handle:[:ex |
|
|
247 |
|errMsg|
|
|
248 |
|
|
249 |
errMsg := ex description asStringCollection first asString.
|
|
250 |
|
|
251 |
"/ Transcript topView raiseDeiconified.
|
|
252 |
"/ Transcript showCR:'ParseError: ', ex description.
|
|
253 |
"/ self halt.
|
|
254 |
self showInfo:(errMsg colorizeAllWith:Color red).
|
|
255 |
newCode := nil.
|
|
256 |
] do:[
|
|
257 |
elements := SortedCollection new.
|
|
258 |
codeView codeAspect == #method ifTrue:[
|
|
259 |
newCode := highlighterClass formatMethod:oldCode in:cls using: nil elementsInto: elements.
|
|
260 |
] ifFalse:[
|
|
261 |
codeView codeAspect == #expression ifTrue:[
|
|
262 |
newCode := highlighterClass formatExpression:oldCode in:cls elementsInto: elements.
|
|
263 |
] ifFalse:[
|
|
264 |
codeView codeAspect == #classDefinition ifTrue:[
|
|
265 |
newCode := highlighterClass formatClassDefinition:oldCode in:cls elementsInto: elements.
|
|
266 |
]
|
|
267 |
].
|
|
268 |
].
|
|
269 |
]
|
|
270 |
].
|
|
271 |
newCode notNil ifTrue:[
|
|
272 |
textView modified ifFalse:[
|
|
273 |
newCode := newCode asStringCollection.
|
|
274 |
textView modified ifFalse:[
|
|
275 |
worker := nil.
|
|
276 |
(textView := codeView textView) notNil ifTrue:[
|
|
277 |
"/ must add this event - and not been interrupted
|
|
278 |
"/ by any arriving key-event.
|
|
279 |
self showInfo:nil.
|
|
280 |
codeView sensor
|
|
281 |
pushUserEvent:#setHighlightedCode:elements:
|
|
282 |
for:self
|
|
283 |
withArguments:(Array with:newCode with: elements).
|
|
284 |
"/self delayedUpdateBufferLabelWithCheckIfModified
|
|
285 |
]
|
|
286 |
]
|
|
287 |
]
|
|
288 |
]
|
|
289 |
]
|
|
290 |
]
|
|
291 |
]
|
|
292 |
]
|
|
293 |
] ensure:[
|
|
294 |
workerRunning := false.
|
|
295 |
worker := nil
|
|
296 |
]
|
|
297 |
] forkAt:prio
|
|
298 |
|
|
299 |
"Modified: / 22-08-2006 / 14:27:09 / cg"
|
|
300 |
"Modified: / 10-04-2011 / 18:21:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
301 |
!
|
|
302 |
|
|
303 |
stopSyntaxHighlightProcess
|
|
304 |
"stop any syntax coloring background process."
|
|
305 |
|
|
306 |
|p|
|
|
307 |
|
|
308 |
(p := worker) ifNil:[^self].
|
|
309 |
worker := nil.
|
|
310 |
p terminate.
|
|
311 |
"/ raise its prio to make it terminate quickly
|
|
312 |
p priority:(Processor userSchedulingPriority + 1)
|
|
313 |
|
|
314 |
"Modified: / 03-09-2010 / 22:27:19 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
315 |
!
|
|
316 |
|
|
317 |
syntaxHighlighterClass
|
|
318 |
|
|
319 |
| lang cls |
|
|
320 |
cls := (lang := codeView languageHolder value)
|
|
321 |
ifNil:[nil]
|
|
322 |
ifNotNil:[lang syntaxHighlighterClass].
|
|
323 |
"Ugly hack because I don't want to branch libcomp :-)"
|
|
324 |
cls == SyntaxHighlighter ifTrue:[cls := SyntaxHighlighter2].
|
|
325 |
^cls
|
|
326 |
|
|
327 |
"Created: / 14-02-2010 / 12:39:15 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
328 |
"Modified: / 06-03-2010 / 19:39:05 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
329 |
! !
|
|
330 |
|
|
331 |
!CodeHighlightingService class methodsFor:'documentation'!
|
|
332 |
|
|
333 |
version_CVS
|
10077
|
334 |
^ '$Header: /cvs/stx/stx/libtool/Tools__CodeHighlightingService.st,v 1.2 2011-07-03 17:50:21 cg Exp $'
|
9980
|
335 |
!
|
|
336 |
|
|
337 |
version_SVN
|
|
338 |
^ '§Id: Tools__CodeHighlightingService.st 7715 2011-04-10 16:32:58Z vranyj1 §'
|
|
339 |
! !
|