author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Mon, 29 Jul 2013 13:23:30 +0200 | |
changeset 194 | cea07153d29c |
parent 168 | 0cbcad0521b6 |
child 200 | e35a3f4b586b |
permissions | -rw-r--r-- |
168 | 1 |
"{ Package: 'stx:goodies/builder/reports' }" |
2 |
||
3 |
"{ NameSpace: Builder }" |
|
4 |
||
5 |
Object subclass:#ReportSourceInfo |
|
6 |
instanceVariableNames:'klass filename offsets lineEnds' |
|
7 |
classVariableNames:'' |
|
8 |
poolDictionaries:'' |
|
9 |
category:'Builder-Reports-Utils' |
|
10 |
! |
|
11 |
||
12 |
Stream subclass:#LineCountingStream |
|
13 |
instanceVariableNames:'position lineEnds' |
|
14 |
classVariableNames:'' |
|
15 |
poolDictionaries:'' |
|
16 |
privateIn:ReportSourceInfo |
|
17 |
! |
|
18 |
||
19 |
!ReportSourceInfo class methodsFor:'documentation'! |
|
20 |
||
21 |
documentation |
|
22 |
" |
|
23 |
Utility class to map line numbers in methods to global |
|
24 |
line number in source file (in chunk-formatted .st file) |
|
25 |
||
26 |
[author:] |
|
27 |
Jan Vrany <jan.vrany@fit.cvut.cz> |
|
28 |
||
29 |
[instance variables:] |
|
30 |
||
31 |
[class variables:] |
|
32 |
||
33 |
[see also:] |
|
34 |
||
35 |
" |
|
36 |
! ! |
|
37 |
||
38 |
!ReportSourceInfo class methodsFor:'instance creation'! |
|
39 |
||
40 |
for: aClass |
|
41 |
^self new setClass: aClass |
|
42 |
||
43 |
"Created: / 01-03-2013 / 17:50:30 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
44 |
! ! |
|
45 |
||
46 |
!ReportSourceInfo methodsFor:'initialization'! |
|
47 |
||
48 |
setClass: aClass |
|
49 |
klass := aClass theNonMetaclass. |
|
50 |
self setup. |
|
51 |
||
52 |
"Created: / 01-03-2013 / 17:49:56 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
53 |
! |
|
54 |
||
55 |
setup |
|
56 |
"To be called after class is set" |
|
57 |
||
194
cea07153d29c
Builder::ReportSourceInfo>>#setup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
168
diff
changeset
|
58 |
| stream sourceF | |
168 | 59 |
|
60 |
stream := LineCountingStream new. |
|
61 |
offsets := Dictionary new. |
|
62 |
||
63 |
[ |
|
64 |
self fileOutOn: stream. |
|
65 |
] on: AbstractSourceFileWriter methodSourceRewriteQuery do:[:rewriteQuery | |
|
66 |
| m | |
|
67 |
||
68 |
m := rewriteQuery method. |
|
194
cea07153d29c
Builder::ReportSourceInfo>>#setup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
168
diff
changeset
|
69 |
offsets at: m put: stream position - 1. |
168 | 70 |
rewriteQuery proceed. |
71 |
]. |
|
194
cea07153d29c
Builder::ReportSourceInfo>>#setup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
168
diff
changeset
|
72 |
lineEnds := stream lineEnds. |
cea07153d29c
Builder::ReportSourceInfo>>#setup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
168
diff
changeset
|
73 |
|
cea07153d29c
Builder::ReportSourceInfo>>#setup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
168
diff
changeset
|
74 |
"/Now, check if all is correct... |
cea07153d29c
Builder::ReportSourceInfo>>#setup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
168
diff
changeset
|
75 |
offsets keysAndValuesDo:[:method :offset | |
cea07153d29c
Builder::ReportSourceInfo>>#setup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
168
diff
changeset
|
76 |
sourceF := method package == klass package |
cea07153d29c
Builder::ReportSourceInfo>>#setup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
168
diff
changeset
|
77 |
ifTrue:[(Smalltalk getPackageDirectoryForPackage: klass package) / ((Smalltalk fileNameForClass: klass) , '.st')] |
cea07153d29c
Builder::ReportSourceInfo>>#setup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
168
diff
changeset
|
78 |
ifFalse:[(Smalltalk getPackageDirectoryForPackage: method package) / 'extensions.st']. |
cea07153d29c
Builder::ReportSourceInfo>>#setup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
168
diff
changeset
|
79 |
sourceF readingFileDo:[:sourceS| |
cea07153d29c
Builder::ReportSourceInfo>>#setup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
168
diff
changeset
|
80 |
| source | |
cea07153d29c
Builder::ReportSourceInfo>>#setup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
168
diff
changeset
|
81 |
|
cea07153d29c
Builder::ReportSourceInfo>>#setup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
168
diff
changeset
|
82 |
sourceS position: offset. |
cea07153d29c
Builder::ReportSourceInfo>>#setup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
168
diff
changeset
|
83 |
source := sourceS nextChunk. |
cea07153d29c
Builder::ReportSourceInfo>>#setup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
168
diff
changeset
|
84 |
"/ self assert: method source = source. |
cea07153d29c
Builder::ReportSourceInfo>>#setup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
168
diff
changeset
|
85 |
] |
cea07153d29c
Builder::ReportSourceInfo>>#setup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
168
diff
changeset
|
86 |
]. |
cea07153d29c
Builder::ReportSourceInfo>>#setup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
168
diff
changeset
|
87 |
|
cea07153d29c
Builder::ReportSourceInfo>>#setup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
168
diff
changeset
|
88 |
|
cea07153d29c
Builder::ReportSourceInfo>>#setup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
168
diff
changeset
|
89 |
|
168 | 90 |
|
91 |
" |
|
194
cea07153d29c
Builder::ReportSourceInfo>>#setup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
168
diff
changeset
|
92 |
ReportSourceInfo for: Builder::ReportRunner |
168 | 93 |
" |
94 |
||
95 |
"Created: / 01-03-2013 / 17:30:58 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
194
cea07153d29c
Builder::ReportSourceInfo>>#setup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
168
diff
changeset
|
96 |
"Modified: / 29-07-2013 / 12:20:44 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
168 | 97 |
! ! |
98 |
||
99 |
!ReportSourceInfo methodsFor:'queries'! |
|
100 |
||
101 |
lineAndColumnOfOffset: offset |
|
102 |
| low high middle element line col | |
|
103 |
||
104 |
low := 1. |
|
105 |
high := lineEnds size. |
|
106 |
[low > high] whileFalse:[ |
|
107 |
middle := (low + high) // 2. |
|
108 |
element := lineEnds at:middle. |
|
109 |
element < offset ifTrue:[ |
|
110 |
"middleelement is smaller than object" |
|
111 |
low := middle + 1 |
|
112 |
] ifFalse:[ |
|
113 |
high := middle - 1 |
|
114 |
] |
|
115 |
]. |
|
116 |
||
117 |
line := low. |
|
118 |
col := offset - (line > 1 ifTrue:[lineEnds at: line - 1] ifFalse:[0]). |
|
119 |
^line @ col. |
|
120 |
||
121 |
"Created: / 03-03-2013 / 10:50:34 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
122 |
! |
|
123 |
||
124 |
offsetOfMethod: aMethod |
|
125 |
^offsets at: aMethod |
|
126 |
||
127 |
"Created: / 03-03-2013 / 10:49:40 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
128 |
! ! |
|
129 |
||
130 |
!ReportSourceInfo methodsFor:'utilities'! |
|
131 |
||
132 |
fileOutOn:aStream |
|
133 |
klass fileOutOn: aStream withTimeStamp:false |
|
134 |
||
135 |
"Created: / 01-03-2013 / 17:51:10 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
136 |
! ! |
|
137 |
||
138 |
!ReportSourceInfo::LineCountingStream class methodsFor:'instance creation'! |
|
139 |
||
140 |
new |
|
141 |
"return an initialized instance" |
|
142 |
||
143 |
^ self basicNew initialize. |
|
144 |
! ! |
|
145 |
||
146 |
!ReportSourceInfo::LineCountingStream methodsFor:'accessing'! |
|
147 |
||
148 |
contents |
|
149 |
"return the entire contents of the stream. |
|
150 |
For a readStream, that is the rest (i.e. upToEnd), |
|
151 |
for a writeStream, that is the collected data. As we do not know here, |
|
152 |
what we are, this is the responsibility of a subclass..." |
|
153 |
||
154 |
^ self shouldNotImplement |
|
155 |
||
156 |
"Modified: / 01-03-2013 / 17:36:51 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
157 |
! |
|
158 |
||
159 |
lineEnds |
|
160 |
^ lineEnds |
|
161 |
! |
|
162 |
||
163 |
position |
|
164 |
^ position |
|
165 |
! ! |
|
166 |
||
167 |
!ReportSourceInfo::LineCountingStream methodsFor:'initialization'! |
|
168 |
||
169 |
initialize |
|
170 |
"Invoked when a new instance is created." |
|
171 |
||
172 |
"/ please change as required (and remove this comment) |
|
173 |
position := 0. |
|
174 |
lineEnds := OrderedCollection new. |
|
175 |
||
176 |
"/ super initialize. -- commented since inherited method does nothing |
|
177 |
||
178 |
"Modified: / 01-03-2013 / 17:39:38 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
179 |
! ! |
|
180 |
||
181 |
!ReportSourceInfo::LineCountingStream methodsFor:'queries'! |
|
182 |
||
183 |
isReadable |
|
184 |
"return true, if reading is supported by the recevier. |
|
185 |
This has to be redefined in concrete subclasses." |
|
186 |
||
187 |
^ false |
|
188 |
||
189 |
"Modified: / 01-03-2013 / 17:36:58 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
190 |
! |
|
191 |
||
192 |
isWritable |
|
193 |
"return true, if writing is supported by the recevier. |
|
194 |
This has to be redefined in concrete subclasses." |
|
195 |
||
196 |
^ true |
|
197 |
||
198 |
"Modified: / 01-03-2013 / 17:37:07 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
199 |
! |
|
200 |
||
201 |
size |
|
202 |
"return the number of elements in the streamed collection." |
|
203 |
||
204 |
^ self shouldImplement |
|
205 |
! ! |
|
206 |
||
207 |
!ReportSourceInfo::LineCountingStream methodsFor:'reading'! |
|
208 |
||
209 |
next |
|
210 |
"return the next element of the stream |
|
211 |
- we do not know here how to do it, it must be redefined in subclass" |
|
212 |
||
213 |
^ self shouldNotImplement |
|
214 |
||
215 |
"Modified: / 01-03-2013 / 17:37:21 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
216 |
! ! |
|
217 |
||
218 |
!ReportSourceInfo::LineCountingStream methodsFor:'testing'! |
|
219 |
||
220 |
atEnd |
|
221 |
"return true if the end of the stream has been reached; |
|
222 |
- we do not know here how to do it, it must be redefined in subclass" |
|
223 |
||
224 |
^ false |
|
225 |
||
226 |
"Modified: / 01-03-2013 / 17:37:27 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
227 |
! |
|
228 |
||
229 |
isEmpty |
|
230 |
"return true, if the contents of the stream is empty" |
|
231 |
||
232 |
^ self shouldNotImplement |
|
233 |
||
234 |
"Modified: / 01-03-2013 / 17:37:32 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
235 |
! ! |
|
236 |
||
237 |
!ReportSourceInfo::LineCountingStream methodsFor:'writing'! |
|
238 |
||
239 |
nextPut:aCharacter |
|
240 |
"put the argument, anObject onto the receiver |
|
241 |
- we do not know here how to do it, it must be redefined in subclass" |
|
242 |
||
243 |
position := position + 1. |
|
244 |
aCharacter == Character cr ifTrue:[ |
|
245 |
lineEnds add: position |
|
246 |
]. |
|
247 |
||
248 |
"Modified: / 01-03-2013 / 17:39:52 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
249 |
! ! |
|
250 |
||
251 |
!ReportSourceInfo class methodsFor:'documentation'! |
|
252 |
||
253 |
version |
|
254 |
^ '$Header$' |
|
255 |
! |
|
256 |
||
257 |
version_CVS |
|
258 |
^ '$Header$' |
|
259 |
! ! |
|
260 |