linecache 0.46 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/NEWS CHANGED
@@ -1,6 +1,11 @@
1
+ 1.3.1
2
+ 15-09-19
3
+
4
+ - JRuby fix, sync with ruby-2.1 and ruby-1.9
5
+
1
6
  0.46
2
7
  06-12-19
3
- - A require_relative dependency snuck in.
8
+ - A require_relative dependency snuck in.
4
9
  Add a rbx-require-relative to handle this.
5
10
 
6
11
  0.45
@@ -12,7 +17,7 @@
12
17
  06-12-08
13
18
  - tolerance for finding windows extension in lib rather than ext.
14
19
 
15
- 0.41
20
+ 0.41
16
21
  - add test/data/* to gem.
17
22
 
18
23
  0.4
@@ -21,19 +26,19 @@
21
26
  0.3
22
27
  - Add tracelines: get line numbers that can be stopped at.
23
28
 
24
- - Add routines to allow line-number
25
- remapping and filename remapping.
29
+ - Add routines to allow line-number
30
+ remapping and filename remapping.
26
31
 
27
32
  - Add access methods to get the number of lines in a file.
28
33
 
29
34
  0.2
30
35
  - Make this work with ruby-debug-base. Add reload-on-change parameters.
31
- add checkcache, cache, cached? sha1, and stat methods.
32
-
36
+ add checkcache, cache, cached? sha1, and stat methods.
37
+
33
38
  - Use SCRIPT_LINES__.
34
39
 
35
40
  0.1
36
41
 
37
42
  - Initial release of LineCache, a module for reading and caching lines.
38
43
 
39
- $Id: NEWS 268 2011-06-19 08:48:31Z rockyb $
44
+ $Id$
data/README CHANGED
@@ -1,50 +1,29 @@
1
- = LineCache - A module to read and cache file information of a Ruby program.
2
-
3
- == SYNOPSIS
4
-
5
1
  The LineCache module allows one to get any line from any file, caching
6
2
  the lines and file information on first access to the file. Although
7
3
  the file may be any file, the common use is when the file is a Ruby
8
4
  script since parsing of the file is done to figure out where the
9
- statement boundaries are.
5
+ statement boundaries are, and we also cache syntax formatting.
10
6
 
11
7
  The routines here may be is useful when a small random sets of lines
12
8
  are read from a single file, in particular in a debugger to show
13
9
  source lines.
14
10
 
15
- == Summary
16
-
17
- require 'linecache'
18
- lines = LineCache::getlines('/tmp/myruby.rb')
19
- # The following lines have same effect as the above.
20
- $: << '/tmp'
21
- Dir.chdir('/tmp') {lines = LineCache::getlines('myruby.rb')
22
-
23
- line = LineCache::getline('/tmp/myruby.rb', 6)
24
- # Note lines[6] == line (if /tmp/myruby.rb has 6 lines)
25
-
26
- LineCache::clear_file_cache
27
- LineCache::clear_file_cache('/tmp/myruby.rb')
28
- LineCache::update_cache # Check for modifications of all cached files.
29
-
30
- == Credits
31
-
32
- This is a port of the module of the same name from the Python distribution.
33
-
34
- The idea for how TraceLineNumbers works, and some code was taken
35
- from ParseTree by Ryan Davis.
36
-
37
- == Other stuff
11
+ *Example*:
38
12
 
39
- Author:: Rocky Bernstein <rockyb@rubyforge.net>
40
- License:: Copyright (c) 2007, 2008 Rocky Bernstein
41
- Released under the GNU GPL 2 license
13
+ ```ruby
14
+ require 'linecache'
15
+ lines = LineCache::getlines('/tmp/myruby.rb')
16
+ # The following lines have same effect as the above.
17
+ $: << '/tmp'
18
+ Dir.chdir('/tmp') {lines = LineCache::getlines('myruby.rb')
42
19
 
43
- == Warranty
20
+ line = LineCache::getline('/tmp/myruby.rb', 6)
21
+ # Note lines[6] == line (if /tmp/myruby.rb has 6 lines)
44
22
 
45
- This program is distributed in the hope that it will be useful,
46
- but WITHOUT ANY WARRANTY; without even the implied warranty of
47
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48
- GNU General Public License for more details.
23
+ LineCache::clear_file_cache
24
+ LineCache::clear_file_cache('/tmp/myruby.rb')
25
+ LineCache::update_cache # Check for modifications of all cached files.
26
+ ```
49
27
 
50
- $Id: README 169 2009-02-08 17:19:33Z rockyb $
28
+ Git branches *ruby-1.9* and *ruby-2.1* for code for those respective
29
+ Ruby versions. A patched version of Ruby is needed for this to work. The patches sources are [here](https://code.google.com/p/ruby-19-debugger/).
data/Rakefile CHANGED
@@ -1,14 +1,14 @@
1
1
  #!/usr/bin/env rake
2
2
  # -*- Ruby -*-
3
3
  require 'rubygems'
4
- require 'rake/gempackagetask'
5
- require 'rake/rdoctask'
4
+ require 'rubygems/package_task'
5
+ require 'rdoc/task'
6
6
  require 'rake/testtask'
7
7
 
8
8
  SO_NAME = 'trace_nums.so'
9
9
 
10
10
  ROOT_DIR = File.dirname(__FILE__)
11
- load File.join %W(#{ROOT_DIR} lib version.rb)
11
+ load File.join %W(#{ROOT_DIR} lib linecache version.rb)
12
12
 
13
13
  PKG_VERSION = LineCache::VERSION
14
14
  PKG_NAME = 'linecache'
@@ -23,18 +23,18 @@ FILES = FileList[
23
23
  'NEWS',
24
24
  'README',
25
25
  'Rakefile',
26
- 'ext/trace_nums.c',
27
- 'ext/trace_nums.h',
28
- 'ext/extconf.rb',
29
- 'lib/*.rb',
26
+ 'ext/linecache/trace_nums.c',
27
+ 'ext/linecache/trace_nums.h',
28
+ 'ext/linecache/extconf.rb',
29
+ 'lib/**/*.rb',
30
30
  'test/*.rb',
31
31
  'test/data/*.rb',
32
32
  'test/short-file'
33
- ]
33
+ ]
34
34
 
35
35
  desc 'Test everything'
36
36
  Rake::TestTask.new(:test) do |t|
37
- t.libs << './lib'
37
+ t.libs << 'ext'
38
38
  t.pattern = 'test/test-*.rb'
39
39
  t.options = '--verbose' if $VERBOSE
40
40
  end
@@ -42,7 +42,7 @@ task :test => :lib
42
42
 
43
43
  desc 'Create the core ruby-debug shared library extension'
44
44
  task :lib do
45
- Dir.chdir('ext') do
45
+ Dir.chdir('ext/linecache') do
46
46
  system("#{Gem.ruby} extconf.rb && make")
47
47
  end
48
48
  end
@@ -51,9 +51,9 @@ end
51
51
  desc 'Test everything - same as test.'
52
52
  task :check => :test
53
53
 
54
- desc 'Create a GNU-style ChangeLog via svn2cl'
54
+ desc 'Create a GNU-style ChangeLog via git2cl'
55
55
  task :ChangeLog do
56
- system('svn2cl --authors=svn2cl_usermap')
56
+ system('git log --pretty --numstat --summary | git2cl > ChangeLog')
57
57
  end
58
58
 
59
59
  gem_file = nil
@@ -61,7 +61,7 @@ gem_file = nil
61
61
  # Base GEM Specification
62
62
  default_spec = Gem::Specification.new do |spec|
63
63
  spec.name = 'linecache'
64
-
64
+
65
65
  spec.homepage = 'http://rubyforge.org/projects/rocky-hacks/linecache'
66
66
  spec.summary = 'Read file with caching'
67
67
  spec.description = <<-EOF
@@ -73,19 +73,18 @@ EOF
73
73
 
74
74
  spec.author = 'R. Bernstein'
75
75
  spec.email = 'rockyb@rubyforge.net'
76
+ spec.licenses = ['GPL2']
76
77
  spec.platform = Gem::Platform::RUBY
77
78
  spec.require_path = 'lib'
78
- spec.files = FILES.to_a
79
- spec.add_dependency('rbx-require-relative', '> 0.0.4')
80
- spec.extensions = ['ext/extconf.rb']
79
+ spec.files = FILES.to_a
80
+ spec.extensions = ['ext/linecache/extconf.rb']
81
81
 
82
82
  spec.required_ruby_version = '>= 1.8.7'
83
83
  spec.date = Time.now
84
- spec.rubyforge_project = 'rocky-hacks'
85
-
84
+
86
85
  # rdoc
87
86
  spec.has_rdoc = true
88
- spec.extra_rdoc_files = ['README', 'lib/linecache.rb', 'lib/tracelines.rb']
87
+ spec.extra_rdoc_files = ['README', 'lib/linecache.rb', 'lib/linecache/tracelines.rb']
89
88
 
90
89
  spec.test_files = FileList['test/*.rb']
91
90
  gem_file = "#{spec.name}-#{spec.version}.gem"
@@ -93,7 +92,7 @@ EOF
93
92
  end
94
93
 
95
94
  # Rake task to build the default package
96
- Rake::GemPackageTask.new(default_spec) do |pkg|
95
+ Gem::PackageTask.new(default_spec) do |pkg|
97
96
  pkg.need_tar = true
98
97
  end
99
98
 
@@ -105,7 +104,6 @@ win_spec.extensions = []
105
104
  ## win_spec.platform = Gem::Platform::WIN32 # deprecated
106
105
  win_spec.platform = 'mswin32'
107
106
  win_spec.files += ["lib/#{SO_NAME}"]
108
- win_spec.add_dependency('rbx-require-relative', '> 0.0.4')
109
107
 
110
108
  desc 'Create Windows Gem'
111
109
  task :win32_gem do
@@ -124,29 +122,8 @@ task :win32_gem do
124
122
  rm(target)
125
123
  end
126
124
 
127
- desc 'Publish linecache to RubyForge.'
128
- task :publish do
129
- require 'rake/contrib/sshpublisher'
130
-
131
- # Get ruby-debug path.
132
- ruby_debug_path = File.expand_path(File.dirname(__FILE__))
133
-
134
- publisher = Rake::SshDirPublisher.new('rockyb@rubyforge.org',
135
- '/var/www/gforge-projects/rocky-hacks/linecache', ruby_debug_path)
136
- end
137
-
138
- desc 'Remove residue from running patch'
139
- task :rm_patch_residue do
140
- FileUtils.rm_rf Dir.glob('**/*.{rej,orig}'), :verbose => true
141
- end
142
-
143
- desc 'Remove ~ backup files'
144
- task :rm_tilde_backups do
145
- FileUtils.rm_rf Dir.glob('**/*~'), :verbose => true
146
- end
147
-
148
125
  desc 'Remove built files'
149
- task :clean => [:clobber_package, :clobber_rdoc, :rm_patch_residue,
126
+ task :clean => [:clobber_package, :clobber_rdoc, :rm_patch_residue,
150
127
  :rm_tilde_backups] do
151
128
  cd 'ext' do
152
129
  if File.exists?('Makefile')
@@ -159,24 +136,22 @@ task :clean => [:clobber_package, :clobber_rdoc, :rm_patch_residue,
159
136
  end
160
137
 
161
138
  # --------- RDoc Documentation ------
162
- desc 'Generate rdoc documentation'
163
- Rake::RDocTask.new('rdoc') do |rdoc|
139
+ require 'rdoc/task'
140
+ desc "Generate rdoc documentation"
141
+ Rake::RDocTask.new("rdoc") do |rdoc|
164
142
  rdoc.rdoc_dir = 'doc'
165
- rdoc.title = "linecache #{LineCache::VERSION} Documentation"
166
- rdoc.options << '--main' << 'README'
167
- rdoc.rdoc_files.include('ext/**/*.c',
168
- 'lib/*.rb',
169
- 'README',
170
- 'COPYING')
171
- end
143
+ rdoc.title = "LineCache #{LineCache::VERSION} Documentation"
172
144
 
173
- desc 'Publish the release files to RubyForge.'
174
- task :rubyforge_upload do
175
- `rubyforge login`
176
- release_command = "rubyforge add_release #{PKG_NAME} #{PKG_NAME} '#{PKG_NAME}-#{PKG_VERSION}' pkg/#{PKG_NAME}-#{PKG_VERSION}.gem"
177
- puts release_command
178
- system(release_command)
145
+ # Show source inline with line numbers
146
+ rdoc.options += %w(--inline-source --line-numbers)
147
+
148
+ # Make the README file the start page for the generated html
149
+ rdoc.options += %w(--main README)
150
+
151
+ rdoc.rdoc_files.include('lib/*.rb', 'README', 'COPYING')
179
152
  end
153
+ desc "Same as rdoc"
154
+ task :doc => :rdoc
180
155
 
181
156
  desc 'Install the gem locally'
182
157
  task :install => :gem do
@@ -185,3 +160,10 @@ task :install => :gem do
185
160
  end
186
161
  end
187
162
 
163
+ namespace :jruby do
164
+ jruby_spec = default_spec.clone
165
+ jruby_spec.platform = "java"
166
+ jruby_spec.files = jruby_spec.files.reject {|f| f =~ /^ext/ }
167
+ jruby_spec.extensions = []
168
+ Gem::PackageTask.new(jruby_spec) {}
169
+ end
@@ -13,4 +13,4 @@ else
13
13
  exit(1)
14
14
  end
15
15
 
16
- create_makefile("trace_nums")
16
+ create_makefile("linecache/trace_nums")
@@ -1,7 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
- # $Id: linecache.rb 266 2011-06-10 23:41:29Z rockyb $
3
- #
4
- # Copyright (C) 2007, 2008, 2010, 2011 Rocky Bernstein <rockyb@rubyforge.net>
2
+ #
3
+ # Copyright (C) 2007, 2008, 2010-2012 Rocky Bernstein <rockyb@rubyforge.net>
5
4
  #
6
5
  # This program is free software; you can redistribute it and/or modify
7
6
  # it under the terms of the GNU General Public License as published by
@@ -22,12 +21,12 @@
22
21
  # Author:: Rocky Bernstein (mailto:rockyb@rubyforge.net)
23
22
  #
24
23
  # = linecache
25
- # A module to read and cache lines of a Ruby program.
24
+ # A module to read and cache lines of a Ruby program.
26
25
 
27
26
  # == SYNOPSIS
28
27
  #
29
28
  # The LineCache module allows one to get any line from any file,
30
- # caching lines of the file on first access to the file. Although the
29
+ # caching lines of the file on first access to the file. Although the
31
30
  # file may be any file, the common use is when the file is a Ruby
32
31
  # script since parsing of the file is done to figure out where the
33
32
  # statement boundaries are.
@@ -62,27 +61,24 @@ SCRIPT_LINES__ = {} unless defined? SCRIPT_LINES__
62
61
  require 'digest/sha1'
63
62
  require 'set'
64
63
 
65
- begin require 'rubygems' rescue LoadError end
66
- require 'require_relative'
67
- require_relative 'tracelines'
68
- require_relative 'version'
64
+ require 'linecache/tracelines'
65
+ require 'linecache/version'
69
66
  # require 'ruby-debug' ; Debugger.start
70
67
 
71
68
  # = module LineCache
72
- # A module to read and cache lines of a Ruby program.
69
+ # A module to read and cache lines of a Ruby program.
73
70
  module LineCache
74
71
  LineCacheInfo = Struct.new(:stat, :line_numbers, :lines, :path, :sha1) unless
75
72
  defined?(LineCacheInfo)
76
-
77
- # The file cache. The key is a name as would be given by Ruby for
78
- # __FILE__. The value is a LineCacheInfo object.
79
- @@file_cache = {}
80
- @@script_cache = {}
81
73
 
74
+ # The file cache. The key is a name as would be given by Ruby for
75
+ # __FILE__. The value is a LineCacheInfo object.
76
+ @@file_cache = {}
77
+ @@script_cache = {}
82
78
 
83
79
  # Used for CodeRay syntax highlighting
84
80
  @@ruby_highlighter = nil
85
-
81
+
86
82
  # Maps a string filename (a String) to a key in @@file_cache (a
87
83
  # String).
88
84
  #
@@ -95,9 +91,9 @@ module LineCache
95
91
  # Another related use is when a template system is used. Here we'll
96
92
  # probably want to remap not only the file name but also line
97
93
  # ranges. Will probably use this for that, but I'm not sure.
98
- @@file2file_remap = {}
94
+ @@file2file_remap = {}
99
95
  @@file2file_remap_lines = {}
100
- @@script2file = {}
96
+ @@script2file = {}
101
97
 
102
98
  module_function
103
99
 
@@ -108,10 +104,11 @@ module LineCache
108
104
  end
109
105
  at_exit { remove_script_temps }
110
106
 
111
-
112
- # Clear the file cache entirely.
107
+
108
+ # Clear the file cache. If no filename is given clear it entirely.
109
+ # if a filename is given, clear just that filename.
113
110
  def clear_file_cache(filename=nil)
114
- if filename
111
+ if filename
115
112
  if @@file_cache[filename]
116
113
  @@file_cache.delete(filename)
117
114
  end
@@ -124,7 +121,7 @@ module LineCache
124
121
 
125
122
  # Remove syntax-formatted lines in the cache. Use this
126
123
  # when you change the CodeRay syntax or Token formatting
127
- # and want to redo how files may have previously been
124
+ # and want to redo how files may have previously been
128
125
  # syntax marked.
129
126
  def clear_file_format_cache
130
127
  @@file_cache.each_pair do |fname, cache_info|
@@ -137,7 +134,7 @@ module LineCache
137
134
 
138
135
  # Remove syntax-formatted lines in the cache. Use this
139
136
  # when you change the CodeRay syntax or Token formatting
140
- # and want to redo how files may have previously been
137
+ # and want to redo how files may have previously been
141
138
  # syntax marked.
142
139
  def clear_file_format_cache
143
140
  @@file_cache.each_pair do |fname, cache_info|
@@ -165,13 +162,13 @@ module LineCache
165
162
  # is found, it will be kept. Return a list of invalidated filenames.
166
163
  # nil is returned if a filename was given but not found cached.
167
164
  def checkcache(filename=nil, opts=false)
168
- use_script_lines =
165
+ use_script_lines =
169
166
  if opts.kind_of?(Hash)
170
167
  opts[:use_script_lines]
171
168
  else
172
169
  opts
173
170
  end
174
-
171
+
175
172
  if !filename
176
173
  filenames = @@file_cache.keys()
177
174
  elsif @@file_cache.member?(filename)
@@ -188,7 +185,7 @@ module LineCache
188
185
  cache_info = @@file_cache[filename].stat
189
186
  stat = File.stat(path)
190
187
  if cache_info
191
- if stat &&
188
+ if stat &&
192
189
  (cache_info.size != stat.size or cache_info.mtime != stat.mtime)
193
190
  result << filename
194
191
  update_cache(filename, opts)
@@ -210,17 +207,6 @@ module LineCache
210
207
  script
211
208
  end
212
209
 
213
- # Cache file name or script object if it's not already cached.
214
- # Return the expanded filename for it in the cache if a filename,
215
- # or the script, or nil if we can't find the file.
216
- def cache(file_or_script, reload_on_change=false)
217
- if file_or_script.kind_of?(String)
218
- cache_file(file_or_script, reload_on_change)
219
- else
220
- cache_script(file_or_script)
221
- end
222
- end
223
-
224
210
  # Cache filename if it's not already cached.
225
211
  # Return the expanded filename for it in the cache
226
212
  # or nil if we can't find the file.
@@ -254,12 +240,12 @@ module LineCache
254
240
  nil
255
241
  end
256
242
  end
257
-
243
+
258
244
  # Return true if file_or_script is cached
259
245
  def cached?(file_or_script)
260
- if file_or_script.kind_of?(String)
246
+ if file_or_script.kind_of?(String)
261
247
  @@file_cache.member?(unmap_file(file_or_script))
262
- else
248
+ else
263
249
  cached_script?(file_or_script)
264
250
  end
265
251
  end
@@ -267,7 +253,7 @@ module LineCache
267
253
  def cached_script?(filename)
268
254
  SCRIPT_LINES__.member?(unmap_file(filename))
269
255
  end
270
-
256
+
271
257
  def empty?(filename)
272
258
  filename=unmap_file(filename)
273
259
  !!@@file_cache[filename].lines[:plain]
@@ -276,22 +262,22 @@ module LineCache
276
262
  # Get line +line_number+ from file named +filename+. Return nil if
277
263
  # there was a problem. If a file named filename is not found, the
278
264
  # function will look for it in the $: array.
279
- #
265
+ #
280
266
  # Examples:
281
- #
267
+ #
282
268
  # lines = LineCache::getline('/tmp/myfile.rb')
283
269
  # # Same as above
284
270
  # $: << '/tmp'
285
271
  # lines = LineCache.getlines('myfile.rb')
286
272
  #
287
273
  def getline(file_or_script, line_number, opts=true)
288
- reload_on_change =
274
+ reload_on_change =
289
275
  if opts.kind_of?(Hash)
290
276
  opts[:reload_on_change]
291
277
  else
292
278
  opts
293
279
  end
294
- lines =
280
+ lines =
295
281
  if file_or_script.kind_of?(String)
296
282
  filename = unmap_file(file_or_script)
297
283
  filename, line_number = unmap_file_line(filename, line_number)
@@ -311,7 +297,7 @@ module LineCache
311
297
  # if we can't get lines
312
298
  def getlines(filename, opts=false)
313
299
  if opts.kind_of?(Hash)
314
- reload_on_change, use_script_lines =
300
+ reload_on_change, use_script_lines =
315
301
  [opts[:reload_on_change], opts[:use_script_lines]]
316
302
  else
317
303
  reload_on_change, use_script_lines = [opts, false]
@@ -322,7 +308,7 @@ module LineCache
322
308
  if @@file_cache.member?(filename)
323
309
  lines = @@file_cache[filename].lines
324
310
  if opts[:output] && !lines[format]
325
- lines[format] =
311
+ lines[format] =
326
312
  highlight_string(lines[:plain].join(''), format).split(/\n/)
327
313
  end
328
314
  return lines[format]
@@ -365,7 +351,7 @@ module LineCache
365
351
  def remap_file_lines(from_file, to_file, range, start)
366
352
  range = (range..range) if range.kind_of?(Fixnum)
367
353
  to_file = from_file unless to_file
368
- if @@file2file_remap_lines[to_file]
354
+ if @@file2file_remap_lines[to_file]
369
355
  # FIXME: need to check for overwriting ranges: whether
370
356
  # they intersect or one encompasses another.
371
357
  @@file2file_remap_lines[to_file] << [from_file, range, start]
@@ -374,12 +360,12 @@ module LineCache
374
360
  end
375
361
  end
376
362
  module_function :remap_file_lines
377
-
363
+
378
364
  # Return SHA1 of filename.
379
365
  def sha1(filename)
380
366
  filename = unmap_file(filename)
381
367
  return nil unless @@file_cache.member?(filename)
382
- return @@file_cache[filename].sha1.hexdigest if
368
+ return @@file_cache[filename].sha1.hexdigest if
383
369
  @@file_cache[filename].sha1
384
370
  sha1 = Digest::SHA1.new
385
371
  @@file_cache[filename].lines[:plain].each do |line|
@@ -388,7 +374,7 @@ module LineCache
388
374
  @@file_cache[filename].sha1 = sha1
389
375
  sha1.hexdigest
390
376
  end
391
-
377
+
392
378
  # Return the number of lines in filename
393
379
  def size(file_or_script)
394
380
  cache(file_or_script)
@@ -418,13 +404,13 @@ module LineCache
418
404
  return nil unless fullname
419
405
  e = @@file_cache[filename]
420
406
  unless e.line_numbers
421
- e.line_numbers =
407
+ e.line_numbers =
422
408
  TraceLineNumbers.lnums_for_str_array(e.lines[:plain])
423
409
  e.line_numbers = false unless e.line_numbers
424
410
  end
425
411
  e.line_numbers
426
412
  end
427
-
413
+
428
414
  def unmap_file(file)
429
415
  @@file2file_remap[file] ? @@file2file_remap[file] : file
430
416
  end
@@ -434,8 +420,8 @@ module LineCache
434
420
  if @@file2file_remap_lines[file]
435
421
  @@file2file_remap_lines[file].each do |from_file, range, start|
436
422
  if range === line
437
- from_file = from_file || file
438
- return [from_file, start+line-range.begin]
423
+ from_file = from_file || file
424
+ return [from_file, start+line-range.begin]
439
425
  end
440
426
  end
441
427
  end
@@ -443,15 +429,15 @@ module LineCache
443
429
  end
444
430
 
445
431
  # Update a cache entry. If something is wrong, return nil. Return
446
- # true if the cache was updated and false if not.
432
+ # true if the cache was updated and false if not.
447
433
  def update_script_cache(script, opts)
448
434
  # return false unless script_is_eval?(script)
449
435
  # string = opts[:string] || script.eval_source
450
436
  lines = {:plain => string.split(/\n/)}
451
437
  lines[opts[:output]] = highlight_string(string, opts[:output]) if
452
438
  opts[:output]
453
- @@script_cache[script] =
454
- LineCacheInfo.new(nil, nil, lines, nil, opts[:sha1],
439
+ @@script_cache[script] =
440
+ LineCacheInfo.new(nil, nil, lines, nil, opts[:sha1],
455
441
  opts[:compiled_method])
456
442
  return true
457
443
  end
@@ -472,13 +458,13 @@ module LineCache
472
458
 
473
459
  @@file_cache.delete(filename)
474
460
  path = File.expand_path(filename)
475
-
461
+
476
462
  if use_script_lines
477
463
  list = [filename]
478
464
  list << @@file2file_remap[path] if @@file2file_remap[path]
479
- list.each do |name|
465
+ list.each do |name|
480
466
  if !SCRIPT_LINES__[name].nil? && SCRIPT_LINES__[name] != true
481
- begin
467
+ begin
482
468
  stat = File.stat(name)
483
469
  rescue
484
470
  stat = nil
@@ -486,7 +472,7 @@ module LineCache
486
472
  raw_lines = SCRIPT_LINES__[name]
487
473
  lines = {:plain => raw_lines}
488
474
  lines[opts[:output]] =
489
- highlight_string(raw_lines.join, opts[:output]).split(/\n/) if
475
+ highlight_string(raw_lines.join, opts[:output]).split(/\n/) if
490
476
  opts[:output]
491
477
  @@file_cache[filename] = LineCacheInfo.new(stat, nil, lines, path, nil)
492
478
  @@file2file_remap[path] = filename
@@ -494,7 +480,7 @@ module LineCache
494
480
  end
495
481
  end
496
482
  end
497
-
483
+
498
484
  if File.exist?(path)
499
485
  stat = File.stat(path)
500
486
  elsif File.basename(filename) == filename
@@ -510,15 +496,13 @@ module LineCache
510
496
  return false unless stat
511
497
  end
512
498
  begin
513
- fp = File.open(path, 'r')
514
- raw_string = fp.read
515
- fp.rewind
516
- lines = {:plain => fp.readlines}
517
- fp.close()
518
- lines[opts[:output]] =
519
- highlight_string(raw_string, opts[:output]).split(/\n/) if
520
- opts[:output]
521
- rescue
499
+ # (GF) rewind does not work in JRuby with a jar:file:... filename
500
+ # (GF) read file once and only create string if required by opts[:output]
501
+ lines = { :plain => File.readlines(path) }
502
+ if opts[:output]
503
+ lines[opts[:output]] = highlight_string(lines[:plain].join, opts[:output]).split(/\n/)
504
+ end
505
+ rescue
522
506
  ## print '*** cannot open', path, ':', msg
523
507
  return nil
524
508
  end
@@ -531,15 +515,15 @@ module LineCache
531
515
  end
532
516
 
533
517
  # example usage
534
- if __FILE__ == $0
535
- def yes_no(var)
518
+ if __FILE__ == $0
519
+ def yes_no(var)
536
520
  return var ? "" : "not "
537
521
  end
538
522
 
539
523
  lines = LineCache::getlines(__FILE__)
540
524
  puts "#{__FILE__} has #{LineCache.size(__FILE__)} lines"
541
525
  line = LineCache::getline(__FILE__, 6)
542
- puts "The 6th line is\n#{line}"
526
+ puts "The 6th line is\n#{line}"
543
527
  line = LineCache::remap_file(__FILE__, 'another_name')
544
528
  puts LineCache::getline('another_name', 7)
545
529
 
@@ -547,21 +531,21 @@ if __FILE__ == $0
547
531
  LineCache::update_cache(__FILE__)
548
532
  LineCache::checkcache(__FILE__)
549
533
  puts "#{__FILE__} has #{LineCache::size(__FILE__)} lines"
550
- puts "#{__FILE__} trace line numbers:\n" +
534
+ puts "#{__FILE__} trace line numbers:\n" +
551
535
  "#{LineCache::trace_line_numbers(__FILE__).to_a.sort.inspect}"
552
- puts("#{__FILE__} is %scached." %
536
+ puts("#{__FILE__} is %scached." %
553
537
  yes_no(LineCache::cached?(__FILE__)))
554
538
  puts LineCache::stat(__FILE__).inspect
555
539
  puts "Full path: #{LineCache::path(__FILE__)}"
556
540
  LineCache::checkcache # Check all files in the cache
557
- LineCache::clear_file_cache
558
- puts("#{__FILE__} is now %scached." %
541
+ LineCache::clear_file_cache
542
+ puts("#{__FILE__} is now %scached." %
559
543
  yes_no(LineCache::cached?(__FILE__)))
560
544
  digest = SCRIPT_LINES__.select{|k,v| k =~ /digest.rb$/}
561
545
  puts digest.first[0] if digest
562
546
  line = LineCache::getline(__FILE__, 7)
563
- puts "The 7th line is\n#{line}"
547
+ puts "The 7th line is\n#{line}"
564
548
  LineCache::remap_file_lines(__FILE__, 'test2', (10..20), 6)
565
- puts LineCache::getline('test2', 10)
566
- puts "Remapped 10th line of test2 is\n#{line}"
549
+ line = LineCache::getline('test2', 11)
550
+ puts "Remapped 11th line of test2 is\n#{line}"
567
551
  end