rbx-linecache 0.44-universal-rubinius-1.2 → 1.0-universal-rubinius-1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/ChangeLog +61 -0
  2. data/NEWS +6 -2
  3. data/lib/linecache.rb +119 -70
  4. metadata +5 -5
data/ChangeLog CHANGED
@@ -1,3 +1,64 @@
1
+ 2011-01-18 20:02 Rocky Bernstein
2
+
3
+ * lib/linecache.rb: Was redoing highlighting incorrectly. Need to
4
+ add more tests.
5
+
6
+ 2011-01-18 18:13 Rocky Bernstein
7
+
8
+ * lib/linecache.rb: Add routine to clear syntax-highlight cache
9
+
10
+ 2011-01-17 23:46 Rocky Bernstein
11
+
12
+ * lib/linecache.rb: Bump version number. We're in dev now and
13
+ syntax highlighting adds incompatible (but probably not noticed)
14
+ changes.
15
+
16
+ 2011-01-17 15:45 Rocky Bernstein
17
+
18
+ * lib/linecache.rb: More on-the-fly formatting possibilities
19
+
20
+ 2011-01-17 15:00 Rocky Bernstein
21
+
22
+ * lib/linecache.rb: getlines: fold reload_on_change into options
23
+ hash
24
+
25
+ 2011-01-17 12:48 Rocky Bernstein
26
+
27
+ * lib/linecache.rb: Add syntax highlighting via CodeRay and
28
+ TERM:ANSIColor (if available).
29
+
30
+ 2010-12-27 15:31 Rocky Bernstein
31
+
32
+ * .gemspec, Rakefile: Packaging: Probably need
33
+ universal-rubinius-1.2 in gem name
34
+
35
+ 2010-12-25 14:27 Rocky Bernstein
36
+
37
+ * release-0.44: Tag release.
38
+
39
+ 2010-12-25 14:22 Rocky Bernstein
40
+
41
+ * ChangeLog, NEWS, Rakefile, lib/linecache.rb, test/data/match3.rb,
42
+ test/test-linecache.rb, test/test-tracelines.rb: Revise for
43
+ Rubinius 1.2 and beyond where line numbers have been improved.
44
+ Get Ready for 0.44 release.
45
+
46
+ 2010-12-14 20:04 Rocky Bernstein
47
+
48
+ * .gemspec, Rakefile: Package now builds with package
49
+ rubinius-universal in the gem name and checks for version 1.1 or
50
+ 1.2
51
+
52
+ 2010-12-14 19:37 Rocky Bernstein
53
+
54
+ * test/data/case2.rb, test/data/match3.rb: Line numbers changed a
55
+ little in rubinius 1.2.
56
+
57
+ 2010-11-27 02:49 Rocky Bernstein
58
+
59
+ * lib/linecache.rb: Bump version number. We've added static scope
60
+ since the last release.
61
+
1
62
  2010-11-21 01:56 Rocky Bernstein
2
63
 
3
64
  * lib/linecache.rb, test/test-linecache.rb: Handle rubinius script
data/NEWS CHANGED
@@ -1,7 +1,11 @@
1
- December 25 2010
1
+ 1.0
2
+ February 1, 2011
3
+ - Add syntax highlight caching
4
+
5
+ December 25, 2010
2
6
  - Handle Rubinius eval_source to temp file remapping.
3
7
 
4
- October 27 2010
8
+ October 27, 2010
5
9
  - First gemcutter rubinius release
6
10
 
7
11
 
data/lib/linecache.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
- # Copyright (C) 2007, 2008, 2010 Rocky Bernstein <rockyb@rubyforge.net>
2
+ # Copyright (C) 2007, 2008, 2010, 2011
3
+ # Rocky Bernstein <rockyb@rubyforge.net>
3
4
  #
4
5
  # This program is free software; you can redistribute it and/or modify
5
6
  # it under the terms of the GNU General Public License as published by
@@ -63,7 +64,7 @@ require_relative 'tracelines'
63
64
  # = module LineCache
64
65
  # A module to read and cache lines of a Ruby program.
65
66
  module LineCache
66
- VERSION = '0.44'
67
+ VERSION = '1.0'
67
68
  LineCacheInfo = Struct.new(:stat, :line_numbers, :lines, :path, :sha1) unless
68
69
  defined?(LineCacheInfo)
69
70
 
@@ -71,6 +72,10 @@ module LineCache
71
72
  # __FILE__. The value is a LineCacheInfo object.
72
73
  @@file_cache = {}
73
74
  @@script_cache = {}
75
+
76
+
77
+ # Used for CodeRay syntax highlighting
78
+ @@ruby_highlighter = nil
74
79
 
75
80
  # Maps a string filename (a String) to a key in @@file_cache (a
76
81
  # String).
@@ -89,34 +94,51 @@ module LineCache
89
94
 
90
95
  @@script2file = {}
91
96
 
97
+ module_function
98
+
92
99
  def remove_script_temps
93
100
  @@script2file.values.each do |filename|
94
- File.unlink(filename)
101
+ File.unlink(filename) if File.exist?(filename)
95
102
  end
96
103
  end
97
- module_function :remove_script_temps
98
104
  at_exit { remove_script_temps }
99
105
 
100
106
 
101
107
  # Clear the file cache entirely.
102
- def clear_file_cache()
103
- @@file_cache = {}
104
- @@file2file_remap = {}
105
- @@file2file_remap_lines = {}
108
+ def clear_file_cache(filename=nil)
109
+ if filename
110
+ if @@file_cache[filename]
111
+ @@file_cache.delete(filename)
112
+ end
113
+ else
114
+ @@file_cache = {}
115
+ @@file2file_remap = {}
116
+ @@file2file_remap_lines = {}
117
+ end
118
+ end
119
+
120
+ # Remove syntax-formatted lines in the cache. Use this
121
+ # when you change the CodeRay syntax or Token formatting
122
+ # and want to redo how files may have previously been
123
+ # syntax marked.
124
+ def clear_file_format_cache
125
+ @@file_cache.each_pair do |fname, cache_info|
126
+ cache_info.lines.each_pair do |format, lines|
127
+ next if :plain == format
128
+ @@file_cache[fname].lines[format] = nil
129
+ end
130
+ end
106
131
  end
107
- module_function :clear_file_cache
108
132
 
109
133
  # Clear the script cache entirely.
110
134
  def clear_script_cache()
111
135
  @@script_cache = {}
112
136
  end
113
- module_function :clear_file_cache
114
137
 
115
138
  # Return an array of cached file names
116
139
  def cached_files()
117
140
  @@file_cache.keys
118
141
  end
119
- module_function :cached_files
120
142
 
121
143
  # Discard cache entries that are out of date. If +filename+ is +nil+
122
144
  # all entries in the file cache +@@file_cache+ are checked.
@@ -124,8 +146,8 @@ module LineCache
124
146
  # if the file was read from __SCRIPT_LINES but no corresponding file
125
147
  # is found, it will be kept. Return a list of invalidated filenames.
126
148
  # nil is returned if a filename was given but not found cached.
127
- def checkcache(filename=nil, use_script_lines=false)
128
-
149
+ def checkcache(filename=nil, opts={})
150
+
129
151
  if !filename
130
152
  filenames = @@file_cache.keys()
131
153
  elsif @@file_cache.member?(filename)
@@ -145,26 +167,24 @@ module LineCache
145
167
  if stat &&
146
168
  (cache_info.size != stat.size or cache_info.mtime != stat.mtime)
147
169
  result << filename
148
- update_cache(filename, use_script_lines)
170
+ update_cache(filename, opts)
149
171
  end
150
172
  else
151
173
  result << filename
152
- update_cache(filename, use_script_lines)
174
+ update_cache(filename, opts)
153
175
  end
154
176
  end
155
177
  end
156
178
  return result
157
179
  end
158
- module_function :checkcache
159
180
 
160
181
  # Cache script if it's not already cached.
161
- def cache_script(script, string=nil, sha1=nil)
182
+ def cache_script(script, opts={})
162
183
  if !@@script_cache.member?(script)
163
- update_script_cache(script, string, sha1)
184
+ update_script_cache(script, opts)
164
185
  end
165
186
  script
166
187
  end
167
- module_function :cache_script
168
188
 
169
189
  # Cache file name or script object if it's not already cached.
170
190
  # Return the expanded filename for it in the cache if a filename,
@@ -176,16 +196,16 @@ module LineCache
176
196
  cache_script(file_or_script)
177
197
  end
178
198
  end
179
- module_function :cache
180
199
 
181
200
  # Cache filename if it's not already cached.
182
201
  # Return the expanded filename for it in the cache
183
202
  # or nil if we can't find the file.
184
- def cache_file(filename, reload_on_change=false)
203
+ def cache_file(filename, reload_on_change=false, opts={})
185
204
  if @@file_cache.member?(filename)
186
205
  checkcache(filename) if reload_on_change
187
206
  else
188
- update_cache(filename, true)
207
+ opts[:use_script_lines] = true
208
+ update_cache(filename, opts)
189
209
  end
190
210
  if @@file_cache.member?(filename)
191
211
  @@file_cache[filename].path
@@ -193,7 +213,6 @@ module LineCache
193
213
  nil
194
214
  end
195
215
  end
196
- module_function :cache_file
197
216
 
198
217
  # Return true if file_or_script is cached
199
218
  def cached?(file_or_script)
@@ -203,18 +222,15 @@ module LineCache
203
222
  cached_script?(file_or_script)
204
223
  end
205
224
  end
206
- module_function :cached?
207
225
 
208
226
  def cached_script?(script)
209
227
  @@script_cache.member?(script)
210
228
  end
211
- module_function :cached_script?
212
229
 
213
230
  def empty?(filename)
214
231
  filename=map_file(filename)
215
- @@file_cache[filename].lines.empty?
232
+ !!@@file_cache[filename].lines[:plain]
216
233
  end
217
- module_function :empty?
218
234
 
219
235
  # Get line +line_number+ from file named +filename+. Return nil if
220
236
  # there was a problem. If a file named filename is not found, the
@@ -227,12 +243,12 @@ module LineCache
227
243
  # $: << '/tmp'
228
244
  # lines = LineCache.getlines('myfile.rb')
229
245
  #
230
- def getline(file_or_script, line_number, reload_on_change=true)
246
+ def getline(file_or_script, line_number, opts={})
231
247
  lines =
232
248
  if file_or_script.kind_of?(String)
233
249
  filename = map_file(file_or_script)
234
250
  filename, line_number = map_file_line(filename, line_number)
235
- getlines(filename, reload_on_change)
251
+ getlines(filename, opts)
236
252
  else
237
253
  script_getlines(file_or_script)
238
254
  end
@@ -242,43 +258,67 @@ module LineCache
242
258
  return nil
243
259
  end
244
260
  end
245
- module_function :getline
246
261
 
247
262
  # Read lines of +script+ and cache the results. However +script+ was
248
263
  # previously cached use the results from the cache. Return nil
249
264
  # if we can't get lines
250
- def script_getlines(script)
251
- if @@script_cache.member?(script)
252
- return @@script_cache[script].lines
253
- else
254
- update_script_cache(script)
265
+ def script_getlines(script, opts={})
266
+ format = opts[:output] || :plain
267
+ line_formats =
255
268
  if @@script_cache.member?(script)
256
- return @@script_cache[script].lines
269
+ @@script_cache[script].lines
257
270
  else
258
- return nil
271
+ update_script_cache(script, opts)
272
+ if @@script_cache.member?(script)
273
+ @@script_cache[script].lines
274
+ else
275
+ nil
276
+ end
259
277
  end
278
+ return nil unless line_formats
279
+ if format != :plain && !line_formats[format]
280
+ highlight_string(line_formats[:plain].join('')).split(/\n/)
281
+ else
282
+ line_formats[format]
260
283
  end
261
284
  end
262
- module_function :script_getlines
263
285
 
264
286
  # Read lines of +filename+ and cache the results. However +filename+ was
265
287
  # previously cached use the results from the cache. Return nil
266
288
  # if we can't get lines
267
- def getlines(filename, reload_on_change=false)
289
+ def getlines(filename, opts={})
268
290
  filename = map_file(filename)
269
- checkcache(filename) if reload_on_change
291
+ checkcache(filename) if opts[:reload_on_change]
292
+ format = opts[:output] || :plain
270
293
  if @@file_cache.member?(filename)
271
- return @@file_cache[filename].lines
294
+ lines = @@file_cache[filename].lines
295
+ if opts[:output] && !lines[format]
296
+ lines[format] =
297
+ highlight_string(lines[:plain].join(''), format).split(/\n/)
298
+ end
299
+ return lines[format]
272
300
  else
273
- update_cache(filename, true)
301
+ opts[:use_script_lines] = true
302
+ update_cache(filename, opts)
274
303
  if @@file_cache.member?(filename)
275
- return @@file_cache[filename].lines
304
+ return @@file_cache[filename].lines[format]
276
305
  else
277
306
  return nil
278
307
  end
279
308
  end
280
309
  end
281
- module_function :getlines
310
+
311
+ def highlight_string(string, output_type)
312
+ require 'rubygems'
313
+ begin
314
+ require 'coderay'
315
+ require 'term/ansicolor'
316
+ rescue LoadError
317
+ return string
318
+ end
319
+ @@ruby_highlighter ||= CodeRay::Duo[:ruby, output_type]
320
+ @@ruby_highlighter.encode(string)
321
+ end
282
322
 
283
323
  # Return full filename path for filename
284
324
  def path(filename)
@@ -287,12 +327,10 @@ module LineCache
287
327
  return nil unless @@file_cache.member?(filename)
288
328
  @@file_cache[filename].path
289
329
  end
290
- module_function :path
291
330
 
292
331
  def remap_file(to_file, from_file)
293
332
  @@file2file_remap[to_file] = from_file
294
333
  end
295
- module_function :remap_file
296
334
 
297
335
  def remap_file_lines(from_file, to_file, range, start)
298
336
  range = (range..range) if range.kind_of?(Fixnum)
@@ -314,13 +352,12 @@ module LineCache
314
352
  return @@file_cache[filename].sha1.hexdigest if
315
353
  @@file_cache[filename].sha1
316
354
  sha1 = Digest::SHA1.new
317
- @@file_cache[filename].lines.each do |line|
355
+ @@file_cache[filename].lines[:plain].each do |line|
318
356
  sha1 << line + "\n"
319
357
  end
320
358
  @@file_cache[filename].sha1 = sha1
321
359
  sha1.hexdigest
322
360
  end
323
- module_function :sha1
324
361
 
325
362
  # Return the number of lines in filename
326
363
  def size(file_or_script)
@@ -328,13 +365,12 @@ module LineCache
328
365
  if file_or_script.kind_of?(String)
329
366
  file_or_script = map_file(file_or_script)
330
367
  return nil unless @@file_cache.member?(file_or_script)
331
- @@file_cache[file_or_script].lines.length
368
+ @@file_cache[file_or_script].lines[:plain].length
332
369
  else
333
370
  return nil unless @@script_cache.member?(file_or_script)
334
- @@script_cache[file_or_script].lines.length
371
+ @@script_cache[file_or_script].lines[:plain].length
335
372
  end
336
373
  end
337
- module_function :size
338
374
 
339
375
  # Return File.stat in the cache for filename.
340
376
  def stat(filename)
@@ -353,17 +389,15 @@ module LineCache
353
389
  e = @@file_cache[filename]
354
390
  unless e.line_numbers
355
391
  e.line_numbers =
356
- TraceLineNumbers.lnums_for_str_array(e.lines)
392
+ TraceLineNumbers.lnums_for_str_array(e.lines[:plain])
357
393
  e.line_numbers = false unless e.line_numbers
358
394
  end
359
395
  e.line_numbers
360
396
  end
361
- module_function :trace_line_numbers
362
397
 
363
398
  def map_file(file)
364
399
  @@file2file_remap[file] ? @@file2file_remap[file] : file
365
400
  end
366
- module_function :map_file
367
401
 
368
402
  def map_script(script)
369
403
  if @@script2file[script]
@@ -377,12 +411,10 @@ module LineCache
377
411
  tempfile = Tempfile.new(["eval-#{sha1.hexdigest[0...7]}-", '.rb'])
378
412
  tempfile.open.puts(string)
379
413
  tempfile.close
380
- # cache_script(script, string, sha1.hexdigest)
381
414
  @@script2file[script] = tempfile.path
382
415
  tempfile.path
383
416
  end
384
417
  end
385
- module_function :map_script
386
418
 
387
419
  def map_file_line(file, line)
388
420
  if @@file2file_remap_lines[file]
@@ -395,30 +427,29 @@ module LineCache
395
427
  end
396
428
  return [map_file(file), line]
397
429
  end
398
- module_function :map_file_line
399
430
 
400
431
  def script_is_eval?(script)
401
432
  !!script.eval_source
402
433
  end
403
- module_function :script_is_eval?
404
434
 
405
435
  # Update a cache entry. If something is wrong, return nil. Return
406
436
  # true if the cache was updated and false if not.
407
- def update_script_cache(script, string=nil, sha1=nil)
437
+ def update_script_cache(script, opts)
408
438
  return false unless script_is_eval?(script)
409
- string = script.eval_source unless string
439
+ string = opts[:string] || script.eval_source
440
+ lines = {:plain => string.split(/\n/)}
441
+ lines[opts[:output]] = highlight_string(string, opts[:output]) if
442
+ opts[:output]
410
443
  @@script_cache[script] =
411
- LineCacheInfo.new(nil, nil, string.split(/\n/), nil, sha1)
444
+ LineCacheInfo.new(nil, nil, lines, nil, opts[:sha1])
412
445
  return true
413
446
  end
414
- module_function :update_script_cache
415
447
 
416
- # Update a cache entry. If something's
417
- # wrong, return nil. Return true if the cache was updated and false
418
- # if not. If use_script_lines is true, use that as the source for the
448
+ # Update a cache entry. If something's wrong, return nil. Return
449
+ # true if the cache was updated and false if not. If
450
+ # opts[:use_script_lines] is true, use that as the source for the
419
451
  # lines of the file
420
- def update_cache(filename, use_script_lines=false)
421
-
452
+ def update_cache(filename, opts={})
422
453
  return nil unless filename
423
454
 
424
455
  @@file_cache.delete(filename)
@@ -440,8 +471,13 @@ module LineCache
440
471
  end
441
472
  begin
442
473
  fp = File.open(path, 'r')
443
- lines = fp.readlines()
474
+ raw_string = fp.read
475
+ fp.rewind
476
+ lines = {:plain => fp.readlines}
444
477
  fp.close()
478
+ lines[opts[:output]] =
479
+ highlight_string(raw_string, opts[:output]).split(/\n/) if
480
+ opts[:output]
445
481
  rescue
446
482
  ## print '*** cannot open', path, ':', msg
447
483
  return nil
@@ -451,8 +487,6 @@ module LineCache
451
487
  @@file2file_remap[path] = filename
452
488
  return true
453
489
  end
454
-
455
- module_function :update_cache
456
490
  end
457
491
 
458
492
  # example usage
@@ -495,4 +529,19 @@ if __FILE__ == $0
495
529
  puts LineCache::size(loc.static_scope.script)")
496
530
  eval("loc = Rubinius::VM::backtrace(0)[0]
497
531
  puts LineCache::map_script(loc.static_scope.script)")
532
+
533
+ # Try new ANSI Terminal syntax coloring
534
+ LineCache::clear_file_cache(__FILE__)
535
+ LineCache::update_cache(__FILE__, :output => :term)
536
+ 50.upto(60) do |i|
537
+ line = LineCache::getline(__FILE__, i, :output => :term)
538
+ # puts line.inspect
539
+ puts line
540
+ end
541
+ puts '-' * 20
542
+ 50.upto(60) do |i|
543
+ line = LineCache::getline(__FILE__, i)
544
+ # puts line.inspect
545
+ puts line
546
+ end
498
547
  end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbx-linecache
3
3
  version: !ruby/object:Gem::Version
4
- hash: 83
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
+ - 1
7
8
  - 0
8
- - 44
9
- version: "0.44"
9
+ version: "1.0"
10
10
  platform: universal-rubinius-1.2
11
11
  authors:
12
12
  - R. Bernstein
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-27 00:00:00 -05:00
17
+ date: 2011-02-01 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -95,7 +95,7 @@ licenses:
95
95
  post_install_message:
96
96
  rdoc_options:
97
97
  - --title
98
- - LineCache 0.44 Documentation
98
+ - LineCache 1.0 Documentation
99
99
  require_paths:
100
100
  - lib
101
101
  required_ruby_version: !ruby/object:Gem::Requirement